simulacra
Version:
Data-binding function for the DOM.
8 lines (7 loc) • 13.5 kB
JavaScript
/*!
* Simulacra.js
* Version 2.2.0
* MIT License
* http://simulacra.js.org/
*/
!function i(a,u,l){function s(t,e){if(!u[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=u[t]={exports:{}};a[t][0].call(o.exports,function(e){return s(a[t][1][e]||e)},o,o.exports,i,a,u,l)}return u[t].exports}for(var d="function"==typeof require&&require,e=0;e<l.length;e++)s(l[e]);return s}({1:[function(e,t,n){"use strict";var b=e("./process_nodes"),r=e("./key_map"),u=r.marker,k=r.meta,g=r.isMarkerLast,w=r.hasDefinition,N=r.isBoundToParent,x=r.replaceAttribute,T=r.retainElement,E=r.memoizedObject,o=3,C=["INPUT","TEXTAREA"];function A(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,E,{value:{},configurable:!0}),Object.defineProperty(t,k,{value:{},configurable:!0}),i=t[k],n)i[a]={keyPath:{key:a,root:o.root,target:t},activeNodes:[],previousValues:[],currentMarker:n[a][u],valueIsArray:null},l(e,t,n,a,r)}function l(l,s,d,c,r){var f=s[E],p=s[k][c],o=d[c],i=!o[w]&&o[1],a=o[w]&&o[1],h=o[g],u=p.keyPath,y=p.activeNodes,m=p.previousValues,v=p.valueIsArray;function e(e){var t,n=f[c];return(f[c]=e)===n||(a&&null!=e?A(l,e,a,r,u):i&&void 0!==(t=i(r,e,void 0===n?null:n,u))&&O(r,t,o[x])),e}function t(e){var t,n,r,o,i,a,u;for(f[c]=e,t=(v=p.valueIsArray=Array.isArray(e))?e:[e],a=0,u=Math.max(m.length,t.length);a<u;a++)o=t[a],i=m[a],r=o&&o===i?null:_(l,s,d,c,o,i,a),n=p.currentMarker,r&&(h?(n.parentNode.appendChild(r),n.parentNode.appendChild(n)):n.parentNode.insertBefore(r,L(a+1,y)||n));if(t.length!==m.length&&(m.length=y.length=t.length),v)for(t.pop=function(r,o,i){var e=r[k][i],a=e.activeNodes,u=e.previousValues;return function(){var e=this.length-1,t=u[e],n=Array.prototype.pop.call(this);return j(r,o,i,null,t,e),u.length=a.length=this.length,n}}(s,d,c),t.push=function(i,a,u,l){var s=a[k][l],d=u[l][g];return function(){var e,t,n,r=this.length,o=Array.prototype.push.apply(this,arguments);for(e=r+arguments.length;r<e;r++)n=_(i,a,u,l,this[r],null,r),t=s.currentMarker,n&&(d?(t.parentNode.appendChild(n),t.parentNode.appendChild(t)):t.parentNode.insertBefore(n,t)),P(i,a,u,l,this,r);return o}}(l,s,d,c),t.shift=function(e,t,n){var r=e[k][n],o=r.activeNodes,i=r.previousValues;return function(){return j(e,t,n,null,i[0],0),Array.prototype.shift.call(i),Array.prototype.shift.call(o),Array.prototype.shift.call(this)}}(s,d,c),t.unshift=function(n,r,o,i){return function(){var e,t;for(t=(e=this.length)+arguments.length;e<t;e++)P(n,r,o,i,this,e);return Array.prototype.unshift.apply(this,arguments)}}(l,s,d,c),t.splice=function(d,c,f,p){var h=c[k][p],y=h.activeNodes,m=h.previousValues,v=f[p][g];return function(e,t){var n,r,o,i,a,u,l,s=[];for(r=(n=e)+t;n<r;n++)j(c,f,p,null,m[n],n);for(n=2,r=arguments.length;n<r;n++)s.push(arguments[n]);for(Array.prototype.splice.apply(m,arguments),Array.prototype.splice.apply(y,[e,t].concat(Array(s.length))),i=Array.prototype.splice.apply(this,arguments),l=e-t>=this.length-1,r=(n=e)+s.length;n<r;n++)u=_(d,c,f,p,s[n-e],null,n),a=h.currentMarker,u&&(v&&l?(a.parentNode.appendChild(u),a.parentNode.appendChild(a)):a.parentNode.insertBefore(u,L(e+s.length,y)||a));if((o=s.length-t)<0)m.length=y.length=this.length;else if(0<o)for(n=this.length-o,r=this.length;n<r;n++)P(d,c,f,p,this,n);return i}}(l,s,d,c),a=0,u=t.length;a<u;a++)P(l,s,d,c,t,a);return e}o[N]?e(s[c]):t(s[c]),Object.defineProperty(s,c,{get:function(){return f[c]},set:o[N]?e:t,enumerable:!0,configurable:!0})}function P(i,a,u,l,s,d){var c=a[k][l],f=c.activeNodes,p=c.previousValues,h=u[l][g],y=s[d];Object.defineProperty(s,d,{get:function(){return y},set:function(e){var t,n,r,o;y=e,(t=s[d])!==(n=p[d])&&(o=_(i,a,u,l,t,n,d)),r=c.currentMarker,o&&(h?(r.parentNode.appendChild(o),r.parentNode.appendChild(r)):r.parentNode.insertBefore(o,L(d+1,f)||r))},enumerable:!0,configurable:!0})}function j(e,t,n,r,o,i){var a,u=e[k][n],l=u.activeNodes,s=u.previousValues,d=u.valueIsArray,c=u.keyPath,f=u.currentMarker,p=t[n],h=!p[w]&&p[1],y=p[w]&&p[1],m=p[2],v=l[i];delete s[i],v&&(delete l[i],d?c.index=i:delete c.index,h?a=h(v,null,o,c):y&&m&&(a=m(v,null,c.target=o,c)),a!==T&&f.parentNode.removeChild(v))}function _(e,t,n,r,o,i,a){var u,l=t[k][r],s=n[r],d=!s[w]&&s[1],c=s[w]&&s[1],f=s[0],p=s[2],h=l.activeNodes,y=l.previousValues,m=l.valueIsArray,v=l.keyPath,g=h[a],N=f;if(void 0===o&&(o=null),void 0===i&&(i=null),null===o)return j(t,n,r,0,i,a),null;if(m?v.index=a:delete v.index,y[a]=o,c)g&&j(t,n,r,0,i,a),N=b(e,f,c),v.target=m?o[a]:o,A(e,o,c,N,v),p&&p(N,v.target=o,null,v);else if(N=g||f.cloneNode(!0),d?void 0!==(u=d(N,o,i,v))&&O(N,u,s[x]):(null===i&&~C.indexOf(N.tagName)&&N.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]}}(s[x],v,r)),O(N,o,s[x])),g)return null;return h[a]=N}function O(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 L(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=A},{"./key_map":5,"./process_nodes":6}],2:[function(e,t,n){"use strict";t.exports=function(e,t){var n,r,o,i,a,u;for(n=0,r=t.length;n<r;n++)if("string"==typeof(u=t[n])[0])for(a=e,o=0,i=u.length;o<i;o++){if(!(u[o]in a))throw new Error("Missing "+u.slice(0,o+1).join(".")+" feature which is required.");a=a[u[o]]}else for(a=u[0],o=1,i=u.length;o<i;o++)if(1<o&&(a=a[u[o]]),void 0===a)throw new Error("Missing "+u[0].name+u.slice(1,o+1).join(".")+" feature which is required.")}},{}],3:[function(e,t,n){"use strict";var i=e("./key_map").retainElement,s="undefined"!=typeof MutationObserver,d="undefined"!=typeof document;function l(t,n){return function(e){return t(e,n)}}function c(e){e.stopPropagation(),e.preventDefault()}t.exports={bindEvents:function(i,a){var u={};void 0===a&&(a=!1);return function(e,t,n,r){var o;if(null===t)for(o in i)e.removeEventListener(o,u[o],a),e.addEventListener(o,c,!0);else if(null===n)for(o in i)u[o]=l(i[o],r),e.addEventListener(o,u[o],a)}},animate:function(l,n,r,o){return function(a,e,t){var u;if("classList"in a)if(null===e){if(l&&a.classList.remove(l),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&&l&&(s&&d&&!document.documentElement.contains(a)?(u=new MutationObserver(function(e){var t,n,r,o,i;for(t=0,n=e.length;t<n;t++)for(r=0,o=(i=e[t]).addedNodes.length;r<o;r++)i.addedNodes[r]===a&&(a.offsetWidth,a.classList.add(l),u.disconnect())})).observe(document.documentElement,{childList:!0,subtree:!0}):a.classList.add(l))}}}},{"./key_map":5}],4:[function(e,t,n){"use strict";var r,u=e("./process_nodes"),l=e("./bind_keys"),o=e("./key_map"),i=e("./helpers"),s=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 u.useCommentNode},set:function(e){u.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,u.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,u,l,s,d;var c;if("object"!=typeof n)throw new TypeError("The second position must be an object.");for(a in n){if("function"==typeof(l=n[a])||"object"==typeof l&&null!==l&&!Array.isArray(l))n[a]=l=[t,l];else if("string"==typeof l)n[a]=l=[l];else if(!Array.isArray(l))throw new TypeError('The binding on key "'+a+'" is invalid.');if("string"==typeof l[0]){if(u=l[0],!(d=t.querySelectorAll(u)).length)throw new Error('An element for selector "'+u+'" was not found.');for(o=1,i=d.length;o<i;o++)d[o].parentNode.removeChild(d[o]);l[0]=d[0]}else if(!l[0])throw new TypeError('The first position on key "'+a+'" must be a CSS selector string.');if("content"in l[0]&&(l[0]=l[0].content),s=l[0],"object"==typeof l[1]&&null!==l[1]){if(Object.defineProperty(l,f,{value:!0}),l[2]&&"function"!=typeof l[2])throw new TypeError('The third position on key "'+a+'" must be a function.')}else if(l[1]&&"function"!=typeof l[1])throw new TypeError('The second position on key "'+a+'" must be an object or a function.');if(t!==s){if(r.push([a,s]),!t.contains(s))throw new Error("The bound DOM element must be either contained in or equal to the element in its parent binding.");l[f]?e(s,l[1]):b(l,s),k(l)}else Object.defineProperty(l,p,{value:!0}),l[f]?e(s,l[1]):"function"==typeof l[1]?b(l,s):console.warn('A change function was not defined on the key "'+a+'".'),k(l)}for(a in n)for(s=n[a][0],o=0,i=r.length;o<i;o++)if(c=r[o][0],r[o][1].contains(s)&&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=u(this,t[0],t[1]),l(this,e,t[1],r,{root:e}),n?(s(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={},u="function"==typeof Symbol;for(r=0,o=i.length;r<o;r++)a[i[r]]=u?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,u,l,s,d,c,f,p,h=e?e.document:window.document;if(i=o[g]){for(n in t=i.node.cloneNode(!0),s=i.indices,c=d=0,f=h.createTreeWalker(t,b,w,!1),o)if(!(r=o[n])[y]){for(;f.nextNode();){if(d===s[c]){r[m]=f.currentNode,d++;break}d++}c++}}else{for(s=[],function(e,t,n){var r,o,i,a,u,l=e?e.document:window.document,s=l.createTreeWalker(t,b,w,!1),d=[],c=0,f=k.useCommentNode?1:0;for(i in n)d.push(n[i][0]);for(;s.nextNode()&&d.length;){for(r=0,o=d.length;r<o;r++)if(a=d[r],s.currentNode.isEqualNode(a)){for(Object.defineProperty(a,v,{value:{index:c+f,node:s.currentNode}}),k.useCommentNode&&f++,u=l.createTreeWalker(a,b,w,!1);u.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&&r?n.index-r.index:0}),d=0;d<p.length;d++)n=p[d],(r=o[n])[y]||(i=r[0][v],s.push(i.index),u=(a=i.node).parentNode,null===a.nextElementSibling&&(r[N]=!0),k.useCommentNode?(l=u.insertBefore(h.createComment(' end "'+n+'" '),a),u.insertBefore(h.createComment(' begin "'+n+'" '),l)):l=u.insertBefore(h.createTextNode(""),a),r[m]=l,u.removeChild(a));Object.defineProperty(o,g,{value:{node:t.cloneNode(!0),indices:s}})}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,P=1,j=8;t.exports=function e(t,n,r,o,i){var a=t?t.document:window.document;var u,l,s,d,c,f,p,h;var y,m,v,g,N,b;for(u in r)if(l=r[u],y=n[C][u],c=!l[x]&&l[1],f=l[x]&&l[1],p=l[2],h=y.keyPath,l[T])s=n[u],f&&null!=s?w(t,s,f,i,h):c&&c(i,s,null,h);else if((v=y.activeNodes).length){for(m=y.valueIsArray,s=m?n[u]:[n[u]],g=0,N=a.createTreeWalker(i,P,A,!1);g<v.length&&N.nextNode();)b=v[g],N.currentNode.isEqualNode(b)&&(v.splice(g,1,N.currentNode),d=s[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 "'+u+'", expected '+v.length+", found "+g+".");b=N.currentNode,k.useCommentNode&&null!==b.nextSibling&&b.nextSibling.nodeType===j?l[E]=b.nextSibling:l[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]);