This repository has been archived by the owner on May 10, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For the communication iframe, removing jQuery and adding micrajax.
issue #1351
- Loading branch information
Shane Tomlinson
committed
Mar 29, 2012
1 parent
aebc76e
commit dcdeaf6
Showing
5 changed files
with
181 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/*jshint browsers:true, forin: true, laxbreak: true */ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
window.Micrajax = (function() { | ||
"use strict"; | ||
|
||
function curry(fToBind) { | ||
var aArgs = [].slice.call(arguments, 1), | ||
fBound = function () { | ||
return fToBind.apply(null, aArgs.concat([].slice.call(arguments))); | ||
}; | ||
|
||
return fBound; | ||
} | ||
|
||
function getXHRObject() { | ||
var xhrObject; | ||
|
||
if (window.ActiveXObject) { | ||
xhrObject = new ActiveXObject('Microsoft.XMLHTTP'); | ||
} | ||
else if (window.XMLHttpRequest) { | ||
xhrObject = new XMLHttpRequest(); | ||
} | ||
|
||
return xhrObject; | ||
} | ||
|
||
function noOp() {} | ||
|
||
function onReadyStateChange(xhrObject, callback) { | ||
try { | ||
if (xhrObject.readyState == 4) { | ||
xhrObject.onreadystatechange = noOp; | ||
|
||
callback && callback(xhrObject.responseText, xhrObject.status); | ||
} | ||
} catch(e) {} | ||
} | ||
|
||
function toRequestString(data) { | ||
var components = [], | ||
requestString = ""; | ||
|
||
for(var key in data) { | ||
if (typeof data[key] !== "undefined") { | ||
components.push(encodeURIComponent(key) + "=" + encodeURIComponent(data[key])); | ||
} | ||
} | ||
|
||
if (components && components.length) { | ||
requestString = components.join("&"); | ||
} | ||
|
||
return requestString; | ||
} | ||
|
||
|
||
function setRequestHeaders(definedHeaders, xhrObject) { | ||
var headers = { | ||
'X-Requested-With': 'XMLHttpRequest', | ||
'Accept': 'application/json;text/plain' | ||
}; | ||
|
||
for(var key in definedHeaders) { | ||
headers[key] = definedHeaders[key]; | ||
} | ||
|
||
for(var key in headers) { | ||
xhrObject.setRequestHeader(key, headers[key]); | ||
} | ||
} | ||
|
||
function getURL(url, type, data) { | ||
var requestString = toRequestString(data); | ||
|
||
if (type === "GET" && requestString) { | ||
url += "?" + requestString; | ||
} | ||
|
||
return url; | ||
} | ||
|
||
function getData(contentType, type, data) { | ||
var sendData; | ||
|
||
if (type !== "GET" && data) { | ||
switch(contentType) { | ||
case "application/json": | ||
if(typeof data === "string") { | ||
sendData = data; | ||
} | ||
else { | ||
sendData = JSON.stringify(data); | ||
} | ||
break; | ||
case 'application/x-www-form-urlencoded': | ||
sendData = toRequestString(data); | ||
break; | ||
default: | ||
// do nothing | ||
break; | ||
} | ||
} | ||
|
||
return sendData || null; | ||
} | ||
|
||
function sendRequest(options, callback, data) { | ||
var xhrObject = getXHRObject(); | ||
|
||
if (xhrObject) { | ||
xhrObject.onreadystatechange = curry(onReadyStateChange, xhrObject, callback); | ||
|
||
var type = (options.type || "GET").toUpperCase(), | ||
contentType = options.contentType || 'application/x-www-form-urlencoded', | ||
url = getURL(options.url, type, options.data), | ||
data = getData(contentType, type, options.data); | ||
|
||
xhrObject.open(type, url, true); | ||
setRequestHeaders({ "Content-type" : contentType }, xhrObject); | ||
xhrObject.send(data); | ||
} | ||
else { | ||
throw "could not get XHR object"; | ||
} | ||
} | ||
|
||
var Micrajax = { | ||
ajax: function(options) { | ||
var error = options.error, | ||
success = options.success, | ||
mockXHR = { readyState: 0 }; | ||
|
||
sendRequest(options, function(responseText, status) { | ||
mockXHR.status = status; | ||
mockXHR.responseText = responseText; | ||
mockXHR.readyState = 4; | ||
|
||
if (status >= 200 && status < 300 || status === 304) { | ||
var respData = responseText; | ||
|
||
try { | ||
// The text response could be text/plain, just ignore the JSON | ||
// parse error in this case. | ||
var respData = JSON.parse(responseText); | ||
} catch(e) {} | ||
|
||
success && success(respData, responseText, mockXHR); | ||
} | ||
else { | ||
error && error(mockXHR, status, responseText); | ||
} | ||
}); | ||
|
||
return mockXHR; | ||
} | ||
}; | ||
|
||
return Micrajax; | ||
|
||
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/*jshint browsers:true, forin: true, laxbreak: true */ | ||
/*global BrowserID: true */ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
BrowserID.XHRTransport = window.Micrajax; |