Created
June 17, 2010 10:52
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
_ = (function(){ | |
//this is the core exposed function | |
function core(e){ | |
var t=this, i=t[_length]=e[_length]; | |
if(i===_undefined){ | |
t[0]=e; | |
t[_length]=1 | |
} | |
for(;i--;)t[i]=e[i]; | |
t.fn = core.prototype; | |
t.o = o; | |
} | |
//save space using with()? | |
var proto = core.prototype, ie = /msie/i.test(navigator.userAgent), | |
o = function(a){return a.fn?a:(new core(a))}, | |
setvar = "var t=this,o=(t.o||(a||e||{}).o);", | |
_length = "length", | |
_undefined = undefined, | |
S = proto.S = function(q){ | |
return Function("e","a",setvar+"return "+q) | |
}, | |
R = proto.R = function(q){ | |
return I(Function("e","a",setvar+q+";return[t]")) | |
}, | |
//not exposed for lack of practical usability | |
C = function(q){ | |
return Function("e","a", "if(!a)return[this."+q+"];this."+q+"=e;return[this]") | |
}, | |
compiled = | |
//BEGIN COMPILED CODE | |
"%no%e.replace(/^\\s+|\\s+$/g,'')%trim%o(e)%get%o(t.A().concat(e))%add%o(t.D().getElementById(e))%id%o(t[e])%index%o(t[t.length-1])%last%+new Date%now%function(){return e.apply(a, arguments)}%bind%t.tag('*')%all%t.A().join(e||'')%tj%t[0].ownerDocument%D%o(t.D().createElement(e||'div'))%create%e.appendTo(t)%append%e.prependTo(t)%prepend%e.insertAfter(t)%after%e.insertBefore(t)%before%t.create().html(e)%make%eval('('+e+')')%decode%t.fade(e)%fadeIn%t.fade(e,1)%fadeOut%t.slide(e,0)%slideDown%t.slide(e,1)%slideUp%t.css('display').is('none')%hidden%t.filter(t.S(e))%sfilter%t.map(t.S(e))%smap" //600bytes | |
//END COMPILED CODE | |
.split('%'), l=compiled[_length]; | |
for(;l--;)proto[compiled[l--]]=S(compiled[l]); | |
/* | |
var proto = {}, lines = {}, compiled = [] | |
function s(text){ | |
for(var nam in proto){ | |
if(!lines[nam]){ | |
lines[nam] = true; | |
compiled.push(nam) | |
} | |
} | |
if(text !== undefined)compiled.push(text); | |
return true; | |
} | |
proto.no = s("") | |
proto.trim = s("e.replace(/^\\s+|\\s+$/g,'')"); | |
proto.get = s("o(e)"); | |
proto.add = s("o(t.A().concat(e))") | |
proto.id = s("o(t.D().getElementById(e))") | |
proto.index =s("o(t[e])") | |
proto.eq =s("o(t[e])") | |
proto.last =s("o(t[t.length-1])") | |
proto.now = s("+new Date") | |
proto.bind = s("function(){return e.apply(a, arguments)}") | |
proto.all = s("t.tag('*')") | |
proto.tj = s("t.A().join(e||'')") //textjoin | |
proto.D = s("t[0].ownerDocument") | |
proto.create = s("o(t.D().createElement(e||'div'))") | |
proto.append = s("e.appendTo(t)") | |
proto.prepend = s("e.prependTo(t)") | |
proto.after = s("e.insertAfter(t)") | |
proto.before = s("e.insertBefore(t)") | |
proto.make = s("t.create().html(e)") | |
proto.decode = s("eval('('+e+')')") | |
proto.fadeIn=s("t.fade(e)") | |
proto.fadeOut=s("t.fade(e,1)") | |
proto.slideDown=s("t.slide(e,0)") | |
proto.slideUp=s("t.slide(e,1)") | |
proto.hidden = s("t.css('display').is('none')") | |
proto.sfilter = s("t.filter(t.S(e))") | |
//format: _.all().sfilter("e==true").length | |
proto.smap = s("t.map(t.S(e))") | |
//format: _.get([1,2,3,4,5,6]).smap("e+1").A() = [2,3,4,5,6,7] | |
s() | |
compiled.join("%") | |
*/ | |
//this is the basic iterator function | |
I = proto.I = function(f){ | |
return function(){ | |
for(var w=[],i=-1,_this=this;++i<_this[_length];) | |
w=w.concat(f.apply(_this[i],o(arguments).A().concat([_this]))); | |
return o(w); | |
} | |
} | |
compiled = | |
//BEGIN COMPILED CODE | |
"[e(t)]%map%e(t)?[t]:[]%filter%o(t.getElementsByTagName(e)).A()%tag%o(t.getElementsByName(e)).A()%name%o(t.parentNode).A()%parent%o(t.childNodes).A()%children%[t&&o(t.className.split(' ')).has(e)>-1]%hasclass%a.fx(0,function(u,p){u==1&&e(p)})%queue%[e==t]%is" //300 bytes | |
//END COMPILED CODE | |
.split('%'); | |
for(l=compiled[_length];l--;)proto[compiled[l--]]=I(S(compiled[l])); | |
/* | |
var z = {}, lines = {}, compiled = [] | |
function I(){return true} | |
function S(text){ | |
for(var nam in z){ | |
if(!lines[nam]){ | |
lines[nam] = true; | |
compiled.push(nam) | |
} | |
} | |
if(text !== undefined)compiled.push(text); | |
} | |
proto.is = I(S("[e==t]")) | |
proto.map = I(S("[e(t)]")) | |
proto.filter = I(S("e(t)?[t]:[]")) | |
proto.tag=I(S("o(t.getElementsByTagName(e)).A()")) | |
proto.name=I(S("o(t.getElementsByName(e)).A()")) | |
proto.parent=I(S("o(t.parentNode).A()")) | |
proto.children=I(S("o(t.childNodes).A()")) | |
proto.hasclass = I(S("[t&&o(t.className.split(' ')).has(e)>-1]")) | |
proto.queue = I(S('a.fx(0,function(u,p){u==1&&e(p)})')) | |
s() | |
compiled.join("%") | |
*/ | |
compiled = | |
//////////////////////////////////////////// | |
"o(this).hasclass(e)[0]||(this.className+=' '+e)%addclass%t.className=o(t.className.split(' ')).del(e).tj(' ')%removeclass%a.ajax(e,function(y){o(t).html(y)})%load%o(t).css('display','none')%hide%e!==false?o(t).css('display',''):o(t).hide()%show%t.attachEvent?t.detachEvent('on'+e,t[e+a]):t.removeEventListener(e,a,0)%un%o(e)[0].appendChild(t)%appendTo%o(e)[0].insertBefore(t,o(e)[0].firstChild)%prependTo%o(e)[0].parentNode.insertBefore(t,o(e)[0].nextSibling)%insertAfter%o(e)[0].parentNode.insertBefore(t,o(e)[0])%insertBefore%t.parentNode.removeChild(t)%remove%t.Q=[]%empty%t.R=0%cancel%e(t)%each" //600 bytes | |
////////////////////////////////////// | |
.split('%'); | |
for(l=compiled[_length];l--;)proto[compiled[l--]]=R(compiled[l]); | |
/* | |
var proto = {}, lines = {}, compiled = [] | |
function r(text){ | |
for(var nam in proto){ | |
if(!lines[nam]){ | |
lines[nam] = true; | |
compiled.push(nam) | |
} | |
} | |
if(text !== undefined)compiled.push(text); | |
return true; | |
} | |
proto.each = r("e(t)") | |
proto.addclass = r("o(this).hasclass(e)[0]||(this.className+=' '+e)") | |
proto.removeclass = r("t.className=o(t.className.split(' ')).del(e).tj(' ')") | |
proto.load = r("a.ajax(e,function(y){o(t).html(y)})") | |
proto.hide = r("o(t).css('display','none')") | |
proto.show = r("e!==false?o(t).css('display',''):o(t).hide()") | |
proto.un = r("t.attachEvent?t.detachEvent('on'+e,t[e+a]):t.removeEventListener(e,a,0)") | |
proto.appendTo = r("o(e)[0].appendChild(t)") | |
proto.prependTo = r("o(e)[0].insertBefore(t,o(e)[0].firstChild)") | |
proto.insertAfter = r("o(e)[0].parentNode.insertBefore(t,o(e)[0].nextSibling)") | |
proto.insertBefore = r("o(e)[0].parentNode.insertBefore(t,o(e)[0])") | |
proto.remove = r("t.parentNode.removeChild(t)") | |
proto.empty = r("t.Q=[]") | |
proto.cancel = r("t.R=0") | |
r() | |
compiled.join("%") | |
*/ | |
proto.A = proto.array = function(){ | |
for(var i=-1, new_array=[]; ++i<this[_length];) | |
new_array.push(this[i]); | |
return new_array | |
} | |
proto.has = function(v){ | |
for(var i = 0, _this=this; //redefine _this to shorten it | |
_this[i]!=v && ++i < _this[_length]; //loop/increment until element is found | |
){}; //closure gets rid of {} | |
return i<_this[_length]?i:-1 //if not found, it's a huge number | |
} | |
proto.unique = function(){ | |
for(var _this = this, b=_this[_length], new_array=[];b--;) | |
o(new_array).has(_this[b])>0? //loop if not in list | |
0:new_array.push(_this[b]); //add it | |
return o(new_array) | |
} | |
//queue a function rather than an animation | |
//example: _.id("fade").fade(1000,1).fade(1000).fade(1000,1).fade(1000).queue(function(){alert("done")}) | |
//loop forever: _.id("fade").queue(function(p){p.fadeOut().fadeIn().queue(arguments.callee)}) | |
//Queue callback is passed with the element as teh first argument | |
proto.cls=I(function(className){ | |
var t = this, h = "getElementsByClassName"; | |
if(t[h]) return o(t[h](className)).A(); | |
//backup getElementByClassName, first arg is classes, second is the element | |
//only supports one class at a time | |
for(var a = o(t).all(), | |
k = a.hasclass(c), | |
l = 0, | |
h = []; | |
++l<a[_length];) | |
k[l] && h.push(a[l]); | |
return h | |
}) | |
proto.del = function(e){ | |
var x = this.has(e), y = this.A() | |
x>0&&y.splice(x,1) | |
return o(y) | |
} | |
proto.ajax = function(u,f,d){ | |
var x = new(window.XMLHttpRequest||ActiveXObject)('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 | |
} | |
//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='') | |
proto.css = I(function(e,f,t){ | |
e=e.replace(/-(.)/,S("a.toUpperCase()")) | |
var i = this; | |
if(!t) return [i.style[e]]; //because of a "design flaw", if f doesnt exist, t gets added at the end :( | |
i.style[e] = f; | |
return [i] | |
}) | |
proto.attr = I(function(e,f,t){ | |
var i = this; | |
if(!t)return [i.getAttribute(e)]; | |
i.setAttribute(e,f); | |
return [i] | |
}) | |
proto.html = I(C("innerHTML")) | |
proto.text = I(function(e, a){ | |
var t = this; | |
if(!a) return [t.textContent||t.innerText]; | |
if(t.innerText!==_undefined)t.innerText=e;else t.textContent=e; | |
return [t] | |
}) | |
proto.val = I(C("value")) | |
proto.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] | |
}) | |
proto.one = function(t, f){ | |
var j = this, g = function(a){ | |
f.call(this,a) | |
j.un(t, g) | |
} | |
return j.on(t, g) | |
} | |
proto.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] | |
}) | |
proto.ready = function(f){ | |
ie? //if unhappy browser | |
setTimeout(f,0): //do ugly hack | |
this.D().addEventListener( | |
'DOMContentLoaded',f, 0) //or else use the nice way | |
} | |
////////THE EASING FUNCTIONS/////////////// | |
//proto.ease = proto.esine = S("(1-Math.cos(e*Math.PI))/2"); | |
// proto.eline = S("e"); | |
proto.fx = I(function(duration, //duration in milliseconds | |
callback, //callback for each iteration args: (post-easing val 0-1, pre-easing val 0-1) | |
t //added by the iterator | |
){ | |
var element = this, | |
now = t.now, //assign a shortuct to this.now(), which is +new Date | |
begin = now(), //store the startup time | |
amount, //here is the amount, its from 0-1 and pre-easing | |
loop //this stores the main animation looper | |
if(element.R){element.Q=(element.Q||[]).concat([arguments]);return[element]}; //add to queue | |
element.R = 1; //set thta the animation is running | |
//start it off with zero. (or 1 if its decrementing) | |
(loop = function(){ //set the main looper | |
if( | |
callback( | |
(1-Math.cos( | |
(amount = (now()-begin)/duration) | |
*Math.PI))/2, | |
element | |
) != 9 //9 terminates execution | |
&& amount<1 && element.R) setTimeout(loop, 10); else{ | |
!(amount%1)||callback(1,element); | |
element.R = 0 | |
element.Q && element.Q.length && | |
o(element).fx.apply(o(element), o((element.Q).shift()).A().slice(0,2)); | |
} | |
})() //start off the looper | |
return [element] | |
}) | |
proto.fade = function(d, r){ | |
var t = this; | |
t.fx(d || 400, function(a,e){ | |
if(!a && o(e).hidden()[0]==r)return 9; | |
if(a==1) t.css("opacity",'').css("filter", '').show(!r); | |
else{ | |
r&&(a=1-a); | |
t.show().css("opacity", a+''); | |
ie && | |
t.css("filter", 'alpha(opacity='+100*a+')'); | |
} | |
}) | |
return t; | |
} | |
proto.slide = function(d, r){ | |
var t = this, p = 0, _height = 'height'; | |
return t.fx(d || 400, function(a, e){ | |
if(!a && o(e).hidden()[0]==r)return 9; | |
a==1? | |
o(e).css(_height, p + "px").show(!r): | |
o(e).css(_height, (r?(1-a):a)*(p||(p=o(e).show().pos()[0].h)) + "px"); | |
}) | |
} | |
proto.encode = function(j){ | |
if(!j)return j+''; | |
var t=[], N = proto.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 | |
} | |
o.J = core; //expose | |
//reuse variables | |
compiled = o(document.documentElement); | |
for(l in proto) | |
o[l] = proto.bind(compiled[l], compiled); | |
o.fn = compiled.fn; | |
o.o = compiled.o; | |
return o; | |
})() | |
////////////////////////////////////////////////////////////////////////////////// | |
////////////////////////////////////////////////////////////////////////////////// | |
////////////////////////////////////////////////////////////////////////////////// | |
////////////////////////////////////////////////////////////////////////////////// | |
////////////////////////////////////////////////////////////////////////////////// | |
////////////////////////////////////////////////////////////////////////////////// | |
////////////////////////////////////////////////////////////////////////////////// | |
////////////////////////////////////////////////////////////////////////////////// | |
////////////////////////////////////////////////////////////////////////////////// | |
////////////////////////////////////////////////////////////////////////////////// | |
////////////////////////////////////////////////////////////////////////////////// | |
////////////////////////////////////////////////////////////////////////////////// | |
////////////////////////////////////////////////////////////////////////////////// | |
////////////////////////////////////////////////////////////////////////////////// | |
//_.fn.ease = _.fn.eline | |
_.fn.loadtext = _.R("a.ajax(e,function(y){o(t).text(y)})") | |
_.fn.hover = _.I(function(enter, leave, root){ | |
enter = enter || _.no; | |
leave = leave || _.no; | |
root = root || leave | |
var h = function(fn){ | |
return function(event){ | |
var related = event.relatedTarget; | |
this !== related && root.o(this).all().has(related) < 0 && fn.call(this, event) | |
} | |
} | |
if("\v"=="v"){ | |
root.o(this).on("mouseenter", h(enter)) | |
root.o(this).on("mouseleave", h(leave)) | |
}else{ | |
root.o(this).on("mouseover", h(enter)) | |
root.o(this).on("mouseout", h(leave)) | |
} | |
return [this] | |
}) | |
_.fn.wslide = function(d, r){ | |
var t = this, p = 0, _width = 'width'; | |
return t.fx(d || 400, function(a, e){ | |
if(!a && _.o(e).hidden()[0]==r)return 9; | |
a==1? | |
t.css(_width, p + "px").show(!r): | |
t.css(_width, (r?(1-a):a)*(p||(p=t.show().pos()[0].w)) + "px"); | |
}) | |
} | |
with(_){ | |
ready(function(){ | |
}) | |
} | |
_.ready(function(){ | |
_.tag("div").index(3).array() | |
_.cls("test") | |
.on("click", function(e){ | |
if(this.alternate = !this.alternate){ | |
_.get(this) | |
.addclass("purple") | |
.removeclass("pink") | |
.css("font-size","x-large") | |
.css("width","200px") | |
}else{ | |
_.get(this) | |
.addclass("pink") | |
.removeclass("purple") | |
.css("font-size","x-small") | |
.css("width","100px") | |
} | |
}) | |
.on("mouseover",function(e){ | |
_.get(this).css("background-color","#007fff") | |
}) | |
.on("mouseout",function(e){ | |
_.get(this).css("backgroundColor","") | |
}) | |
_.tag("button") | |
.one("click", function(){ | |
alert("this should happen only once") | |
_.cls("test") | |
.index(2) | |
.remove() | |
_.create("a") | |
.appendTo(_.tag("body")) | |
.text("hello world at "+_.now()) | |
.attr("href","http://www.google.com") | |
_.create("pre") | |
.appendTo(_.tag("body")) | |
.loadtext("lib.js") | |
}) | |
_.create() | |
.html(_.encode(_.decode(_.encode(_.cls("test").index(2).pos()[0])))) | |
.appendTo(_.tag("body")) | |
.after(_.make("<span>Click me!</span>").css("color","red")) | |
.on("click",function(){ | |
_.cls("test").fadeOut().fadeIn() | |
}) | |
_.create() | |
.appendTo(_.tag("body")) | |
.prepend(_.cls("test")) | |
//*/ | |
/* | |
//operates on the same demo that is used in the jQuery mouseenter | |
var moo = 0; | |
_.cls("overout").on("mouseover", function(){ | |
_.get(this).tag("p").last().text(++moo); | |
_.get(this).tag("p").index(0).text("mouse over"); | |
}).on("mouseout", function(){ | |
_.get(this).tag("p").index(0).text("mouse out"); | |
}) | |
var mel = 0 | |
_.cls("enterleave").hover(function(){ | |
_.get(this).tag("p").last().text(++mel); | |
_.get(this).tag("p").index(0).text("mouse enter"); | |
}, function(){ | |
_.get(this).tag("p").index(0).text("mouse leave"); | |
}) | |
*/ | |
//* | |
_.id("fadehover").wslide(500,1).wslide(500,0) | |
_.id("fade").slideUp(1000) | |
_.id("fadehover").hover(function(){ | |
_.id("fade").empty().slideDown() | |
}, function(){ | |
_.id("fade").empty().slideUp() | |
}) | |
//*/ | |
function t(result,value){ | |
if(result == value){ | |
_.get(document.body).append(_.create("span").text("Pass ").css("color","green")).on("mouseover",function(){ | |
_.get(this).text(result+" == "+value+" ").css("color","") | |
}).on("mouseout",function(){ | |
_.get(this).text("Pass ").css("color","green") | |
}) | |
}else{ | |
_.get(document.body).append(_.create("span").text("Fail ("+result+") should be "+value+" ").css("color","red")) | |
} | |
} | |
_.make("<hr><b>Unit</b> Tests:<br>").appendTo(_.tag("body")) | |
t(_.trim(" hello ") , "hello") | |
t(_.get([1,3,5,67,7]).has(5) , 2) | |
t(_.get([1,3,5,67,7]).has(25) , -1) | |
t(_.get([1,3,5,"poop",7,"eek"]).has("hello") , -1) | |
t(_.get([1,3,5,"poop",7,"eek"]).has("poop") , 3) | |
t(_.cls("test").hasclass("test")[0] , true) | |
t(_([1,2,3,4,5,6]).sfilter("e%2==0").length, 3) | |
t(_([1,2,3,4,5,6]).tj() , "123456") | |
t(_([1,2,3,4,5,6,4,4,8]).del(4).tj() , "12356448") //notice that deletions go backwards | |
t(_([1,2,3,4,5,6]).map(function(e){return e+1}).tj() , "234567") | |
t(_([1,2,3,4,5,6]).map(_.S('e+1')).tj() , "234567") | |
t(_([1,2,3,4,5,6]).smap('e+1').tj() , "234567") | |
t(_([1,2,3,4,5,6]).filter(_.S('e>3')).tj() , "456") | |
t(_.no(),undefined) | |
t(_.encode({alpha:'beta','num':42,arr:['pir','ate'], rec: {urs: 'ion', 'is': ['g'], 'r': 8}}), "{'alpha':'beta','num':42,'arr':['pir','ate'],'rec':{'urs':'ion','is':['g'],'r':8}}") | |
t(_.make("cheesyness").text().tj() , "cheesyness") | |
t(_.make("cheesyness").html().tj() , "cheesyness") | |
t(_.create("input").attr("value","poops").val().tj() , "poops"); | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment