@@ -364,14 +364,16 @@ export class GridStack {
364
364
this . _isAutoCellHeight = ( opts . cellHeight === 'auto' ) ;
365
365
if ( this . _isAutoCellHeight || opts . cellHeight === 'initial' ) {
366
366
// make the cell content square initially (will use resize/column event to keep it square)
367
- this . cellHeight ( undefined , false ) ;
367
+ this . cellHeight ( undefined ) ;
368
368
} else {
369
369
// append unit if any are set
370
370
if ( typeof opts . cellHeight == 'number' && opts . cellHeightUnit && opts . cellHeightUnit !== gridDefaults . cellHeightUnit ) {
371
371
opts . cellHeight = opts . cellHeight + opts . cellHeightUnit ;
372
372
delete opts . cellHeightUnit ;
373
373
}
374
- this . cellHeight ( opts . cellHeight , false ) ;
374
+ const val = opts . cellHeight ;
375
+ delete opts . cellHeight ; // force initial cellHeight() call to set the value
376
+ this . cellHeight ( val ) ;
375
377
}
376
378
377
379
// see if we need to adjust auto-hide
@@ -400,13 +402,10 @@ export class GridStack {
400
402
this . _writePosAttr ( el , n ) ;
401
403
}
402
404
} ) ;
403
- this . _updateStyles ( ) ;
405
+ this . _updateContainerHeight ( ) ;
404
406
}
405
407
} ) ;
406
408
407
- // create initial global styles BEFORE loading children so resizeToContent margin can be calculated correctly
408
- this . _updateStyles ( ) ;
409
-
410
409
if ( opts . auto ) {
411
410
this . batchUpdate ( ) ; // prevent in between re-layout #1535 TODO: this only set float=true, need to prevent collision check...
412
411
this . engine . _loading = true ; // loading collision check
@@ -422,7 +421,6 @@ export class GridStack {
422
421
if ( children . length ) this . load ( children ) ; // don't load empty
423
422
}
424
423
425
- // if (this.engine.nodes.length) this._updateStyles(); // update based on # of children. done in engine onChange CB
426
424
this . setAnimation ( ) ;
427
425
428
426
// dynamic grids require pausing during drag to detect over to nest vs push
@@ -851,17 +849,16 @@ export class GridStack {
851
849
*
852
850
* @param val the cell height. If not passed (undefined), cells content will be made square (match width minus margin),
853
851
* if pass 0 the CSS will be generated by the application instead.
854
- * @param update (Optional) if false, styles will not be updated
855
852
*
856
853
* @example
857
854
* grid.cellHeight(100); // same as 100px
858
855
* grid.cellHeight('70px');
859
856
* grid.cellHeight(grid.cellWidth() * 1.2);
860
857
*/
861
- public cellHeight ( val ?: numberOrString , update = true ) : GridStack {
858
+ public cellHeight ( val ?: numberOrString ) : GridStack {
862
859
863
860
// if not called internally, check if we're changing mode
864
- if ( update && val !== undefined ) {
861
+ if ( val !== undefined ) {
865
862
if ( this . _isAutoCellHeight !== ( val === 'auto' ) ) {
866
863
this . _isAutoCellHeight = ( val === 'auto' ) ;
867
864
this . _updateResizeEvent ( ) ;
@@ -883,11 +880,11 @@ export class GridStack {
883
880
this . opts . cellHeightUnit = data . unit ;
884
881
this . opts . cellHeight = data . h ;
885
882
883
+ // finally update var and container
884
+ this . el . style . setProperty ( '--gs-cell-height' , `${ this . opts . cellHeight } ${ this . opts . cellHeightUnit } ` ) ;
885
+ this . _updateContainerHeight ( ) ;
886
886
this . resizeToContentCheck ( ) ;
887
887
888
- if ( update ) {
889
- this . _updateStyles ( ) ;
890
- }
891
888
return this ;
892
889
}
893
890
@@ -1305,7 +1302,7 @@ export class GridStack {
1305
1302
const opts = this . opts ;
1306
1303
if ( o . acceptWidgets !== undefined ) this . _setupAcceptWidget ( ) ;
1307
1304
if ( o . animate !== undefined ) this . setAnimation ( ) ;
1308
- if ( o . cellHeight ) { this . cellHeight ( o . cellHeight , true ) ; delete o . cellHeight ; }
1305
+ if ( o . cellHeight ) { this . cellHeight ( o . cellHeight ) ; delete o . cellHeight ; }
1309
1306
if ( o . class && o . class !== opts . class ) { if ( opts . class ) this . el . classList . remove ( opts . class ) ; this . el . classList . add ( o . class ) ; }
1310
1307
if ( typeof ( o . column ) === 'number' && ! o . columnOpts ) { this . column ( o . column ) ; delete o . column ; } // responsive column take over actual count
1311
1308
if ( o . margin !== undefined ) this . margin ( o . margin ) ;
@@ -1360,7 +1357,7 @@ export class GridStack {
1360
1357
// restore any sub-grid back
1361
1358
if ( n . subGrid ?. el ) {
1362
1359
itemContent . appendChild ( n . subGrid . el ) ;
1363
- n . subGrid . _updateStyles ( ) ;
1360
+ n . subGrid . _updateContainerHeight ( ) ;
1364
1361
}
1365
1362
}
1366
1363
delete w . content ;
@@ -1508,7 +1505,7 @@ export class GridStack {
1508
1505
*/
1509
1506
public margin ( value : numberOrString ) : GridStack {
1510
1507
const isMultiValue = ( typeof value === 'string' && value . split ( ' ' ) . length > 1 ) ;
1511
- // check if we can skip re-creating our CSS file ... won't check if multi values (too much hassle)
1508
+ // check if we can skip... won't check if multi values (too much hassle)
1512
1509
if ( ! isMultiValue ) {
1513
1510
const data = Utils . parseHeight ( value ) ;
1514
1511
if ( this . opts . marginUnit === data . unit && this . opts . margin === data . h ) return ;
@@ -1518,8 +1515,6 @@ export class GridStack {
1518
1515
this . opts . marginTop = this . opts . marginBottom = this . opts . marginLeft = this . opts . marginRight = undefined ;
1519
1516
this . _initMargin ( ) ;
1520
1517
1521
- this . _updateStyles ( ) ;
1522
-
1523
1518
return this ;
1524
1519
}
1525
1520
@@ -1598,34 +1593,6 @@ export class GridStack {
1598
1593
return this ;
1599
1594
}
1600
1595
1601
- private setVar ( el : HTMLElement , varName : string , varValue : string ) {
1602
- el . style . setProperty ( varName , varValue ) ;
1603
- }
1604
-
1605
- /**
1606
- * Updates the CSS variables (used in CSS and inline style) for row based layout and initial margin setting,
1607
- * Variables are scoped in DOM so they works for nested grids as well
1608
- * @internal
1609
- */
1610
- protected _updateStyles ( ) : GridStack {
1611
- this . _updateContainerHeight ( ) ;
1612
-
1613
- // if user is telling us they will handle the CSS themselves by setting heights to 0. Do we need this opts really ??
1614
- if ( this . opts . cellHeight === 0 ) {
1615
- return this ;
1616
- }
1617
-
1618
- // Set CSS var of cell height
1619
- this . setVar ( this . el , "--gs-cell-height" , `${ this . opts . cellHeight } ${ this . opts . cellHeightUnit } ` ) ;
1620
- // content margins
1621
- this . setVar ( this . el , "--gs-item-margin-top" , `${ this . opts . marginTop } ${ this . opts . marginUnit } ` ) ;
1622
- this . setVar ( this . el , "--gs-item-margin-bottom" , `${ this . opts . marginBottom } ${ this . opts . marginUnit } ` ) ;
1623
- this . setVar ( this . el , "--gs-item-margin-right" , `${ this . opts . marginRight } ${ this . opts . marginUnit } ` ) ;
1624
- this . setVar ( this . el , "--gs-item-margin-left" , `${ this . opts . marginLeft } ${ this . opts . marginUnit } ` ) ;
1625
-
1626
- return this ;
1627
- }
1628
-
1629
1596
/** @internal */
1630
1597
protected _updateContainerHeight ( ) : GridStack {
1631
1598
if ( ! this . engine || this . engine . batchMode ) return this ;
@@ -1880,7 +1847,6 @@ export class GridStack {
1880
1847
1881
1848
/** @internal initialize margin top/bottom/left/right and units */
1882
1849
protected _initMargin ( ) : GridStack {
1883
-
1884
1850
let data : HeightData ;
1885
1851
let margin = 0 ;
1886
1852
@@ -1904,41 +1870,28 @@ export class GridStack {
1904
1870
}
1905
1871
1906
1872
// see if top/bottom/left/right need to be set as well
1907
- if ( this . opts . marginTop === undefined ) {
1908
- this . opts . marginTop = margin ;
1909
- } else {
1910
- data = Utils . parseHeight ( this . opts . marginTop ) ;
1911
- this . opts . marginTop = data . h ;
1912
- delete this . opts . margin ;
1913
- }
1914
-
1915
- if ( this . opts . marginBottom === undefined ) {
1916
- this . opts . marginBottom = margin ;
1917
- } else {
1918
- data = Utils . parseHeight ( this . opts . marginBottom ) ;
1919
- this . opts . marginBottom = data . h ;
1920
- delete this . opts . margin ;
1921
- }
1922
-
1923
- if ( this . opts . marginRight === undefined ) {
1924
- this . opts . marginRight = margin ;
1925
- } else {
1926
- data = Utils . parseHeight ( this . opts . marginRight ) ;
1927
- this . opts . marginRight = data . h ;
1928
- delete this . opts . margin ;
1929
- }
1930
-
1931
- if ( this . opts . marginLeft === undefined ) {
1932
- this . opts . marginLeft = margin ;
1933
- } else {
1934
- data = Utils . parseHeight ( this . opts . marginLeft ) ;
1935
- this . opts . marginLeft = data . h ;
1936
- delete this . opts . margin ;
1937
- }
1873
+ const keys = [ 'marginTop' , 'marginRight' , 'marginBottom' , 'marginLeft' ] ;
1874
+ keys . forEach ( k => {
1875
+ if ( this . opts [ k ] === undefined ) {
1876
+ this . opts [ k ] = margin ;
1877
+ } else {
1878
+ data = Utils . parseHeight ( this . opts [ k ] ) ;
1879
+ this . opts [ k ] = data . h ;
1880
+ delete this . opts . margin ;
1881
+ }
1882
+ } ) ;
1938
1883
this . opts . marginUnit = data . unit ; // in case side were spelled out, use those units instead...
1939
1884
if ( this . opts . marginTop === this . opts . marginBottom && this . opts . marginLeft === this . opts . marginRight && this . opts . marginTop === this . opts . marginRight ) {
1940
1885
this . opts . margin = this . opts . marginTop ; // makes it easier to check for no-ops in setMargin()
1941
1886
}
1887
+
1888
+ // finally Update the CSS margin variables (inside the cell height) */
1889
+ const style = this . el . style ;
1890
+ style . setProperty ( '--gs-item-margin-top' , `${ this . opts . marginTop } ${ this . opts . marginUnit } ` ) ;
1891
+ style . setProperty ( '--gs-item-margin-bottom' , `${ this . opts . marginBottom } ${ this . opts . marginUnit } ` ) ;
1892
+ style . setProperty ( '--gs-item-margin-right' , `${ this . opts . marginRight } ${ this . opts . marginUnit } ` ) ;
1893
+ style . setProperty ( '--gs-item-margin-left' , `${ this . opts . marginLeft } ${ this . opts . marginUnit } ` ) ;
1894
+
1942
1895
return this ;
1943
1896
}
1944
1897
@@ -2355,7 +2308,6 @@ export class GridStack {
2355
2308
this . resizeToContentCheck ( false , node ) ;
2356
2309
if ( subGrid ) {
2357
2310
subGrid . parentGridNode = node ;
2358
- subGrid . _updateStyles ( ) ; // re-create sub-grid styles now that we've moved
2359
2311
}
2360
2312
this . _updateContainerHeight ( ) ;
2361
2313
}
0 commit comments