diff --git a/src/viewer.js b/src/viewer.js index e5269a8b..eeee5fbf 100644 --- a/src/viewer.js +++ b/src/viewer.js @@ -207,6 +207,7 @@ $.Viewer = function( options ) { this._sequenceIndex = 0; this._firstOpen = true; this._updateRequestId = null; + this._loadQueue = []; this.currentOverlays = []; //Inherit some behaviors and properties @@ -1260,12 +1261,22 @@ $.extend( $.Viewer.prototype, $.EventSource.prototype, $.ControlDock.prototype, $.console.assert(options, "[Viewer.addTiledImage] options is required"); $.console.assert(options.tileSource, "[Viewer.addTiledImage] options.tileSource is required"); - var _this = this, - tileSource = options.tileSource; + var _this = this; this._hideMessage(); + var myQueueItem = { + options: options + }; + function raiseAddItemFailed( event ) { + for (var i = 0; i < _this._loadQueue; i++) { + if (_this._loadQueue[i] === myQueueItem) { + _this._loadQueue.splice(i, 1); + break; + } + } + /** * Raised when an error occurs while adding a item. * @event add-item-failed @@ -1284,7 +1295,9 @@ $.extend( $.Viewer.prototype, $.EventSource.prototype, $.ControlDock.prototype, } } - getTileSourceImplementation( this, tileSource, function( tileSource ) { + this._loadQueue.push(myQueueItem); + + getTileSourceImplementation( this, options.tileSource, function( tileSource ) { if ( tileSource instanceof Array ) { raiseAddItemFailed({ @@ -1295,59 +1308,72 @@ $.extend( $.Viewer.prototype, $.EventSource.prototype, $.ControlDock.prototype, return; } - var tiledImage = new $.TiledImage({ - viewer: _this, - source: tileSource, - viewport: _this.viewport, - drawer: _this.drawer, - tileCache: _this.tileCache, - imageLoader: _this.imageLoader, - x: options.x, - y: options.y, - width: options.width, - height: options.height, - imageLoaderLimit: _this.imageLoaderLimit, - minZoomImageRatio: _this.minZoomImageRatio, - wrapHorizontal: _this.wrapHorizontal, - wrapVertical: _this.wrapVertical, - immediateRender: _this.immediateRender, - blendTime: _this.blendTime, - alwaysBlend: _this.alwaysBlend, - minPixelRatio: _this.minPixelRatio, - debugMode: _this.debugMode, - debugGridColor: _this.debugGridColor - }); + myQueueItem.tileSource = tileSource; - _this.world.addItem( tiledImage, { - index: options.index - }); + // add everybody at the front of the queue that's ready to go + var queueItem, tiledImage, optionsClone; + while (_this._loadQueue.length) { + queueItem = _this._loadQueue[0]; + if (!queueItem.tileSource) { + break; + } - if (_this.collectionMode) { - _this.world.arrange({ - rows: _this.collectionRows, - layout: _this.collectionLayout, - tileSize: _this.collectionTileSize, - tileMargin: _this.collectionTileMargin - }); - } + _this._loadQueue.splice(0, 1); - if (_this.world.getItemCount() === 1 && !_this.preserveViewport) { - _this.viewport.goHome(true); - } - - if (_this.navigator) { - var optionsClone = $.extend({}, options, { - originalTiledImage: tiledImage, - tileSource: tileSource + tiledImage = new $.TiledImage({ + viewer: _this, + source: queueItem.tileSource, + viewport: _this.viewport, + drawer: _this.drawer, + tileCache: _this.tileCache, + imageLoader: _this.imageLoader, + x: queueItem.options.x, + y: queueItem.options.y, + width: queueItem.options.width, + height: queueItem.options.height, + imageLoaderLimit: _this.imageLoaderLimit, + minZoomImageRatio: _this.minZoomImageRatio, + wrapHorizontal: _this.wrapHorizontal, + wrapVertical: _this.wrapVertical, + immediateRender: _this.immediateRender, + blendTime: _this.blendTime, + alwaysBlend: _this.alwaysBlend, + minPixelRatio: _this.minPixelRatio, + debugMode: _this.debugMode, + debugGridColor: _this.debugGridColor }); - _this.navigator.addTiledImage(optionsClone); - } - - if (options.success) { - options.success({ - item: tiledImage + _this.world.addItem( tiledImage, { + index: queueItem.options.index }); + + if (_this.collectionMode) { + _this.world.arrange({ + rows: _this.collectionRows, + layout: _this.collectionLayout, + tileSize: _this.collectionTileSize, + tileMargin: _this.collectionTileMargin + }); + } + + if (_this.world.getItemCount() === 1 && !_this.preserveViewport) { + _this.viewport.goHome(true); + } + + if (_this.navigator) { + optionsClone = $.extend({}, queueItem.options, { + originalTiledImage: tiledImage, + tileSource: queueItem.tileSource + }); + + _this.navigator.addTiledImage(optionsClone); + } + + if (queueItem.options.success) { + queueItem.options.success({ + item: tiledImage + }); + } } }, function( event ) { event.options = options;