@@ -1438,6 +1438,129 @@ describe('regression >', () => {
14381438 expect ( DDManager . dropElement ) . toBe ( subDrop ( ) ) ;
14391439 } ) ;
14401440
1441+ it ( 'drops into a hidden (collapsed) sub-grid as its first widget, which its parent does not push out of the way' , ( ) => {
1442+ main . addWidget ( { x : 6 , y : 0 , w : 6 , h : 4 , id : 'g2' , subGridOpts : { acceptWidgets : true , children : [ { x : 0 , y : 0 , id : 'j' } ] } } ) ;
1443+ const g2 = main . engine . nodes . find ( n => n . id === 'g2' ) ! , sub2 = g2 . subGrid ! , sub2Drop = sub2 . el . ddElement ! . ddDroppable ! ;
1444+ const cell2Box = box ( 500 , 100 , 400 , 400 ) ;
1445+ vi . spyOn ( g2 . el ! , 'getBoundingClientRect' ) . mockImplementation ( ( ) => cell2Box ) ;
1446+ // a hidden grid isn't hit itself, only its cell (eg: the section header)
1447+ ( document as unknown as { elementFromPoint : ( x : number , y : number ) => Element } ) . elementFromPoint = ( x , y ) =>
1448+ inside ( cellBox , x , y ) ? sub . el : inside ( cell2Box , x , y ) ? g2 . el ! : inside ( mainBox , x , y ) ? main . el : document . body ;
1449+ ( sub2 . el as unknown as { checkVisibility : ( ) => boolean } ) . checkVisibility = ( ) => false ; // jsdom has no layout
1450+ delete g2 . subGrid ; // as an app may do while it's collapsed
1451+
1452+ moveTo ( 150 , 40 ) ; // out to the parent
1453+ const accepting = ( main as unknown as { _acceptingSubGrid ( n : GridStackNode , p : object ) : GridStackNode | undefined } ) . _acceptingSubGrid ;
1454+ expect ( accepting . call ( main , main . engine . nodes . find ( n => n . id === 'i' ) ! , { x : 6 , y : 0 } ) ) . toBe ( g2 ) ;
1455+ moveTo ( 650 , 200 ) ; // on its middle
1456+ expect ( DDManager . dropElement ) . toBe ( sub2Drop ) ;
1457+ expect ( sub2 . el . classList . contains ( 'ui-droppable-over' ) ) . toBe ( true ) ;
1458+ const yOf = ( id : string ) => sub2 . engine . nodes . find ( n => n . id === id ) ?. y ;
1459+ expect ( [ yOf ( 'i' ) , yOf ( 'j' ) ] ) . toEqual ( [ 0 , 1 ] ) ;
1460+ moveTo ( 150 , 40 ) ; // out again
1461+ expect ( sub2 . el . classList . contains ( 'ui-droppable-over' ) ) . toBe ( false ) ;
1462+ moveTo ( 650 , 200 ) ;
1463+ sub2Drop . drop ( new MouseEvent ( 'mouseup' ) ) ;
1464+ expect ( item . parentElement ) . toBe ( sub2 . el ) ;
1465+ expect ( [ yOf ( 'i' ) , yOf ( 'j' ) ] ) . toEqual ( [ 0 , 1 ] ) ;
1466+ } ) ;
1467+
1468+ it ( 'goes into a hidden (collapsed) sub-grid from its middle only, so near its edges it goes in between instead' , ( ) => {
1469+ // two collapsed ones stacked, as tall as the widget: some of it is always over one or the other
1470+ const hidden = ( y : number , b : DOMRect ) => {
1471+ const n = main . addWidget ( { x : 6 , y, w : 6 , h : 1 , subGridOpts : { acceptWidgets : true } } ) . gridstackNode ! ;
1472+ vi . spyOn ( n . el ! , 'getBoundingClientRect' ) . mockImplementation ( ( ) => b ) ;
1473+ ( n . subGrid ! . el as unknown as { checkVisibility : ( ) => boolean } ) . checkVisibility = ( ) => false ; // jsdom has no layout
1474+ return n ;
1475+ } ;
1476+ const pBox = box ( 500 , 100 , 400 , 100 ) , sBox = box ( 500 , 200 , 400 , 100 ) ; // middles at y 125-175 and 225-275
1477+ const p = hidden ( 0 , pBox ) , s = hidden ( 1 , sBox ) ;
1478+ const pDrop = p . subGrid ! . el . ddElement ! . ddDroppable ! , sDrop = s . subGrid ! . el . ddElement ! . ddDroppable ! ;
1479+ ( document as unknown as { elementFromPoint : ( x : number , y : number ) => Element } ) . elementFromPoint = ( x , y ) =>
1480+ inside ( cellBox , x , y ) ? sub . el : inside ( pBox , x , y ) ? p . el ! : inside ( sBox , x , y ) ? s . el ! : inside ( mainBox , x , y ) ? main . el : document . body ;
1481+
1482+ moveTo ( 150 , 40 ) ; // out to the parent
1483+ moveTo ( 650 , 200 ) ; // centered on the lower one
1484+ expect ( DDManager . dropElement ) . toBe ( sDrop ) ;
1485+ moveTo ( 650 , 160 ) ; // center on its top edge (60% of the widget still over it): in between, in the parent
1486+ expect ( DDManager . dropElement ) . toBe ( mainDrop ( ) ) ;
1487+ moveTo ( 650 , 200 ) ;
1488+ moveTo ( 650 , 125 ) ; // its top edge to the upper one's bottom edge: in between too, rather than straight in the upper one
1489+ expect ( DDManager . dropElement ) . toBe ( mainDrop ( ) ) ;
1490+ moveTo ( 650 , 100 ) ; // centered on the upper one
1491+ expect ( DDManager . dropElement ) . toBe ( pDrop ) ;
1492+ } ) ;
1493+
1494+ it ( 'moves past a collapsed sub-grid in its parent like any widget, but into it from its middle' , ( ) => {
1495+ vi . spyOn ( main , 'cellWidth' ) . mockReturnValue ( 800 / 12 ) ;
1496+ vi . spyOn ( main , 'getCellHeight' ) . mockReturnValue ( 100 ) ;
1497+ main . update ( cell , { h : 1 } ) ; // collapsed: 1 row, its middle at y 125-175 while at the top
1498+ cellBox = box ( 100 , 100 , 400 , 100 ) ;
1499+ ( sub . el as unknown as { checkVisibility : ( ) => boolean } ) . checkVisibility = ( ) => false ;
1500+ ( sub as unknown as { _onStartMoving ( ...a : unknown [ ] ) : void } ) . _onStartMoving ( item , new Event ( 'dragstart' ) , { position : { top : 0 , left : 0 } } , sub . engine . nodes [ 0 ] , 66 , 100 ) ;
1501+ moveTo ( 150 , 40 ) ; // into the parent above it
1502+ const g = main . engine . nodes . find ( n => n . id === 'g' ) ! , i = main . engine . nodes . find ( n => n . id === 'i' ) ! ;
1503+ expect ( [ i . y , g . y ] ) . toEqual ( [ 0 , 1 ] ) ;
1504+ // the parent's drag handling, the widget drawn there (top relative to the grid)
1505+ const dragTo = ( top : number ) => {
1506+ vi . spyOn ( self ( ) . helper ! , 'getBoundingClientRect' ) . mockReturnValue ( box ( 150 , 100 + top , 100 , 100 ) ) ;
1507+ ( main as unknown as { _dragOrResize ( ...a : unknown [ ] ) : void } )
1508+ . _dragOrResize ( item , { type : 'drag' , target : item } , { position : { left : 0 , top} } , i , 800 / 12 , 100 ) ;
1509+ } ;
1510+ dragTo ( 80 ) ; // down onto it: past it
1511+ expect ( [ g . y , i . y ] ) . toEqual ( [ 0 , 1 ] ) ;
1512+ dragTo ( 30 ) ; // back up, center on its middle: stay below it (going in)
1513+ expect ( [ g . y , i . y ] ) . toEqual ( [ 0 , 1 ] ) ;
1514+ dragTo ( - 30 ) ; // center on its top edge: above it
1515+ expect ( g . y ) . toBe ( 1 ) ;
1516+ expect ( i . y ) . toBeLessThan ( 1 ) ;
1517+ } ) ;
1518+
1519+ it ( 'goes by the grabbed handle, so a tall widget leaves by its header' , ( ) => {
1520+ mainBox = box ( 100 , 0 , 800 , 500 ) ; // room above the sub-grid
1521+ moveTo ( 150 , 80 ) ; // 80% of the widget still over the sub-grid
1522+ expect ( DDManager . dropElement ) . toBe ( subDrop ( ) ) ;
1523+ const content = item . querySelector < HTMLElement > ( '.grid-stack-item-content' ) ! ; // the handle
1524+ vi . spyOn ( item , 'getBoundingClientRect' ) . mockReturnValue ( box ( 150 , 150 , 100 , 100 ) ) ;
1525+ vi . spyOn ( content , 'getBoundingClientRect' ) . mockReturnValue ( box ( 150 , 150 , 100 , 20 ) ) ;
1526+ const d = dd as unknown as { mouseDownEvent : object , _handleBox ?: object , _getHandleBox ( ) : object } ;
1527+ d . mouseDownEvent = { target : content } ;
1528+ d . _handleBox = d . _getHandleBox ( ) ;
1529+ expect ( d . _handleBox ) . toEqual ( { left : 0 , top : 0 , width : 100 , height : 20 } ) ;
1530+ moveTo ( 150 , 81 ) ; // the handle is out
1531+ expect ( DDManager . dropElement ) . toBe ( mainDrop ( ) ) ;
1532+ } ) ;
1533+
1534+ it ( 'grows a small handle to 10% of the widget each way, kept inside it' , ( ) => {
1535+ const content = item . querySelector < HTMLElement > ( '.grid-stack-item-content' ) ! ;
1536+ const d = dd as unknown as { mouseDownEvent : object , _getHandleBox ( ) : object | undefined } ;
1537+ d . mouseDownEvent = { target : content } ;
1538+ vi . spyOn ( item , 'getBoundingClientRect' ) . mockReturnValue ( box ( 0 , 0 , 200 , 400 ) ) ;
1539+ const handleAt = ( b : DOMRect ) => { vi . spyOn ( content , 'getBoundingClientRect' ) . mockReturnValue ( b ) ; return d . _getHandleBox ( ) ; } ;
1540+ expect ( handleAt ( box ( 0 , 0 , 200 , 20 ) ) ) . toEqual ( { left : 0 , top : 0 , width : 200 , height : 40 } ) ; // header: taller, down from the top
1541+ expect ( handleAt ( box ( 90 , 190 , 4 , 4 ) ) ) . toEqual ( { left : 82 , top : 172 , width : 20 , height : 40 } ) ; // grip: around its center
1542+ expect ( handleAt ( box ( 196 , 396 , 4 , 4 ) ) ) . toEqual ( { left : 180 , top : 360 , width : 20 , height : 40 } ) ; // in the corner: slid back inside
1543+ expect ( handleAt ( box ( 0 , 0 , 0 , 0 ) ) ) . toBeUndefined ( ) ; // nothing to measure: the whole widget
1544+ } ) ;
1545+
1546+ it ( 'comes out of a sub-grid on the side its grabbed handle is, not the widget center' , ( ) => {
1547+ vi . spyOn ( main , 'cellWidth' ) . mockReturnValue ( 800 / 12 ) ;
1548+ vi . spyOn ( main , 'getCellHeight' ) . mockReturnValue ( 100 ) ;
1549+ vi . spyOn ( sub , 'getCellHeight' ) . mockReturnValue ( 100 ) ;
1550+ main . mode ( 'float' ) ;
1551+ main . update ( cell , { y : 1 , h : 2 } ) ; // rows 1-2, center at 2
1552+ cellBox = box ( 100 , 100 , 400 , 200 ) ;
1553+ mainBox = box ( 100 , 0 , 800 , 500 ) ;
1554+ sub . update ( item , { w : 6 , h : 3 } ) ;
1555+ ( sub as unknown as { _onStartMoving ( ...a : unknown [ ] ) : void } ) . _onStartMoving ( item , new Event ( 'dragstart' ) , { position : { top : 0 , left : 0 } } , sub . engine . nodes [ 0 ] , 66 , 100 ) ;
1556+ dd . _handleBox = { left : 0 , top : 0 , width : 400 , height : 60 } ; // its top 60px
1557+ vi . spyOn ( self ( ) . helper ! , 'getBoundingClientRect' ) . mockReturnValue ( box ( 100 , 60 , 400 , 300 ) ) ; // center below the sub-grid's, handle above
1558+ self ( ) . _checkDropTarget ( new MouseEvent ( 'mousemove' ) ) ;
1559+ const g = main . engine . nodes . find ( n => n . id === 'g' ) ! , i = main . engine . nodes . find ( n => n . id === 'i' ) ! ;
1560+ expect ( DDManager . dropElement ) . toBe ( mainDrop ( ) ) ;
1561+ expect ( i . y ) . toBeLessThan ( g . y ! ) ; // above it (by the widget center, it'd be below)
1562+ } ) ;
1563+
14411564 it ( 'coming out of a sub-grid below or beside it lands there in the parent, not above it (pushed out of the way)' , ( ) => {
14421565 vi . spyOn ( main , 'cellWidth' ) . mockReturnValue ( 800 / 12 ) ;
14431566 vi . spyOn ( main , 'getCellHeight' ) . mockReturnValue ( 100 ) ;
0 commit comments