UNPKG

istesequi

Version:

Lightweight and intuitive javascript library

2 lines 5.94 kB
/* Umbrella JS 1.7.0 umbrellajs.com */ function ajax(a,b,c,d,e){d=d||Function;var f=new XMLHttpRequest;return u([f]).on("error timeout abort",function(){d(new Error,null,f)}).on("load",function(){var a=/^(2|3)/.test(f.status)?null:new Error(f.status),b=parseJson(f.response)||f.response;return d(a,b,f)}),f.open(a||"GET",b),f.setRequestHeader("X-Requested-With","XMLHttpRequest"),f.setRequestHeader("Content-Type","application/x-www-form-urlencoded"),e&&e(f),f.send("string"==typeof c?c:u().param(c)),f}function parseJson(a){try{var b=JSON.parse(a);if(b&&"object"==typeof b)return b}catch(c){}return!1}var u=function(a,b){return this instanceof u?a instanceof u?a:("string"==typeof a&&(a=this.select(a,b)),a&&a.nodeName&&(a=[a]),Array.isArray(a)||(a=this.slice(a)),this.nodes=a,this):new u(a,b)};u.prototype={get length(){return this.nodes.length}},u.prototype.slice=function(a){return a?[].slice.call(a,0):[]},u.prototype.str=function(a,b){return function(c){return"function"==typeof c?c.call(this,a,b):c.toString()}},u.prototype.args=function(a,b,c){return"string"!=typeof a&&(a=this.slice(a).map(this.str(b,c))),a.toString().split(/[\s,]+/).filter(function(a){return a.length})},u.prototype.unique=function(){return u(this.nodes.reduce(function(a,b){return b&&-1===a.indexOf(b)?a.concat(b):a},[]))},u.prototype.uri=function(a){return encodeURIComponent(a).replace(/!/g,"%21").replace(/'/g,"%27").replace(/\(/g,"%28").replace(/\)/g,"%29").replace(/\*/g,"%2A").replace(/%20/g,"+")},u.prototype.param=function(a){var b="";for(var c in a)b+="&"+this.uri(c)+"="+this.uri(a[c]);return b.slice(1)},u.prototype.nodes=[],u.prototype.addClass=function(){return this.eacharg(arguments,function(a,b){a.classList.add(b)})},u.prototype.adjacent=function(a,b,c){return this.each(function(d){u(c||[""]).each(function(c,e){var f="function"==typeof b?b(c,e):b;d.insertAdjacentHTML(a,f)})})},u.prototype.after=function(a,b){return this.adjacent("afterend",a,b)},u.prototype.ajax=function(a,b){return this.on("submit",function(c){c.preventDefault(),ajax(u(this).attr("method"),u(this).attr("action"),u(this).serialize(),a,b)})},u.prototype.append=function(a,b){return this.adjacent("beforeend",a,b)},u.prototype.attr=function(a,b){if(void 0!==b){var c=a;a={},a[c]=b}return"object"==typeof a?this.each(function(b){for(var c in a)null!==a[c]?b.setAttribute(c,a[c]):b.removeAttribute(c)}):this.nodes.length?this.first().getAttribute(a):""},u.prototype.before=function(a,b){return this.adjacent("beforebegin",a,b)},u.prototype.children=function(a){var b=this;return this.join(function(a){return b.slice(a.children)}).filter(a)},u.prototype.closest=function(a){return this.join(function(b){do if(u(b).is(a))return b;while(b=b.parentNode)})},u.prototype.data=function(a,b){if("object"==typeof a){var c={};for(var d in a)c["data-"+d]=a[d];return this.attr(c)}return this.attr("data-"+a,b)},u.prototype.each=function(a){return this.nodes.forEach(function(b,c){a.call(this,b,c)},this),this},u.prototype.eacharg=function(a,b){return this.each(function(c,d){this.args(a,c,d).forEach(function(a){b.call(this,c,a)})})},u.prototype.filter=function(a){var b=function(b){return b.matches=b.matches||b.msMatchesSelector||b.webkitMatchesSelector,b.matches(a||"*")};return"function"==typeof a&&(b=a),a instanceof u&&(b=function(b){return-1!==a.nodes.indexOf(b)}),u(this.nodes.filter(b))},u.prototype.find=function(a){return this.join(function(b){return u(a||"*",b).nodes})},u.prototype.first=function(){return this.nodes[0]||!1},u.prototype.hasClass=function(a){return a=this.args(arguments),this.nodes.some(function(b){return a.every(function(a){return b.classList.contains(a)})})},u.prototype.html=function(a){return void 0===a?this.first().innerHTML||"":this.each(function(b){b.innerHTML=a})},u.prototype.is=function(a){return this.filter(a).nodes.length>0},u.prototype.join=function(a){return u(this.nodes.reduce(function(b,c,d){return b.concat(a(c,d))},[])).unique()},u.prototype.last=function(){return this.nodes[this.nodes.length-1]||!1},u.prototype.not=function(a){return this.filter(function(b){return!u(b).is(a||!0)})},u.prototype.on=function(a,b){return this.each(function(c){this.args(a).forEach(function(a){c.addEventListener(a,b)})})},u.prototype.parent=function(a){return this.join(function(a){return a.parentNode}).filter(a)},u.prototype.prepend=function(a,b){return this.adjacent("afterbegin",a,b)},u.prototype.remove=function(){return this.each(function(a){a.parentNode.removeChild(a)})},u.prototype.removeClass=function(){return this.eacharg(arguments,function(a,b){a.classList.remove(b)})},u.prototype.select=function(a,b){if(b)return this.select.byCss(a,b);for(var c in this.selectors)if(b=c.split("/"),new RegExp(b[1],b[2]).test(a))return this.selectors[c](a);return this.select.byCss(a)},u.prototype.select.byCss=function(a,b){return(b||document).querySelectorAll(a)},u.prototype.selectors={},u.prototype.selectors[/^\.[\w\-]+$/]=function(a){return document.getElementsByClassName(a.substring(1))},u.prototype.selectors[/^\w+$/]=document.getElementsByTagName.bind(document),u.prototype.selectors[/^\#[\w\-]+$/]=function(a){return document.getElementById(a.substring(1))},u.prototype.selectors[/^\</]=function(a){return u(document.createElement("div")).html(a).children().nodes},u.prototype.serialize=function(){return this.param(this.slice(this.first().elements).reduce(function(a,b){return!b.name||"file"===b.type||/(checkbox|radio)/.test(b.type)&&!b.checked||(a[b.name]=b.value),a},{}))},u.prototype.siblings=function(a){return this.parent().children(a).not(this)},u.prototype.toggleClass=function(a,b){return!!b===b?this[b?"addClass":"removeClass"](a):this.eacharg(a,function(a,b){a.classList.toggle(b)})},u.prototype.trigger=function(a){var b={bubbles:!0,cancelable:!0};try{a="string"==typeof a?new Event(a,b):a}catch(c){var d=a;a=document.createEvent("Event"),a.initEvent(d,b.bubbles,b.cancelable)}return this.each(function(b){b.dispatchEvent(a)})};