maokuvue
Version:
cdvue mvue mvvm framework
1 lines • 8.81 kB
JavaScript
;function _toConsumableArray(t){return _arrayWithoutHoles(t)||_iterableToArray(t)||_unsupportedIterableToArray(t)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(t,e){if(t){if("string"==typeof t)return _arrayLikeToArray(t,e);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?_arrayLikeToArray(t,e):void 0}}function _iterableToArray(t){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(t))return Array.from(t)}function _arrayWithoutHoles(t){if(Array.isArray(t))return _arrayLikeToArray(t)}function _arrayLikeToArray(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r<e;r++)n[r]=t[r];return n}function _typeof(t){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function def(t,e,r,n){Object.defineProperty(t,e,{value:r,enumerable:!!n,configurable:!0,writable:!0})}function Mvue(t){this.$options=t||{};var e=this._data=this.$options.data,r=this;Object.keys(e).forEach(function(t){r._proxyData(t)}),observe(e,this),new Compile(t.el||document.body,this)}function set(t,e,r){if(Array.isArray(t)&&"number"==typeof e)return t.length=Math.max(t.length,e),t.splice(e,1,r),r;if(hasOwn(t,e))return t[e]=r;var n=t.__ob__;return n?(defineReactive$$1(n.value,e,r),n.dep.notify(),r):t[e]=r}function del(t,e){var r;Array.isArray(t)&&"number"==typeof e?t.splice(e,1):(r=t.__ob__,hasOwn(t,e)&&(delete t[e],r&&r.dep.notify()))}Mvue.prototype={_proxyData:function(e,t){var r=this;t=t||Object.defineProperty(r,e,{configurable:!1,enumerable:!0,get:function(){return r._data[e]},set:function(t){r._data[e]=t}})},$set:set,$delete:del};var arrayProto=Array.prototype,arrayMethods=Object.create(arrayProto);["push","pop","shift","unshift","splice","sort","reverse"].forEach(function(a){var s=arrayMethods[a];def(arrayMethods,a,function(){for(var t=arguments,e=arguments.length,r=new Array(e);e--;)r[e]=t[e];var n,i=s.apply(this,r),o=this.__ob__;switch(a){case"push":case"unshift":n=r;break;case"splice":n=r.slice(2)}return n&&o.observeArray(n),o.dep.notify(),i})});var arrayKeys=Object.getOwnPropertyNames(arrayMethods),hasProto="__proto__"in{},hasOwnProperty=Object.prototype.hasOwnProperty;function protoAugment(t,e){t.__proto__=e}function copyAugment(t,e,r){for(var n=0,i=r.length;n<i;n++){var o=r[n];def(t,o,e[o])}}function hasOwn(t,e){return hasOwnProperty.call(t,e)}function Observer(t){this.value=t,this.dep=new Dep,def(t,"__ob__",this),Array.isArray(t)?((hasProto?protoAugment:copyAugment)(t,arrayMethods,arrayKeys),this.observeArray(t)):this.walk(t)}function defineReactive$$1(t,e,r){var n=new Dep,i=observe(r);Object.defineProperty(t,e,{enumerable:!0,configurable:!0,get:function(){return Dep.target&&(n.depend(),i&&i.dep.depend()),r},set:function(t){r===t||t!=t&&r!=r||(i=observe(r=t),n.notify())}})}function observe(t){if(t&&"object"===_typeof(t)){var e=hasOwn(t,"__ob__")&&t.__ob__ instanceof Observer?t.__ob__:new Observer(t);return e}}Observer.prototype={walk:function(e){Object.keys(e).forEach(function(t){defineReactive$$1(e,t,e[t])})},observeArray:function(t){for(var e=0,r=t.length;e<r;e++)observe(t[e])}};var $elm,uid=0;function Dep(){this.id=uid++,this.subs=[]}function Compile(t,e){this.$vm=e,this.$el=this.isElementNode(t)?t:document.querySelector(t),this.$el&&(this.$fragment=this.nodeFragment(this.$el),this.compileElement(this.$fragment),this.$el.appendChild(this.$fragment))}Dep.target=null,Dep.prototype={addSub:function(t){this.subs.push(t)},removeSub:function(t){var e=this.subs.indexOf(t);-1!==e&&this.subs.splice(e,1)},notify:function(){console.log("notify"),this.subs.forEach(function(t){t.update()})},depend:function(){Dep.target.addDep(this)}},Compile.prototype={compileElement:function(t){var i=this,e=t.childNodes,o=/(\{\{\s*\S+?\s*\}\})/,a=/\{\{\s*(\S+?)\s*}\}/;[].slice.call(e).forEach(function(t){var e,r,n=t.textContent;i.isElementNode(t)?i.compile(t):i.isTextNode(t)&&o.test(n)&&(e=n.split(o),r=t.parentNode,e.forEach(function(t){var e;a.test(t)?(e=document.createTextNode(""),i.compileText(e,RegExp.$1)):e=document.createTextNode(t),r.appendChild(e)}),t.textContent=""),t.childNodes&&t.childNodes.length&&i.compileElement(t)})},nodeFragment:function(t){for(var e,r=document.createDocumentFragment();e=t.firstChild;)r.appendChild(e);return r},compile:function(s){var t=s.attributes,c=this;[].slice.call(t).forEach(function(t){var e,r,n,i,o,a=t.name;c.isDirective(a)?(e=t.value,r=a.substring(2),c.isEventDirective(r)?compileUtil.eventHandler(s,c.$vm,e,r):c.isAttrDirective(r)?(n=r.split(":")[1],compileUtil.attr(s,c.$vm,e,n)):compileUtil[r]&&compileUtil[r](s,c.$vm,e),s.removeAttribute(a)):c.isSimpleDirective(a)&&(i=t.value,o=a.substring(1),c.isEventDirective(a)?compileUtil.eventHandler(s,c.$vm,i,o):c.isAttrDirective(a)&&(compileUtil[o]||compileUtil.attr).call(compileUtil,s,c.$vm,i,o),s.removeAttribute(a))})},compileText:function(t,e){compileUtil.text(t,this.$vm,e)},isElementNode:function(t){return 1===t.nodeType},isTextNode:function(t){return 3===t.nodeType},isDirective:function(t){return 0===t.indexOf("v-")},isAttrDirective:function(t){return 0===t.indexOf("bind")||0===t.indexOf(":")},isEventDirective:function(t){return 0===t.indexOf("on")||0===t.indexOf("@")},isSimpleDirective:function(t){return 0===t.indexOf(":")||0===t.indexOf("@")}};var timer=null,compileUtil={html:function(t,e,r){this.bind(t,e,r,"html")},text:function(t,e,r){this.bind(t,e,r,"text")},class:function(t,e,r){this.bind(t,e,r,"class")},style:function(r,n,t){var i=this,e=t.trim();e&&/[\{:\}]/.test(e)&&e.replace(/[\{\}]/g,"").split(",").map(function(t){var e=t.split(":");return i.bindStyle(r,n,e[1],e[0]),e.join(":")})},attr:function(t,e,r,n){this.bind(t,e,r,n)},model:function(t,r,n){this.bind(t,r,n,"model");var i=this,o=this._getVmVal(r,n);t.addEventListener("input",function(t){var e=t.target.value;$elm=t.target,o!==e&&(clearTimeout(timer),timer=setTimeout(function(){i._setVmVal(r,n,e),o=e},20))})},bind:function(r,t,e,n){var i=(i=updater[n+"Updater"])||updater.attrUpdater,o=this._getVmVal(t,e);i(r,o,null,n),o!==e&&new Watcher(t,e,function(t,e){i&&i(r,t,e,n)})},bindStyle:function(r,t,e,n){var i=updater.styleUpdater,o=this._getVmVal(t,e);i(r,o,null,n),o!==e&&new Watcher(t,e,function(t,e){i&&i(r,t,e,n)})},eventHandler:function(t,r,e,n){var i=/\s*\(.*\)\s*/.exec(e),o=this,i=i?(e=e.replace(i,""),i[0].replace(/[\(\)\'\"]/g,"").split(",")):[],a=-1<n.indexOf(":")?n.split(":")[1]:n,s=(s=r.$options.methods&&r.$options.methods[e])&&s.bind(r);a&&t.addEventListener(a.trim(),function(t){var e=[];i.length&&(e=i.map(function(t){return o._getVmVal(r,t)})),s.apply(void 0,[t].concat(_toConsumableArray(e)))},!1)},_getVmVal:function(t,e){for(var r,n=t,i=e.split(".");0<i.length;){if(!hasOwn(n,r=i.shift().trim()))return e.trim();n=n[r]}return n},_setVmVal:function(t,e,r){var n=t,i=e.split(".");i.forEach(function(t,e){t=t.trim(),e<i.length-1?n=n[t]:n[t]=r})}},updater={htmlUpdater:function(t,e){t.innerHTML=void 0===e?"":e},textUpdater:function(t,e){t.textContent=void 0===e?"":e},classUpdater:function(e,t){var r;e.classList?-1<t.indexOf(" ")?t.split(/\s+/).forEach(function(t){return e.classList.add(t)}):e.classList.add(t):(r=" ".concat(e.getAttribute("class")||""," ")).indexOf(" "+t+" ")<0&&e.setAttribute("class",(r+t).trim())},styleUpdater:function(t,e,r,n){var i=t.getAttribute("style")||"",o="".concat(n,":").concat(e),a=i.split(";");(a=a.filter(function(t){var e=new RegExp("^"+n+":");return t&&!e.test(t)})).push(o),t.setAttribute("style",a.join(";"))},modelUpdater:function(t,e){if($elm===t)return!1;$elm=void 0,t.value=void 0===e?"":e},attrUpdater:function(t,e,r,n){t.setAttribute(n,e)}};function Watcher(t,e,r){this.vm=t,e=e.trim(),this.expOrFn=e,this.cb=r,this.depIds={},this.getter="function"==typeof e?e:this.parseGetter(e),this.value=this.get()}Watcher.prototype={update:function(){this.run()},run:function(){var t=this.get(),e=this.value;t!==e&&(this.value=t,this.cb.call(this.vm,t,e))},get:function(){var t=(Dep.target=this).getter.call(this.vm,this.vm);return Dep.target=null,t},addDep:function(t){this.depIds.hasOwnProperty(t.id)||(t.addSub(this),this.depIds[t.id]=t)},parseGetter:function(t){if(!/[^\w.$]/.test(t)){var n=t.split(".");return function(t){for(var e=0,r=n.length;e<r;e++){if(!t)return;t=t[n[e]]}return t}}}},String.prototype.trim||(String.prototype.trim=function(){return this.replace(/(^\s+)|(\s+$)/g,"")});