-
Notifications
You must be signed in to change notification settings - Fork 3
/
dominate.min.js
20 lines (20 loc) · 8.4 KB
/
dominate.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
var dominate=function(e,a){a=a||document;return dominate.wrap_list(U.args(a.querySelectorAll(e)))};dominate.all=dominate;dominate.one=function(e,a){return dominate.utils.is_node(e)?dominate.wrap(e):"string"===typeof e?(a=a||document,dominate.wrap(a.querySelector(e))):null};
(function(e){var a=e.utils={is_node:function(a){try{return a instanceof Node}catch(c){return a&&a.nodeType}},foreach:function(a,c,b){var b=b||null,g;if(a instanceof Array)for(var d=0;d<a.length;d++){if(g=c.call(b,a[d],d),void 0!==g)return g}else for(d in a)if(a.hasOwnProperty(d)&&(g=c.call(b,a[d],d),void 0!==g))return g},map:function(f,c,b){var b=b||null,g;g=f instanceof Array?[]:{};a.foreach(f,function(d,a){g[a]=c.call(b,d,a)});return g},filter:function(f,c,b){var b=b||null,c=c||function(d){return!!d},
g;f instanceof Array?(g=[],a.foreach(f,function(d,a){c.call(b,d,a)&&g.push(d)})):(g={},a.foreach(f,function(d,a){c.call(b,d,a)&&(g[a]=d)}));return g},remove:function(f,c,b){b=b||null;return a.filter(f,function(a,d){return!c.call(b,a,d)})},mix:function(f,c,b){a.foreach(c,function(a,d){if(b||!f.hasOwnProperty(d))f[d]=a});return f},dict:function(f,c){var b={};a.foreach(f,function(a,d){b[a]=d&&c&&c[d]});return b},args:function(a,c){return Array.prototype.slice.call(a,c||0)},json:function(a){return"string"===
typeof a?JSON.parse(a):JSON.stringify(a)},values:function(f){var c=[];a.foreach(f,function(a){c.push(a)});return c},keys:function(f){var c=[];a.foreach(f,function(a,g){c.push(g)});return c}};a.json.parse=JSON.parse;a.json.stringify=JSON.stringify})(dominate);
(function(e){var a=e.utils,f=function(a,b,g){var g=g||" ",d=Math.max(0,Math.abs(b)-a.length),g=Array(d+1).join(g);return 0>b?g+a:a+g};e.utils.format=function(c){named_args={};pos_args=[];a.foreach(a.args(arguments,1),function(b){"string"==typeof b||"number"===typeof b?pos_args.push(b):"object"==typeof b&&a.mix(named_args,b,!0)});return c.replace(/%(\((\w+)\))?(-?(\d*)(\.(\d+))?)?([sdf])/gi,function(a,c,d,e,h,i,j,k){c=h&&"0"===h.charAt(0);h=(h||0)-0;j=(j||0)-0;d=d?named_args[d]:pos_args.shift();switch(k){case "s":return f(""+
d,h);case "d":d=""+Math.floor(d-0);case "f":return d=(d-0).toFixed(j),f(d,-h,c&&"0");default:return a}})}})(dominate);
(function(e){var a=e.utils;e.utils.listen=function(f,c){f.subscribe(function(c){var g=a.args(arguments,1),d=this[c];d&&d.apply(this,g)},c)};e.utils.event=function(){var f=[],c=function(){var c=a.args(arguments);a.foreach(f,function(a){var d=a.args.concat(c);a.callback.apply(a.context,d)})};c.subscribe=function(c,g){f.push({callback:c,context:g,args:a.args(arguments,2)})};c.unsubscribe=function(c,g){f=a.remove(f,function(a){return a.callback==c&&a.context==g})};c.len=function(){return f.length};return c}})(dominate);
(function(e){var a=e.utils,f=e.html={empty:function(a){for(var b;b=a.lastChild;)a.removeChild(b)},add:function(a){for(var b=1;b<arguments.length;b++){var g=arguments[b];g instanceof e.wrapper?a.appendChild(g.dom):a.appendChild(g)}},css:function(c){a.foreach(a.args(arguments,1),function(b){a.foreach(b,function(a,d){c.style[d.replace(/(?:-|_)(.)/g,function(a,d){return d.toUpperCase()})]=a})})},get_classes:function(a){a=a.className;return""===a?[]:a.split(/\s+/)},add_class:function(c){var b=a.dict(f.get_classes(c));
a.mix(b,a.dict(a.args(arguments,1)));c.className=a.keys(b).join(" ")},remove_class:function(c){var b=a.dict(f.get_classes(c));a.foreach(a.args(arguments,1),function(a){delete b[a]});c.className=a.keys(b).join(" ")},toggle_class:function(c){var b=a.dict(f.get_classes(c));a.foreach(a.args(arguments,1),function(a){b.hasOwnProperty(a)?delete b[a]:b[a]=!0});c.className=a.keys(b).join(" ")},has_class:function(c){var b=a.dict(f.get_classes(c));return a.filter(a.args(arguments,1),function(a){return b.hasOwnProperty(a)}).length===
arguments.length-1},set_class:function(c,b,g){var d=a.dict(f.get_classes(c));g||void 0===g?d[b]=1:delete d[b];c.className=a.keys(d).join(" ")},"class":function(c){c.className=a.args(arguments,1).join(" ")},update:function(c){for(var b=c,g=1;g<arguments.length;g++){var d=arguments[g],e,h;if(a.is_node(d))c.appendChild(d);else if("string"===typeof d||"number"===typeof d)c.appendChild(document.createTextNode(d));else if(d instanceof Array)for(e=0;e<d.length;e++)f.update(c,d[e]);else if("function"===typeof d)if(d.hasOwnProperty("dom")&&
a.is_node(d.dom))c.appendChild(d.dom);else if("binding"===d.name)e=document.createTextNode(d()),d.register(function(a){this.nodeValue=a},e),c.appendChild(e);else throw"Not a DOM node";else for(h in"object"===typeof d&&d.hasOwnProperty("context")&&(b=d.context),d)if(d.hasOwnProperty(h)){var i=d[h];if("context"!==h)if("class"===h||"cls"===h)i&&(e=i.split(/\s+/),e=a.filter(e.concat(c.className.split(/\s+/))),c.className=e.join(" "));else if("style"===h)f.css(c,i);else if(/^on/.test(h)){e=h.slice(2);
var j=i,k=[],l=b,m=!1;"function"!==typeof i&&(j=i.func,k=i.args||k,l=i.context||l,m=i.capture||m);i=l!==c;if(0!==k.length)var o=j,j=function(){o.apply(l,k.concat(a.args(arguments)))};else i&&(o=j,j=function(){o.apply(l,arguments)});c.addEventListener(e,j,m)}else c.setAttribute(h,i)}}}}})(dominate);
(function(e){var a=e.tag_names="a address article aside audio blockquote b br button canvas caption code col colgroup dd div dl dt em fieldset figure footer form h1 h2 h3 h4 h5 h6 header hgroup hr i img input label legend li link main math nav ol optgroup option output p pre progress script section select small source span strong style sub sup table tbody td template textarea tfoot th thead time tr ul video".split(" "),f=e.html,c=e.utils,b=e.tags={};b.text=function(a){return document.createTextNode(""+
a)};b.create_tag=function(a){b[a]=function(){var d=document.createElement(a);f.update.apply(this,[d].concat(c.args(arguments)));return d}};c.foreach(a,b.create_tag)})(dominate);
(function(e){var a=e.utils,f=e.io={xhr:function(a,b,g,d,e){var e=e||null,f=new XMLHttpRequest;f.open(a,b,!0);f.addEventListener("readystatechange",function(a){4===f.readyState&&d.call(e,f.responseText,f,a)});f.send(g)},get:function(a,b,g){f.xhr("GET",a,null,b,g)},post:function(a,b,g,d){f.xhr("POST",a,b,g,d)},form:function(c){var b=new FormData;a.foreach(c,function(a,d){b.append(d,a)});return b}}})(dominate);
(function(e){var a=e.utils;e.bind=function(e){var c=[],b=function(b,d){if(e instanceof Array){if(0===arguments.length)return e;if(1===arguments.length)return e[b];e[b]=d}else{if(0===arguments.length)return e;e=b}a.foreach(c,function(a){a.func.call(a.context,e,b,d)})};b.register=function(a,d){c.push({func:a,context:d})};b.unregister=function(b){c=a.remove(c,function(a){return a.func===b&&a.context===context})};return b}})(dominate);
(function(e){var a=e.utils,f=e.html,c="onkeydown onkeypress onkeyup onhelp onmousedown onmousemove onmouseup onmouseout onclick ondblclick onfocus onblur onabort onerror onchange onload onselect onsubmit".split(" "),b=e.wrapper=function(a){this.dom=a},g=e.wrap=function(d){return!a.is_node(d)?null:new b(d)};a.mix(b.prototype,a.map(e.tags,function(d,c){return function(){var d=document.createElement(c),e=a.args(arguments);e.unshift(d);f.update.apply(null,e);this.dom.appendChild(d);return new b(d)}}));
a.mix(b.prototype,a.map(e.html,function(d){return function(){var b=a.args(arguments);b.unshift(this.dom);b=d.apply(null,b);return void 0===b?this:a.is_node(b)?g(b):b}}));b.prototype.on=function(a,c,e,f){var j=this.dom,f=f||!1,k=function(b){var m=e||g(this);!1===c.call(m,b,k)&&j.removeEventListener(a,k,f)};j.addEventListener(a,k,f);return b};a.foreach(c,function(a){b.prototype[a]=function(b,c,e){return this.on(a.slice(2),b,c,e)}});b.prototype.all=function(a){return e.all(a,this.dom)};b.prototype.one=
function(a){return e.one(a,this.dom)};e.wrap_list=function n(b){b.on=function(c,e,f,l){l=l||!1;a.foreach(b,function(a){var b=function(h){var n=f||g(this);!1===e.call(n,h,b)&&a.removeEventListener(c,b,l)};a.addEventListener(c,b,l)});return b};b.foreach=function(c,e){a.foreach(b,function(a,b){c.call(e,g(a),b)})};a.mix(b,a.map(e.tags,function(c){return function(){var e=a.args(arguments);return n(a.map(b,function(a){var b=c.apply(null,e);a.appendChild(b);return b}))}}));a.mix(b,a.map(e.html,function(c){return function(){var e=
a.args(arguments),f=a.map(b,function(a){var b=e.slice();b.unshift(a);return c.apply(this,b)});return 0!==a.filter(f).length?f:b}}));a.foreach(c,function(a){b[a]=function(c,e,f){return b.on(a.slice(2),c,e,f)}});return b}})(dominate);(function(){window.D=dominate;window.U=dominate.utils;window.H=dominate.html;window.I=dominate.io;window.all=dominate;window.one=dominate.one;U.mix(window,dominate.tags)})(dominate);