@@ -681,7 +681,7 @@ export class GridStack {
681
681
* @param val if true widget will be locked.
682
682
*/
683
683
public locked ( els : GridStackElement , val : boolean ) : GridStack {
684
- this . getElements ( els ) . forEach ( el => {
684
+ GridStack . getElements ( els ) . forEach ( el => {
685
685
let node = el . gridstackNode ;
686
686
if ( ! node ) return ;
687
687
node . locked = ( val || false ) ;
@@ -703,10 +703,10 @@ export class GridStack {
703
703
* @example
704
704
* let grid = GridStack.init();
705
705
* grid.el.appendChild('<div id="gsi-1" data-gs-width="3"></div>');
706
- * grid.makeWidget('gsi-1');
706
+ * grid.makeWidget('# gsi-1');
707
707
*/
708
708
public makeWidget ( els : GridStackElement ) : GridItemHTMLElement {
709
- let el = this . getElement ( els ) ;
709
+ let el = GridStack . getElement ( els ) ;
710
710
this . _prepareElement ( el , true ) ;
711
711
this . _updateContainerHeight ( ) ;
712
712
this . _triggerAddEvent ( ) ;
@@ -756,7 +756,7 @@ export class GridStack {
756
756
* @param val if true widget will be draggable.
757
757
*/
758
758
public movable ( els : GridStackElement , val : boolean ) : GridStack {
759
- this . getElements ( els ) . forEach ( el => {
759
+ GridStack . getElements ( els ) . forEach ( el => {
760
760
let node = el . gridstackNode ;
761
761
if ( ! node ) { return }
762
762
node . noMove = ! ( val || false ) ;
@@ -861,7 +861,7 @@ export class GridStack {
861
861
* @param triggerEvent if `false` (quiet mode) element will not be added to removed list and no 'removed' callbacks will be called (Default? true).
862
862
*/
863
863
public removeWidget ( els : GridStackElement , removeDOM = true , triggerEvent = true ) : GridStack {
864
- this . getElements ( els ) . forEach ( el => {
864
+ GridStack . getElements ( els ) . forEach ( el => {
865
865
if ( el . parentElement !== this . el ) return ; // not our child!
866
866
let node = el . gridstackNode ;
867
867
// For Meteor support: https://github.com/gridstack/gridstack.js/pull/272
@@ -924,7 +924,7 @@ export class GridStack {
924
924
* @param val if true widget will be resizable.
925
925
*/
926
926
public resizable ( els : GridStackElement , val : boolean ) : GridStack {
927
- this . getElements ( els ) . forEach ( el => {
927
+ GridStack . getElements ( els ) . forEach ( el => {
928
928
let node = el . gridstackNode ;
929
929
if ( ! node ) { return ; }
930
930
node . noResize = ! ( val || false ) ;
@@ -1450,7 +1450,7 @@ export class GridStack {
1450
1450
1451
1451
/** @internal */
1452
1452
private _updateElement ( els : GridStackElement , callback : ( el : GridItemHTMLElement , node : GridStackNode ) => void ) : GridStack {
1453
- let el = this . getElement ( els ) ;
1453
+ let el = GridStack . getElement ( els ) ;
1454
1454
if ( ! el ) { return this ; }
1455
1455
let node = el . gridstackNode ;
1456
1456
if ( ! node ) { return this ; }
@@ -1687,28 +1687,38 @@ export class GridStack {
1687
1687
return this ;
1688
1688
}
1689
1689
1690
- /** @internal */
1691
- private getElement ( els : GridStackElement = '.grid-stack-item' ) : GridItemHTMLElement {
1692
- return ( typeof els === 'string' ?
1693
- ( document . querySelector ( els ) || document . querySelector ( '#' + els ) || document . querySelector ( '.' + els ) ) : els ) ;
1690
+ /** @internal convert a potential selector into actual element */
1691
+ private static getElement ( els : GridStackElement = '.grid-stack-item' ) : GridItemHTMLElement {
1692
+ if ( typeof els === 'string' ) {
1693
+ let el = document . querySelector ( els ) ;
1694
+ if ( ! el && els [ 0 ] !== '.' && els [ 0 ] !== '#' ) {
1695
+ el = document . querySelector ( '#' + els ) ;
1696
+ if ( ! el ) { el = document . querySelector ( '.' + els ) }
1697
+ }
1698
+ return el as GridItemHTMLElement ;
1699
+ }
1700
+ return els ;
1694
1701
}
1695
- /** @internal */
1696
- private getElements ( els : GridStackElement = '.grid-stack-item' ) : GridItemHTMLElement [ ] {
1702
+
1703
+ /** @internal convert a potential selector into actual list of elements */
1704
+ private static getElements ( els : GridStackElement = '.grid-stack-item' ) : GridItemHTMLElement [ ] {
1697
1705
if ( typeof els === 'string' ) {
1698
1706
let list = document . querySelectorAll ( els ) ;
1699
- if ( ! list . length ) { list = document . querySelectorAll ( '.' + els ) }
1700
- if ( ! list . length ) { list = document . querySelectorAll ( '#' + els ) }
1707
+ if ( ! list . length && els [ 0 ] !== '.' && els [ 0 ] !== '#' ) {
1708
+ list = document . querySelectorAll ( '.' + els ) ;
1709
+ if ( ! list . length ) { list = document . querySelectorAll ( '#' + els ) }
1710
+ }
1701
1711
return Array . from ( list ) as GridItemHTMLElement [ ] ;
1702
1712
}
1703
1713
return [ els ] ;
1704
1714
}
1705
1715
/** @internal */
1706
1716
private static getGridElement ( els : string | HTMLElement = '.grid-stack' ) : GridHTMLElement {
1707
- return ( typeof els === 'string' ? document . querySelector ( els ) : els ) ;
1717
+ return GridStack . getElement ( els ) as GridHTMLElement ;
1708
1718
}
1709
1719
/** @internal */
1710
1720
private static getGridElements ( els : string | HTMLElement = '.grid-stack' ) : GridHTMLElement [ ] {
1711
- return ( typeof els === 'string' ) ? Array . from ( document . querySelectorAll ( els ) ) : [ els ] ;
1721
+ return GridStack . getElements ( els ) as GridHTMLElement [ ] ;
1712
1722
}
1713
1723
1714
1724
/** @internal initialize margin top/bottom/left/right and units */
@@ -1755,7 +1765,7 @@ export class GridStack {
1755
1765
1756
1766
/** @internal called to update an element(s) attributes and node values */
1757
1767
private _updateAttr ( els : GridStackElement , val : number , attr : string , field : string ) : GridStack {
1758
- this . getElements ( els ) . forEach ( el => {
1768
+ GridStack . getElements ( els ) . forEach ( el => {
1759
1769
if ( val ) {
1760
1770
el . setAttribute ( attr , String ( val ) ) ;
1761
1771
} else {
0 commit comments