Skip to content
This repository has been archived by the owner on May 10, 2019. It is now read-only.

Commit

Permalink
For the communication iframe, removing jQuery and adding micrajax.
Browse files Browse the repository at this point in the history
issue #1351
  • Loading branch information
Shane Tomlinson committed Mar 29, 2012
1 parent aebc76e commit dcdeaf6
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 6 deletions.
5 changes: 4 additions & 1 deletion lib/static_resources.js
Expand Up @@ -21,6 +21,7 @@ var common_js = [
'/lib/underscore-min.js',
'/lib/vepbundle.js',
'/lib/ejs.js',
'/lib/micrajax.js',
'/shared/javascript-extensions.js',
'/i18n/:locale/client.json',
'/shared/gettext.js',
Expand All @@ -43,6 +44,7 @@ var common_js = [
'/shared/error-messages.js',
'/shared/error-display.js',
'/shared/storage.js',
'/shared/xhr_transport.js',
'/shared/xhr.js',
'/shared/network.js',
'/shared/provisioning.js',
Expand Down Expand Up @@ -112,17 +114,18 @@ exports.resources = {
'/css/m.css'
],
'/production/communication_iframe.js': [
'/lib/jquery-1.7.1.min.js',
'/lib/jschannel.js',
'/lib/winchan.js',
'/lib/underscore-min.js',
'/lib/vepbundle.js',
'/lib/hub.js',
'/lib/micrajax.js',
'/shared/javascript-extensions.js',
'/shared/browserid.js',
'/shared/mediator.js',
'/shared/helpers.js',
'/shared/storage.js',
'/shared/xhr_transport.js',
'/shared/xhr.js',
'/shared/network.js',
'/shared/user.js',
Expand Down
163 changes: 163 additions & 0 deletions resources/static/lib/micrajax.js
@@ -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;

}());
8 changes: 6 additions & 2 deletions resources/static/shared/user.js
Expand Up @@ -90,8 +90,12 @@ BrowserID.User = (function() {
}

function setAuthenticationStatus(authenticated) {
var func = authenticated ? 'addClass' : 'removeClass';
$('body')[func]('authenticated');
if(window.$) {
// TODO get this out of here!
// jQuery is not included in the communication_iframe
var func = authenticated ? 'addClass' : 'removeClass';
$('body')[func]('authenticated');
}

if (!authenticated) {
storage.clear();
Expand Down
5 changes: 2 additions & 3 deletions resources/static/shared/xhr.js
Expand Up @@ -10,7 +10,7 @@ BrowserID.XHR = (function() {
mediator = bid.Mediator,
context,
csrf_token,
transport = $,
transport = bid.XHRTransport,
time_until_delay;

function clearContext() {
Expand All @@ -31,14 +31,13 @@ BrowserID.XHR = (function() {

function xhrError(cb, info, jqXHR, textStatus, errorThrown) {
info = info || {};
var network = _.extend(info.network || {}, {
info.network = _.extend(info.network || {}, {
status: jqXHR && jqXHR.status,
textStatus: textStatus,
errorThrown: errorThrown,
responseText: jqXHR.responseText
});

info.network = network;
mediator.publish("xhr_error", info);

if (cb) cb(info);
Expand Down
6 changes: 6 additions & 0 deletions resources/static/shared/xhr_transport.js
@@ -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;

0 comments on commit dcdeaf6

Please sign in to comment.