-
Notifications
You must be signed in to change notification settings - Fork 5
/
foio.js
299 lines (273 loc) · 9.51 KB
/
foio.js
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
(function() {
/********************全局数据结构begin****************************/
var foio = {};
//用于
var rmsAttr = /foio-(\w+)-?(.*)/;
var openTag = '{{';
var closeTag = '}}';
foio.vmodels = [];
//每个指令相关的操作
var directives = {
//model指令相关操作
model: function(binding) {
var elem = binding.element;
var closetVmodel = getClosetVmodel(binding);
if (!binding.xtype) {
binding.xtype = elem.tagName === "SELECT" ? "select" :
elem.type === "checkbox" ? "checkbox" :
elem.type === "radio" ? "radio" :
/^change/.test(elem.getAttribute("data-duplex-event")) ? "change" :
"input";
}
binding.bound = function(type, callback) {
if (elem.addEventListener) {
elem.addEventListener(type, callback, false);
} else {
elem.attachEvent("on" + type, callback);
}
};
var updateVModel = function(e) {
var val = elem.value;
//搜索vmodels
closetVmodel[binding.expr] = val;
};
switch (binding.xtype) {
case "input":
binding.bound('input', updateVModel);
binding.bound('DOMAutoComplete', updateVModel);
elem.value = closetVmodel.binder[binding.expr].apply(binding);
binding.updateView = function(newVal) {
elem.value = newVal;
};
break;
}
},
//text指令相关操作
text: function(binding) {
binding.updateView = function(newVal) {
binding.element.nodeValue = newVal;
};
//初始化绑定
var closetVmodel = getClosetVmodel(binding);
binding.element.nodeValue = closetVmodel.binder[binding.expr].apply(binding);
},
};
/********************全局数据结构end****************************/
function getClosetVmodel(binding) {
var vmodels = binding.vmodels;
//搜索vmodels
for (var i = binding.vmodels.length - 1; i >= 0; i--) {
var vmodel = binding.vmodels[i];
if (vmodel.hasOwnProperty(binding.expr)) {
return vmodel;
}
}
}
/********************节点扫描相关函数begin****************************/
foio.scan = function(elem, vmodel) {
var elem = (elem || window.document.body);
var vmodels = vmodel ? [].concat(vmodel) : [];
scanTag(elem, vmodels);
};
function scanTag(elem, vmodels) {
//首先扫描节点上的foio-controller属性
var cnode = elem.getAttributeNode('foio-controller');
//当节点有controller标记时
if (cnode) {
//防止后续属性扫描时被重复处理
var newVmodel = foio.vmodels[cnode.value];
//controller未定义
if (!newVmodel) {
return;
}
//形成controller的作用域链接,方便像父scope搜索变量
vmodels = [newVmodel].concat(vmodels);
elem.removeAttribute(cnode.name);
}
//扫描其他属性
scanAttr(elem, vmodels);
}
function scanText(textNode, vmodels) {
var bindings = [];
var tokens = scanExpr(textNode.data);
var docFragment = document.createDocumentFragment();
if (tokens.length) {
for (var i = 0, token; (token = tokens[i++]);) {
var node = document.createTextNode(token.value);
if (token.expr) {
token.expr = token.value;
token.type = 'text';
token.element = node;
bindings.push(token);
}
docFragment.appendChild(node);
}
}
textNode.parentNode.replaceChild(docFragment, textNode);
if (bindings.length) {
executeBindings(bindings, vmodels);
}
}
function scanExpr(str) {
var tokens = [],
value, start = 0,
stop;
do {
stop = str.indexOf(openTag, start);
if (stop === -1) {
break;
}
value = str.slice(start, stop);
if (value) {
tokens.push({
value: value,
expr: false
});
}
start = stop + openTag.length;
stop = str.indexOf(closeTag, start);
if (stop === -1) {
break;
}
value = str.slice(start, stop);
if (value) {
tokens.push({
value: value,
expr: true
});
}
start = stop + closeTag.length;
} while (1);
value = str.slice(start);
if (value) {
tokens.push({
value: value,
expr: false
});
}
return tokens;
}
function scanAttr(elem, vmodels) {
var bindings = [];
var match = false;
/*
当vmodels.length为0时,表示还没有遍历到foio-controller节点,
因此不需要扫描本节点的其他属性
*/
if (vmodels.length) {
var attributes = elem.getAttributes ? elem.getAttributes(elem) : elem.attributes;
//遍历节点的属性
for (var i = 0, attr; (attr = attributes[i++]);) {
//如果已指定属性specified为true
if (attr.specified) {
//获取指令,其中rmsAttr = /foio-(\w+)-?(.*)/
if ((match = attr.name.match(rmsAttr))) {
var type = match[1];
var param = match[2] || "";
var value = attr.value;
var name = attr.name;
//存在相关指令
if (directives[type]) {
var binding = {
type: type,
param: param,
element: elem,
name: name,
expr: value,
};
bindings.push(binding);
}
}
}
}
//处理绑定
if (bindings.length) {
executeBindings(bindings, vmodels);
}
}
scanChildNodes(elem, vmodels);
}
function scanChildNodes(parent, vmodels) {
var nodes = Array.prototype.slice.call(parent.childNodes);
for (var i = 0, node; (node = nodes[i++]);) {
switch (node.nodeType) {
case 1:
scanTag(node, vmodels);
break;
case 3:
scanText(node, vmodels);
break;
}
}
}
function executeBindings(bindings, vmodels) {
for (var i = 0, binding; (binding = bindings[i++]);) {
binding.vmodels = vmodels;
directives[binding.type](binding);
};
}
/********************节点扫描相关函数end****************************/
foio.controller = function(options) {
var $id = options.$id;
//id不能为空
if (!$id) {
throw Error('controller必须指定id');
}
//$id不能重复
if (foio.vmodels[$id]) {
throw Error('重复定义controller:' + $id);
}
var vmodel = modelFactory(options);
return foio.vmodels[$id] = vmodel;
};
function modelFactory(options) {
//要返回的对象
var $vmodel = {};
for (var name in options) {
var value = options[name];
//通过vmodle的binder,处理依赖注册
if (!$vmodel['binder']) {
$vmodel['binder'] = {};
}
//忽略特殊model
if (name.charAt(0) !== "$") {
var accessor = makeAccessor(name, value);
$vmodel = Object.defineProperty($vmodel, name, accessor);
$vmodel['binder'][name] = accessor.get;
}
}
return $vmodel;
}
function makeAccessor(name, value) {
//依赖数组
var dependencyList = [];
var oldVal = value;
return {
get: function() {
if (this.element && !this.$active) {
dependencyList.push(this);
this.$active = true;
}
return value;
},
set: function(newVal) {
if (oldVal === newVal) {
return;
}
oldVal = newVal;
for (var dependIdx in dependencyList) {
if (dependencyList[dependIdx].$active) {
dependencyList[dependIdx].updateView(newVal);
}
}
},
enumerable: true,
configurable: true
};
}
window.foio = foio;
})();
//DOM加载完成后开始扫描DOM
document.addEventListener("DOMContentLoaded", function(event) {
foio.scan(window.document.body);
});