Last active
August 29, 2015 14:15
-
-
Save low-ghost/74a247ba6f45393b40e1 to your computer and use it in GitHub Desktop.
Utils
This file contains hidden or 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
(function(window) { | |
var Utils = window.Utils || function() { /* no need to set initial params as of yet */ }; | |
Utils.prototype = { | |
ajaxGet: function(url, cb, passVar) { | |
var request = new XMLHttpRequest(); | |
request.open('GET', url, true); | |
request.onreadystatechange = function() { | |
if (this.readyState === 4) { | |
if (this.status >= 200 && this.status < 400) | |
cb(request.responseText, passVar); | |
else | |
console.log("Something went wrong with the XMLHttpRequest!\nERROR: " + this.status); | |
} | |
}; | |
request.send(); | |
}, | |
addListener: function(obj, type, fn) { | |
if (obj.attachEvent) { | |
obj['e' + type + fn] = fn; | |
obj[type + fn] = function() { | |
obj['e' + type + fn](window.event); | |
} | |
obj.attachEvent('on' + type, obj[type + fn]); | |
} else | |
obj.addEventListener(type, fn, false); | |
}, | |
containsAll: function(haystack, needles) { | |
var i = needles.length; | |
while (i--) { | |
if (haystack.indexOf(needles[i]) === -1) | |
return false; | |
} | |
return true; | |
}, | |
copyObj: function(original, copy) { | |
var copy = copy || {}; | |
if (original === null || typeof original !== "object") | |
return original; | |
for (var prop in original) { | |
if (original.hasOwnProperty(prop)) { | |
copy[prop] = (copy[prop] && typeof copy[prop] === "object") ? this.copyObj(original[prop], copy[prop]) | |
: (copy[prop] === 0 || copy[prop]) ? copy[prop] | |
: original[prop]; | |
} | |
} | |
return copy; | |
}, | |
getChildren: function(el) { | |
var children = [], | |
i = el.children.length; | |
while (i--) { | |
if (el.children[i].nodeType != 8) | |
children.unshift(el.children[i]); | |
} | |
return children; | |
}, | |
ready: function(fn) { | |
if (document.readyState != 'loading') | |
fn(); | |
else if (document.addEventListener) | |
document.addEventListener('DOMContentLoaded', fn); | |
else { | |
document.attachEvent('onreadystatechange', function() { | |
if (document.readyState != 'loading') | |
fn(); | |
}); | |
} | |
}, | |
wait: (function(originalArr, callback) { | |
var self = this; | |
self.eventsArray = []; | |
self.varObj = {}; | |
self.originalArr = originalArr; | |
return function(variable, functionName) { | |
self.eventsArray.push(functionName); | |
self.varObj[functionName] = variable; | |
if (Utils.containsAll(self.eventsArray, self.originalArr)) | |
callback(self.varObj); | |
}; | |
}), | |
}; | |
//export Utils | |
window.Utils = Utils; | |
//export querySelectors | |
window.$qs = function(selector, scope) { | |
return (scope || document).querySelector(selector); | |
}; | |
window.$qsa = function(selector, scope) { | |
return (scope || document).querySelectorAll(selector); | |
}; | |
// Loop on nodes | |
NodeList.prototype.forEach = Array.prototype.forEach; | |
})(window); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment