Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/js/LayoutManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ lm.utils.copy( lm.LayoutManager.prototype, {
*************************/
_$maximiseItem: function( contentItem ) {
if( this._maximisedItem !== null ) {
this._$minimiseItem( this._maximisedItem );
this._maximisedItem.toggleMaximise();
Comment thread
OfekShilon marked this conversation as resolved.
Resolved
}
this._maximisedItem = contentItem;
this._maximisedItem.addId( '__glMaximised' );
Expand Down
9 changes: 8 additions & 1 deletion src/js/controls/DragSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ lm.utils.copy( lm.controls.DragSource.prototype, {
* @returns {void}
*/
_onDragStart: function( x, y ) {
var itemConfig = this._itemConfig;
var itemConfig = this._itemConfig,
maximisedItem = this._layoutManager._maximisedItem;

// As when dragging a tab: the drop targets are hidden behind a maximised item
if( maximisedItem ) {
maximisedItem.toggleMaximise();
}

if( lm.utils.isFunction( itemConfig ) ) {
itemConfig = itemConfig();
}
Expand Down
35 changes: 31 additions & 4 deletions src/js/items/AbstractContentItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ lm.utils.copy( lm.items.AbstractContentItem.prototype, {
throw new Error( 'Can\'t remove child item. Unknown content item' );
}

this._minimiseMaximisedDescendant();

/**
* Call ._$destroy on the content item. This also calls ._$destroy on all its children
*/
Expand Down Expand Up @@ -172,6 +174,8 @@ lm.utils.copy( lm.items.AbstractContentItem.prototype, {
*/
replaceChild: function( oldChild, newChild, _$destroyOldChild ) {

this._minimiseMaximisedDescendant();

newChild = this.layoutManager._$normalizeContentItem( newChild );

var index = lm.utils.indexOf( oldChild, this.contentItems ),
Expand Down Expand Up @@ -241,13 +245,14 @@ lm.utils.copy( lm.items.AbstractContentItem.prototype, {
*/
toggleMaximise: function( e ) {
e && e.preventDefault();
if( this.isMaximised === true ) {
this.layoutManager._$minimiseItem( this );
} else {
// Set before the layout manager emits 'maximised' or 'minimised', so listeners see the new state
this.isMaximised = !this.isMaximised;
if( this.isMaximised ) {
this.layoutManager._$maximiseItem( this );
} else {
this.layoutManager._$minimiseItem( this );
}

this.isMaximised = !this.isMaximised;
this.emitBubblingEvent( 'stateChanged' );
},

Expand Down Expand Up @@ -445,6 +450,28 @@ lm.utils.copy( lm.items.AbstractContentItem.prototype, {
}
},

/**
* A maximised item's element is moved out of its parent into the root, leaving a
* placeholder in its place, until it is minimised again. Adding, removing or
* replacing items around it would leave its element or the placeholder in the
* wrong place, and resizing its siblings would shrink it to its share of a row or
* column, so items minimise any maximised descendant before doing either.
*
* @returns {void}
*/
_minimiseMaximisedDescendant: function() {
var maximisedItem = this.layoutManager._maximisedItem,
item = maximisedItem && maximisedItem.parent;

while( item ) {
if( item === this ) {
maximisedItem.toggleMaximise();
return;
}
item = item.parent;
}
},

/**
* Destroys this item ands its children
*
Expand Down
22 changes: 2 additions & 20 deletions src/js/items/RowOrColumn.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ lm.utils.copy( lm.items.RowOrColumn.prototype, {
throw new Error( 'Can\'t remove child. ContentItem is not child of this Row or Column' );
}

this._minimiseMaximisedDescendant();

/**
* Remove the splitter before the item or after if the item happens
* to be the first in the row/column
Expand Down Expand Up @@ -407,26 +409,6 @@ lm.utils.copy( lm.items.RowOrColumn.prototype, {
}
},

/**
* A maximised item's element is moved out of its parent into the root until it is
* minimised again, so new siblings can't be placed next to it, and resizing this
* item's children would shrink it to its share of this row or column.
*
* @returns {void}
*/
_minimiseMaximisedDescendant: function() {
var maximisedItem = this.layoutManager._maximisedItem,
item = maximisedItem && maximisedItem.parent;

while( item ) {
if( item === this ) {
maximisedItem.toggleMaximise();
return;
}
item = item.parent;
}
},

/**
* Instantiates a new lm.controls.Splitter, binds events to it and adds
* it to the array of splitters at the position specified as the index argument
Expand Down
243 changes: 241 additions & 2 deletions test/maximise-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@ function component() {
return { type: 'component', componentName: 'testComponent' };
}

function stack( componentCount ) {
var content = [], i;
for( i = 0; i < ( componentCount || 1 ); i++ ) {
content.push( component() );
}
return { type: 'stack', content: content };
}

function layoutOf( item ) {
return testTools.createLayout( { content: [ item ] } );
}

function rowOfStacks( count ) {
var content = [], i;
for( i = 0; i < count; i++ ) {
content.push( { type: 'stack', content: [ component() ] } );
content.push( stack() );
}
return testTools.createLayout( { content: [ { type: 'row', content: content } ] } );
return layoutOf( { type: 'row', content: content } );
}

function expectNothingMaximised( layout ) {
Expand All @@ -20,6 +32,25 @@ function expectNothingMaximised( layout ) {
expect( $( '.lm_maximise_place' ).length ).toBe( 0 );
}

function expectMaximised( layout, item ) {
expect( layout._maximisedItem ).toBe( item );
expect( layout.root.getItemsByFilter( function( item ) { return item.isMaximised; } ) ).toEqual( [ item ] );
expect( item.element.width() ).toBe( layout.container.width() );
expect( item.element.height() ).toBe( layout.container.height() );
}

/**
* Nothing is maximised, nothing is saved as maximised, and every row and
* column holds its children's elements in order.
*/
function expectRestored( layout ) {
expectNothingMaximised( layout );
expect( layout.toConfig().maximisedItemId ).toBe( null );
layout.root.getItemsByFilter( function( item ) {
return item.isRow || item.isColumn;
} ).forEach( expectChildrenInPlace );
}

/**
* Each child's element, and a splitter between each pair, in order, is what
* the row or column's element holds once nothing in it is maximised.
Expand Down Expand Up @@ -133,3 +164,211 @@ describe( 'Adding a child to a row or column while an item elsewhere is maximise
myLayout.destroy();
} );
} );

describe( 'Removing an item while another item is maximised', function() {

it( 'restores the maximised item when a sibling is removed', function() {
var layout = rowOfStacks( 3 ),
row = layout.root.contentItems[ 0 ];
row.contentItems[ 1 ].toggleMaximise();

row.removeChild( row.contentItems[ 0 ] );

expect( row.contentItems.length ).toBe( 2 );
expectRestored( layout );
layout.destroy();
} );

it( 'restores the maximised item when removing its only sibling collapses their row', function() {
var layout = layoutOf( {
type: 'column',
content: [ { type: 'row', content: [ stack(), stack() ] }, stack() ]
} ),
column = layout.root.contentItems[ 0 ],
row = column.contentItems[ 0 ],
maximised = row.contentItems[ 1 ];
maximised.toggleMaximise();

row.removeChild( row.contentItems[ 0 ] );

expect( column.contentItems[ 0 ] ).toBe( maximised );
expectRestored( layout );
layout.destroy();
} );

it( 'restores the maximised item when an item elsewhere in the row is removed', function() {
var layout = layoutOf( {
type: 'row',
content: [ stack(), { type: 'column', content: [ stack(), stack() ] }, stack() ]
} ),
row = layout.root.contentItems[ 0 ];
row.contentItems[ 1 ].contentItems[ 1 ].toggleMaximise();

row.removeChild( row.contentItems[ 2 ] );

expectRestored( layout );
layout.destroy();
} );

it( 'keeps a stack maximised when one of its tabs is closed', function() {
var layout = layoutOf( { type: 'row', content: [ stack(), stack( 2 ) ] } ),
maximised = layout.root.contentItems[ 0 ].contentItems[ 1 ];
maximised.toggleMaximise();

maximised.removeChild( maximised.contentItems[ 0 ] );

expectMaximised( layout, maximised );
layout.destroy();
} );
} );

describe( 'Closing a maximised item', function() {

it( 'leaves nothing maximised', function() {
var layout = rowOfStacks( 3 ),
row = layout.root.contentItems[ 0 ],
maximised = row.contentItems[ 1 ];
maximised.toggleMaximise();

maximised.removeChild( maximised.contentItems[ 0 ] );

expect( row.contentItems.length ).toBe( 2 );
expectRestored( layout );
layout.destroy();
} );

it( 'leaves nothing maximised when it was the only item', function() {
var layout = layoutOf( stack() ),
maximised = layout.root.contentItems[ 0 ];
maximised.toggleMaximise();

maximised.removeChild( maximised.contentItems[ 0 ] );

expect( layout.root.contentItems.length ).toBe( 0 );
expectRestored( layout );
layout.destroy();
} );
} );

describe( 'Dropping an item while another item is maximised', function() {

it( 'restores the maximised stack when an item is dropped on its edge', function() {
var layout = rowOfStacks( 2 ),
row = layout.root.contentItems[ 0 ],
maximised = row.contentItems[ 1 ];
maximised.toggleMaximise();

maximised._dropSegment = 'bottom';
maximised._$onDrop( layout._$normalizeContentItem( component() ) );

expect( row.contentItems[ 1 ].isColumn ).toBe( true );
expectRestored( layout );
layout.destroy();
} );

it( 'restores the maximised stack when an item is dropped on the edge of its sibling', function() {
var layout = rowOfStacks( 2 ),
row = layout.root.contentItems[ 0 ];
row.contentItems[ 0 ].toggleMaximise();

row.contentItems[ 1 ]._dropSegment = 'bottom';
row.contentItems[ 1 ]._$onDrop( layout._$normalizeContentItem( component() ) );

expect( row.contentItems[ 1 ].isColumn ).toBe( true );
expectRestored( layout );
layout.destroy();
} );

it( 'restores the maximised stack when an item is dropped on the edge of the layout', function() {
var layout = rowOfStacks( 2 );
layout.root.contentItems[ 0 ].contentItems[ 0 ].toggleMaximise();

layout.root._$onDrop( layout._$normalizeContentItem( component() ), { side: 'y2' } );

expect( layout.root.contentItems[ 0 ].isColumn ).toBe( true );
expectRestored( layout );
layout.destroy();
} );

it( 'restores the maximised stack before an item is dragged from a drag source', function() {
var layout = layoutOf( {
type: 'row',
content: [ {
type: 'stack',
content: [ {
type: 'component',
componentName: 'testComponent',
componentState: { html: '<div class="drag-source"></div>' }
} ]
}, stack() ]
} ),
maximised = layout.root.contentItems[ 0 ].contentItems[ 0 ],
dragSource = maximised.element.find( '.drag-source' ),
maximisedWhileDragging,
mouse;
layout.createDragSource( dragSource, {
type: 'component',
componentName: 'testComponent',
componentState: { html: '<div class="dragged"></div>' }
} );
maximised.toggleMaximise();

mouse = $.Event( 'mousedown' );
mouse.pageX = dragSource.position().left;
mouse.pageY = dragSource.position().top;
mouse.button = 0;
dragSource.trigger( mouse );

mouse = $.Event( 'mousemove' );
mouse.pageX = dragSource.position().left + 50;
mouse.pageY = dragSource.position().top + 50;
dragSource.trigger( mouse );
maximisedWhileDragging = layout._maximisedItem;

dragSource.trigger( 'mouseup' );

expect( maximisedWhileDragging ).toBe( null );
expect( $( '.dragged' ).length ).toBe( 1 );
expectRestored( layout );
layout.destroy();
} );
} );

describe( 'Maximising an item while another item is maximised', function() {

it( 'minimises the other item', function() {
var layout = rowOfStacks( 2 ),
row = layout.root.contentItems[ 0 ],
first = row.contentItems[ 0 ],
second = row.contentItems[ 1 ];
first.toggleMaximise();

second.toggleMaximise();

expect( first.isMaximised ).toBe( false );
expectMaximised( layout, second );

second.toggleMaximise();
expectRestored( layout );
layout.destroy();
} );

it( 'minimises the other item when maximised from its maximised listener', function() {
var layout = rowOfStacks( 2 ),
row = layout.root.contentItems[ 0 ],
first = row.contentItems[ 0 ],
second = row.contentItems[ 1 ];
first.on( 'maximised', function() {
second.toggleMaximise();
} );

first.toggleMaximise();

expect( first.isMaximised ).toBe( false );
expectMaximised( layout, second );

second.toggleMaximise();
expectRestored( layout );
layout.destroy();
} );
} );
Loading