2011-12-05 22:50:25 -05:00
|
|
|
|
|
|
|
(function( $ ){
|
|
|
|
|
2012-01-25 14:14:02 -05:00
|
|
|
/**
|
|
|
|
* @class
|
|
|
|
*/
|
2012-01-23 22:48:45 -05:00
|
|
|
$.DziTileSource = function( width, height, tileSize, tileOverlap, tilesUrl, fileFormat, displayRects ) {
|
|
|
|
var i,
|
|
|
|
rect,
|
|
|
|
level;
|
2011-12-05 22:50:25 -05:00
|
|
|
|
2012-01-23 22:48:45 -05:00
|
|
|
$.TileSource.call( this, width, height, tileSize, tileOverlap, null, null );
|
2011-12-05 22:50:25 -05:00
|
|
|
|
2012-01-23 22:48:45 -05:00
|
|
|
this._levelRects = {};
|
|
|
|
this.tilesUrl = tilesUrl;
|
|
|
|
this.fileFormat = fileFormat;
|
2011-12-05 22:50:25 -05:00
|
|
|
this.displayRects = displayRects;
|
2011-12-13 18:34:12 -05:00
|
|
|
|
|
|
|
if ( this.displayRects ) {
|
2012-01-23 22:48:45 -05:00
|
|
|
for ( i = this.displayRects.length - 1; i >= 0; i-- ) {
|
|
|
|
rect = this.displayRects[ i ];
|
|
|
|
for ( level = rect.minLevel; level <= rect.maxLevel; level++ ) {
|
|
|
|
if ( !this._levelRects[ level ] ) {
|
|
|
|
this._levelRects[ level ] = [];
|
2011-12-13 18:29:25 -05:00
|
|
|
}
|
2012-01-23 22:48:45 -05:00
|
|
|
this._levelRects[ level ].push( rect );
|
2011-12-05 22:50:25 -05:00
|
|
|
}
|
|
|
|
}
|
2011-12-13 18:34:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
$.extend( $.DziTileSource.prototype, $.TileSource.prototype, {
|
2011-12-05 22:50:25 -05:00
|
|
|
|
2012-01-23 22:48:45 -05:00
|
|
|
getTileUrl: function( level, x, y ) {
|
|
|
|
return [ this.tilesUrl, level, '/', x, '_', y, '.', this.fileFormat ].join( '' );
|
2011-12-13 18:29:25 -05:00
|
|
|
},
|
2011-12-05 22:50:25 -05:00
|
|
|
|
2012-01-23 22:48:45 -05:00
|
|
|
tileExists: function( level, x, y ) {
|
|
|
|
var rects = this._levelRects[ level ],
|
|
|
|
rect,
|
|
|
|
scale,
|
|
|
|
xMin,
|
|
|
|
yMin,
|
|
|
|
xMax,
|
|
|
|
yMax,
|
|
|
|
i;
|
|
|
|
|
|
|
|
if ( !rects || !rects.length ) {
|
2011-12-13 18:29:25 -05:00
|
|
|
return true;
|
|
|
|
}
|
2011-12-05 22:50:25 -05:00
|
|
|
|
2012-01-23 22:48:45 -05:00
|
|
|
for ( i = rects.length - 1; i >= 0; i-- ) {
|
|
|
|
rect = rects[ i ];
|
2011-12-05 22:50:25 -05:00
|
|
|
|
2012-01-23 22:48:45 -05:00
|
|
|
if ( level < rect.minLevel || level > rect.maxLevel ) {
|
2011-12-13 18:29:25 -05:00
|
|
|
continue;
|
|
|
|
}
|
2011-12-05 22:50:25 -05:00
|
|
|
|
2012-01-23 22:48:45 -05:00
|
|
|
scale = this.getLevelScale( level );
|
|
|
|
xMin = rect.x * scale;
|
|
|
|
yMin = rect.y * scale;
|
|
|
|
xMax = xMin + rect.width * scale;
|
|
|
|
yMax = yMin + rect.height * scale;
|
2011-12-05 22:50:25 -05:00
|
|
|
|
2012-01-23 22:48:45 -05:00
|
|
|
xMin = Math.floor( xMin / this.tileSize );
|
|
|
|
yMin = Math.floor( yMin / this.tileSize );
|
|
|
|
xMax = Math.ceil( xMax / this.tileSize );
|
|
|
|
yMax = Math.ceil( yMax / this.tileSize );
|
2011-12-05 22:50:25 -05:00
|
|
|
|
2012-01-23 22:48:45 -05:00
|
|
|
if ( xMin <= x && x < xMax && yMin <= y && y < yMax ) {
|
2011-12-13 18:29:25 -05:00
|
|
|
return true;
|
|
|
|
}
|
2011-12-05 22:50:25 -05:00
|
|
|
}
|
|
|
|
|
2011-12-13 18:29:25 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2011-12-05 22:50:25 -05:00
|
|
|
|
2012-01-25 14:14:02 -05:00
|
|
|
/**
|
|
|
|
* @static
|
|
|
|
*/
|
2011-12-13 18:24:04 -05:00
|
|
|
$.DziTileSourceHelper = {
|
2012-01-23 22:48:45 -05:00
|
|
|
|
|
|
|
createFromXml: function( xmlUrl, xmlString, callback ) {
|
|
|
|
var async = typeof (callback) == "function",
|
|
|
|
error = null,
|
|
|
|
urlParts,
|
|
|
|
filename,
|
|
|
|
lastDot,
|
|
|
|
tilesUrl,
|
|
|
|
handler;
|
|
|
|
|
|
|
|
if ( !xmlUrl ) {
|
|
|
|
this.error = $.getString( "Errors.Empty" );
|
|
|
|
if ( async ) {
|
|
|
|
window.setTimeout( function() {
|
|
|
|
callback( null, error );
|
|
|
|
}, 1 );
|
2011-12-05 22:50:25 -05:00
|
|
|
return null;
|
|
|
|
}
|
2012-01-23 22:48:45 -05:00
|
|
|
throw new Error( error );
|
2011-12-05 22:50:25 -05:00
|
|
|
}
|
|
|
|
|
2012-01-23 22:48:45 -05:00
|
|
|
urlParts = xmlUrl.split( '/' );
|
|
|
|
filename = urlParts[ urlParts.length - 1 ];
|
|
|
|
lastDot = filename.lastIndexOf( '.' );
|
2011-12-05 22:50:25 -05:00
|
|
|
|
2012-01-23 22:48:45 -05:00
|
|
|
if ( lastDot > -1 ) {
|
|
|
|
urlParts[ urlParts.length - 1 ] = filename.slice( 0, lastDot );
|
2011-12-05 22:50:25 -05:00
|
|
|
}
|
|
|
|
|
2012-01-23 22:48:45 -05:00
|
|
|
tilesUrl = urlParts.join( '/' ) + "_files/";
|
|
|
|
|
|
|
|
function finish( func, obj ) {
|
2011-12-05 22:50:25 -05:00
|
|
|
try {
|
2012-01-23 22:48:45 -05:00
|
|
|
return func( obj, tilesUrl );
|
|
|
|
} catch ( e ) {
|
|
|
|
if ( async ) {
|
2011-12-05 22:50:25 -05:00
|
|
|
return null;
|
|
|
|
} else {
|
2011-12-13 18:17:46 -05:00
|
|
|
throw e;
|
2011-12-05 22:50:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-23 22:48:45 -05:00
|
|
|
|
|
|
|
if ( async ) {
|
|
|
|
if ( xmlString ) {
|
|
|
|
handler = $.delegate( this, this.processXml );
|
|
|
|
window.setTimeout( function() {
|
|
|
|
var source = finish( handler, $.parseXml( xmlString ) );
|
|
|
|
// call after finish sets error
|
|
|
|
callback( source, error );
|
2011-12-05 22:50:25 -05:00
|
|
|
}, 1);
|
|
|
|
} else {
|
2012-01-23 22:48:45 -05:00
|
|
|
handler = $.delegate( this, this.processResponse );
|
|
|
|
$.makeAjaxRequest( xmlUrl, function( xhr ) {
|
|
|
|
var source = finish( handler, xhr );
|
|
|
|
// call after finish sets error
|
|
|
|
callback( source, error );
|
2011-12-05 22:50:25 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2012-01-23 22:48:45 -05:00
|
|
|
if ( xmlString ) {
|
|
|
|
return finish(
|
|
|
|
$.delegate( this, this.processXml ),
|
|
|
|
$.parseXml( xmlString )
|
|
|
|
);
|
2011-12-05 22:50:25 -05:00
|
|
|
} else {
|
2012-01-23 22:48:45 -05:00
|
|
|
return finish(
|
|
|
|
$.delegate( this, this.processResponse ),
|
|
|
|
$.makeAjaxRequest( xmlUrl )
|
|
|
|
);
|
2011-12-05 22:50:25 -05:00
|
|
|
}
|
|
|
|
},
|
2012-01-23 22:48:45 -05:00
|
|
|
processResponse: function( xhr, tilesUrl ) {
|
|
|
|
var status,
|
|
|
|
statusText,
|
|
|
|
doc = null;
|
|
|
|
|
|
|
|
if ( !xhr ) {
|
|
|
|
throw new Error( $.getString( "Errors.Security" ) );
|
|
|
|
} else if ( xhr.status !== 200 && xhr.status !== 0 ) {
|
|
|
|
status = xhr.status;
|
|
|
|
statusText = ( status == 404 ) ?
|
|
|
|
"Not Found" :
|
|
|
|
xhr.statusText;
|
|
|
|
throw new Error( $.getString( "Errors.Status", status, statusText ) );
|
2011-12-05 22:50:25 -05:00
|
|
|
}
|
|
|
|
|
2012-01-23 22:48:45 -05:00
|
|
|
if ( xhr.responseXML && xhr.responseXML.documentElement ) {
|
2011-12-05 22:50:25 -05:00
|
|
|
doc = xhr.responseXML;
|
2012-01-23 22:48:45 -05:00
|
|
|
} else if ( xhr.responseText ) {
|
|
|
|
doc = $.parseXml( xhr.responseText );
|
2011-12-05 22:50:25 -05:00
|
|
|
}
|
|
|
|
|
2012-01-23 22:48:45 -05:00
|
|
|
return this.processXml( doc, tilesUrl );
|
2011-12-05 22:50:25 -05:00
|
|
|
},
|
|
|
|
|
2012-01-23 22:48:45 -05:00
|
|
|
processXml: function( xmlDoc, tilesUrl ) {
|
|
|
|
|
|
|
|
if ( !xmlDoc || !xmlDoc.documentElement ) {
|
|
|
|
throw new Error( $.getString( "Errors.Xml" ) );
|
2011-12-05 22:50:25 -05:00
|
|
|
}
|
|
|
|
|
2012-01-23 22:48:45 -05:00
|
|
|
var root = xmlDoc.documentElement,
|
|
|
|
rootName = root.tagName;
|
2011-12-05 22:50:25 -05:00
|
|
|
|
2012-01-23 22:48:45 -05:00
|
|
|
if ( rootName == "Image" ) {
|
2011-12-05 22:50:25 -05:00
|
|
|
try {
|
2012-01-23 22:48:45 -05:00
|
|
|
return this.processDzi( root, tilesUrl );
|
|
|
|
} catch ( e ) {
|
|
|
|
throw (e instanceof Error) ?
|
|
|
|
e :
|
|
|
|
new Error( $.getString("Errors.Dzi") );
|
2011-12-05 22:50:25 -05:00
|
|
|
}
|
2012-01-23 22:48:45 -05:00
|
|
|
} else if ( rootName == "Collection" ) {
|
|
|
|
throw new Error( $.getString( "Errors.Dzc" ) );
|
|
|
|
} else if ( rootName == "Error" ) {
|
|
|
|
return this.processError( root );
|
2011-12-05 22:50:25 -05:00
|
|
|
}
|
|
|
|
|
2012-01-23 22:48:45 -05:00
|
|
|
throw new Error( $.getString( "Errors.Dzi" ) );
|
2011-12-05 22:50:25 -05:00
|
|
|
},
|
|
|
|
|
2012-01-23 22:48:45 -05:00
|
|
|
processDzi: function( imageNode, tilesUrl ) {
|
|
|
|
var fileFormat = imageNode.getAttribute( "Format" ),
|
|
|
|
sizeNode = imageNode.getElementsByTagName( "Size" )[ 0 ],
|
|
|
|
dispRectNodes = imageNode.getElementsByTagName( "DisplayRect" ),
|
|
|
|
width = parseInt( sizeNode.getAttribute( "Width" ) ),
|
|
|
|
height = parseInt( sizeNode.getAttribute( "Height" ) ),
|
|
|
|
tileSize = parseInt( imageNode.getAttribute( "TileSize" ) ),
|
|
|
|
tileOverlap = parseInt( imageNode.getAttribute( "Overlap" ) ),
|
|
|
|
dispRects = [],
|
|
|
|
dispRectNode,
|
|
|
|
rectNode,
|
|
|
|
i;
|
|
|
|
|
|
|
|
if ( !$.imageFormatSupported( fileFormat ) ) {
|
|
|
|
throw new Error(
|
|
|
|
$.getString( "Errors.ImageFormat", fileFormat.toUpperCase() )
|
|
|
|
);
|
2011-12-05 22:50:25 -05:00
|
|
|
}
|
|
|
|
|
2012-01-23 22:48:45 -05:00
|
|
|
for ( i = 0; i < dispRectNodes.length; i++ ) {
|
|
|
|
dispRectNode = dispRectNodes[ i ];
|
|
|
|
rectNode = dispRectNode.getElementsByTagName( "Rect" )[ 0 ];
|
2011-12-05 22:50:25 -05:00
|
|
|
|
2012-01-23 22:48:45 -05:00
|
|
|
dispRects.push( new $.DisplayRect(
|
|
|
|
parseInt( rectNode.getAttribute( "X" ) ),
|
|
|
|
parseInt( rectNode.getAttribute( "Y" ) ),
|
|
|
|
parseInt( rectNode.getAttribute( "Width" ) ),
|
|
|
|
parseInt( rectNode.getAttribute( "Height" ) ),
|
2011-12-05 22:50:25 -05:00
|
|
|
0, // ignore MinLevel attribute, bug in Deep Zoom Composer
|
2012-01-23 22:48:45 -05:00
|
|
|
parseInt( dispRectNode.getAttribute( "MaxLevel" ) )
|
2011-12-05 22:50:25 -05:00
|
|
|
));
|
|
|
|
}
|
2012-01-23 22:48:45 -05:00
|
|
|
return new $.DziTileSource(
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
tileSize,
|
|
|
|
tileOverlap,
|
|
|
|
tilesUrl,
|
|
|
|
fileFormat,
|
|
|
|
dispRects
|
|
|
|
);
|
2011-12-05 22:50:25 -05:00
|
|
|
},
|
|
|
|
|
2012-01-23 22:48:45 -05:00
|
|
|
processError: function( errorNode ) {
|
|
|
|
var messageNode = errorNode.getElementsByTagName( "Message" )[ 0 ],
|
|
|
|
message = messageNode.firstChild.nodeValue;
|
2011-12-05 22:50:25 -05:00
|
|
|
|
2011-12-13 18:17:46 -05:00
|
|
|
throw new Error(message);
|
2011-12-05 22:50:25 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}( OpenSeadragon ));
|