moy-dom
Version:
A flexiable Virtual DOM library for building modern web interface.
2 lines (1 loc) • 8.34 kB
JavaScript
var classCallCheck=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},createClass=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),slicedToArray=function(){return function(e,t){if(Array.isArray(e))return e;if(Symbol.iterator in Object(e))return function(e,t){var r=[],n=!0,o=!1,a=void 0;try{for(var l,i=e[Symbol.iterator]();!(n=(l=i.next()).done)&&(r.push(l.value),!t||r.length!==t);n=!0);}catch(e){o=!0,a=e}finally{try{!n&&i.return&&i.return()}finally{if(o)throw a}}return r}(e,t);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}();function makeKeyIndexAndFree(e){var t=new Map,r=[],n=void 0,o=!0,a=!1,l=void 0;try{for(var i,d=e.entries()[Symbol.iterator]();!(o=(i=d.next()).done);o=!0){var c=i.value,u=slicedToArray(c,2),p=u[0],s=u[1];void 0!==(n=s.key)?t.set(n,p):r.push(s)}}catch(e){a=!0,l=e}finally{try{!o&&d.return&&d.return()}finally{if(a)throw l}}return{keyIndex:t,free:r}}function remove(e,t,r){t.splice(e,1),r.push({type:0,index:e})}function move(e,t,r,n){var o=[r[t],r[e]];r[e]=o[0],r[t]=o[1],n.push({type:1,toIndex:e,fromIndex:t})}function insert(e,t,r,n){r.splice(e,0,t),n.push({type:2,index:e,item:t})}function append(e,t,r){t.push(e),r.push({type:3,item:e})}function listDiff(e,t){var r=makeKeyIndexAndFree(e),n=r.keyIndex,o=r.free,a=[],l=[],i=o.length,d=0,c=void 0,u=void 0,p=!0,s=!1,f=void 0;try{for(var y,v=t[Symbol.iterator]();!(p=(y=v.next()).done);p=!0){void 0!==(u=y.value.key)?(c=n.get(u),a.push(void 0!==c?e[c]:null)):a.push(d<i?o[d++]:null)}}catch(e){s=!0,f=e}finally{try{!p&&v.return&&v.return()}finally{if(s)throw f}}for(var h=[].concat(a),m=0,b=h.length;m<b;)null===h[m]?(remove(m,h,l),b--):m++;m=0;for(var g=0,x=e.length,k=void 0,N=void 0,j=void 0,w=void 0;m<b&&g<x;)N=h[m].key,(w=(j=e[g]).key)===N?(m++,g++):(k||(k=makeKeyIndexAndFree(h).keyIndex),(c=k.get(w))?(move(g++,c,h,l),m++):(insert(g,j,h,l),b++,m++,g++));for(;g<x;)append(e[g++],h,l);return{children:a,changes:l}}function diffProps(e,t){var r={},n=!0,o=!1,a=void 0;try{for(var l,i=Object.entries(t)[Symbol.iterator]();!(n=(l=i.next()).done);n=!0){var d=l.value,c=slicedToArray(d,2),u=c[0],p=c[1];e[u]!==p&&(r[u]=e[u])}}catch(e){o=!0,a=e}finally{try{!n&&i.return&&i.return()}finally{if(o)throw a}}var s=!0,f=!1,y=void 0;try{for(var v,h=Object.entries(e)[Symbol.iterator]();!(s=(v=h.next()).done);s=!0){var m=v.value,b=slicedToArray(m,2),g=b[0],x=b[1];Reflect.has(t,g)||(r[g]=x)}}catch(e){f=!0,y=e}finally{try{!s&&h.return&&h.return()}finally{if(f)throw y}}for(var k in r)return r;return null}var diffChildren=function(e,t,r,n,o){var a=listDiff(e,t),l=a.children,i=a.changes;i.length&&o.push({type:1,changes:i}),e=l;var d=null,c=r,u=!0,p=!1,s=void 0;try{for(var f,y=t.entries()[Symbol.iterator]();!(u=(f=y.next()).done);u=!0){var v=f.value,h=slicedToArray(v,2),m=h[0],b=h[1];dfsWalk(e[m],b,c+=1+(d&&d.count?d.count:0),n),d=b}}catch(e){p=!0,s=e}finally{try{!u&&y.return&&y.return()}finally{if(p)throw s}}};function dfsWalk(e,t,r,n){var o=[];if(null===e);else if("[object String]"===Object.prototype.toString.call(e)&&"[object String]"===Object.prototype.toString.call(t))e!==t&&o.push({type:3,content:e});else if(e.tagName===t.tagName&&e.key===t.key){var a=diffProps(e.props,t.props);a&&o.push({type:2,props:a}),e.props.ignore||diffChildren(e.children,t.children,r,n,o)}else o.push({type:0,node:e});o.length&&(n[r]=o)}function diff(e,t){var r={};return null===t?(null!==e&&(r.rootWithNull={type:4,vnode:e}),r):null===e?(r.rootWithNull={type:5},r):("[object Element]"!==Object.prototype.toString.call(e)&&(e+=""),"[object Element]"!==Object.prototype.toString.call(t)&&(t+=""),dfsWalk(e,t,0,r),r)}var domAttrs={selected:!0,checked:!0,muted:!0,value:!0},boolAttrs={allowfullscreen:!0,async:!0,autofocus:!0,autoplay:!0,compact:!0,controls:!0,declare:!0,default:!0,defaultchecked:!0,defaultmuted:!0,defaultselected:!0,defer:!0,disabled:!0,enabled:!0,formnovalidate:!0,hidden:!0,indeterminate:!0,inert:!0,ismap:!0,itemscope:!0,loop:!0,multiple:!0,nohref:!0,noresize:!0,noshade:!0,novalidate:!0,nowrap:!0,open:!0,pauseonexit:!0,readonly:!0,required:!0,reversed:!0,scoped:!0,seamless:!0,sortable:!0,translate:!0,truespeed:!0,typemustmatch:!0,visible:!0};function setProps(e,t){var r=!0,n=!1,o=void 0;try{for(var a,l=Object.entries(t)[Symbol.iterator]();!(r=(a=l.next()).done);r=!0){var i=a.value,d=slicedToArray(i,2),c=d[0],u=d[1];domAttrs[c]||/^on\w+/.test(c)?e[c]=void 0===u?null:u:boolAttrs[c]?u?e.setAttribute(c,c):e.removeAttribute(c):void 0===u?e.removeAttribute(c):e.setAttribute(c,u)}}catch(e){n=!0,o=e}finally{try{!r&&l.return&&l.return()}finally{if(n)throw o}}}function renderChildren(e,t){var r=!0,n=!1,o=void 0;try{for(var a,l=t[Symbol.iterator]();!(r=(a=l.next()).done);r=!0){var i=a.value,d=i.index,c=i.type,u=i.item,p=i.toIndex,s=i.fromIndex;if(0===c)e.removeChild(e.childNodes[d]);else if(1===c)e.insertBefore(e.childNodes[p],e.childNodes[s]),e.insertBefore(e.childNodes[s],e.childNodes[p]);else if(2===c){var f="[object Element]"===Object.prototype.toString.call(u)?u.render():document.createTextNode(u);e.insertBefore(f,e.childNodes[d])}else{if(3!==c)throw new Error("Unknown reorder type "+c);var y="[object Element]"===Object.prototype.toString.call(u)?u.render():document.createTextNode(u);e.appendChild(y)}}}catch(e){n=!0,o=e}finally{try{!r&&l.return&&l.return()}finally{if(n)throw o}}}function applyPatches(e,t){var r=!0,n=!1,o=void 0;try{for(var a,l=t[Symbol.iterator]();!(r=(a=l.next()).done);r=!0){var i=a.value;switch(i.type){case 0:var d="[object Element]"===Object.prototype.toString.call(i.node)?i.node.render():document.createTextNode(i.node);e.parentNode.replaceChild(d,e);break;case 1:renderChildren(e,i.changes);break;case 2:setProps(e,i.props);break;case 3:e.textContent=i.content;break;default:throw new Error("Unknown patch type "+i.type)}}}catch(e){n=!0,o=e}finally{try{!r&&l.return&&l.return()}finally{if(n)throw o}}}function dfsWalk$1(e,t,r){for(var n=r[t.index],o=e.childNodes.length,a=0;a<o;a++){var l=e.childNodes[a];t.index++,dfsWalk$1(l,t,r)}n&&applyPatches(e,n)}function patch(e,t){var r=t.rootWithNull;if(r){var n=r.type,o=r.vnode;if(4===n){var a="[object Element]"===Object.prototype.toString.call(o)?o.render():document.createTextNode(o);e.appendChild(a)}5===n&&e.removeChild(e.lastChild)}else{dfsWalk$1(e.lastChild,{index:0},t)}}var Element=function(){function e(t,r,n){classCallCheck(this,e),this.tagName=t,this.props=r,this.key=r.key;for(var o=0,a=[],l=n.length,i=void 0,d=0;d<l;d++)null!==(i=n[d])&&("[object Element]"===Object.prototype.toString.call(i)?(o+=i.count+1,a.push(i)):(o++,a.push(""+i)));this.children=a,this.count=o}return createClass(e,[{key:"render",value:function(){var e=document.createElement(this.tagName),t=!0,r=!1,n=void 0;try{for(var o,a=Object.entries(this.props)[Symbol.iterator]();!(t=(o=a.next()).done);t=!0){var l=o.value,i=slicedToArray(l,2),d=i[0],c=i[1];domAttrs[d]||/^on\w+/.test(d)?e[d]=void 0===c?null:c:boolAttrs[d]?c&&e.setAttribute(d,d):void 0!==c&&e.setAttribute(d,c)}}catch(e){r=!0,n=e}finally{try{!t&&a.return&&a.return()}finally{if(r)throw n}}var u=!0,p=!1,s=void 0;try{for(var f,y=this.children[Symbol.iterator]();!(u=(f=y.next()).done);u=!0){var v=f.value;e.appendChild("[object Element]"===Object.prototype.toString.call(v)?v.render():document.createTextNode(v))}}catch(e){p=!0,s=e}finally{try{!u&&y.return&&y.return()}finally{if(p)throw s}}return e}},{key:Symbol.toStringTag,get:function(){return"Element"}}],[{key:"of",value:function(t){for(var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=arguments.length,o=Array(n>2?n-2:0),a=2;a<n;a++)o[a-2]=arguments[a];return"[object Object]"!==Object.prototype.toString.call(r)&&(o.unshift(r),r={}),new e(t,r,o)}}]),e}(),appRoot=void 0,appGetNode=void 0,appCurrentNode=void 0,render=function(e,t){appCurrentNode=t(),appRoot=e,appGetNode=t,null!==appCurrentNode&&appRoot.appendChild("[object Element]"===Object.prototype.toString.call(appCurrentNode)?appCurrentNode.render():document.createTextNode(appCurrentNode))},reRender=function(){var e=appGetNode();patch(appRoot,diff(e,appCurrentNode)),appCurrentNode=e};export{Element,render,reRender};