Merge pull request #706 from LarissaSmith/master

Added option in addTiledImage to replace tiledImage at index.
This commit is contained in:
Ian Gilman 2015-08-25 09:06:59 -07:00
commit d673a453c6
2 changed files with 45 additions and 13 deletions

View File

@ -1193,6 +1193,10 @@ $.extend( $.Viewer.prototype, $.EventSource.prototype, $.ControlDock.prototype,
* named 'getTileUrl', it is treated as a custom TileSource. * named 'getTileUrl', it is treated as a custom TileSource.
* @param {Number} [options.index] The index of the item. Added on top of * @param {Number} [options.index] The index of the item. Added on top of
* all other items if not specified. * all other items if not specified.
* @param {Boolean} [options.replace=false] If true, the item at options.index will be
* removed and the new item is added in its place. options.tileSource will be
* interpreted and fetched if necessary before the old item is removed to avoid leaving
* a gap in the world.
* @param {Number} [options.x=0] The X position for the image in viewport coordinates. * @param {Number} [options.x=0] The X position for the image in viewport coordinates.
* @param {Number} [options.y=0] The Y position for the image in viewport coordinates. * @param {Number} [options.y=0] The Y position for the image in viewport coordinates.
* @param {Number} [options.width=1] The width for the image in viewport coordinates. * @param {Number} [options.width=1] The width for the image in viewport coordinates.
@ -1216,9 +1220,15 @@ $.extend( $.Viewer.prototype, $.EventSource.prototype, $.ControlDock.prototype,
addTiledImage: function( options ) { addTiledImage: function( options ) {
$.console.assert(options, "[Viewer.addTiledImage] options is required"); $.console.assert(options, "[Viewer.addTiledImage] options is required");
$.console.assert(options.tileSource, "[Viewer.addTiledImage] options.tileSource is required"); $.console.assert(options.tileSource, "[Viewer.addTiledImage] options.tileSource is required");
$.console.assert(!options.replace || (options.index > -1 && options.index < this.world.getItemCount()),
"[Viewer.addTiledImage] if options.replace is used, options.index must be a valid index in Viewer.world");
var _this = this; var _this = this;
if (options.replace) {
options.replaceItem = _this.world.getItemAt(options.index);
}
this._hideMessage(); this._hideMessage();
if (options.placeholderFillStyle === undefined) { if (options.placeholderFillStyle === undefined) {
@ -1283,6 +1293,14 @@ $.extend( $.Viewer.prototype, $.EventSource.prototype, $.ControlDock.prototype,
_this._loadQueue.splice(0, 1); _this._loadQueue.splice(0, 1);
if (queueItem.options.replace) {
var newIndex = _this.world.getIndexOfItem(options.replaceItem);
if (newIndex != -1) {
options.index = newIndex;
}
_this.world.removeItem(options.replaceItem);
}
tiledImage = new $.TiledImage({ tiledImage = new $.TiledImage({
viewer: _this, viewer: _this,
source: queueItem.tileSource, source: queueItem.tileSource,

View File

@ -27,7 +27,7 @@
// ---------- // ----------
asyncTest( 'Multi-image operations', function() { asyncTest( 'Multi-image operations', function() {
expect( 21 ); expect( 24 );
viewer.addHandler( "open", function( ) { viewer.addHandler( "open", function( ) {
equal( 1, viewer.world.getItemCount( ), equal( 1, viewer.world.getItemCount( ),
"One item should be present after opening." ); "One item should be present after opening." );
@ -95,6 +95,19 @@
equal( viewer.world.getIndexOfItem( item2 ), 1, equal( viewer.world.getIndexOfItem( item2 ), 1,
"Item 2 should stay at index 1." ); "Item 2 should stay at index 1." );
options.index = 2;
options.replace = true;
viewer.addTiledImage( options );
viewer.world.addHandler( "add-item", function replaceAddItemHandler( event ) {
viewer.world.removeHandler( "add-item", replaceAddItemHandler );
var item4 = event.item;
equal( viewer.world.getItemCount( ), 4,
"4 items should still be present after replacing the second item." );
equal( viewer.world.getIndexOfItem( item4 ), 2,
"Item 4 should be added with index 2." );
equal( viewer.world.getIndexOfItem( item3 ), -1,
"Item 3 should be at index -1." );
viewer.world.addHandler( "remove-item", function removeItemHandler( event ) { viewer.world.addHandler( "remove-item", function removeItemHandler( event ) {
viewer.world.removeHandler( "remove-item", removeItemHandler ); viewer.world.removeHandler( "remove-item", removeItemHandler );
@ -104,8 +117,8 @@
"Item 1 should be at index 2." ); "Item 1 should be at index 2." );
equal( viewer.world.getIndexOfItem( item2 ), -1, equal( viewer.world.getIndexOfItem( item2 ), -1,
"Item 2 should be at index -1." ); "Item 2 should be at index -1." );
equal( viewer.world.getIndexOfItem( item3 ), 1, equal( viewer.world.getIndexOfItem( item4 ), 1,
"Item 3 should be at index 1." ); "Item 4 should be at index 1." );
start(); start();
}); });
@ -115,6 +128,7 @@
}); });
}); });
}); });
});
viewer.open( '/test/data/testpattern.dzi' ); viewer.open( '/test/data/testpattern.dzi' );
}); });