Created
February 11, 2010 02:17
-
-
Save antimatter15/301129 to your computer and use it in GitHub Desktop.
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(){ | |
function core(e){ | |
var i=this.length=e.length; | |
if(!i){this[0]=e;this.length=1}; | |
for(;i--;)this[i]=e[i]; | |
this.core = core; | |
} | |
//this is the core exposed function | |
var o = function(a){return a.core?a:(new core(a))} | |
//generators to help shrink code | |
var s = function(q){return Function("e","var t=this;return "+q)} | |
var r = function(q){return Function("e","var t=this;"+q+";return [t]")} | |
var c = function(q){return Function("e", | |
"if(!e)return [this."+q+"];this."+q+"=e;return [this]")} | |
//this is the basic iterator function | |
function I(f){ | |
return function(){ | |
for(var w=[],i=this.length;i--;) | |
w=f.apply(this[i],arguments).concat(w); | |
return o(w); | |
} | |
} | |
//save space using with()? | |
var z = core.prototype; | |
z.extend = function(o, a){ | |
for(var y in a) o[y]=a[y]; | |
return o | |
} | |
z.remove = function(e){ | |
var x = this.has(e), y = this.A() | |
x>0&&y.splice(x,1) | |
return o(y) | |
} | |
z.ajax = function(u,f,d){ | |
var x = new(window.ActiveXObject||XMLHttpRequest)('Microsoft.XMLHTTP') | |
x.open(d?'POST':'GET',u,1); | |
d&&x.setRequestHeader('Content-type','application/x-www-form-urlencoded'); | |
x.onreadystatechange=function(){ | |
x.readyState>3&&f&&f(x.responseText,x) | |
}; | |
x.send(d) | |
return x | |
} | |
/* | |
z.load = I(function(u,h){ | |
var t = this; | |
z.ajax(u,function(y){ | |
t[h||'text'](y) | |
}) | |
}) | |
*/ | |
z.load = I(r("z.ajax(u,function(y){t.html(y)})")) | |
z.trim = s("e.replace(/^\\s+|\\s+$/g,'')"); | |
z.get = s("o(e)"); | |
z.id = s("o(t[0].ownerDocument.getElementById(e))") | |
z.now = s("new Date -0") | |
z.A = z.array = function(){ | |
for(var i=-1,n=[];++i<this.length;)n.push(this[i]); | |
//for(var i=this.length,n=[];i--;)n=this[i].concat(n); | |
return n | |
} | |
z.map = I(s("[e(t)]")) | |
z.filter = I(s("e(t)?[t]:[]")) | |
z.has = function(v){ | |
//taken from vXJS's index function | |
for(var i=this.length;i--&&this[i]!=v;){}; //closure gets rid of the {} | |
return i | |
} | |
z.index=s("o(t[e])") | |
z.add=s("o(t.A().concat(e))") | |
z.tag=I(s("o(t.getElementsByTagName(e)).A()")) | |
//z.cls=I(s("o((t.getElementsByClassName||L)(e,t)).A()")) | |
z.hasclass = I(function(c){ | |
return [o(this.className.split(" ")).has(c)>-1] | |
}) | |
z.addclass = I(function(c){ | |
if(!o(this).hasclass(c)[0]) | |
this.className+=" "+c; | |
return [this] | |
}) | |
z.unique = function(){ | |
for(var t=this,b=t.length, c=[];b--;) | |
o(c).has(t[b])>0? //loop if not in list | |
0:c.push(t[b]); //add it | |
return o(c) | |
} | |
z.cls=I(function(c){ | |
var t=this, h = "getElementsByClassName" | |
if(t[h]) return o(t[h](c)).A(); | |
//backup getElementByClassName, first arg is classes, second is the element | |
//only supports one class at a time | |
for(var a=o(t).all(), //a list of all elements | |
l=a.length, | |
h=[]; //store the matching elements | |
l--;) //iterate backwards | |
//if class name is not in the element | |
o(a[l]).hasclass(c)[0] && //else | |
h.push(a[l]); //add element to the list | |
return h | |
}) | |
z.all=s("t.tag('*')") | |
z.tj=s("t.A().join(e||'')") //textjoin | |
z.name=I(s("o(t.getElementsByName(e)).A()")) | |
z.parent=I(s("o(t.parentNode).A()")) | |
z.children=I(s("o(t.childNodes).A()")) | |
//chaining html ops. Differs from jQuery a bit when retrieving | |
//setting is basically the same _.id("box").html("<html here>") | |
//but getting is different, as its _.id("box").html()[0] or to | |
//do what jQuery does, it would be _.id("box").html().array().join("") | |
//another shortcut for .array().join('') is .tj(delimiter='') | |
z.css = I(function(e,f){ | |
if(f===null)return [this.style[e]]; | |
this.style[e] = f; | |
return [this] | |
}) | |
z.attr = I(function(e,f){ | |
if(f===null)return [this.getAttribute(e)]; | |
this.setAttribute(e,f); | |
return [this] | |
}) | |
z.pos = I(function(){ | |
var e = this; | |
var a = {l:0, t:0, w:e.offsetWidth, h:e.offsetHeight}; | |
do{ | |
a.l += e.offsetLeft; | |
a.t += e.offsetTop | |
}while(e = e.offsetParent) | |
return [a] | |
}) | |
z.html = I(c("innerHTML")) | |
z.text = I(c("innerText")) | |
z.val = I(c("value")) | |
z.one = function(t, f){ | |
var j = this, g = function(a){ | |
f.call(this,a) | |
j.un(t, g) | |
} | |
return j.on(t, g) | |
} | |
z.on = I(function(t, f){ | |
var e = this; | |
if(e.attachEvent || e.addEventListener(t,f,0)){ | |
e['e'+t+f]=f; | |
e[t+f]=function(){e['e'+t+f].call(event.srcElement,event)}; | |
e.attachEvent('on'+t,e[t+f]) | |
} | |
return [e] | |
}) | |
z.un = I(function(t, f){ | |
var e = this; | |
e.attachEvent ? e.detachEvent('on'+t,e[t+f]) : e.removeEventListener(t,f,0) | |
return [e] | |
}) | |
z.ready = function(f){ | |
"\v"=="v"? //if unhappy browser | |
setTimeout(f,0): //do ugly hack | |
this[0].ownerDocument.addEventListener( | |
'DOMContentLoaded',f, 0) //or else use the nice way | |
} | |
z.create = s("o(t[0].ownerDocument.createElement(e||'div'))") | |
z.append = s("e.appendTo(t)") | |
z.appendTo = I(r("o(e)[0].appendChild(t)")) | |
z.prepend = s("e.prependTo(t)") | |
z.prependTo = I(r("o(e)[0].insertBefore(t,o(e)[0].firstChild)")) | |
z.bind = function(f,s){ | |
return function(){return f.apply(s,arguments)} | |
} | |
z.encode = function(j){ | |
if(!j)return j+''; | |
var t=[], N = z.encode; | |
if(j.pop){ //array | |
for(x in j) t.push(N(j[x])); | |
j='['+t.join()+']' | |
}else if(j.split) //string | |
j="'"+j.replace(/\'/g,"\\'")+"'" | |
else if(typeof j=='object'){ | |
for(x in j) t.push(N(x)+':'+N(j[x])); | |
j='{'+t.join()+'}' | |
}else j+=''; | |
return j | |
} | |
window.o = o | |
})() | |
var _ = o([document.documentElement]) | |
_.ready(function(){ | |
console.log(_.tag("div").index(3).array()) | |
//test.cls("purple").children() | |
_.cls("test") | |
.on("click", function(e){ | |
_.get(this) | |
.addclass("purple") | |
.css("font-size","x-large") | |
}) | |
.on("mouseover",function(e){ | |
_.get(this).css("background-color","#007fff") | |
}) | |
.on("mouseout",function(e){ | |
_.get(this).css("background-color","") | |
}) | |
_.tag("button") | |
.one("click", function(){ | |
alert("this should happen only once") | |
}) | |
_.create() | |
.html(_.encode(_.cls("test").index(2).pos()[0])) | |
.appendTo(_.tag("body")) | |
.prepend(_.cls("test")) | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment