From cd62ba7eca66769d9e1062fadf78ea42ad62b89a Mon Sep 17 00:00:00 2001 From: Antoine Vandecreme Date: Sat, 11 Jan 2014 19:15:28 -0500 Subject: [PATCH] Prevent adding collections as layers. --- src/viewer.js | 38 ++++++++++++++++++++++++++------------ test/layers.js | 39 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 63 insertions(+), 14 deletions(-) diff --git a/src/viewer.js b/src/viewer.js index 5e536336..6d57d945 100644 --- a/src/viewer.js +++ b/src/viewer.js @@ -1012,7 +1012,32 @@ $.extend( $.Viewer.prototype, $.EventSource.prototype, $.ControlDock.prototype, throw new Error( "No tile source provided as new layer." ); } + function raiseAddLayerFailed( event ) { + /** + * Raised when an error occurs while adding a layer. + * @event add-layer-failed + * @memberOf OpenSeadragon.Viewer + * @type {object} + * @property {OpenSeadragon.Viewer} eventSource - A reference to the Viewer which raised the event. + * @property {String} message + * @property {String} source + * @property {Object} options The options passed to the addLayer method. + * @property {?Object} userData - Arbitrary subscriber-defined object. + */ + _this.raiseEvent( 'add-layer-failed', event ); + } + getTileSourceImplementation( this, tileSource, function( tileSource ) { + + if ( tileSource instanceof Array ) { + raiseAddLayerFailed({ + message: "Collections can not be added as layers.", + source: tileSource, + options: options + }); + return; + } + var drawer = new $.Drawer({ viewer: _this, source: tileSource, @@ -1054,18 +1079,7 @@ $.extend( $.Viewer.prototype, $.EventSource.prototype, $.ControlDock.prototype, }); }, function( event ) { event.options = options; - /** - * Raised when an error occurs while adding a layer. - * @event add-layer-failed - * @memberOf OpenSeadragon.Viewer - * @type {object} - * @property {OpenSeadragon.Viewer} eventSource - A reference to the Viewer which raised the event. - * @property {String} message - * @property {String} source - * @property {Object} options The options passed to the addLayer method. - * @property {?Object} userData - Arbitrary subscriber-defined object. - */ - _this.raiseEvent( 'add-layer-failed', event ); + raiseAddLayerFailed(event); } ); return this; diff --git a/test/layers.js b/test/layers.js index a0fe4883..50aff801 100644 --- a/test/layers.js +++ b/test/layers.js @@ -37,7 +37,7 @@ tileSource: { type: 'legacy-image-pyramid', levels: [ { - url: "test/data/A.png", + url: "data/A.png", width: 1000, height: 1000 } ] @@ -89,7 +89,7 @@ "The layer at level 2 should be layer1." ); options.level = 2; - options.tileSource.levels[0].url = "test/data/CCyan.png"; + options.tileSource.levels[0].url = "data/CCyan.png"; options.opacity = 0.5; viewer.addLayer( options ); viewer.addHandler( "add-layer", function addThirdLayerHandler( event ) { @@ -125,5 +125,40 @@ viewer.open( '/test/data/testpattern.dzi' ); }); + asyncTest( 'Collections as layers', function() { + var options = { + tileSource: [{ + type: 'legacy-image-pyramid', + levels: [{ + url: "data/A.png", + width: 1000, + height: 1000 + }] + }, { + type: 'legacy-image-pyramid', + levels: [{ + url: "data/BBlue.png", + width: 1000, + height: 1000 + }] + }] + }; + + viewer.addHandler( "open", function openHandler() { + viewer.removeHandler( "open", openHandler ); + + viewer.addHandler( "add-layer-failed", + function addLayerFailedHandler( event ) { + viewer.removeHandler( "add-layer-failed", addLayerFailedHandler ); + + equal( event.message, "Collections can not be added as layers." ); + equal( event.options, options, "Layer failed event should give the options." ); + start(); + } ); + viewer.addLayer( options ); + + }); + viewer.open( '/test/data/testpattern.dzi' ); + }); })();