2014-04-16 15:40:03 +00:00
|
|
|
/**
|
|
|
|
* @namespace Typertext
|
|
|
|
* @module Transport
|
|
|
|
*/
|
2014-04-11 18:51:54 +00:00
|
|
|
module Typertext.Transport {
|
2014-04-15 16:23:57 +00:00
|
|
|
import HttpMethod = Typertext.Http.HttpMethod;
|
|
|
|
import HttpUrl = Typertext.Http.HttpUrl
|
|
|
|
import HttpPostData = Typertext.Http.HttpPostData;
|
|
|
|
import HttpResponseHandler = Typertext.Http.HttpResponseHandler;
|
|
|
|
|
2014-04-11 18:51:54 +00:00
|
|
|
export class TransportChooser {
|
2014-04-15 16:23:57 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {HttpMethod} method
|
|
|
|
* @param {HttpUrl} request
|
|
|
|
* @param {HttpPostData} postData
|
|
|
|
* @param {HttpResponseHandler} callback
|
|
|
|
* @returns {GenericTransport}
|
|
|
|
*/
|
2014-04-16 15:40:03 +00:00
|
|
|
static Transport(method:HttpMethod, request:HttpUrl, postData:HttpPostData, callback:HttpResponseHandler):TransportConstructor {
|
2014-04-15 16:23:57 +00:00
|
|
|
//Prepare to test if we are in IE
|
|
|
|
var ieTestDiv = document.createElement("div");
|
|
|
|
ieTestDiv.innerHTML = "<!--[if lte IE 7]><i></i><![endif]-->";
|
2014-04-11 18:51:54 +00:00
|
|
|
|
2014-04-15 16:23:57 +00:00
|
|
|
if (ieTestDiv.getElementsByTagName("i").length === 1) {
|
|
|
|
//There is currently no supported transport for IE lte 7
|
|
|
|
throw {};
|
|
|
|
}
|
|
|
|
|
|
|
|
//Now test how we should proceed normally
|
|
|
|
ieTestDiv.innerHTML = "<!--[if lte IE 9]><i></i><![endif]-->";
|
|
|
|
var ieLte9 = (ieTestDiv.getElementsByTagName("i").length === 1);
|
|
|
|
var origin = HttpUrl.FromUrl(window.location.href);
|
|
|
|
|
2014-04-18 04:15:48 +00:00
|
|
|
//If this is a standard request, or a CORS request in a modern browser
|
|
|
|
if (origin.SameOriginCheck(origin) || !ieLte9) {
|
2014-04-15 16:23:57 +00:00
|
|
|
//Just use a standard XHR request
|
2014-04-16 15:40:03 +00:00
|
|
|
return XHR;
|
2014-04-11 18:51:54 +00:00
|
|
|
}
|
|
|
|
|
2014-04-15 16:23:57 +00:00
|
|
|
//Otherwise if we aren't cross protocol
|
|
|
|
if (origin.GetProtocol() === request.GetProtocol()) {
|
|
|
|
//Use IE's silly XDomainRequest
|
2014-04-16 15:40:03 +00:00
|
|
|
return XDR;
|
2014-04-15 16:23:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Otherwise there is no supported transport
|
2014-04-11 18:51:54 +00:00
|
|
|
throw {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|