Merge pull request #379 from avandecreme/ajax-file

Fix ajax call for file: and ftp: #73
This commit is contained in:
iangilman 2014-04-24 10:02:44 -07:00
commit 669925776a
3 changed files with 100 additions and 55 deletions

View File

@ -1802,38 +1802,64 @@ window.OpenSeadragon = window.OpenSeadragon || function( options ){
return value ? value : null;
},
createAjaxRequest: function(){
var request;
if ( window.XMLHttpRequest ) {
$.createAjaxRequest = function( ){
return new XMLHttpRequest();
};
request = new XMLHttpRequest();
} else if ( window.ActiveXObject ) {
/*jshint loopfunc:true*/
/* global ActiveXObject:true */
for ( var i = 0; i < ACTIVEX.length; i++ ) {
try {
request = new ActiveXObject( ACTIVEX[ i ] );
$.createAjaxRequest = function( ){
return new ActiveXObject( ACTIVEX[ i ] );
};
break;
} catch (e) {
continue;
}
}
/**
* Retrieves the protocol used by the url. The url can either be absolute
* or relative.
* @function
* @private
* @param {String} url The url to retrieve the protocol from.
* @return {String} The protocol (http:, https:, file:, ftp: ...)
*/
getUrlProtocol: function( url ) {
var match = url.match(/^([a-z]+:)\/\//i);
if ( match === null ) {
// Relative URL, retrive the protocol from window.location
return window.location.protocol;
}
if ( !request ) {
throw new Error( "Browser doesn't support XMLHttpRequest." );
}
return request;
return match[1].toLowerCase();
},
/**
* Create an XHR object
* @private
* @param {type} [local] If set to true, the XHR will be file: protocol
* compatible if possible (but may raise a warning in the browser).
* @returns {XMLHttpRequest}
*/
createAjaxRequest: function( local ) {
// IE11 does not support window.ActiveXObject so we just try to
// create one to see if it is supported.
// See: http://msdn.microsoft.com/en-us/library/ie/dn423948%28v=vs.85%29.aspx
var supportActiveX;
try {
/* global ActiveXObject:true */
supportActiveX = !!new ActiveXObject( "Microsoft.XMLHTTP" );
} catch( e ) {
supportActiveX = false;
}
if ( supportActiveX ) {
if ( window.XMLHttpRequest ) {
$.createAjaxRequest = function( local ) {
if ( local ) {
return new ActiveXObject( "Microsoft.XMLHTTP" );
}
return new XMLHttpRequest();
};
} else {
$.createAjaxRequest = function() {
return new ActiveXObject( "Microsoft.XMLHTTP" );
};
}
} else if ( window.XMLHttpRequest ) {
$.createAjaxRequest = function() {
return new XMLHttpRequest();
};
} else {
throw new Error( "Browser doesn't support XMLHttpRequest." );
}
return $.createAjaxRequest( local );
},
/**
* Makes an AJAX request.
@ -1844,7 +1870,8 @@ window.OpenSeadragon = window.OpenSeadragon || function( options ){
* @throws {Error}
*/
makeAjaxRequest: function( url, onSuccess, onError ) {
var request = $.createAjaxRequest();
var protocol = $.getUrlProtocol( url );
var request = $.createAjaxRequest( protocol === "file:" );
if ( !$.isFunction( onSuccess ) ) {
throw new Error( "makeAjaxRequest requires a success callback" );
@ -1855,10 +1882,12 @@ window.OpenSeadragon = window.OpenSeadragon || function( options ){
if ( request.readyState == 4 ) {
request.onreadystatechange = function(){};
if ( request.status == 200 ) {
var successStatus =
protocol === "http:" || protocol === "https:" ? 200 : 0;
if ( request.status === successStatus ) {
onSuccess( request );
} else {
$.console.log( "AJAX request returned %s: %s", request.status, url );
$.console.log( "AJAX request returned %d: %s", request.status, url );
if ( $.isFunction( onError ) ) {
onError( request );
@ -1995,23 +2024,9 @@ window.OpenSeadragon = window.OpenSeadragon || function( options ){
* @returns {Document}
*/
parseXml: function( string ) {
//TODO: yet another example where we can determine the correct
// implementation once at start-up instead of everytime we use
// the function. DONE.
if ( window.ActiveXObject ) {
if ( window.DOMParser ) {
$.parseXml = function( string ){
var xmlDoc = null;
xmlDoc = new ActiveXObject( "Microsoft.XMLDOM" );
xmlDoc.async = false;
xmlDoc.loadXML( string );
return xmlDoc;
};
} else if ( window.DOMParser ) {
$.parseXml = function( string ){
$.parseXml = function( string ) {
var xmlDoc = null,
parser;
@ -2020,6 +2035,17 @@ window.OpenSeadragon = window.OpenSeadragon || function( options ){
return xmlDoc;
};
} else if ( window.ActiveXObject ) {
$.parseXml = function( string ) {
var xmlDoc = null;
xmlDoc = new ActiveXObject( "Microsoft.XMLDOM" );
xmlDoc.async = false;
xmlDoc.loadXML( string );
return xmlDoc;
};
} else {
throw new Error( "Browser doesn't support XML DOM." );
}
@ -2060,12 +2086,7 @@ window.OpenSeadragon = window.OpenSeadragon || function( options ){
};
var ACTIVEX = [
"Msxml2.XMLHTTP",
"Msxml3.XMLHTTP",
"Microsoft.XMLHTTP"
],
FILEFORMATS = {
var FILEFORMATS = {
"bmp": false,
"jpeg": true,
"jpg": true,

View File

@ -56,7 +56,7 @@
equal($(".openseadragon-message").length, 1, "Open failures should display a message");
ok(testLog.log.contains('["AJAX request returned %s: %s",404,"/test/data/not-a-real-file"]'),
ok(testLog.log.contains('["AJAX request returned %d: %s",404,"/test/data/not-a-real-file"]'),
"AJAX failures should be logged to the console");
start();

View File

@ -87,6 +87,30 @@
);
});
test("getUrlProtocol", function() {
equal(OpenSeadragon.getUrlProtocol("test"), window.location.protocol,
"'test' url protocol should be window.location.protocol");
equal(OpenSeadragon.getUrlProtocol("/test"), window.location.protocol,
"'/test' url protocol should be window.location.protocol");
equal(OpenSeadragon.getUrlProtocol("//test"), window.location.protocol,
"'//test' url protocol should be window.location.protocol");
equal(OpenSeadragon.getUrlProtocol("http://test"), "http:",
"'http://test' url protocol should be http:");
equal(OpenSeadragon.getUrlProtocol("https://test"), "https:",
"'https://test' url protocol should be https:");
equal(OpenSeadragon.getUrlProtocol("file://test"), "file:",
"'file://test' url protocol should be file:");
equal(OpenSeadragon.getUrlProtocol("FTP://test"), "ftp:",
"'FTP://test' url protocol should be ftp:");
});
// ----------
asyncTest("requestAnimationFrame", function() {
var timeWatcher = Util.timeWatcher();