uav
Version:
Simple is good
1 lines • 9.12 kB
JavaScript
;function _typeof(e){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}!function(){function e(e,n){var t=Array.from(document.querySelectorAll(e));return n&&t.forEach(n),t}function n(n,t){return t?e(n,t):document.querySelector(n)||document.createElement("div")}function t(e,n,t){return Array.prototype[n].apply(e,t)}function r(e){try{return e&&(Object.getPrototypeOf(e)===Object.prototype||Array.isArray(e))}catch(e){return}}function o(e,n){e[n]&&e[n].forEach(function(e){return e.binding(e)})}function u(e,n){e&&e._uav&&r(n)&&(Object.keys(e).forEach(function(t){u(e[t],n[t])}),n._uav=e._uav,e=null)}function a(e){if(!r(e)||e._uav)return e;var t={};return Array.isArray(e)&&(t=[],m.defineProp(t,"_loops",[]),_(t,function(){return o(t._uav,0)})),m.defineProp(t,"_uav",{}),m.defineProp(t,"_watch",function(r,i){e[i]=a(r),Object.defineProperty(t,i,{get:function(){if(n.state&&(!t._loops||"0"===i)){var r=n.state;t._uav[i]=t._uav[i]||[],-1===t._uav[i].indexOf(r)&&t._uav[i].push(r),n.node._uav.push(function(){t._uav[i]&&t._uav[i].splice(t._uav[i].indexOf(r),1),r=null})}return n.last={vm:t,key:i},e[i]},set:function(r){if(e[i]!==r){var c=r&&r._uav;r=a(r),!c&&e[i]&&e[i]._uav&&u(e[i],r),e[i]=r,t._loops?n._pause||(t._loops.forEach(function(n){return n.replace(e[i],i)}),o(t._uav,0)):o(t._uav,i)}},configurable:!0,enumerable:!0})}),Object.keys(e).forEach(function(n){return t._watch(e[n],n)}),t}function i(e,t){var r=function(e){return function(n){e.checked=t(n.vm,n.ctx).toString()===e.value}};e.push(function(e){return e.el.addEventListener("change",function(){t(e.vm,e.ctx),n.last.vm[n.last.key]=e.el.value,n.last=null}),m.bindStep(r(e.el),e),e})}function c(e,t){var r=function(e){return function(r){var o=t(r.vm,r.ctx);n.state=null,Array.isArray(o)?e.checked=-1!==o.map(String).indexOf(e.value):e.checked=!!o}};e.push(function(e){e.el.addEventListener("change",function(){var r=t(e.vm,e.ctx);if(Array.isArray(r)){var o=r.indexOf(e.el.value);-1===o&&e.el.checked?r.push(e.el.value):-1===o||e.el.checked||r.splice(o,1)}else n.last.vm[n.last.key]=e.el.checked;return n.last=null,e});var o=t(e.vm,e.ctx);if(Array.isArray(o)){var u=function(){return r(e.el)(e)};o._loops.push({add:u,remove:u,replace:u})}return m.bindStep(r(e.el),e)})}function f(e,t){var r=function(e){return function(n){var r=e.selectionStart;e.value=t(n.vm,n.ctx);try{e.setSelectionRange(r,r)}catch(e){}}};e.push(function(e){return e.el.addEventListener("input",function(){t(e.vm,e.ctx),n.last.vm[n.last.key]=e.el.value,n.last=null}),m.bindStep(r(e.el),e),e})}function l(e,n){var t=m.stripTags(e.value),r=b(t),o=function(e){return function(n){var o=r(n.vm,n.ctx);t&&e.removeAttribute(t),!1!==o&&(t=!0===o?t:o)&&e.setAttribute(t,"")}};n.push(function(e){return m.bindStep(o(e.el),e)})}function s(e,t){var r=e.value.match(n.expRX),o=r.map(m.stripTags),u=o.map(b),a=e.value,i=e.name.substring(2),c=function(e){return function(n){for(var t=a,c=0;c<u.length;c++){var f=u[c](n.vm,n.ctx);switch(_typeof(f)){case"function":return void(e[i]=f);case"boolean":f=f?o[c]:""}t=t.replace(r[c],f)}e.setAttribute(i,t)}};t.push(function(e){return m.bindStep(c(e.el),e)})}function p(e,n,t){if(0===e.name.indexOf("u-")){switch(e={name:e.name,value:e.value},t.removeAttribute(e.name),e.name){case"u-for":return g(e,n,t);case"u-attr":return l(e,n);case"u-bind":return x(e,n,t)}s(e,n)}else n.push(function(n){return n.el.setAttribute(e.name,e.value),n})}function v(e,n){var t=b(n),r=function(e){return function(n){var r=t(n.vm,n.ctx);if(r&&(r._el||r.tagName)){var o=r._el?r._el:r;o!==e&&m.unbind(e),e.parentNode&&(e.parentNode.replaceChild(o,e),e=o)}else m.unbind(e),e.textContent=r}};e.push(function(e){var n=document.createTextNode("");return e.el.appendChild(n),m.bindStep(r(n),e)})}function d(e,t){var r=e.textContent.split(n.expRX);if(r.length>1)r.forEach(function(e){if(e.trim()){var r=document.createTextNode(e);e.match(n.expRX)?v(t,m.stripTags(e)):t.push(function(e){return e.el.appendChild(r.cloneNode()),e})}});else{var o=e.textContent;t.push(function(e){return e.el.appendChild(document.createTextNode(o)),e})}}function h(e){var n=[];return n.root=function(){return e.cloneNode()},Array.from(e.attributes).forEach(function(t){p(t,n,e)}),e.value&&"OPTION"!==e.tagName&&p({name:"value",value:e.value},n,e),Array.from(e.childNodes).forEach(function(e){if(3===e.nodeType)d(e,n);else{var t=h(e);n.push(function(e){return e.el.appendChild(m.render(t,e.vm,e.ctx)),e})}}),n}n.all=e;var m={defineProp:function(e,n,t){return Object.defineProperty(e,n,{value:t,configurable:!0,writable:!0,enumerable:!1})},unbind:function(e){e&&e._uav&&(Array.from(e.children).forEach(m.unbind),e._uav.forEach(function(e){return e()}),e._uav=null)},setTag:function(e,t){n.tagRX=new RegExp("(^".concat(e,"|").concat(t,"$)"),"g"),n.expRX=new RegExp("(".concat(e,".*?").concat(t,")"),"g")},stripTags:function(e){return e.replace(n.tagRX,"")},bindStep:function(e,t){return n.state=Object.create(t),n.state.binding=e,e(n.state),n.state=null,t},render:function(e,t,r){return n.node=e.root(),m.defineProp(n.node,"_uav",[]),[{vm:t,ctx:r,el:n.node}].concat(e).reduce(function(e,n){return n(e)}).el}},_=function(e,r){m.defineProp(e,"fill",function(o){var u=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,a=arguments.length>2&&void 0!==arguments[2]?arguments[2]:e.length;for(n._pause=!0;u<0;)u+=e.length;for(;a<0;)a+=e.length;t(e,"fill",[o,u,a]);for(var i=0;i<a;i++)!function(n){e._watch(o,n),e._loops.forEach(function(e){return e.replace(o,n)})}(i);return r(),delete n._pause,e}),m.defineProp(e,"push",function(){for(var n=e.length,o=arguments.length,u=new Array(o),a=0;a<o;a++)u[a]=arguments[a];t(e,"push",u);for(var i=n;i<n+u.length;i++)!function(n){e._watch(e[n],n),e._loops.forEach(function(t){return t.add(e[n],n)})}(i);return r(),e}),m.defineProp(e,"pop",function(){var n=e.length-1;e._loops.forEach(function(e){return e.remove(n)});var o=t(e,"pop");return r(),o}),m.defineProp(e,"reverse",function(){return t(e,"reverse"),r(),e}),m.defineProp(e,"shift",function(){n._pause=!0;var o=t(e,"shift");return e._loops.forEach(function(n){n.hasIndex?(e.forEach(n.replace),n.remove(e.length)):n.remove(0)}),r(),delete n._pause,o}),m.defineProp(e,"sort",function(n){var o=t(e,"sort",[n]);return r(),o}),m.defineProp(e,"splice",function(){for(var o=arguments.length,u=new Array(o),a=0;a<o;a++)u[a]=arguments[a];n._pause=!0;var i=u[0],c=u[1]||0,f=e.length,l=t(e,"splice",u);return e._loops.forEach(function(n){if(n.hasIndex){e.forEach(n.replace);for(var t=f;t>e.length;t--)n.remove(t-1)}else{for(var r=0;r<c;r++)n.remove(i);for(var o=2;o<u.length;o++)n.insert(u[o],i+o-2)}}),e.forEach(e._watch),r(),delete n._pause,l}),m.defineProp(e,"unshift",function(){for(var o=arguments.length,u=new Array(o),a=0;a<o;a++)u[a]=arguments[a];return n._pause=!0,t(e,"unshift",u),e._loops.forEach(function(n){n.hasIndex?e.forEach(n.replace):u.forEach(function(e,t){n.insert(e,t)})}),e.forEach(e._watch),r(),delete n._pause,e})},y=function(e,n){var t=n?n.cloneNode():document.createElement("div");return t.innerHTML=e,1!==t.children.length&&console.error("Template must have 1 root node:",e),t.firstElementChild},b=function(e){var n=new Function("with(arguments[0]){with(arguments[1]){return ".concat(e,"}}"));return function(t,r){var o;try{o=n(t,r||{})}catch(n){o="",console.warn(n,e)}return void 0===o||null===o?"":o}},g=function(e,t,r){var o=m.stripTags(e.value).split(" in "),u=b(o[1]),i=o[0].split(","),c=i[0].trim(),f=i[1]?i[1].trim():null,l=h(r.firstElementChild);r.innerHTML="";var s=function(e){return function(t){function r(e,n){var r=t.ctx?Object.create(t.ctx):{};return r[c]=e,r[f]=n,m.render(l,t.vm,r)}Array.from(e.children).forEach(m.unbind),e.innerHTML="";var o=a(u(t.vm,t.ctx)||[]);n.state=null;var i={hasIndex:f,add:function(n,t){var o=r(n,t);e.appendChild(o)},insert:function(n,t){var o=r(n,t);e.insertBefore(o,e.children[t])},remove:function(n){e.children[n]&&(m.unbind(e.children[n]),e.children[n].remove())},replace:function(n,t){var o=e.children[t],u=r(n,t);o?(m.unbind(o),e.replaceChild(u,o)):e.appendChild(u)}};o._loops.push(i),e._uav.push(function(){o._loops.splice(o._loops.indexOf(i),1)}),o.forEach(i.add)}};t.push(function(e){return m.bindStep(s(e.el),e)})},x=function(e,n,t){var r=b(m.stripTags(e.value));switch(t.getAttribute("type")){case"radio":i(n,r);break;case"checkbox":c(n,r);break;default:f(n,r)}};Array.from||(Array.from=function(e){return e?[].slice.call(e):[]}),m.setTag("{","}"),n.component=function(e,t,r){var o=arguments,u=y(e.innerHTML||e);if(!t)return u;var i=h(u);(t=a(t))._el=m.render(i,t),r&&("string"==typeof r&&(r=n(r)),r.tagName&&(n.unbind(r.firstElementChild),r.innerHTML="",r.appendChild(t._el)));for(var c=1;c<arguments.length&&"break"!==function(e){if("function"==typeof o[e])return setTimeout(function(){return o[e](t._el)}),"break"}(c);c++);return t},n.model=a,n.setTag=m.setTag,n.unbind=m.unbind,window.uav=n,"undefined"!=typeof module&&module.exports&&(module.exports=n)}();