compile解析模板指令,将模板中的变量替换成数据,然后初始化渲染页面视图,并将每个指令对应的节点绑定更新函数,添加监听数据的订阅者,一旦数据有变动,收到通知,更新视图
代码如下
function Compile(el, vm) {
this.$vm = vm; //this Compile的实例 $vm 是MVVM的实例 (vm)
// el == "#app" 判断当前用户传递的el属性是元素节点还是选择器,如果是元素节点则直接保存到$el中通,
//如果不是 则根据选择器 去查找对应的元素 然后保存
this.$el = this.isElementNode(el) ? el : document.querySelector(el);
//确定元素是否真正存在
if (this.$el) {//#app
this.$fragment = this.node2Fragment(this.$el);
this.init();
this.$el.appendChild(this.$fragment);
}
}
Compile.prototype = {
/**
* node to fragment 把节点转换成文档碎片
* @param el
* @returns {DocumentFragment}
*/
node2Fragment: function(el) {
var fragment = document.createDocumentFragment(),//文档碎片
child;
// 将原生节点拷贝到fragment
while (child = el.firstChild) {
fragment.appendChild(child);
}
return fragment;
},
/**
* 初始化
*/
init: function() {
this.compileElement(this.$fragment);
},
/**
* 解析html元素
* @param el 元素
*/
compileElement: function(el) {
var childNodes = el.childNodes,
me = this;
[].slice.call(childNodes).forEach(function(node) {
var text = node.textContent;
var reg = /\{\{(.*)\}\}/; //{{name+{{age}}+phone}} //()非贪婪匹配 ->name+{{age}}+phone
// var reg = /(.*)/; //{{name}}
//判断当前节点是不是元素节点
if (me.isElementNode(node)) {
//解析指令
me.compile(node);
} else if (me.isTextNode(node) && reg.test(text)) {
//判断当前元素是否为文本节点 并且 文本节点中是否拥有{{xxx}}
me.compileText(node, RegExp.$1);
}
//如果当前节点还有子节点 那么就需要递归查找所有的子节点是否符合以上条件
if (node.childNodes && node.childNodes.length) {
me.compileElement(node);
}
});
},
compile: function(node) {
//获取属性节点
var nodeAttrs = node.attributes,
me = this;
[].slice.call(nodeAttrs).forEach(function(attr) {
var attrName = attr.name;
if (me.isDirective(attrName)) {
var exp = attr.value;
var dir = attrName.substring(2);
// 事件指令
if (me.isEventDirective(dir)) {
compileUtil.eventHandler(node, me.$vm, exp, dir);
// 普通指令
} else {
compileUtil[dir] && compileUtil[dir](node, me.$vm, exp);
}
node.removeAttribute(attrName);
}
});
},
compileText: function(node, exp) {
compileUtil.text(node, this.$vm, exp);
},
isDirective: function(attr) {
return attr.indexOf('v-') == 0;
},
isEventDirective: function(dir) {
return dir.indexOf('on') === 0;
},
/**
* 判断当前的node是不是元节点节点
* @param node 节点
* @returns {boolean}
*/
isElementNode: function(node) {
// node = "#app"
//node.nodeType 1 element元素
return node.nodeType == 1;
},
/**
* 判断当前的node是不是文本节点
* @param node 节点
* @returns {boolean}
*/
isTextNode: function(node) {
return node.nodeType == 3;
}
};
// 指令处理集合
var compileUtil = {
text: function(node, vm, exp) {
this.bind(node, vm, exp, 'text');
},
html: function(node, vm, exp) {
this.bind(node, vm, exp, 'html');
},
model: function(node, vm, exp) {
this.bind(node, vm, exp, 'model');
var me = this,
val = this._getVMVal(vm, exp);
node.addEventListener('input', function(e) {
var newValue = e.target.value;
if (val === newValue) {
return;
}
me._setVMVal(vm, exp, newValue);
val = newValue;
});
},
class: function(node, vm, exp) {
this.bind(node, vm, exp, 'class');
},
bind: function(node, vm, exp, dir) {
var updaterFn = updater[dir + 'Updater'];
// updaterFn && updaterFn(node, this._getVMVal(vm, exp));
if(updaterFn){
//node 当前的文本节点, 值 name
//updaterFn ==> node #text {{name}} _data.name
// updaterFn(node, this._data.name);
updaterFn(node, this._getVMVal(vm, exp));
}
new Watcher(vm, exp, function(value, oldValue) {
updaterFn && updaterFn(node, value, oldValue);
});
},
// 事件处理
eventHandler: function(node, vm, exp, dir) {
var eventType = dir.split(':')[1],
fn = vm.$options.methods && vm.$options.methods[exp];
if (eventType && fn) {
node.addEventListener(eventType, fn.bind(vm), false);
}
},
_getVMVal: function(vm, exp) {
//vm => $vm exp==>"name" "age.a1"
var val = vm._data;
// {
// name: "aa",
// age: {
// a1: 18
// }
// }
exp = exp.split('.'); //[age, a1]
exp.forEach(function(k) {//age
//val = {
// a1: 18
// }
val = val[k];
});
return val;
},
_setVMVal: function(vm, exp, value) {
var val = vm._data;
exp = exp.split('.');
exp.forEach(function(k, i) {
// 非最后一个key,更新val的值
if (i < exp.length - 1) {
val = val[k];
} else {
val[k] = value;
}
});
}
};
// a-> b ->c ->d 函数嵌套调用
// a-> a -> a -> 递归 ->特殊的函数嵌套
//更新器
var updater = {
textUpdater: function(node, value) {
node.textContent = typeof value == 'undefined' ? '' : value;
},
htmlUpdater: function(node, value) {
node.innerHTML = typeof value == 'undefined' ? '' : value;
},
classUpdater: function(node, value, oldValue) {
var className = node.className;
className = className.replace(oldValue, '').replace(/\s$/, '');
var space = className && String(value) ? ' ' : '';
node.className = className + space + value;
},
modelUpdater: function(node, value, oldValue) {
node.value = typeof value == 'undefined' ? '' : value;
}
};