Skip to content

Commit 319b34a

Browse files
committed
setupDragIn() signature fix
* set root: HTMLElement | Document so either can be passed now more fix #2275
1 parent 072a52c commit 319b34a

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ Change log
8888
## 8.0.2 TBD
8989
* break: remove `GridStackOptions.minWidth` obsolete since 5.1, use `oneColumnSize` instead
9090
* optimize: CSS files now even 25% smaller (after being halfed in 8.0.0) by removing `.grid-stack` prefix for anything already gs based, and 3 digit rounding.
91+
* fix: [#2275](https://github.com/gridstack/gridstack.js/issues/2275) `setupDragIn()` signature tweaks (HTMLElement | Document)
9192

9293
## 8.0.1 (2023-04-29)
9394
* 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)

doc/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ gridstack.js API
2828
- [`init(options: GridStackOptions = {}, elOrString: GridStackElement = '.grid-stack'): GridStack`](#initoptions-gridstackoptions---elorstring-gridstackelement--grid-stack-gridstack)
2929
- [`initAll(options: GridStackOptions = {}, selector = '.grid-stack'): GridStack[]`](#initalloptions-gridstackoptions---selector--grid-stack-gridstack)
3030
- [`addGrid(parent: HTMLElement, opt: GridStackOptions = {}): GridStack `](#addgridparent-htmlelement-opt-gridstackoptions---gridstack-)
31-
- [`setupDragIn(dragIn?: string | HTMLElement[], dragInOptions?: DDDragInOpt, root = document)`](#setupdragindragin-string--htmlelement-draginoptions-dddraginopt-root--document)
31+
- [`setupDragIn(dragIn?: string | HTMLElement[], dragInOptions?: DDDragInOpt, root = HTMLElement | Document)`](#setupdragindragin-string--htmlelement-draginoptions-dddraginopt-root--htmlelement--document)
3232
- [`GridStack.registerEngine(engineClass: typeof GridStackEngine)`](#gridstackregisterengineengineclass-typeof-gridstackengine)
3333
- [API](#api)
3434
- [`addWidget(el?: GridStackWidget | GridStackElement, options?: GridStackWidget)`](#addwidgetel-gridstackwidget--gridstackelement-options-gridstackwidget)
@@ -323,13 +323,13 @@ grids.forEach(...)
323323
* @param opt grids options used to initialize the grid, and list of children
324324
* see [nested.html](https://github.com/gridstack/gridstack.js/tree/master/demo/nested.html) demo
325325

326-
### `setupDragIn(dragIn?: string | HTMLElement[], dragInOptions?: DDDragInOpt, root = document)`
326+
### `setupDragIn(dragIn?: string | HTMLElement[], dragInOptions?: DDDragInOpt, root = HTMLElement | Document)`
327327

328328
* call to setup dragging in from the outside (say toolbar), by specifying the class selection and options.
329329
Called during `GridStack.init()` as options, but can also be called directly (last param are cached) in case the toolbar is dynamically create and needs to change later.
330330
* @param dragIn string selector (ex: `'.sidebar .grid-stack-item'`) or list of dom elements
331331
* @param dragInOptions options - see `DDDragInOpt`. (default: `{handle: '.grid-stack-item-content', appendTo: 'body'}`
332-
* @param root - default to document (for shadow dom support)
332+
* @param root - default to document. for shadow dom support pass the parent container.
333333
but you will probably also want `helper: 'clone'` or your own callback function).
334334

335335
### `GridStack.registerEngine(engineClass: typeof GridStackEngine)`

src/gridstack.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,9 +1660,9 @@ export class GridStack {
16601660
* is dynamically create and needs to be set later.
16611661
* @param dragIn string selector (ex: '.sidebar .grid-stack-item') or list of dom elements
16621662
* @param dragInOptions options - see DDDragInOpt. (default: {handle: '.grid-stack-item-content', appendTo: 'body'}
1663-
* @param root optional root which defaults to document (for shadow dom)
1663+
* @param root optional root which defaults to document (for shadow dom pas the parent HTMLDocument)
16641664
**/
1665-
public static setupDragIn(dragIn?: string | HTMLElement[], dragInOptions?: DDDragInOpt, root = document): void {
1665+
public static setupDragIn(dragIn?: string | HTMLElement[], dragInOptions?: DDDragInOpt, root: HTMLElement | Document = document): void {
16661666
if (dragInOptions?.pause !== undefined) {
16671667
DDManager.pauseDrag = dragInOptions.pause;
16681668
}

src/utils.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,15 @@ export function obsoleteAttr(el: HTMLElement, oldName: string, newName: string,
5454
export class Utils {
5555

5656
/** 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[] {
57+
static getElements(els: GridStackElement, root: HTMLElement | Document = document): HTMLElement[] {
58+
const doc = ('getElementById' in root) ? root as Document : undefined;
5859
if (typeof els === 'string') {
5960

6061
// Note: very common for people use to id='1,2,3' which is only legal as HTML5 id, but not CSS selectors
6162
// so if we start with a number, assume it's an id and just return that one item...
6263
// see https://github.com/gridstack/gridstack.js/issues/2234#issuecomment-1523796562
63-
if(!isNaN(+els[0])) { // start with digit
64-
const el = root.getElementById(els);
64+
if (doc && !isNaN(+els[0])) { // start with digit
65+
const el = doc.getElementById(els);
6566
return el ? [el] : [];
6667
}
6768

@@ -76,24 +77,25 @@ export class Utils {
7677
}
7778

7879
/** convert a potential selector into actual single element. optional root which defaults to document (for shadow dom) */
79-
static getElement(els: GridStackElement, root = document): HTMLElement {
80+
static getElement(els: GridStackElement, root: HTMLElement | Document = document): HTMLElement {
81+
const doc = ('getElementById' in root) ? root as Document : undefined;
8082
if (typeof els === 'string') {
8183
if (!els.length) return null;
82-
if (els[0] === '#') {
83-
return root.getElementById(els.substring(1));
84+
if (doc && els[0] === '#') {
85+
return doc.getElementById(els.substring(1));
8486
}
85-
if (els[0] === '.' || els[0] === '[') {
87+
if (els[0] === '#' || els[0] === '.' || els[0] === '[') {
8688
return root.querySelector(els);
8789
}
8890

8991
// if we start with a digit, assume it's an id (error calling querySelector('#1')) as class are not valid CSS
90-
if(!isNaN(+els[0])) { // start with digit
91-
return root.getElementById(els);
92+
if (doc && !isNaN(+els[0])) { // start with digit
93+
return doc.getElementById(els);
9294
}
9395

9496
// finally try string, then id, then class
9597
let el = root.querySelector(els);
96-
if (!el) { el = root.getElementById(els) }
98+
if (doc && !el) { el = doc.getElementById(els) }
9799
if (!el) { el = root.querySelector('.' + els) }
98100
return el as HTMLElement;
99101
}

0 commit comments

Comments
 (0)