-
Notifications
You must be signed in to change notification settings - Fork 0
/
ten.min.js
23 lines (23 loc) · 9.86 KB
/
ten.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* ten.js
@copyright 2012-2013 Kevin von Flotow <[email protected]>
@license MIT License <http://opensource.org/licenses/MIT> */
(function(q,y){function M(a){a=(e.isElement(this)?this.parentNode:k).querySelectorAll(a);var b=a.length,c;for(c=0;c<b;c++)if(a[c]===this)return!0;return!1}function h(a){return a?r(a)?e.ready(a):e.isString(a)&&0===a.indexOf("<")?e.create(a):e.find(a):a}function C(a){var b="";f(a,function(a,d){b+=d.outerHTML});return b}function D(a){var b="";f(a,function(a,d){b=d instanceof h?b+C(d):e.isElement(d)?b+d.outerHTML:b+d});return b}function r(a){return"function"===typeof a}function s(a,b,c){var d;d=/<.*?>/g;
if(1>a.length)return a;e.isArray(b)?b=D(b):b instanceof h&&(b=C(b));if("inner"===c)if(b){var l=a.length;if(1<l)for(d=0;d<l;d++)a[d].innerHTML=b;else a[0].innerHTML=b}else a=a.innerHTML;else if(d.test(b)||"afterend"==c)f(a,function(a,d){d.insertAdjacentHTML(c,b)});else{var g;n.hasOwnProperty(b)&&!1!==n[b]?g=n[b]:(g=k.createTextNode(b),n[b]=n.hasOwnProperty(b)&&!1===n[b]?g:!1);"afterbegin"==c?f(a,function(a,b){b.insertBefore(g.cloneNode(!0),b.firstChild)}):"beforeend"==c?f(a,function(a,b){b.appendChild(g.cloneNode(!0))}):
"beforebegin"==c&&f(a,function(a,b){b.insertBefore(g.cloneNode(!0),b)})}return a}function E(a,b){for(var c=a.className?a.className.split(" "):[],d=c.length,e=0;e<d;e++)this[e]=c[e];this.which=a;this.length=e;return this}function u(a,b,c){a=a.which;e.isString(b)&&(b=-1<b.indexOf(" ")?b.split(" "):[b]);for(var d=0,l=b.length;d<l;d++){var g=RegExp("\\b"+b[d]+"\\b","g");if(c==z&&!g.test(a.className))a.className+=(0<a.className.length?" ":"")+b[d];else if(c==A&&g.test(a.className))a.className=a.className.replace(RegExp("(?:s+)?"+
b[d]+"(?:s+)?"),"");else if(c==B)g.test(a.className)?a.className=a.className.replace(RegExp("(?:s+)?"+b[d]+"(?:s+)?"),""):a.className+=(0<a.className.length?" ":"")+b[d];else if(c==v)return g.test(a.className)}a.className=e.trim(a.className)}function w(a,b,c){f(a,function(a,l){if(l){var g=l.classList||new E(l,b);if(e.isString(b)){if(c==v)return g[c](b);g[c](b)}else e.isArray(b)&&f(b,function(a,b){g[c](b)})}});return a}function F(a){return a.replace(/(^\s+|\s+$)/g,"").replace(/\s\s+/g," ")}function G(a,
b){e.isArray(b)&&(b=D(b));return a=-1!=a.indexOf("?")?a.split("?")[0]+"?"+b+"&"+a.split("?")[1]:-1!=a.indexOf("#")?a.split("#")[0]+"?"+b+"#"+a.split("#")[1]:a+"?"+b}function H(a,b,c){if(c)"opacity"!==b&&e.isNumeric(c)&&(c+="px"),f(a,function(a,e){e.style[I(b)]=c});else{if(q.getComputedStyle)return q.getComputedStyle(a[0],null).getPropertyValue(b);if(a[0]&&a[0].currentStyle)return a[0].currentStyle[I(b)]}return a}function I(a){J.test(a)&&(a=a.replace(J,function(a){return a.slice(1).toUpperCase()}));
return a}function K(a,b,c){var d=k.createElement("script"),e=k.getElementsByTagName("head")[0]||k.getElementsByTagName("body")[0];if("js"==c)d.async=!0,d.src=a;else if("css"==c)link.type="text/css",link.rel="stylesheet",link.href=a;else return!1;d.onload=d.onreadystatechange=function(){b&&b();d.onload=d.onreadystatechange=null};e.appendChild(d);return d}function L(a){return a.preventDefault?a.preventDefault:function(){a.returnValue=!1}}function f(a,b){var c=!1;if(e.isArray(a))for(var c=!0,d=0,l=a.length;d<
l;d++)b(d,a[d]);else if(a instanceof h)c=!0,a.each(function(a,c){b(a,c)});else if(e.isObject(a))for(d in c=!0,a)b(d,a[d]);return c}var e={},m={},x={},n={},k=q.document,t=k.documentElement,p=t.matchesSelector||t.webkitMatchesSelector||t.mozMatchesSelector||t.oMatchesSelector||t.msMatchesSelector||M,J=/[\-\s_]./g,z="add",A="remove",B="toggle",v="contains";E.prototype={add:function(a){u(this,a,z)},remove:function(a){u(this,a,A)},toggle:function(a){u(this,a,B)},contains:function(a){return u(this,a,v)}};
e={version:"0.0.10",getScript:function(a,b){return K(a,b,"js")},getCSS:function(a,b){return K(a,b,"css")},parseJSON:function(a){try{ret=JSON.parse(a)}catch(b){ret=a}return ret},ajax:function(a){var b=e.extend({url:!1,type:"GET",data:!1,dataType:"html",charset:"UTF-8",success:!1,error:!1,complete:!1,cache:!0,target:!1},a);a={json:"application/json",jsonp:"application/javascript",html:"text/html",plain:"text/plain"};b.dataType=b.dataType.toLowerCase();b.type=b.type.toUpperCase();b.charset=b.charset.toUpperCase();
if("string"===typeof b.url&&a.hasOwnProperty(b.dataType)){!1===b.cache&&(b.url=G(b.url,"_="+(new Date).getTime()));var c=new XMLHttpRequest;a=a[b.dataType];c.open(b.type,b.url,!0);if("POST"==b.type&&b.data){var d="";e.each(b.data,function(a,b){d=G(d,a+"="+b)});a="application/x-www-form-urlencoded";d=d.replace(/^\?/,"")}c.setRequestHeader("Content-Type",a+";charset="+b.charset);c.send(d&&d);c.onreadystatechange=function(){if(4===c.readyState){r(b.complete)&&b.complete();if(200===c.status){var a=c.responseText||
c.responseXML;"json"==b.dataType&&(a=e.parseJSON(a));b.success.call(b.target instanceof h?b.target:this,a)}else r(b.error)&&b.error.call(b.target instanceof h?b.target:this,c.statusText);c.onreadystatechange=null}}}else b.error.call(b.target instanceof h?b.target:this,"invalid usage")},length:function(a){var b=0,c;if(e.isObject(a))for(c in a)a.hasOwnProperty(c)&&b++;return b},extend:function(){var a=arguments,b=a[0]||[];2<=a.length&&(a=Array.prototype.slice.call(a,1),f(a,function(a,d){null!=d&&f(d,
function(a,c){a!==y&&(b[a]=c)})}));return b},ready:function(a){var b=k.readyState;"complete"==b||"loaded"==b?a():k.onreadystatechange=function(){b=k.readyState;if("complete"==b||"loaded"==b)a(),k.onreadystatechange=null}},create:function(a){var b=a.match(/^<(.*?)\s?\/?>(?:(.*?)<\/.*?>)?$/);a=b[2];b=k.createElement(b[1]);a&&(b.innerHTML=a);return e.find(b)},find:function(a){var b=a;a=new h;if(e.isString(b)){var b=k.querySelectorAll(b)||[],c,d=b.length;for(c=0;c<d;c++)a[c]=b[c];a.length=b.length}else if(e.isArray(b)){c=
0;for(d=b.length;c<d;c++)a[c]=b[c];a.length=c}else a[0]=b,a.length=1;return a},each:f,isElement:function(a){return"object"===typeof HTMLElement?a instanceof HTMLElement:a&&"object"===typeof a&&1===a.nodeType&&"string"===typeof a.nodeName},isString:function(a){return"string"===typeof a},isObject:function(a){return"[object Object]"===Object.prototype.toString.call(a)},isArray:Array.isArray||function(a){return"[object Array]"===Object.prototype.toString.call(a)},isNumeric:function(a){return!isNaN(parseFloat(a))&&
isFinite(a)},isTen:function(a){return a instanceof h},trim:function(a){r(a)?a=a():e.isString(a)?a=F(a):e.isArray(a)&&f(a,function(b,c){e.isString(c)&&(a[b]=F(c))});return a}};h.prototype={addClass:function(a){return w(this,a,z)},removeClass:function(a){return w(this,a,A)},toggleClass:function(a){return w(this,a,B)},hasClass:function(a){return this[0]?this[0].classList?this[0].classList.contains(a):w(this[0],a,v):!1},clone:function(){var a=[];f(this,function(b,c){a[b]=c.cloneNode(!0)});return e.find(a)},
append:function(a){return s(this,a,"beforeend")},prepend:function(a){return s(this,a,"afterbegin")},html:function(a){return s(this,a,"inner")},before:function(a){return s(this,a,"beforebegin")},after:function(a){return s(this,a,"afterend")},find:function(a){var b=[],c="ten-find-"+Math.floor(2E10*Math.random()+1);this.addClass(c);b=e.find("."+c+" "+a);this.removeClass(c);return b},each:function(a){for(var b=0,c=this.length;b<c;b++)a(b,this[b]);return this},text:function(){return this[0].innerText||
this[0].textContent||this[0].innerHTML.replace(/<.*?>/g,"")},first:function(){return e.find(this[0])},last:function(){return e.find(this[this.length-1])},show:function(){f(this,function(a,b){b.style.display=x[this]});return this},hide:function(){f(this,function(a,b){var c=q.getComputedStyle(b,null).getPropertyValue("display");x.hasOwnProperty(this)&&"none"!=x[this]&&(x[this]=c);b.style.display="none"});return this},fadeOut:function(a,b){var c=this,d=c.css("opacity"),e=setInterval(function(){d-=a;
d>a?c.css({opacity:d}):(b.call(c),clearInterval(e))},25);return c},on:function(a,b,c,d){r(b)&&(c=b,b=y);b in m||(m[b]={});for(var f=function(a){a=a||q.event;a.target=a.target||a.srcElement;a.preventDefault=L(a);if(b&&p.call(a.target,b))c.call(a.target,a);else if(b&&p.call(a.target,b+" *")){for(var d=a.target.parentNode;null!=d&&!p.call(d,b);)d=d.parentNode;c.call(d,a)}else b||c.call(a.target,a)},g=0,k=this.length;g<k;g++){var h=this[g];a in m[b]?e.find(h).off(a,b,!0):m[b][a]={};if(1===d){var n=c;
c=function(c){c=c||q.event;c.target=c.target||c.srcElement;c.preventDefault=L(c);n.call(c.target,c);e.find(h).off(a,b)}}m[b][a].func=f;h.addEventListener?h.addEventListener(a,f,!1):h.attachEvent("on"+a,f)}return this},one:function(a,b,c){return this.on(a,b,c,1)},off:function(a,b,c){b=b||y;if(b in m&&a in m[b]){var d=m[b][a];for(func in d)f(this,function(b,c){c.removeEventListener?c.removeEventListener(a,d.func,!1):c.detachEvent("on"+a,d.func)}),!0!==c&&delete m[b]}return this},load:function(a,b){var c=
this;e.ajax({url:a,target:c,success:function(a){f(c,function(b,c){c.innerHTML=a});b&&b.call(c,a)},error:function(a){b.call(c,a)}})},css:function(a,b){var c=this;if(e.isString(a))return H(c,a,b);e.isObject(a)&&f(a,function(a,b){H(c,a,b)});return c},prop:function(a,b){return a&&!b&&this[0].attributes[a]?this[0].attributes[a].value:b?this[0].setAttribute(a,b):!1},width:function(){return this[0].offsetWidth||this[0].clientWidth},height:function(){return this[0].offsetHeight||this[0].clientHeight},offset:function(){return this[0].offsetTop?
{top:this[0].offsetTop,left:this[0].offsetLeft}:{top:0,left:0}},parent:function(){return e.find(this[0].parentNode)},is:function(a){return p.call(this[0],a)},closest:function(a){var b=this[0];if(!p.call(b,a))for(b=this[0].parentNode;null!=b&&!p.call(b,a);)b=b.parentNode;return e.find(b)},hasParent:function(a){var b=this[0],c=!1;if(p.call(b,a))c=!0;else for(b=this[0].parentNode;null!=b;b=b.parentNode)if(b.parentNode&&p.call(b,a)){c=!0;break}return c},remove:function(){f(this,function(a,b){b.remove?
b.remove():b.parentNode&&b.parentNode.removeChild(b)})},eq:function(a){return 0==a%1&&0<this.length?e.find(this[0<a?a-1:0>=a&&this.length-1- -a]):!1}};q.ten=e.extend(function(a){return new h(a)},e,{fn:h.prototype})})(window);