diff --git a/src/viewer.js b/src/viewer.js index d3847f95..fcd54dc1 100644 --- a/src/viewer.js +++ b/src/viewer.js @@ -1181,6 +1181,7 @@ $.extend( $.Viewer.prototype, $.EventSource.prototype, $.ControlDock.prototype, }, /** + * Remove a layer. If there is only one layer, close the viewer. * @function * @param {OpenSeadragon.Drawer} drawer The underlying drawer of the layer * to remove @@ -1193,7 +1194,14 @@ $.extend( $.Viewer.prototype, $.EventSource.prototype, $.ControlDock.prototype, return this; } if ( index === 0 ) { - throw new Error( "Cannot remove base layer." ); + if ( THIS[ this.hash ].sequenced ) { + throw new Error( "Cannot remove base layer when in sequence mode." ); + } + if ( this.drawers.length === 1 ) { + this.close(); + return this; + } + this.drawer = this.drawers[ 1 ]; } this.drawers.splice( index, 1 ); diff --git a/test/layers.js b/test/layers.js index 33b0ab73..78606303 100644 --- a/test/layers.js +++ b/test/layers.js @@ -162,11 +162,11 @@ var options = { tileSource: { type: 'legacy-image-pyramid', - levels: [ { + levels: [{ url: "data/A.png", width: 1000, height: 1000 - } ] + }] }, level: 0 }; @@ -180,12 +180,19 @@ equal( viewer.drawer, aDrawer, "Viewer.drawer should be set to aDrawer." ); viewer.setLayerLevel( aDrawer, 1 ); equal( viewer.drawer, testPatternDrawer, "Viewer.drawer should be set back to testPatternDrawer." ); - start( ); - } ); + + viewer.removeLayer( viewer.drawer ); + equal( viewer.drawer, aDrawer, "Viewer.drawer must be reassigned when removing base layer." ); + + viewer.removeLayer( viewer.drawer ); + ok( !viewer.isOpen(), "Viewer should be closed when removing last layer." ); + + start(); + }); viewer.addLayer( options ); - } ); + }); viewer.open( '/test/data/testpattern.dzi' ); - } ); + }); asyncTest( 'Layers and sequences', function() { expect( 1 ); @@ -209,37 +216,37 @@ // }]}] ); viewer.close(); - viewer = OpenSeadragon( { + viewer = OpenSeadragon({ id: 'layersexample', prefixUrl: '/build/openseadragon/images/', springStiffness: 100, // Faster animation = faster tests - tileSources: [ { + tileSources: [{ type: 'legacy-image-pyramid', - levels: [ { + levels: [{ url: "data/A.png", width: 1000, height: 1000 - } ] + }] }, { type: 'legacy-image-pyramid', - levels: [ { + levels: [{ url: "data/BBlue.png", width: 1000, height: 1000 - } ] } ] - - } ); + }] + }] + }); // End workaround var options = { tileSource: { type: 'legacy-image-pyramid', - levels: [ { + levels: [{ url: "data/CCyan.png", width: 1000, height: 1000 - } ] + }] } };