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,39 +1802,65 @@ window.OpenSeadragon = window.OpenSeadragon || function( options ){
return value ? value : null; return value ? value : null;
}, },
/**
* 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;
}
return match[1].toLowerCase();
},
createAjaxRequest: function(){ /**
var request; * 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 ) { 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() { $.createAjaxRequest = function() {
return new XMLHttpRequest(); return new XMLHttpRequest();
}; };
request = new XMLHttpRequest(); } else {
} 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;
}
}
}
if ( !request ) {
throw new Error( "Browser doesn't support XMLHttpRequest." ); throw new Error( "Browser doesn't support XMLHttpRequest." );
} }
return $.createAjaxRequest( local );
return request;
}, },
/** /**
* Makes an AJAX request. * Makes an AJAX request.
* @function * @function
@ -1844,7 +1870,8 @@ window.OpenSeadragon = window.OpenSeadragon || function( options ){
* @throws {Error} * @throws {Error}
*/ */
makeAjaxRequest: function( url, onSuccess, onError ) { makeAjaxRequest: function( url, onSuccess, onError ) {
var request = $.createAjaxRequest(); var protocol = $.getUrlProtocol( url );
var request = $.createAjaxRequest( protocol === "file:" );
if ( !$.isFunction( onSuccess ) ) { if ( !$.isFunction( onSuccess ) ) {
throw new Error( "makeAjaxRequest requires a success callback" ); throw new Error( "makeAjaxRequest requires a success callback" );
@ -1855,10 +1882,12 @@ window.OpenSeadragon = window.OpenSeadragon || function( options ){
if ( request.readyState == 4 ) { if ( request.readyState == 4 ) {
request.onreadystatechange = function(){}; request.onreadystatechange = function(){};
if ( request.status == 200 ) { var successStatus =
protocol === "http:" || protocol === "https:" ? 200 : 0;
if ( request.status === successStatus ) {
onSuccess( request ); onSuccess( request );
} else { } else {
$.console.log( "AJAX request returned %s: %s", request.status, url ); $.console.log( "AJAX request returned %d: %s", request.status, url );
if ( $.isFunction( onError ) ) { if ( $.isFunction( onError ) ) {
onError( request ); onError( request );
@ -1995,21 +2024,7 @@ window.OpenSeadragon = window.OpenSeadragon || function( options ){
* @returns {Document} * @returns {Document}
*/ */
parseXml: function( string ) { parseXml: function( string ) {
//TODO: yet another example where we can determine the correct if ( window.DOMParser ) {
// implementation once at start-up instead of everytime we use
// the function. DONE.
if ( window.ActiveXObject ) {
$.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, var xmlDoc = null,
@ -2020,6 +2035,17 @@ window.OpenSeadragon = window.OpenSeadragon || function( options ){
return xmlDoc; 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 { } else {
throw new Error( "Browser doesn't support XML DOM." ); throw new Error( "Browser doesn't support XML DOM." );
} }
@ -2060,12 +2086,7 @@ window.OpenSeadragon = window.OpenSeadragon || function( options ){
}; };
var ACTIVEX = [ var FILEFORMATS = {
"Msxml2.XMLHTTP",
"Msxml3.XMLHTTP",
"Microsoft.XMLHTTP"
],
FILEFORMATS = {
"bmp": false, "bmp": false,
"jpeg": true, "jpeg": true,
"jpg": true, "jpg": true,

View File

@ -56,7 +56,7 @@
equal($(".openseadragon-message").length, 1, "Open failures should display a message"); 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"); "AJAX failures should be logged to the console");
start(); 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() { asyncTest("requestAnimationFrame", function() {
var timeWatcher = Util.timeWatcher(); var timeWatcher = Util.timeWatcher();