@@ -48,10 +48,6 @@ export interface CellPosition {
48
48
y : number ;
49
49
}
50
50
51
- interface GridHTMLStyleElement extends HTMLStyleElement {
52
- _max ?: number ; // internal tracker of the max # of rows we created
53
- }
54
-
55
51
// extend with internal fields we need - TODO: move other items in here
56
52
interface InternalGridStackOptions extends GridStackOptions {
57
53
_alwaysShowResizeHandle ?: true | false | 'mobile' ; // so we can restore for save
@@ -245,8 +241,6 @@ export class GridStack {
245
241
protected _ignoreLayoutsNodeChange : boolean ;
246
242
/** @internal */
247
243
public _gsEventHandler = { } ;
248
- /** @internal */
249
- protected _styles : GridHTMLStyleElement ;
250
244
/** @internal flag to keep cells square during resize */
251
245
protected _isAutoCellHeight : boolean ;
252
246
/** @internal limit auto cell resizing method */
@@ -394,8 +388,6 @@ export class GridStack {
394
388
float : this . opts . float ,
395
389
maxRow : this . opts . maxRow ,
396
390
onChange : ( cbNodes ) => {
397
- let maxH = 0 ;
398
- this . engine . nodes . forEach ( n => { maxH = Math . max ( maxH , n . y + n . h ) } ) ;
399
391
cbNodes . forEach ( n => {
400
392
let el = n . el ;
401
393
if ( ! el ) return ;
@@ -406,12 +398,12 @@ export class GridStack {
406
398
this . _writePosAttr ( el , n ) ;
407
399
}
408
400
} ) ;
409
- this . _updateStyles ( false , maxH ) ; // false = don't recreate, just append if need be
401
+ this . _updateStyles ( ) ;
410
402
}
411
403
} ) ;
412
404
413
405
// create initial global styles BEFORE loading children so resizeToContent margin can be calculated correctly
414
- this . _updateStyles ( false , 0 ) ;
406
+ this . _updateStyles ( ) ;
415
407
416
408
if ( this . opts . auto ) {
417
409
this . batchUpdate ( ) ; // prevent in between re-layout #1535 TODO: this only set float=true, need to prevent collision check...
@@ -878,7 +870,7 @@ export class GridStack {
878
870
this . resizeToContentCheck ( ) ;
879
871
880
872
if ( update ) {
881
- this . _updateStyles ( true ) ; // true = force re-create for current # of rows
873
+ this . _updateStyles ( ) ;
882
874
}
883
875
return this ;
884
876
}
@@ -995,7 +987,6 @@ export class GridStack {
995
987
} else {
996
988
this . el . parentNode . removeChild ( this . el ) ;
997
989
}
998
- this . _removeStylesheet ( ) ;
999
990
if ( this . parentGridItem ) delete this . parentGridItem . subGrid ;
1000
991
delete this . parentGridItem ;
1001
992
delete this . opts ;
@@ -1320,7 +1311,7 @@ export class GridStack {
1320
1311
// restore any sub-grid back
1321
1312
if ( n . subGrid ?. el ) {
1322
1313
itemContent . appendChild ( n . subGrid . el ) ;
1323
- if ( ! n . subGrid . opts . styleInHead ) n . subGrid . _updateStyles ( true ) ; // force create
1314
+ n . subGrid . _updateStyles ( ) ;
1324
1315
}
1325
1316
}
1326
1317
delete w . content ;
@@ -1437,7 +1428,7 @@ export class GridStack {
1437
1428
this . opts . marginTop = this . opts . marginBottom = this . opts . marginLeft = this . opts . marginRight = undefined ;
1438
1429
this . _initMargin ( ) ;
1439
1430
1440
- this . _updateStyles ( true ) ; // true = force re-create
1431
+ this . _updateStyles ( ) ;
1441
1432
1442
1433
return this ;
1443
1434
}
@@ -1515,78 +1506,30 @@ export class GridStack {
1515
1506
return this ;
1516
1507
}
1517
1508
1518
- /** @internal called to delete the current dynamic style sheet used for our layout */
1519
- protected _removeStylesheet ( ) : GridStack {
1520
-
1521
- if ( this . _styles ) {
1522
- const styleLocation = this . opts . styleInHead ? undefined : this . el . parentNode as HTMLElement ;
1523
- Utils . removeStylesheet ( this . _styleSheetClass , styleLocation ) ;
1524
- delete this . _styles ;
1525
- }
1526
- return this ;
1509
+ private setVar ( el : HTMLElement , varName : string , varValue : string ) {
1510
+ el . style . setProperty ( varName , varValue ) ;
1527
1511
}
1528
1512
1529
1513
/** @internal updated/create the CSS styles for row based layout and initial margin setting */
1530
- protected _updateStyles ( forceUpdate = false , maxH ?: number ) : GridStack {
1531
- // call to delete existing one if we change cellHeight / margin
1532
- if ( forceUpdate ) {
1533
- this . _removeStylesheet ( ) ;
1534
- }
1535
-
1536
- if ( maxH === undefined ) maxH = this . getRow ( ) ;
1514
+ protected _updateStyles ( ) : GridStack {
1537
1515
this . _updateContainerHeight ( ) ;
1538
1516
1539
1517
// if user is telling us they will handle the CSS themselves by setting heights to 0. Do we need this opts really ??
1540
1518
if ( this . opts . cellHeight === 0 ) {
1541
1519
return this ;
1542
1520
}
1543
1521
1544
- let cellHeight = this . opts . cellHeight as number ;
1545
- let cellHeightUnit = this . opts . cellHeightUnit ;
1546
- let prefix = `.${ this . _styleSheetClass } > .${ this . opts . itemClass } ` ;
1522
+ const cellHeight = this . opts . cellHeight as number ;
1523
+ const cellHeightUnit = this . opts . cellHeightUnit ;
1524
+
1525
+ // these are done once only
1526
+ this . setVar ( this . el . parentElement , "--gs-cell-height" , `${ cellHeight } ${ cellHeightUnit } ` ) ;
1527
+ // content margins
1528
+ this . setVar ( this . el . parentElement , "--gs-item-margin-top" , `${ this . opts . marginTop } ${ this . opts . marginUnit } ` ) ;
1529
+ this . setVar ( this . el . parentElement , "--gs-item-margin-bottom" , `${ this . opts . marginBottom } ${ this . opts . marginUnit } ` ) ;
1530
+ this . setVar ( this . el . parentElement , "--gs-item-margin-right" , `${ this . opts . marginRight } ${ this . opts . marginUnit } ` ) ;
1531
+ this . setVar ( this . el . parentElement , "--gs-item-margin-left" , `${ this . opts . marginLeft } ${ this . opts . marginUnit } ` ) ;
1547
1532
1548
- // create one as needed
1549
- if ( ! this . _styles ) {
1550
- // insert style to parent (instead of 'head' by default) to support WebComponent
1551
- const styleLocation = this . opts . styleInHead ? undefined : this . el . parentNode as HTMLElement ;
1552
- this . _styles = Utils . createStylesheet ( this . _styleSheetClass , styleLocation , {
1553
- nonce : this . opts . nonce ,
1554
- } ) ;
1555
- if ( ! this . _styles ) return this ;
1556
- this . _styles . _max = 0 ;
1557
-
1558
- // these are done once only
1559
- Utils . addCSSRule ( this . _styles , prefix , `height: ${ cellHeight } ${ cellHeightUnit } ` ) ;
1560
- // content margins
1561
- let top : string = this . opts . marginTop + this . opts . marginUnit ;
1562
- let bottom : string = this . opts . marginBottom + this . opts . marginUnit ;
1563
- let right : string = this . opts . marginRight + this . opts . marginUnit ;
1564
- let left : string = this . opts . marginLeft + this . opts . marginUnit ;
1565
- let content = `${ prefix } > .grid-stack-item-content` ;
1566
- let placeholder = `.${ this . _styleSheetClass } > .grid-stack-placeholder > .placeholder-content` ;
1567
- Utils . addCSSRule ( this . _styles , content , `top: ${ top } ; right: ${ right } ; bottom: ${ bottom } ; left: ${ left } ;` ) ;
1568
- Utils . addCSSRule ( this . _styles , placeholder , `top: ${ top } ; right: ${ right } ; bottom: ${ bottom } ; left: ${ left } ;` ) ;
1569
- // resize handles offset (to match margin)
1570
- Utils . addCSSRule ( this . _styles , `${ prefix } > .ui-resizable-n` , `top: ${ top } ;` ) ;
1571
- Utils . addCSSRule ( this . _styles , `${ prefix } > .ui-resizable-s` , `bottom: ${ bottom } ` ) ;
1572
- Utils . addCSSRule ( this . _styles , `${ prefix } > .ui-resizable-ne` , `right: ${ right } ` ) ;
1573
- Utils . addCSSRule ( this . _styles , `${ prefix } > .ui-resizable-e` , `right: ${ right } ` ) ;
1574
- Utils . addCSSRule ( this . _styles , `${ prefix } > .ui-resizable-se` , `right: ${ right } ; bottom: ${ bottom } ` ) ;
1575
- Utils . addCSSRule ( this . _styles , `${ prefix } > .ui-resizable-nw` , `left: ${ left } ` ) ;
1576
- Utils . addCSSRule ( this . _styles , `${ prefix } > .ui-resizable-w` , `left: ${ left } ` ) ;
1577
- Utils . addCSSRule ( this . _styles , `${ prefix } > .ui-resizable-sw` , `left: ${ left } ; bottom: ${ bottom } ` ) ;
1578
- }
1579
-
1580
- // now update the height specific fields
1581
- maxH = maxH || this . _styles . _max ;
1582
- if ( maxH > this . _styles . _max ) {
1583
- let getHeight = ( rows : number ) : string => ( cellHeight * rows ) + cellHeightUnit ;
1584
- for ( let i = this . _styles . _max + 1 ; i <= maxH ; i ++ ) { // start at 1
1585
- Utils . addCSSRule ( this . _styles , `${ prefix } [gs-y="${ i } "]` , `top: ${ getHeight ( i ) } ` ) ;
1586
- Utils . addCSSRule ( this . _styles , `${ prefix } [gs-h="${ i + 1 } "]` , `height: ${ getHeight ( i + 1 ) } ` ) ; // start at 2
1587
- }
1588
- this . _styles . _max = maxH ;
1589
- }
1590
1533
return this ;
1591
1534
}
1592
1535
@@ -1645,17 +1588,27 @@ export class GridStack {
1645
1588
return this ;
1646
1589
}
1647
1590
1648
- /** @internal call to write position x,y,w,h attributes back to element */
1649
- protected _writePosAttr ( el : HTMLElement , n : GridStackPosition ) : GridStack {
1650
- if ( n . x !== undefined && n . x !== null ) { el . setAttribute ( 'gs-x' , String ( n . x ) ) ; }
1651
- if ( n . y !== undefined && n . y !== null ) { el . setAttribute ( 'gs-y' , String ( n . y ) ) ; }
1652
- n . w > 1 ? el . setAttribute ( 'gs-w' , String ( n . w ) ) : el . removeAttribute ( 'gs-w' ) ;
1653
- n . h > 1 ? el . setAttribute ( 'gs-h' , String ( n . h ) ) : el . removeAttribute ( 'gs-h' ) ;
1591
+ /**
1592
+ * Call to write position x,y,w,h attributes back to element
1593
+ * In addition, updates the inline top/height inline style as well
1594
+ * @internal
1595
+ */
1596
+ protected _writePosAttr ( el : HTMLElement , node : GridStackNode ) : GridStack {
1597
+ if ( node . x !== undefined && node . x !== null ) { el . setAttribute ( 'gs-x' , String ( node . x ) ) ; }
1598
+ if ( node . y !== undefined && node . y !== null ) { el . setAttribute ( 'gs-y' , String ( node . y ) ) ; }
1599
+ node . w > 1 ? el . setAttribute ( 'gs-w' , String ( node . w ) ) : el . removeAttribute ( 'gs-w' ) ;
1600
+ node . h > 1 ? el . setAttribute ( 'gs-h' , String ( node . h ) ) : el . removeAttribute ( 'gs-h' ) ;
1601
+ // Avoid overwriting the inline style of the draggable element, but update the placeholder
1602
+ if ( ! node . _moving || this . _placeholder === el ) {
1603
+ // Set inline style, refer CSS variables
1604
+ el . style . top = `calc(${ node . y } * var(--gs-cell-height))` ;
1605
+ el . style . height = `calc(${ node . h } * var(--gs-cell-height))` ;
1606
+ }
1654
1607
return this ;
1655
1608
}
1656
1609
1657
1610
/** @internal call to write any default attributes back to element */
1658
- protected _writeAttr ( el : HTMLElement , node : GridStackWidget ) : GridStack {
1611
+ protected _writeAttr ( el : HTMLElement , node : GridStackNode ) : GridStack {
1659
1612
if ( ! node ) return this ;
1660
1613
this . _writePosAttr ( el , node ) ;
1661
1614
@@ -2243,7 +2196,7 @@ export class GridStack {
2243
2196
this . _prepareElement ( el , true , node ) ;
2244
2197
if ( subGrid ) {
2245
2198
subGrid . parentGridItem = node ;
2246
- if ( ! subGrid . opts . styleInHead ) subGrid . _updateStyles ( true ) ; // re-create sub-grid styles now that we've moved
2199
+ subGrid . _updateStyles ( ) ; // re-create sub-grid styles now that we've moved
2247
2200
}
2248
2201
this . _updateContainerHeight ( ) ;
2249
2202
this . engine . addedNodes . push ( node ) ; // @ts -ignore
@@ -2406,7 +2359,7 @@ export class GridStack {
2406
2359
node . el = this . placeholder ;
2407
2360
node . _lastUiPosition = ui . position ;
2408
2361
node . _prevYPix = ui . position . top ;
2409
- node . _moving = ( event . type === 'dragstart' ) ; // 'dropover' are not initially moving so they can go exactly where they enter (will push stuff out of the way)
2362
+ node . _moving = ( event . type === 'dragstart' || event . type === 'resizestart' ) ; // 'dropover' are not initially moving so they can go exactly where they enter (will push stuff out of the way)
2410
2363
delete node . _lastTried ;
2411
2364
2412
2365
if ( event . type === 'dropover' && node . _temporaryRemoved ) {
0 commit comments