Created
July 28, 2010 21:54
-
-
Save bdougherty/496468 to your computer and use it in GitHub Desktop.
A super simple JS framework. Firefox 3.1+, IE8+, Safari 3.1+ only.
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
// ==ClosureCompiler== | |
// @output_file_name framework_min.js | |
// @compilation_level ADVANCED_OPTIMIZATIONS | |
// ==/ClosureCompiler== | |
(function() { | |
var docProto = Document.prototype, | |
htmlElementProto = HTMLElement.prototype, | |
nodeListProto = NodeList.prototype; | |
function qs(query) { | |
return this.querySelector(query); | |
} | |
function qsa(query) { | |
return this.querySelectorAll(query); | |
} | |
docProto['qs'] = qs; | |
docProto['qsa'] = qsa; | |
htmlElementProto['qs'] = qs; | |
htmlElementProto['qsa'] = qsa; | |
htmlElementProto['computeStyle'] = function(prop) { | |
return window.getComputedStyle(this, null).getPropertyValue(prop).split('px').join(''); | |
} | |
htmlElementProto['addClass'] = function(className) { | |
this.className = (this.className.split(className).join('') + ' ' + className)['trim'](); | |
return this; | |
} | |
htmlElementProto['removeClass'] = function(className) { | |
this.className = this.className.split(className).join('')['trim'](); | |
return this; | |
} | |
nodeListProto['addClass'] = function(className) { | |
for (var i = 0; i < this.length; ++i) { | |
this[i]['addClass'](className); | |
} | |
} | |
nodeListProto['removeClass'] = function(className) { | |
for (var i = 0; i < this.length; ++i) { | |
this[i]['removeClass'](className); | |
} | |
} | |
window['XHR'] = (function() { | |
function parseResponse(responseText) { | |
try { | |
return JSON.parse(responseText); | |
} | |
catch (error) { | |
return responseText; | |
} | |
} | |
function request(method, url, data, callback) { | |
var req = new XMLHttpRequest(); | |
req.open(method, url, !!callback); | |
if (!!callback) { | |
req.onreadystatechange = function(e) { | |
if (req.readyState === 4 && req.status === 200) { | |
callback(parseResponse(req.responseText)); | |
} | |
} | |
} | |
req.send(data); | |
return (!!callback) ? null : parseResponse(req.responseText); | |
} | |
return { | |
'get': function(url, callback) { | |
return request('GET', url, null, callback); | |
}, | |
'post': function(url, data, callback) { | |
return request('POST', url, data, callback); | |
} | |
} | |
}()); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment