simulacra
Version:
Data-binding function for the DOM.
8 lines (7 loc) • 13.1 kB
JavaScript
/*!
* Simulacra.js
* Version 2.1.16
* MIT License
* http://simulacra.js.org/
*/
!function i(a,l,s){function u(t,e){if(!l[t]){if(!a[t]){var n="function"==typeof require&&require;if(!e&&n)return n(t,!0);if(d)return d(t,!0);var r=new Error("Cannot find module '"+t+"'");throw r.code="MODULE_NOT_FOUND",r}var o=l[t]={exports:{}};a[t][0].call(o.exports,function(e){return u(a[t][1][e]||e)},o,o.exports,i,a,l,s)}return l[t].exports}for(var d="function"==typeof require&&require,e=0;e<s.length;e++)u(s[e]);return u}({1:[function(e,t,n){"use strict";var j=e("./process_nodes"),r=e("./key_map"),l=r.marker,P=r.meta,i=r.isMarkerLast,_=r.hasDefinition,O=r.isBoundToParent,M=r.replaceAttribute,L=r.retainElement,S=r.memoizedObject,o=3,B=["INPUT","TEXTAREA"];function q(e,t,n,r,o){var i,a;if("object"!=typeof t||null===t)throw new TypeError('Invalid type of value "'+t+'", object expected.');for(a in Object.defineProperty(t,S,{value:{},configurable:!0}),Object.defineProperty(t,P,{value:{},configurable:!0}),i=t[P],n)i[a]={keyPath:{key:a,root:o.root,target:t},activeNodes:[],previousValues:[],currentMarker:n[a][l],valueIsArray:null},s(e,t,n,a,r)}function s(a,e,t,s,r){var u=e[S],d=e[P][s],l=t[s],c=l[0],f=!l[_]&&l[1],p=l[_]&&l[1],h=l[2],y=l[i],m=d.keyPath,v=d.activeNodes,g=d.previousValues,N=d.valueIsArray;function n(e){var t,n=u[s];return(u[s]=e)===n||(p&&null!=e?q(a,e,p,r,m):f&&void 0!==(t=f(r,e,void 0===n?null:n,m))&&D(r,t,l[M])),e}function o(e){var t,n,r,o,i,a,l;for(u[s]=e,t=(N=d.valueIsArray=Array.isArray(e))?e:[e],a=0,l=Math.max(g.length,t.length);a<l;a++)o=t[a],i=g[a],r=o&&o===i?null:w(o,i,a),n=d.currentMarker,r&&(y?(n.parentNode.appendChild(r),n.parentNode.appendChild(n)):n.parentNode.insertBefore(r,W(a+1,v)||n));if(t.length!==g.length&&(g.length=v.length=t.length),N)for(t.pop=x,t.push=T,t.shift=E,t.unshift=C,t.splice=A,a=0,l=t.length;a<l;a++)b(t,a);return e}function b(i,a){var l=i[a];Object.defineProperty(i,a,{get:function(){return l},set:function(e){var t,n,r,o;l=e,(t=i[a])!==(n=g[a])&&(o=w(t,n,a)),r=d.currentMarker,o&&(y?(r.parentNode.appendChild(o),r.parentNode.appendChild(r)):r.parentNode.insertBefore(o,W(a+1,v)||r))},enumerable:!0,configurable:!0})}function k(e,t,n){var r,o=d.currentMarker,i=v[n];delete g[n],i&&(delete v[n],N?m.index=n:delete m.index,f?r=f(i,null,t,m):p&&h&&(m.target=t,r=h(i,null,t,m)),r!==L&&o.parentNode.removeChild(i))}function w(e,t,n){var r,o=v[n],i=c;if(void 0===e&&(e=null),void 0===t&&(t=null),null===e)return k(0,t,n),null;if(N?m.index=n:delete m.index,g[n]=e,p)o&&k(0,t,n),i=j(a,c,p),m.target=N?e[n]:e,q(a,e,p,i,m),h&&(m.target=e,h(i,e,null,m));else if(i=o||c.cloneNode(!0),f?void 0!==(r=f(i,e,t,m))&&D(i,r,l[M]):(null===t&&~B.indexOf(i.tagName)&&i.addEventListener("input",function(t,e,n){var r=e.target,o=e.index,i=n;"number"==typeof o&&(r=r[n],i=o);return function(e){r[i]=e.target[t]}}(l[M],m,s)),D(i,e,l[M])),o)return null;return v[n]=i}function x(){var e=this.length-1,t=g[e],n=Array.prototype.pop.call(this);return k(0,t,e),g.length=v.length=this.length,n}function T(){var e,t,n=this.length,r=arguments.length,o=Array.prototype.push.apply(this,arguments);for(r=n+arguments.length;n<r;n++)t=w(this[n],null,n),e=d.currentMarker,t&&(y?(e.parentNode.appendChild(t),e.parentNode.appendChild(e)):e.parentNode.insertBefore(t,e)),b(this,n);return o}function E(){return k(0,g[0],0),Array.prototype.shift.call(g),Array.prototype.shift.call(v),Array.prototype.shift.call(this)}function C(){var e,t,n,r,o=this.length,i=Array.prototype.unshift.apply(this,arguments);for(Array.prototype.unshift.apply(g,arguments),Array.prototype.unshift.apply(v,Array(t)),e=0,t=arguments.length;e<t;e++)r=w(arguments[e],null,e),n=d.currentMarker,r&&(y?(n.parentNode.appendChild(r),n.parentNode.appendChild(n)):n.parentNode.insertBefore(r,W(arguments.length,v)||n));for(e=o+arguments.length;o<e;o++)b(this,o);return i}function A(e,t){var n,r,o,i,a,l,s,u=[];for(r=(n=e)+t;n<r;n++)k(0,g[n],n);for(n=2,r=arguments.length;n<r;n++)u.push(arguments[n]);for(Array.prototype.splice.apply(g,arguments),Array.prototype.splice.apply(v,[e,t].concat(Array(u.length))),i=Array.prototype.splice.apply(this,arguments),s=e-t>=this.length-1,n=e+u.length-1,r=e;r<=n;n--)l=w(u[n-e],null,n),a=d.currentMarker,l&&(y&&s?(a.parentNode.appendChild(l),a.parentNode.appendChild(a)):a.parentNode.insertBefore(l,W(e+u.length,v)||a));if((o=u.length-t)<0)g.length=v.length=this.length;else if(0<o)for(n=this.length-o,r=this.length;n<r;n++)b(this,n);return i}l[O]?n(e[s]):o(e[s]),Object.defineProperty(e,s,{get:function(){return u[s]},set:l[O]?n:o,enumerable:!0,configurable:!0})}function D(e,t,n){var r;switch(n){case"textContent":(r=e.firstChild)&&!r.nextSibling&&r.nodeType===o?r.textContent=t:e.textContent=t;break;case"checked":e.checked=Boolean(t);break;case"value":e.value!==t&&(e.value=t)}}function W(e,t){var n,r,o;for(n=e,r=t.length;n<r;n++)if(t[n]){o=t[n];break}return o}t.exports=q},{"./key_map":5,"./process_nodes":6}],2:[function(e,t,n){"use strict";t.exports=function(e,t){var n,r,o,i,a,l;for(n=0,r=t.length;n<r;n++)if("string"==typeof(l=t[n])[0])for(a=e,o=0,i=l.length;o<i;o++){if(!(l[o]in a))throw new Error("Missing "+l.slice(0,o+1).join(".")+" feature which is required.");a=a[l[o]]}else for(a=l[0],o=1,i=l.length;o<i;o++)if(1<o&&(a=a[l[o]]),void 0===a)throw new Error("Missing "+l[0].name+l.slice(1,o+1).join(".")+" feature which is required.")}},{}],3:[function(e,t,n){"use strict";var i=e("./key_map").retainElement,u="undefined"!=typeof MutationObserver,d="undefined"!=typeof document;function s(t,n){return function(e){return t(e,n)}}function c(e){e.stopPropagation(),e.preventDefault()}t.exports={bindEvents:function(i,a){var l={};void 0===a&&(a=!1);return function(e,t,n,r){var o;if(null===t)for(o in i)e.removeEventListener(o,l[o],a),e.addEventListener(o,c,!0);else if(null===n)for(o in i)l[o]=s(i[o],r),e.addEventListener(o,l[o],a)}},animate:function(s,n,r,o){return function(a,e,t){var l;if("classList"in a)if(null===e){if(s&&a.classList.remove(s),r&&a.classList.add(r),o)return setTimeout(function(){a.parentNode.removeChild(a)},o),i}else null!==e&&null!==t&&n?(a.classList.contains(n)&&(a.classList.remove(n),a.offsetWidth),a.classList.add(n)):null===t&&s&&(u&&d&&!document.documentElement.contains(a)?(l=new MutationObserver(function(e){var t,n,r,o,i;for(t=0,n=e.length;t<n;t++)for(i=e[t],r=0,o=i.addedNodes.length;r<o;r++)i.addedNodes[r]===a&&(a.offsetWidth,a.classList.add(s),l.disconnect())})).observe(document.documentElement,{childList:!0,subtree:!0}):a.classList.add(s))}}}},{"./key_map":5}],4:[function(e,t,n){"use strict";var r,l=e("./process_nodes"),s=e("./bind_keys"),o=e("./key_map"),i=e("./helpers"),u=e("./rehydrate"),d=e("./feature_check"),c=Array.isArray,f=o.hasDefinition,a=o.replaceAttribute,p=o.isBoundToParent,h=o.isProcessed,y=o.marker,m=["INPUT","PROGRESS"],v=["checkbox","radio"],g=[[Object,"defineProperty"],["document","createTreeWalker"],["Node","prototype","cloneNode"],["Node","prototype","normalize"],["Node","prototype","insertBefore"],["Node","prototype","isEqualNode"],["Node","prototype","removeChild"]];for(r in Object.defineProperty(N,"retainElement",{enumerable:!0,value:o.retainElement}),Object.defineProperty(N,"useCommentNode",{get:function(){return l.useCommentNode},set:function(e){l.useCommentNode=e},enumerable:!0}),i)N[r]=i[r];function N(e,t,n){var r,o,i=this?this.document:window.document,a=this?this.Node:window.Node;if(d(this||window,g),null===e||"object"!=typeof e||c(e))throw new TypeError("First argument must be a singular object.");if(!c(t))throw new TypeError("Second argument must be an array.");if("string"==typeof t[0]){if(o=t[0],t[0]=i.querySelector(o),!t[0])throw new Error('Top-level Node "'+o+'" could not be found in the document.')}else if(!(t[0]instanceof a))throw new TypeError("The first position of the top-level must be either a Node or a CSS selector string.");return t[h]||("content"in t[0]&&(t[0]=t[0].content),t[0]=t[0].cloneNode(!0),function(e,t){var n,r=(e?e.document:window.document).createTreeWalker(t,4,l.acceptNode,!1);for(;r.nextNode();)(n=r.currentNode).textContent=n.textContent.trim();t.normalize()}(this,t[0]),function e(t,n){var r=[];var o,i,a,l,s,u,d;var c;if("object"!=typeof n)throw new TypeError("The second position must be an object.");for(a in n){if("function"==typeof(s=n[a])||"object"==typeof s&&null!==s&&!Array.isArray(s))n[a]=s=[t,s];else if("string"==typeof s)n[a]=s=[s];else if(!Array.isArray(s))throw new TypeError('The binding on key "'+a+'" is invalid.');if("string"==typeof s[0]){if(l=s[0],!(d=t.querySelectorAll(l)).length)throw new Error('An element for selector "'+l+'" was not found.');for(o=1,i=d.length;o<i;o++)d[o].parentNode.removeChild(d[o]);s[0]=d[0]}else if(!s[0])throw new TypeError('The first position on key "'+a+'" must be a CSS selector string.');if("content"in s[0]&&(s[0]=s[0].content),u=s[0],"object"==typeof s[1]&&null!==s[1]){if(Object.defineProperty(s,f,{value:!0}),s[2]&&"function"!=typeof s[2])throw new TypeError('The third position on key "'+a+'" must be a function.')}else if(s[1]&&"function"!=typeof s[1])throw new TypeError('The second position on key "'+a+'" must be an object or a function.');if(t!==u){if(r.push([a,u]),!t.contains(u))throw new Error("The bound DOM element must be either contained in or equal to the element in its parent binding.");s[f]?e(u,s[1]):b(s,u),k(s)}else Object.defineProperty(s,p,{value:!0}),s[f]?e(u,s[1]):"function"==typeof s[1]?b(s,u):console.warn('A change function was not defined on the key "'+a+'".'),k(s)}for(a in n)for(u=n[a][0],o=0,i=r.length;o<i;o++)if(c=r[o][0],r[o][1].contains(u)&&c!==a)throw new Error('The element for key "'+a+'" is contained in the element for the adjacent key "'+c+'".');k(n)}(t[0],t[1]),k(t)),r=l(this,t[0],t[1]),s(this,e,t[1],r,{root:e}),n?(u(this,e,t[1],r,n),n):r}function b(e,t){Object.defineProperty(e,a,{value:~m.indexOf(t.nodeName)?~v.indexOf(t.type)?"checked":"value":"textContent"})}function k(e){Object.defineProperty(e,h,{value:!0}),Object.defineProperty(e,y,{value:null,writable:!0})}t.exports=N},{"./bind_keys":1,"./feature_check":2,"./helpers":3,"./key_map":5,"./process_nodes":6,"./rehydrate":7}],5:[function(e,t,n){"use strict";var r,o,i=["hasDefinition","isBoundToParent","isProcessed","isMarkerLast","marker","meta","memoizedObject","matchedNode","replaceAttribute","retainElement","template"],a={},l="function"==typeof Symbol;for(r=0,o=i.length;r<o;r++)a[i[r]]=l?Symbol(i[r]):"__"+i[r]+"__";t.exports=a},{}],6:[function(e,t,n){"use strict";var r=e("./key_map"),y=r.isBoundToParent,m=r.marker,v=r.matchedNode,g=r.template,N=r.isMarkerLast,b=4294967295;function k(e,t,o){var n,r,i,a,l,s,u,d,c,f,p,h=e?e.document:window.document;if(i=o[g]){for(n in t=i.node.cloneNode(!0),u=i.indices,c=d=0,f=h.createTreeWalker(t,b,w,!1),o)if(!(r=o[n])[y]){for(;f.nextNode();){if(d===u[c]){r[m]=f.currentNode,d++;break}d++}c++}}else{for(u=[],function(e,t,n){var r,o,i,a,l,s=e?e.document:window.document,u=s.createTreeWalker(t,b,w,!1),d=[],c=0,f=k.useCommentNode?1:0;for(i in n)d.push(n[i][0]);for(;u.nextNode()&&d.length;){for(r=0,o=d.length;r<o;r++)if(a=d[r],u.currentNode.isEqualNode(a)){for(Object.defineProperty(a,v,{value:{index:c+f,node:u.currentNode}}),k.useCommentNode&&f++,l=s.createTreeWalker(a,b,w,!1);l.nextNode();)f--;d.splice(r,1);break}c++}}(e,t=t.cloneNode(!0),o),p=Object.keys(o).sort(function(e,t){var n=o[e][0][v],r=o[t][0][v];return n.index-r.index}),d=0;d<p.length;d++)n=p[d],(r=o[n])[y]||(i=r[0][v],u.push(i.index),l=(a=i.node).parentNode,null===a.nextElementSibling&&(r[N]=!0),k.useCommentNode?(s=l.insertBefore(h.createComment(' end "'+n+'" '),a),l.insertBefore(h.createComment(' begin "'+n+'" '),s)):s=l.insertBefore(h.createTextNode(""),a),r[m]=s,l.removeChild(a));Object.defineProperty(o,g,{value:{node:t.cloneNode(!0),indices:u}})}return t}function w(){return 1}k.useCommentNode=!1,k.acceptNode=w,t.exports=k,w.acceptNode=w},{"./key_map":5}],7:[function(e,t,n){"use strict";var k=e("./process_nodes"),w=e("./bind_keys"),r=e("./key_map"),x=r.hasDefinition,T=r.isBoundToParent,E=r.marker,C=r.meta,A=k.acceptNode,j=1,P=8;t.exports=function e(t,n,r,o,i){var a=t?t.document:window.document;var l,s,u,d,c,f,p,h;var y,m,v,g,N,b;for(l in r)if(s=r[l],y=n[C][l],c=!s[x]&&s[1],f=s[x]&&s[1],p=s[2],h=y.keyPath,s[T])u=n[l],f&&null!=u?w(t,u,f,i,h):c&&c(i,u,null,h);else if((v=y.activeNodes).length){for(m=y.valueIsArray,u=m?n[l]:[n[l]],g=0,N=a.createTreeWalker(i,j,A,!1);g<v.length&&N.nextNode();)b=v[g],N.currentNode.isEqualNode(b)&&(v.splice(g,1,N.currentNode),d=u[g],m?h.index=g:delete h.index,f?(e(t,d,f,b,N.currentNode),p&&(h.target=d,p(N.currentNode,d,null,h))):c&&c(N.currentNode,d,null,h),g++);if(g!==v.length)throw new Error('Matching nodes could not be found on key "'+l+'", expected '+v.length+", found "+g+".");b=N.currentNode,k.useCommentNode&&null!==b.nextSibling&&b.nextSibling.nodeType===P?s[E]=b.nextSibling:s[E]=b.parentNode.insertBefore(a.createTextNode(""),b.nextSibling)}}},{"./bind_keys":1,"./key_map":5,"./process_nodes":6}],8:[function(e,t,n){window.simulacra=e("../lib/index")},{"../lib/index":4}]},{},[8]);