1 line
8.1 KiB
JavaScript
1 line
8.1 KiB
JavaScript
var u=function(e,t){if(!(this instanceof u))return new u(e,t);if(e instanceof u)return e;typeof e=="string"&&(e=this.select(e,t)),e&&e.nodeName&&(e=[e]),this.nodes=this.slice(e)};u.prototype={get length(){return this.nodes.length}},u.prototype.nodes=[],u.prototype.addClass=function(){return this.eacharg(arguments,function(e,t){e.classList.add(t)})},u.prototype.adjacent=function(e,t,n){return typeof t=="number"&&(t===0?t=[]:t=new Array(t).join().split(",").map(Number.call,Number)),this.each(function(s,o){var i=document.createDocumentFragment();u(t||{}).map(function(t,n){var i=typeof e=="function"?e.call(this,t,n,s,o):e;return typeof i=="string"?this.generate(i):u(i)}).each(function(e){this.isInPage(e)?i.appendChild(u(e).clone().first()):i.appendChild(e)}),n.call(this,s,i)})},u.prototype.after=function(e,t){return this.adjacent(e,t,function(e,t){e.parentNode.insertBefore(t,e.nextSibling)})},u.prototype.append=function(e,t){return this.adjacent(e,t,function(e,t){e.appendChild(t)})},u.prototype.args=function(e,t,n){return typeof e=="function"&&(e=e(t,n)),typeof e!="string"&&(e=this.slice(e).map(this.str(t,n))),e.toString().split(/[\s,]+/).filter(function(e){return e.length})},u.prototype.array=function(e){e=e;var t=this;return this.nodes.reduce(function(n,s,o){var i;return e?(i=e.call(t,s,o),i||(i=!1),typeof i=="string"&&(i=u(i)),i instanceof u&&(i=i.nodes)):i=s.innerHTML,n.concat(i!==!1?i:[])},[])},u.prototype.attr=function(e,t,n){return n=n?"data-":"",this.pairs(e,t,function(e,t){return e.getAttribute(n+t)},function(e,t,s){s?e.setAttribute(n+t,s):e.removeAttribute(n+t)})},u.prototype.before=function(e,t){return this.adjacent(e,t,function(e,t){e.parentNode.insertBefore(t,e)})},u.prototype.children=function(e){return this.map(function(e){return this.slice(e.children)}).filter(e)},u.prototype.clone=function(){return this.map(function(e){var n=e.cloneNode(!0),s=this.getAll(n);return this.getAll(e).each(function(e,t){for(var n in this.mirror)this.mirror[n]&&this.mirror[n](e,s.nodes[t])}),n})},u.prototype.getAll=function(t){return u([t].concat(u("*",t).nodes))},u.prototype.mirror={},u.prototype.mirror.events=function(e,t){if(!e._e)return;for(var n in e._e)e._e[n].forEach(function(e){u(t).on(n,e.callback)})},u.prototype.mirror.select=function(e,t){u(e).is("select")&&(t.value=e.value)},u.prototype.mirror.textarea=function(e,t){u(e).is("textarea")&&(t.value=e.value)},u.prototype.closest=function(e){return this.map(function(t){do if(u(t).is(e))return t;while((t=t.parentNode)&&t!==document)})},u.prototype.data=function(e,t){return this.attr(e,t,!0)},u.prototype.each=function(e){return this.nodes.forEach(e.bind(this)),this},u.prototype.eacharg=function(e,t){return this.each(function(n,s){this.args(e,n,s).forEach(function(e){t.call(this,n,e)},this)})},u.prototype.empty=function(){return this.each(function(e){for(;e.firstChild;)e.removeChild(e.firstChild)})},u.prototype.filter=function(e){var t=function(t){return t.matches=t.matches||t.msMatchesSelector||t.webkitMatchesSelector,t.matches(e||"*")};return typeof e=="function"&&(t=e),e instanceof u&&(t=function(t){return e.nodes.indexOf(t)!==-1}),u(this.nodes.filter(t))},u.prototype.find=function(e){return this.map(function(t){return u(e||"*",t)})},u.prototype.first=function(){return this.nodes[0]||!1},u.prototype.generate=function(e){return/^\s*<tr[> ]/.test(e)?u(document.createElement("table")).html(e).children().children().nodes:/^\s*<t(h|d)[> ]/.test(e)?u(document.createElement("table")).html(e).children().children().children().nodes:/^\s*</.test(e)?u(document.createElement("div")).html(e).children().nodes:document.createTextNode(e)},u.prototype.handle=function(){var e=this.slice(arguments).map(function(e){return typeof e=="function"?function(t){t.preventDefault(),e.apply(this,arguments)}:e},this);return this.on.apply(this,e)},u.prototype.hasClass=function(){return this.is("."+this.args(arguments).join("."))},u.prototype.html=function(e){return e===void 0?this.first().innerHTML||"":this.each(function(t){t.innerHTML=e})},u.prototype.is=function(e){return this.filter(e).length>0},u.prototype.isInPage=function(t){return t!==document.body&&document.body.contains(t)},u.prototype.last=function(){return this.nodes[this.length-1]||!1},u.prototype.map=function(e){return e?u(this.array(e)).unique():this},u.prototype.not=function(e){return this.filter(function(t){return!u(t).is(e||!0)})},u.prototype.off=function(e,t,n){var i=t==null&&n==null,s=null,o=t;return typeof t=="string"&&(s=t,o=n),this.eacharg(e,function(e,t){u(e._e?e._e[t]:[]).each(function(n){(i||n.orig_callback===o&&n.selector===s)&&e.removeEventListener(t,n.callback)})})},u.prototype.on=function(e,t,n){function i(e,t){try{Object.defineProperty(e,"currentTarget",{value:t,configurable:!0})}catch{}}var o,s=null,a=t;return typeof t=="string"&&(s=t,a=n,t=function(e){var t=arguments;u(e.currentTarget).find(s).each(function(s){if(!s.contains(e.target))return;var o=e.currentTarget;i(e,s),n.apply(s,t),i(e,o)})}),o=function(e){return t.apply(this,[e].concat(e.detail||[]))},this.eacharg(e,function(e,t){e.addEventListener(t,o),e._e=e._e||{},e._e[t]=e._e[t]||[],e._e[t].push({callback:o,orig_callback:a,selector:s})})},u.prototype.pairs=function(e,t,n,s){if(typeof t!="undefined"){var o=e;e={},e[o]=t}return typeof e=="object"?this.each(function(t,n){for(var o in e)typeof e[o]=="function"?s(t,o,e[o](t,n)):s(t,o,e[o])}):this.length?n(this.first(),e):""},u.prototype.param=function(e){return Object.keys(e).map(function(t){return this.uri(t)+"="+this.uri(e[t])}.bind(this)).join("&")},u.prototype.parent=function(e){return this.map(function(e){return e.parentNode}).filter(e)},u.prototype.prepend=function(e,t){return this.adjacent(e,t,function(e,t){e.insertBefore(t,e.firstChild)})},u.prototype.remove=function(){return this.each(function(e){e.parentNode&&e.parentNode.removeChild(e)})},u.prototype.removeClass=function(){return this.eacharg(arguments,function(e,t){e.classList.remove(t)})},u.prototype.replace=function(e,t){var n=[];return this.adjacent(e,t,function(e,t){n=n.concat(this.slice(t.children)),e.parentNode.replaceChild(t,e)}),u(n)},u.prototype.scroll=function(){return this.first().scrollIntoView({behavior:"smooth"}),this},u.prototype.select=function(e,t){return e=e.replace(/^\s*/,"").replace(/\s*$/,""),/^</.test(e)?u().generate(e):(t||document).querySelectorAll(e)},u.prototype.serialize=function(){var e=this;return this.slice(this.first().elements).reduce(function(t,n){return!n.name||n.disabled||n.type==="file"?t:/(checkbox|radio)/.test(n.type)&&!n.checked?t:n.type==="select-multiple"?(u(n.options).each(function(s){s.selected&&(t+="&"+e.uri(n.name)+"="+e.uri(s.value))}),t):t+"&"+e.uri(n.name)+"="+e.uri(n.value)},"").slice(1)},u.prototype.siblings=function(e){return this.parent().children(e).not(this)},u.prototype.size=function(){return this.first().getBoundingClientRect()},u.prototype.slice=function(e){return!e||e.length===0||typeof e=="string"||e.toString()==="[object Function]"?[]:e.length?[].slice.call(e.nodes||e):[e]},u.prototype.str=function(e,t){return function(n){return typeof n=="function"?n.call(this,e,t):n.toString()}},u.prototype.text=function(e){return e===void 0?this.first().textContent||"":this.each(function(t){t.textContent=e})},u.prototype.toggleClass=function(e,t){return!!t===t?this[t?"addClass":"removeClass"](e):this.eacharg(e,function(e,t){e.classList.toggle(t)})},u.prototype.trigger=function(e){var t=this.slice(arguments).slice(1);return this.eacharg(e,function(e,n){var s,o={bubbles:!0,cancelable:!0,detail:t};try{s=new window.CustomEvent(n,o)}catch{s=document.createEvent("CustomEvent"),s.initCustomEvent(n,!0,!0,t)}e.dispatchEvent(s)})},u.prototype.unique=function(){return u(this.nodes.reduce(function(e,t){var n=t!=null&&t!==!1;return n&&e.indexOf(t)===-1?e.concat(t):e},[]))},u.prototype.uri=function(e){return encodeURIComponent(e).replace(/!/g,"%21").replace(/'/g,"%27").replace(/\(/g,"%28").replace(/\)/g,"%29").replace(/\*/g,"%2A").replace(/%20/g,"+")},u.prototype.wrap=function(e){function t(e){for(;e.firstElementChild;)e=e.firstElementChild;return u(e)}return this.map(function(n){return u(e).each(function(e){t(e).append(n.cloneNode(!0)),n.parentNode.replaceChild(e,n)})})},typeof module=="object"&&module.exports&&(module.exports=u,module.exports.u=u) |