Skip to content

Commit 4f45e7a

Browse files
authored
Merge pull request #2287 from adumesny/master
setupDragIn() shadow DOM support
2 parents a5249d2 + 612f621 commit 4f45e7a

File tree

4 files changed

+30
-26
lines changed

4 files changed

+30
-26
lines changed

doc/CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Change log
55
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
66
**Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)*
77

8+
- [8.0.0-dev (TBD)](#800-dev-tbd)
89
- [8.0.0 (2023-04-29)](#800-2023-04-29)
910
- [7.3.0 (2023-04-01)](#730-2023-04-01)
1011
- [7.2.3 (2023-02-02)](#723-2023-02-02)
@@ -83,6 +84,9 @@ Change log
8384

8485
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
8586

87+
## 8.0.0-dev (TBD)
88+
* feat: [#2275](https://github.com/gridstack/gridstack.js/issues/2275) `setupDragIn()` now can take an array or elements (in addition to selector string) and optional parent root (for shadow DOM support)
89+
8690
## 8.0.0 (2023-04-29)
8791
* package is now ES2020 (TS exported files), webpack all.js still umd (better than commonjs for browsers), still have es5/ files unchanged (for now)
8892
* optimize [#2243](https://github.com/gridstack/gridstack.js/issues/2243) removed `gs-min|max_w|h` attribute generated in CSS or written out as they are never used for rendering, only for initial load. This reduce our column/row CSS in half!

src/dd-draggable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ export class DDDraggable extends DDBaseImplement implements HTMLElementExtendOpt
266266
helper = Utils.cloneNode(this.el);
267267
}
268268
if (!document.body.contains(helper)) {
269-
Utils.appendTo(helper, this.option.appendTo === 'parent' ? this.el.parentNode : this.option.appendTo);
269+
Utils.appendTo(helper, this.option.appendTo === 'parent' ? this.el.parentElement : this.option.appendTo);
270270
}
271271
if (helper === this.el) {
272272
this.dragElementOriginStyle = DDDraggable.originStyleProp.map(prop => this.el.style[prop]);

src/gridstack.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,20 +1666,20 @@ export class GridStack {
16661666
* call to setup dragging in from the outside (say toolbar), by specifying the class selection and options.
16671667
* Called during GridStack.init() as options, but can also be called directly (last param are used) in case the toolbar
16681668
* is dynamically create and needs to be set later.
1669-
* @param dragIn string selector (ex: '.sidebar .grid-stack-item')
1669+
* @param dragIn string selector (ex: '.sidebar .grid-stack-item') or list of dom elements
16701670
* @param dragInOptions options - see DDDragInOpt. (default: {handle: '.grid-stack-item-content', appendTo: 'body'}
1671+
* @param root optional root which defaults to document (for shadow dom)
16711672
**/
1672-
public static setupDragIn(dragIn?: string, dragInOptions?: DDDragInOpt): void {
1673+
public static setupDragIn(dragIn?: string | HTMLElement[], dragInOptions?: DDDragInOpt, root = document): void {
16731674
if (dragInOptions?.pause !== undefined) {
16741675
DDManager.pauseDrag = dragInOptions.pause;
16751676
}
16761677

1677-
if (typeof dragIn === 'string') {
1678-
dragInOptions = {...dragInDefaultOptions, ...(dragInOptions || {})};
1679-
Utils.getElements(dragIn).forEach(el => {
1680-
if (!dd.isDraggable(el)) dd.dragIn(el, dragInOptions);
1681-
});
1682-
}
1678+
dragInOptions = {...dragInDefaultOptions, ...(dragInOptions || {})};
1679+
let els: HTMLElement[] = (typeof dragIn === 'string') ? Utils.getElements(dragIn, root) : dragIn;
1680+
if (els.length) els?.forEach(el => {
1681+
if (!dd.isDraggable(el)) dd.dragIn(el, dragInOptions);
1682+
});
16831683
}
16841684

16851685
/**

src/utils.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,39 +53,39 @@ export function obsoleteAttr(el: HTMLElement, oldName: string, newName: string,
5353
*/
5454
export class Utils {
5555

56-
/** convert a potential selector into actual list of html elements */
57-
static getElements(els: GridStackElement): HTMLElement[] {
56+
/** convert a potential selector into actual list of html elements. optional root which defaults to document (for shadow dom) */
57+
static getElements(els: GridStackElement, root = document): HTMLElement[] {
5858
if (typeof els === 'string') {
59-
let list = document.querySelectorAll(els);
59+
let list = root.querySelectorAll(els);
6060
if (!list.length && els[0] !== '.' && els[0] !== '#') {
61-
list = document.querySelectorAll('.' + els);
62-
if (!list.length) { list = document.querySelectorAll('#' + els) }
61+
list = root.querySelectorAll('.' + els);
62+
if (!list.length) { list = root.querySelectorAll('#' + els) }
6363
}
6464
return Array.from(list) as HTMLElement[];
6565
}
6666
return [els];
6767
}
6868

69-
/** convert a potential selector into actual single element */
70-
static getElement(els: GridStackElement): HTMLElement {
69+
/** convert a potential selector into actual single element. optional root which defaults to document (for shadow dom) */
70+
static getElement(els: GridStackElement, root = document): HTMLElement {
7171
if (typeof els === 'string') {
7272
if (!els.length) return null;
7373
if (els[0] === '#') {
74-
return document.getElementById(els.substring(1));
74+
return root.getElementById(els.substring(1));
7575
}
7676
if (els[0] === '.' || els[0] === '[') {
77-
return document.querySelector(els);
77+
return root.querySelector(els);
7878
}
7979

8080
// if we start with a digit, assume it's an id (error calling querySelector('#1')) as class are not valid CSS
8181
if(!isNaN(+els[0])) { // start with digit
82-
return document.getElementById(els);
82+
return root.getElementById(els);
8383
}
8484

85-
// finally try string, then id then class
86-
let el = document.querySelector(els);
87-
if (!el) { el = document.getElementById(els) }
88-
if (!el) { el = document.querySelector('.' + els) }
85+
// finally try string, then id, then class
86+
let el = root.querySelector(els);
87+
if (!el) { el = root.getElementById(els) }
88+
if (!el) { el = root.querySelector('.' + els) }
8989
return el as HTMLElement;
9090
}
9191
return els;
@@ -452,12 +452,12 @@ export class Utils {
452452
return node;
453453
}
454454

455-
public static appendTo(el: HTMLElement, parent: string | HTMLElement | Node): void {
455+
public static appendTo(el: HTMLElement, parent: string | HTMLElement): void {
456456
let parentNode: HTMLElement;
457457
if (typeof parent === 'string') {
458-
parentNode = document.querySelector(parent as string);
458+
parentNode = Utils.getElement(parent);
459459
} else {
460-
parentNode = parent as HTMLElement;
460+
parentNode = parent;
461461
}
462462
if (parentNode) {
463463
parentNode.appendChild(el);

0 commit comments

Comments
 (0)