Last active
December 24, 2015 16:29
-
-
Save joshbeckman/6828581 to your computer and use it in GitHub Desktop.
Judo.js is tiny front-end javascript library of utilitarian mini-functions for custom development. Like physical judo leveraging the opponent against him/herself, this library leverages the browser into manipulating itself.
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
window.judo = { | |
scrollTo: function(element, to, duration) { | |
var start = element.scrollTop, | |
change = to - start, | |
currentTime = 0, | |
increment = 20, | |
easeInOutQuad = function (t, b, c, d) { | |
t /= d/2; | |
if (t < 1) return c/2*t*t + b; | |
t--; | |
return -c/2 * (t*(t-2) - 1) + b; | |
}, | |
animateScroll = function(){ | |
currentTime += increment; | |
var val = easeInOutQuad(currentTime, start, change, duration); | |
element.scrollTop = val; | |
if(currentTime < duration) { | |
setTimeout(animateScroll, increment); | |
} | |
}; | |
animateScroll(); | |
}, | |
create: function(name, props){ | |
var el = document.createElement(name); | |
for (var p in props){ | |
if(typeof props[p] === 'object'){ | |
for(var q in props[p]){ | |
el[p][q] = props[p][q]; | |
} | |
}else{ | |
el[p] = props[p]; | |
} | |
} | |
return el; | |
}, | |
request: function(url,cb,method,post,contenttype){ | |
var requestTimeout,xhr; | |
try{ xhr = new XMLHttpRequest(); }catch(e){ | |
try{ xhr = new ActiveXObject("Msxml2.XMLHTTP"); }catch (error){ | |
if(console)console.log("tinyxhr: XMLHttpRequest not supported"); | |
return null; | |
} | |
} | |
requestTimeout = setTimeout(function() {xhr.abort(); cb(new Error("tinyxhr: aborted by a timeout"), "",xhr); }, 10000); | |
xhr.onreadystatechange = function(){ | |
if (xhr.readyState != 4) return; | |
clearTimeout(requestTimeout); | |
cb(xhr.status != 200?new Error("tinyxhr: server respnse status is "+xhr.status):false, xhr.responseText,xhr); | |
}; | |
xhr.open(method?method.toUpperCase():"GET", url, true); | |
if(!post){ | |
xhr.send(); | |
}else{ | |
xhr.setRequestHeader('Content-type', contenttype?contenttype:'application/x-www-form-urlencoded'); | |
xhr.send(post); | |
} | |
}, | |
escapeHTML: function(str) { | |
var div = document.createElement('div'); | |
div.appendChild(document.createTextNode(str)); | |
return div.innerHTML; | |
}, | |
unescapeHTML: function(escapedStr) { | |
var div = document.createElement('div'); | |
div.innerHTML = escapedStr; | |
var child = div.childNodes[0]; | |
return child ? child.nodeValue : ''; | |
} | |
}; |
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
window.judo={scrollTo:function(e,t,n){var r=e.scrollTop,i=t-r,s=0,o=20,u=function(e,t,n,r){e/=r/2;if(e<1)return n/2*e*e+t;e--;return-n/2*(e*(e-2)-1)+t},a=function(){s+=o;var t=u(s,r,i,n);e.scrollTop=t;if(s<n){setTimeout(a,o)}};a()},create:function(e,t){var n=document.createElement(e);for(var r in t)n[r]=t[r];return n},request:function(e,t,n,r,i){var s,o;try{o=new XMLHttpRequest}catch(u){try{o=new ActiveXObject("Msxml2.XMLHTTP")}catch(a){if(console)console.log("tinyxhr: XMLHttpRequest not supported");return null}}s=setTimeout(function(){o.abort();t(new Error("tinyxhr: aborted by a timeout"),"",o)},1e4);o.onreadystatechange=function(){if(o.readyState!=4)return;clearTimeout(s);t(o.status!=200?new Error("tinyxhr: server respnse status is "+o.status):false,o.responseText,o)};o.open(n?n.toUpperCase():"GET",e,true);if(!r){o.send()}else{o.setRequestHeader("Content-type",i?i:"application/x-www-form-urlencoded");o.send(r)}},escapeHTML:function(e){var t=document.createElement("div");t.appendChild(document.createTextNode(e));return t.innerHTML},unescapeHTML:function(e){var t=document.createElement("div");t.innerHTML=e;var n=t.childNodes[0];return n?n.nodeValue:""}} |
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
window.judoUsage = { | |
request: { | |
credit: 'Shimon Doodkin - licanse: public doamin - https://gist.github.com/4706967', | |
usage: 'judo.request("http://site.com/ajaxaction",function (err,data,xhr){ if (err) console.log("goterr ",err,\'status=\'+xhr.status); console.log(data) },\'POST',\'value1=1&value2=2\');' | |
}, | |
scrollTo: { | |
credit:'', | |
usage: 'document.getElementsByTagName("button")[0].onclick = function () { judo.scrollTo(document.body, 0, 1250); }' | |
}, | |
create: { | |
credit: '', | |
usage: 'var newElem = judo.create("p",{className: "lead",onclick: "funciton();"});' | |
}, | |
escapeHTML: { | |
credit: '', | |
usage: 'var cleanText = judo.escapeHTML("<h1>This be ugl<em>y</em> text");' | |
}, | |
unescapeHTML: { | |
credit: '', | |
usage: 'var dirtyText = judo.unescapeHTML("<h1>This be ugl<em>y</em> text");' | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment