ts-query
Version:
its simple library for dom control.
1 lines • 7.49 kB
JavaScript
var $=function(t,n){"use strict";function e(){return Math.floor(65536*(1+Math.random())).toString(16).substring(1)}var i="prototype",s="constructor",o=console,r=Array,a=function(t){this._state={},this._node=t},u=a.prototype;u.attr=function(t,n){return a._hasVal(n)?void this._node.setAttribute(t,n):this._node.getAttribute(t)||""},u.addClass=function(t){if(!this.hasClass(t)){var n=this._node.className.split(" ");n.push(t),this._node.className=c.trim(n.join(" "))}},u.removeClass=function(t){if(this.hasClass(t)){var n=this._node.className.split(" ");n.splice(n.indexOf(t),1),this._node.className=c.trim(n.join(" "))}},u.css=function(t,n){return"object"==typeof t?(c.forEach(t,function(t,n){this._setCss(n,t)},this),this):a._hasVal(n)?void this._setCss(t,n):this._getCss(t)},u.hide=function(){var t=this.css("display");return"none"!=t&&(this._state.display=t,this.css("display","none")),this},u.show=function(){var t=this.css("display");return"none"===t&&this.css("display",this._state.display||"block"),this},u.toggleDisplay=function(t){return a._hasVal(t)||(t="none"==this.css("display")),t?this.show():this.hide()},u.toggleClass=function(t,n){return a._hasVal(n)||(n=!this.hasClass(t)),n?this.addClass(t):this.removeClass(t)},u.val=function(t){return a._hasVal(t)?void(this._node.value=t):this._node.value||""},u.hasClass=function(t){return-1!=this._node.className.split(" ").indexOf(t)},u.removeAttr=function(t){this._node.removeAttribute(t)},["width","height"].forEach(function(t){u[t]=function(n){return a._hasVal(n)?void this._setCss(t,"number"==typeof n?n+"px":n):this._node[c.camelCase("client "+t)]||parseInt(this._getCss(t))||0}}),u.html=function(t){return a._hasVal(t)?void(this._node.innerHTML=t):this._node.innerHTML},u.remove=function(){this._remove(),this._node.parentNode&&this._node.parentNode.removeChild(this._node)},u._remove=function(){},u._setCss=function(t,n){var e;t=c.camelCase(t),t in a.test.style||(a._activePrefix?(e=c.camelCase(a._activePrefix+t),e in a.test.style?t=e:o.warn("Не корректный стиль!",t,n)):a._prefexes.some(function(n){var e=c.camelCase(n+t);return e in a.test.style?(a._activePrefix=n,t=e,!0):void 0})||o.warn("Не удалось найти префикс!",t,n)),this._node.style[t]=n},u._getCss=function(t){t=c.camelCase(t);var n=[this._node.style,getComputedStyle(this._node)],e=n[0][t]||n[1][t]||"",i=function(t,i){return n.some(function(n){var s=c.camelCase(t);return s in n?(e=n[s],i?i():!1,!0):void 0})};return e||(a._activePrefix?i(a._activePrefix+t)||o.warn("Не удалось получить стиль!",t):a._prefexes.some(function(n){return i(n+t,function(){a._activePrefix=n})})||o.warn("Не удалось получить стиль и префикс!",t)),e},a._hasVal=function(t){return void 0!=t},a._createE=function(t){return new a(t)},a._activePrefix="",a._prefexes=["-moz-","-ms-","-webkit-","-o-"],a.test=n.createElement("DIV");var c=function d(t){return this instanceof d?void r[i].forEach.call(arguments,function(t){this.add(t)},this):new d(t)};c[i]=[],c[i][s]=c;var h=c.prototype;["addClass","removeClass","toggleClass","removeAttr","show","hide","toggleDisplay"].forEach(function(t){h[t]=function(n){return this._toAll(t,[n]),this}}),["val","html","width","height"].forEach(function(t){h[t]=function(n){return this._setOrGet(t,n)}}),["attr"].forEach(function(t){h[t]=function(n,e){return this._setOrGet(t,e,n)}}),h.add=function(t){return r.isArray(t)?t.forEach(this.add,this):this._toElem(t).forEach(function(t){this.push(t)},this),this},h.hasClass=function(t){return this._has()?this[0].hasClass(t):!1},h.find=function(t){return this._has()?new c(r[i].slice.call(this[0]._node.querySelectorAll(t))):this},h.clone=function(){return this._has()?new c(this[0]._node.cloneNode(!0)):this},h.css=function(t,n){return"object"==typeof t?this._toAll("css",[t]):a._hasVal(n)?this._toAll("css",[t,n]):this[0].css(t)},h.each=function(t){return this.forEach(function(n,e){t(this.eq(e),n._node,e)},this),this},h.children=function(){return this._has()?new c(r[i].slice.call(this[0]._node.childNodes)):this},h.eq=function(t){return this._has()?new c(this[t||0]):this},h.get=function(t){return this._has()?this[t||0]._node:null},h.parent=function(){return this._has()?new c(this[0]._node.parentNode):this},h.append=function(t){return this._has()?void this._toElem(t,!0).forEach(function(t){this[0]._node.appendChild(t._node)},this):this},h.prepend=function(t){return this._has()?void this._toElem(t,!0).forEach(function(t){var n=r[i].filter.call(this[0]._node.childNodes,function(t){return c.isElement(t)});n.length?this[0]._node.insertBefore(t._node,n[0]):this[0]._node.appendChild(t._node)},this):this},h._toElem=function(t,e){return c.isElement(t)?[a._createE(t)]:t instanceof a?[t]:t instanceof c?[t[0]]:"string"==typeof t?/<.+?>/.test(t)?c.parse(t).map(function(t){return a._createE(t)}):e?[{_node:document.createTextNode(t)}]:r[i].slice.call(n.querySelectorAll(t)).map(function(t){return a._createE(t)}):(o.log("wrong param!",t),[])},h._toAll=function(t,n){return this.forEach(function(e){e[t].apply(e,n)}),this},h._has=function(){return this.length?!0:(o.warn("not length!"),!1)},h._setOrGet=function(t,n,e){if(!this._has())return this;if(a._hasVal(n)){var i=e?[e,n]:[n];return this._toAll(t,i),this}return this[0][t](e)},c.parse=function(t){var e=n.createElement("div");return e.innerHTML=t,r[i].filter.call(e.childNodes,function(t){return c.isElement(t)})},c.random=function(t,n){return Math.random()*(n-t)+t},c.randomInt=function(t,n){return Math.floor(Math.random()*(n-t+1))+t},c.trim=function(){return"trim"in String[i]?function(t){return t.trim()}:function(t){return(t||"").replace(/^\s+|\s+$/g,"")}}(),c.camelCase=function(t){return t.split(/[^\d\w]/).map(function(t,n){return n?t.charAt(0).toUpperCase()+t.substr(1):t}).join("")},c.guuid=function(){return e()+e()+"-"+e()+"-"+e()+"-"+e()+"-"+e()+e()+e()},c.isElement=function(t){return t instanceof Node&&1==t.nodeType},c.forEach=function(t,n,e,i){r.isArray(t)?t.forEach(function(t,s){n.call(e||this,t,s,i)}):Object.keys(t).forEach(function(s){n.call(e||this,t[s],s,i)})},c.some=function(t,n,e,i){return r.isArray(t)?t.some(function(t,s){return n.call(e||this,t,s,i)}):Object.keys(t).some(function(s){return n.call(e||this,t[s],s,i)})},c.every=function(t,n,e,i){return!c.some(t,function(t,s){return!n.call(e||this,t,s,i)})},c.findFirst=function(t,n,e,i){var s={id:null,value:null};return c.some(t,function(t,o){return n.call(e||window,t,o,i)?(s={id:o,value:t},!0):void 0}),s},c.floor=function(t){return t+.5<<0},c.roundTo=function(t,n){var e=Math.pow(10,n);return c.floor(t*e)/e},c.addPlugin=function(t){t(c,a)},c.events={},c.isPointer=!1;var l=function(t){t="on"+t;var n=document.createElement("div");n.setAttribute(t,"");var e="function"==typeof n[t];return"undefined"!=typeof n[t]&&(n[t]=null),n.removeAttribute(t),e},f={mouse:{start:"mousedown",move:"mousemove",end:"mouseup"},mobile:{start:"touchstart",move:"touchmove",end:"touchend"},win:{start:"MSPointerDown",move:"MSPointerMove",end:"MSPointerUp"}};return function(){window.navigator.msPointerEnabled?(c.events=f.win,c.isPointer=!0):c.events=l("touchmove")?f.mobile:f.mouse}(),window.requestAnimationFrame=function(){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||function(t){window.setTimeout(t,1e3/60)}}(),c}(window.$window||window,window.$document||document);