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