1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
| const compileUtil = { getVal(expr, vm) { if (expr.indexOf('{') === -1) { return expr.split('.').reduce((data, currentVal) => { return data[currentVal] }, vm.$data) } else return eval(`(${expr})`) }, setVal(expr, vm, inputVal) { return expr.split('.').reduce((data, currentVal) => { if (typeof data[currentVal] !== 'object') { data[currentVal] = inputVal } return data[currentVal] }, vm.$data) }, getContentVal(expr, vm) { return expr.replace(/\{\{(.+?)\}\}/g, (...args) => { return this.getVal(args[1], vm) }) }, text(node, expr, vm) { let value if (expr.indexOf('{{') !== -1) { value = expr.replace(/\{\{(.+?)\}\}/g, (...args) => { new Watcher(vm, args[1], newVal => { this.updater.textUpdater(node, this.getContentVal(expr, vm)) }) return this.getVal(args[1], vm) }) } else value = this.getVal(expr, vm) this.updater.textUpdater(node, value) }, html(node, expr, vm) { const value = this.getVal(expr, vm) new Watcher(vm, expr, newVal => { this.updater.htmlUpdater(node, newVal) }) this.updater.htmlUpdater(node, value) }, for(node, expr, vm) {}, model(node, expr, vm) { const value = this.getVal(expr, vm) new Watcher(vm, expr, newVal => { this.updater.modelUpdater(node, newVal) }) node.addEventListener('input', e => { this.setVal(expr, vm, e.target.value) }) this.updater.modelUpdater(node, value) }, on(node, expr, vm, eventName) { const fn = vm.$options.methods?.[expr] node.addEventListener(eventName, fn.bind(vm), false) }, bind(node, expr, vm, attrName) { const value = this.getVal(expr, vm) new Watcher(vm, expr, newVal => { this.updater.bindUpdater(node, newVal, attrName) }) this.updater.bindUpdater(node, value, attrName) }, updater: { textUpdater: (node, value) => (node.textContent = value), htmlUpdater: (node, value) => (node.innerHTML = value), modelUpdater: (node, value) => (node.value = value), bindUpdater: (node, value, attrName) => { if (value instanceof Object) { let objStr = '' Object.keys(value).forEach(key => { objStr += `${key}:${value[key]};` node.setAttribute(attrName, objStr) }) } else node.setAttribute(attrName, value) } } } class Compile { constructor(el, vm) { this.el = this.isElementNode(el) ? el : document.querySelector(el) this.vm = vm const fragment = this.node2Fragment(this.el) this.compile(fragment) this.el.appendChild(fragment) }
compile(fragment) { const childNodes = fragment.childNodes ;[...childNodes].forEach(child => { if (this.isElementNode(child)) { this.compileElement(child) } else { this.compileText(child) } if (child.childNodes && child.childNodes.length) this.compile(child) }) } isDirective(attrName) { return attrName.startsWith('v-') } isEventName(attrName) { return attrName.startsWith('@') } compileElement(node) { const { attributes } = node ;[...attributes].forEach(attr => { const { name, value } = attr if (this.isDirective(name)) { const [, dirctive] = name.split('-') const [dirName, eventName] = dirctive.split(':') compileUtil[dirName](node, value, this.vm, eventName) node.removeAttribute('v-' + dirctive) } else if (this.isEventName(name)) { const [, evenName] = name.split('@') compileUtil['on'](node, value, this.vm, evenName) node.removeAttribute('@' + evenName) } }) } compileText(node) { const content = node.textContent if (/\{\{(.+?)\}\}/.test(content)) { compileUtil['text'](node, content, this.vm) } } node2Fragment(el) { const f = document.createDocumentFragment() let firstChild while ((firstChild = el.firstChild)) { f.appendChild(firstChild) } return f } isElementNode(node) { return node.nodeType === 1 } } class Mvue { constructor(options) { const { el, data } = options this.$el = el this.$data = data this.$options = options if (this.$el) { new Observer(this.$data) new Compile(this.$el, this) this.proxyData(this.$data) } } proxyData(data) { for (const key in data) { Object.defineProperty(this, key, { get() { return data[key] }, set(newVal) { data[key] = newVal } }) } } }
|