Skip to content

Instantly share code, notes, and snippets.

@rudylattae
Created August 6, 2011 08:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rudylattae/1129169 to your computer and use it in GitHub Desktop.
Save rudylattae/1129169 to your computer and use it in GitHub Desktop.
Zyx.js -- a collection of microjs helpers
/**
* Simple guid generator based on http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
*/
function _s1 (c) {var r = Math.random()*16|0,v=c=='x'?r:r&0x3|0x8;return v.toString(16);}
function guid(format, rx) {
var format = format || 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
var rx = rx || /[x]/g;
return format.replace(rx, _s1);
}
var Jaxy = function() {
this.xhr = new XMLHttpRequest;
};
Jaxy.prototype.get = function(options) {
if (typeof options.url === 'undefined') return;
var self = this;
var url = options.url || '';
var success = options.success || null;
var error = options.error || null;
this.xhr.onreadystatechange = function() {
if (this.readyState === 4) {
if (self.isSuccess(this.status)) {
success(this.responseText);
} else {
error(this.responseText);
}
}
};
this.xhr.open("GET", url, true);
this.xhr.send(null);
}
Jaxy.prototype.post = function(options) {
if (typeof options.url === 'undefined') return;
var self = this;
var url = options.url || '';
var success = options.success || null;
var error = options.error || null;
this.xhr.onreadystatechange = function() {
if (this.readyState === 4) {
if (self.isSuccess(this.status)) {
success(this.responseText);
} else {
error(this.responseText);
}
}
};
this.xhr.open("POST", url, true);
this.xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
this.xhr.send(options.data);
}
Jaxy.prototype.isSuccess = function(code) {
if (code >= 200 && code < 400) {
return true;
}
return false;
}
/**
* Functions to attach to a list of nodes that make life easier
*/
var NodeHelpers = {
text: function(text) {
if (this.length === 0) return;
if (typeof text !== 'undefined') {
for (var i=0; i<this.length; i++) {
this[i].textContent = text;
}
} else {
var all = '';
for (var i=0; i<this.length; i++) {
all += this[i].textContent;
}
return all;
}
},
html: function(html) {
if (this.length === 0) return;
if (typeof html !== 'undefined') {
for (var i=0; i<this.length; i++) {
this[i].innerHTML = html;
}
} else {
var all = '';
for (var i=0; i<this.length; i++) {
all += this[i].innerHTML;
}
return all;
}
},
bind: function(type, listener, capture) {
if (this.length === 0) return;
if (typeof type !== 'undefined' && typeof listener !== 'undefined') {
for (var i=0; i<this.length; i++) {
if (this[i].addEventListener) {
this[i].addEventListener(type, listener, capture);
} else if (this[i].attachEvent) {
this[i].attachEvent('on' + type, listener);
}
}
}
},
val: function(val) {
if (this.length === 0) return;
if (typeof val !== 'undefined') {
for (var i=0; i<this.length; i++) {
this[i].value = val;
}
} else {
var all = '';
for (var i=0; i<this.length; i++) {
all += this[i].value;
}
return all;
}
},
hide: function() {
if (this.length === 0) return;
for (var i=0; i<this.length; i++) {
this[i].style.display = 'none';
}
},
show: function() {
if (this.length === 0) return;
for (var i=0; i<this.length; i++) {
this[i].style.display = 'block';
}
}
};
/**
* NodeList polyfill for browsers without
*/
if (typeof NodeList === 'undefined') {
function NodeList(el) {
if (el instanceof Array) {
for (var i=0, len=el.length; i<len; i++) {
this[i] = el[i];
}
this.length = el.length;
} else {
this['0'] = el;
this.length = 1;
}
}
NodeList.prototype.item = function() {};
}
var q = function(document, helpers) {
var sel = document.querySelectorAll ?
function(a,b) { return document.querySelectorAll(a,b); } :
function(a,b){a=a.match(/^(\W)?(.*)/);return(b||document)["getElement"+(a[1]?a[1]=="#"?"ById":"sByClassName":"sByTagName")](a[2])};
function isNodeList(el) {
if (typeof el.length === 'number' && typeof el.item !== 'undefined')
{
return true;
}
return false;
}
return function(selector, context) {
var nodes = sel(selector, context);
if (!isNodeList(nodes)) { nodes = new NodeList(nodes); }
if (typeof helpers !== 'undefined') {
for (helper in helpers) {
nodes[helper] = helpers[helper];
}
}
return nodes;
};
}
/**
* Your friendly neighborhood "$" selector, with just the right amount of "super"
*/
var $ = $ || q(document, NodeHelpers);
/**
* Quick debug output to the Ti console
*/
function dbg(item) {
Ti.API.debug(item);
}
/**
* Show notification (if supported)
*/
function notify(title, message, callback, timeout, icon) {
var content = {};
if (typeof(message) !== 'undefined') content.message = message;
if (typeof(callback) !== 'undefined') content.callback = callback;
if (typeof(timeout) !== 'undefined') content.timeout = timeout;
if (typeof(icon) !== 'undefined') content.icon = icon;
var notification = Ti.Notification.createNotification(content);
}
/**
* Xly is a useless XML assistant!
*/
var Xly = function(text) {
var p = new DOMParser;
this.xml = p.parseFromString(text, 'text/xml');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment