Skip to content

Commit

Permalink
Add Ajax, querySelector util
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Martin committed Aug 27, 2014
1 parent 75e96ca commit 8a0879f
Show file tree
Hide file tree
Showing 9 changed files with 523 additions and 10 deletions.
176 changes: 174 additions & 2 deletions dist/z.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*!
* z.js JavaScript Library v0.0.2
* z.js JavaScript Library v0.0.3
* https://github.com/NEURS/z.js
*
* Copyright 2014 NEURS LLC, Kevin J. Martin, and other contributors
* Released under the MIT license
* https://github.com/NEURS/z.js/blob/master/LICENSE
*
* Date: 2014-08-15T15:10Z
* Date: 2014-08-27T17:10Z
*/
;(function (window, document) {

Expand Down Expand Up @@ -36,6 +36,8 @@ function z(elem, scope) {
return _find(scope, elem);
}

function noop(){}

try {
iframe = document.createElement("iframe");

Expand Down Expand Up @@ -65,6 +67,151 @@ z.fn.find = function (strElem) {

return _find(this, strElem);
};

var ajaxDefaults, ajaxTypes,
ajaxMimes = {}

ajaxDefaults = {
method: "GET",
requestType: "text",
responseType: "text",
url: window.location + "",
query: null,
data: null,
setup: noop,
success: noop,
error: noop
};

ajaxTypes = {
text: function (data) {
return (data || "") + "";
}
};

z.ajax = function (options) {
var data,
req = new XMLHttpRequest();

options = z.extend({
context: req
}, ajaxDefaults, options);

if (!ajaxTypes[options.requestType]) {
throw new Error("Invalid option `requestType`");
} else if (!ajaxTypes[options.responseType]) {
throw new Error("Invalid option `responseType`");
}

if (options.query && ~["HEAD", "GET"].indexOf(options.method.toUpperCase())) {
options.url += ~options.url.indexOf("?") ? "&" : "?";
options.url += z.queryString(options.query);
options.url = options.url.replace(/(\?|&)&/g, "$1");
}

req.open(options.method, options.url, true);

req.onload = function () {
var resp;

if (req.status >= 200 && req.status < 400) {
resp = ajaxTypes[options.responseType].call(req, req.responseText, true);
options.success.call(options.context, resp);
} else {
options.error.call(options.context, req.status, req.statusText);
}
};

req.onerror = function () {
options.error.call(options.context, req.status, req.statusText);
};

if (!~["HEAD", "GET"].indexOf(options.method.toUpperCase())) {
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
}

if (options.data) {
options.data = ajaxTypes[options.requestType].call(req, options.data, false);
}

options.setup.call(req, req);

req.send(options.data);
};

ajaxDefaults.requestType = "detect";
ajaxDefaults.responseType = "detect";

z.registerAjaxType = function (type, mime, fn) {
if (!fn && typeof mime === "function") {
fn = mime;
mime = false;
}

ajaxTypes[type] = fn;

if (mime) {
ajaxMimes[mime] = type;
}
};

z.registerAjaxType("detect", function (data, isResponse) {
var header,
type = "text";

if (isResponse) {
header = this.getResponseHeader("Content-Type") || "",
header = header.split(";")[0].trim();
type = ajaxMimes[header] || "text";
} else {
if (data && typeof data === "object" && data.toString === ({}).toString) {
type = "json";
}
}

return ajaxTypes[type].call(this, data, isResponse);
});

z.registerAjaxType("json", "application/json", function (data, isResponse) {
return isResponse ? JSON.parse(data) : JSON.stringify(data);
});

z.registerAjaxType("html", "text/html", function (data, isResponse) {
var doc, arr;

if (!isResponse) {
return data.outerHTML;
}

arr = new zArray();
doc = document.implementation.createHTMLDocument();

doc.documentElement.innerHTML = data;

arr.push.apply(arr, arr.slice.call(doc.body.children, 0));

return arr;
});

z.registerAjaxType("xml", "text/xml", ajaxXMLParser);
z.registerAjaxType("xml", "application/xml", ajaxXMLParser);

function ajaxXMLParser(data, isResponse) {
var parser;

if (!isResponse) {
parser = new XMLSerializer();
return parser.serializeToString(data);
}

if (this.responseXML) {
return this.responseXML;
}

parser = new DOMParser();
return parser.parseFromString(data, "application/xml");
}

if ("dataset" in document.body) {
z.fn.data = function (key, value) {
var i, l;
Expand Down Expand Up @@ -538,4 +685,29 @@ z.fn.each = _each(function each(fn) {
return this;
});

z.queryString = function (obj, prefix) {
var i, key, val,
strings = [];

for (i in obj) {
if (obj.hasOwnProperty(i)) {
if (prefix) {
key = prefix + "[" + i + "]";
} else {
key = i;
}

val = obj[i];

if (val && typeof val === "object") {
strings.push(queryString(val, key));
} else {
strings.push(encodeURIComponent(key) + "=" + encodeURIComponent(val));
}
}
}

return strings.join("&");
};

})(window, document);
2 changes: 1 addition & 1 deletion dist/z.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8a0879f

Please sign in to comment.