Skip to content

Instantly share code, notes, and snippets.

@antimatter15
Created February 12, 2010 16:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save antimatter15/302745 to your computer and use it in GitHub Desktop.
Save antimatter15/302745 to your computer and use it in GitHub Desktop.
(function(){
//this is the core exposed function
var o = function(a){return a.fn?a:(new core(a))}
function core(e){
var i=this[L]=e[L];
if(!i){this[0]=e;this[L]=1};
for(;i--;)this[i]=e[i];
this.fn = core.prototype;
this.o = o;
}
o.j = core; //expose
//save space using with()?
var z = core.prototype;
//generators to help shrink code
//var s = function(q){return eval("function(e){var t=this,o=this.o;return "+q+"}")}
var
H = "var t=this,o=(a||t).o;",
L = "length",
_undefined = undefined,
s = z.s = function(q){
return Function("e","a",H+"return "+q)
},
r = z.r = function(q){
return Function("e","a",H+q+";return [t]")
},
c = z.c = function(q){
return Function("e","a", "if(!e)return [this."+q+"];this."+q+"=e;return [this]")
}
//this is the basic iterator function
I=z.I=function(f){
return function(){
for(var w=[],i=-1;++i<this[L];)
w=w.concat(f.apply(this[i],o(arguments).A().concat([this])));
return o(w);
}
}
//z.no = function(){}
z.extend = function(o, a){
for(var y in a) o[y]=a[y];
return o
}
z.del = 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.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
}
/*
z.load = I(function(u,a){
var t = this;
a.ajax(u,function(y){
o(t).html(y)
})
return [t]
})
//*/
z.load = I(r("a.ajax(e,function(y){o(t).html(y)})"))
//CONSIDER ADDDING
//z.loadtext = I(r("a.ajax(e,function(y){o(t).text(y)})"))
C =
//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%t.create().html(e)%make%t.fade(e)%fadeIn%t.fade(e,1)%fadeOut%t.slide(e,0)%slideDown%t.slide(e,1)%slideUp"
//END COMPILED CODE
.split('%')
for(l=C[L];l--;)z[C[l--]]=s(C[l]);
/*
var z = {}
var lines = {}
var compiled = []
function s(text){
for(var nam in z){
if(!lines[nam]){
lines[nam] = true;
compiled.push(nam)
console.log("NAME")
}
}
if(text !== undefined){
compiled.push(text);
console.log("CODE")
}
return true;
}
z.no = s("")
z.trim = s("e.replace(/^\\s+|\\s+$/g,'')");
z.get = s("o(e)");
z.add = s("o(t.A().concat(e))")
z.id = s("o(t.D().getElementById(e))")
z.index =s("o(t[e])")
z.last =s("o(t[t.length-1])")
z.now = s("+new Date")
z.bind = s("function(){return e.apply(a, arguments)}")
z.all = s("t.tag('*')")
z.tj = s("t.A().join(e||'')") //textjoin
z.D = s("t[0].ownerDocument")
z.create = s("o(t.D().createElement(e||'div'))")
z.append = s("e.appendTo(t)")
z.prepend = s("e.prependTo(t)")
z.make = s("t.create().html(e)")
z.fadeIn=s("t.fade(e)")
z.fadeOut=s("t.fade(e,1)")
z.slideDown=s("t.slide(e,0)")
z.slideUp=s("t.slide(e,1)")
s()
compiled.join("%")
*/
z.A = z.array = function(){
for(var i=-1,n=[];++i<this[L];)n.push(this[i]);
//for(var i=this[L],n=[];i--;)n=this[i].concat(n);
return n
}
//*
var C =
//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"
//END COMPILED CODE
.split('%'),l=C[L]
for(;l--;)z[C[l--]]=I(s(C[l]));
/*
var z = {}
var lines = {}
var compiled = []
function I(e){return e}
function s(text){
for(var nam in z){
if(!lines[nam]){
lines[nam] = true;
compiled.push(nam)
console.log("NAME")
}
}
if(text){
compiled.push(text);
console.log("CODE")
}
return true;
}
z.map = I(s("[e(t)]"))
z.filter = I(s("e(t)?[t]:[]"))
z.tag=I(s("o(t.getElementsByTagName(e)).A()"))
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()"))
z.hasclass = I(s("[t&&o(t.className.split(' ')).has(e)>-1]"))
z.queue = I(s('a.fx(0,function(u,p){u==1&&e(p)})'))
s()
compiled.join("%")
*/
z.has = function(v){
//taken from vXJS's index function
for(var i=this[L];i--&&this[i]!=v;){}; //closure gets rid of the {}
return i
}
/*
z.hasclass = I(function(c){
return [this && o(this.className.split(" ")).has(c)>-1]
})
*/
/*
z.addclass = I(function(c){
o(this).hasclass(c)[0]||(this.className+=" "+a)
if(!o(this).hasclass(c)[0])
this.className+=" "+c;
return [this]
})
*/
z.addclass = I(r("o(this).hasclass(e)[0]||(this.className+=' '+e)"))
z.removeclass = I(r("t.className=o(t.className.split(' ')).del(e).tj(' ')"))
/*
z.removeclass = I(function(c){
this.className = o(this.className.split(" ")).del(c).tj(" ")
return [this]
})
*/
z.unique = function(){
for(var t=this,b=t[L], c=[];b--;)
o(c).has(t[b])>0? //loop if not in list
0:c.push(t[b]); //add it
return o(c)
}
//queue a function rather than an animation
//example: _.id("fade").fade(1000,1).fade(1000).fade(1000,1).fade(1000).queue(function(){console.log("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
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[L],
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.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(),
k = a.hasclass(c),
l = a[L],
h = [];
l--;)
k[l] && h.push(a[l]);
return h
})
//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){
e=e.replace(/-(.)/,s("a.toUpperCase()"))
if(f===_undefined)return [this.style[e]];
this.style[e] = f;
return [this]
})
z.attr = I(function(e,f){
if(f===_undefined)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(function(e){
var t = this;
if(!e) return [t.textContent||t.innerText];
if(t.innerText!==_undefined)t.innerText=e;else t.textContent=e;
return [t]
})
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(r("t.attachEvent?t.detachEvent('on'+e,t[e+a]):t.removeEventListener(e,a,0)"))
/*
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.D().addEventListener(
'DOMContentLoaded',f, 0) //or else use the nice way
}
z.appendTo = I(r("o(e)[0].appendChild(t)"))
z.prependTo = I(r("o(e)[0].insertBefore(t,o(e)[0].firstChild)"))
z.remove = I(r("t.parentNode.removeChild(t)"))
z.empty = I(r("t.Q=[]"))
z.ease = z.esine = s("(1-Math.cos(e*Math.PI))/2")
z.eline = s("e")
z.fx = I(function(d, //duration in milliseconds
c, //callback for each iteration args: (post-easing val 0-1, pre-easing val 0-1)
t //added by the iterator
){
var e = this,
n = t.now, //assign a shortuct to this.now(), which is +new Date
b = n(), //store the startup time
a, //here is the amount, its from 0-1 and pre-easing
l //this stores the main animation looper
if(e.R) return e.Q=(e.Q||[]).concat([arguments]);
(l = function(){ //set the main looper
e.R=1
c( //call the main callbacker
t.ease( //run it through the easing function
a = //set the % through the animation to be used in the next section
(n()-b) //calculate the time delta from start to now
/d //divide it all by the total duration
), //close easing
t //pass the original element
)//close callbacker
if(a<1) setTimeout(l, 10); else{
c(1,t)
e.R=0
e.Q&&e.Q.length &&
o(e).fx.apply(t, o((e.Q).shift()).A().slice(0,2));
}
})() //start off the looper
return [e]
})
z.hide = I(r("o(t).css('display','none')"))
z.show = I(r("e!==false?o(t).css('display',''):o(t).hide()"))
z.fade = function(d, r){
var t = this.show();
t.fx(d || 400, function(a){
r&&(a=1-a)
t.css("opacity",a+'')
"\v"=="v" && t.css("filter", 'alpha(opacity='+100*a+')');
if(a==+!r){
t.css("opacity",'')
t.css("filter", '');
t.show(!r)
}
})
return t;
}
z.slide = I(function(d, r){
o(this).queue(function(b){
var t = o(b).show(), p = t.pos()[0].h;
t.fx(d || 400, function(a){
r&&(a=1-a);
t.css('height', a*p + "px")
if(a==+!r){
t.css('height', p + "px")
t.show(!r)
}
})
})
return [this];
})
/*
function(f,s){
return function(){return f.apply(s, arguments)}
}
*/
z.decode = s("eval('('+e+')')")
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
}
_ = o(document.documentElement)
})()
//_.fn.ease = _.fn.eline
_.fn.loadtext = _.I(_.r("a.ajax(e,function(y){o(t).text(y)})"))
_.fn.hover = _.I(function(enter, leave, root){
enter = enter || _.no;
leave = leave || _.no;
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]
})
_.ready(function(){
//_.tag("div")
_.tag("div").index(3).array()
//*
//test.cls("purple").children()
_.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"))
.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("fade").slideUp(600)
_.id("fadehover").hover(function(){
_.id("fade").slideDown(200)
}, function(){
_.id("fade").slideUp(200)
})
function t(r,v){
if(r == v){
_.get(document.body).append(_.create("span").text("Pass ").css("color","green")).on("click",function(){
alert(r+" == "+v)
})
}else{
_.get(document.body).append(_.create("span").text("Fail ("+v+") should be "+r+" ").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(_.cls("test").hasclass("test")[0] , true)
t(_.get([1,2,3,4,5,6]).tj() , "123456")
t(_.get([1,2,3,4,5,6,4,4,8]).del(4).tj() , "12345648") //notice that deletions go backwards
t(_.get([1,2,3,4,5,6]).map(function(e){return e+1}).tj() , "234567")
t(_.get([1,2,3,4,5,6]).map(_.s('e+1')).tj() , "234567")
t(_.get([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}}")
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment