forked from RubyLouvre/mass-Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
avalon.router.js
369 lines (363 loc) · 13.4 KB
/
avalon.router.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*********************************************************************
* History *
**********************************************************************/
//暴露如下接口:avalon.history.start, avalon.history.stop, avalon.history.interval
//avalon.Router.extend, avalon.Router.navigate
if (![].reduce) {
Array.prototype.reduce = function(fn, lastResult, scope) {
if (this.length == 0)
return lastResult;
var i = lastResult !== undefined ? 0 : 1;
var result = lastResult !== undefined ? lastResult : this[0];
for (var n = this.length; i < n; i++)
result = fn.call(scope, result, this[i], i, this);
return result;
}
}
avalon.history = new function() {
var oldIE = !"1" [0];
var started = false;
var self = this;
var iframeWin, iframe, history_hash, timeoutID;
var last_hash = "#!" + getFragment();
var supportPushState = /[native code]/.test(history.pushState);
var html = '<!doctype html><html><body>@</body></html>';
if (this.domain) {
html = html.replace("<body>", "<script>document.domain =" + this.domain + "</script><body>");
}
function createIframe() {
if (!iframe && oldIE) {
iframe = document.createElement("iframe");
iframe.tabIndex = -1;
iframe.style.display = "none"
iframe.src = "javascript:false";
(document.body || document.documentElement).appendChild(iframe);
var doc = iframe.contentDocument || iframe.contentWindow.document;
doc.write(html.replace("@", last_hash));
doc.close();
timeoutID = setInterval(poll, self.interval);
}
}
// IE6直接用location.hash取hash,可能会取少一部分内容
// 比如 http://www.cnblogs.com/rubylouvre#stream/xxxxx?lang=zh_c
// ie6 => location.hash = #stream/xxxxx
// 其他浏览器 => location.hash = #stream/xxxxx?lang=zh_c
// firefox 会自作多情对hash进行decodeURIComponent
// 又比如 http://www.cnblogs.com/rubylouvre/#!/home/q={%22thedate%22:%2220121010~20121010%22}
// firefox 15 => #!/home/q={"thedate":"20121010~20121010"}
// 其他浏览器 => #!/home/q={%22thedate%22:%2220121010~20121010%22}
function getHash(url, shim) {//用于取得当前窗口或iframe窗口的hash值
url = url || document.URL;
return url.slice(url.indexOf("#") + (~~shim));
}
function getHistory() {
return getHash(iframeWin.location);
}
function setHistory(hash, history_hash) {
if (hash !== history_hash) {//只有当新hash不等于iframe中的hash才重写
//用于产生历史
try {
var iframeDoc = getDoc();
iframeDoc.open();
iframeDoc.write(html.replace("@", hash));
iframeDoc.close();
} catch (e) {
clearInterval(timeoutID);
}
}
}
function getFragment() {
var href = location.href;
var index = href.indexOf(location.pathname.slice(1));
return href.slice(index);
}
function getDoc() {
return iframe.contentDocument || iframe.contentWindow.document;
}
function poll(e) {
if (iframe) {
var iframeDoc = getDoc(),
hash = getHash();//取得主窗口中的hash
history_hash = iframeDoc.body.innerText;//取得现在iframe中的hash
if (hash !== last_hash) {//如果是主窗口的hash发生变化
if (hash.indexOf("#!") !== 0) {
hash = "#!" + getFragment();
location.hash = hash;
}
avalon.Router.navigate(hash.split("#")[2] || "不存在");
setHistory(last_hash = hash, history_hash);
} else if (history_hash !== last_hash) {//如果按下回退键,
// avalon.log("用户点了回退键,导致iframe中的hash发生变化" + history_hash);
location.href = location.href.replace(/#.*/, '') + history_hash;
}
}
}
avalon.mix(this, {
interval: 35,
start: function(html5mode) {
if (started)
avalon.error("start已经触发过了");
started = true;
createIframe();
this.html5mode = !!html5mode;
if (window.opera || window.VBArray || !supportPushState) {
this.html5mode = false;
}
//如果我们想在改动URL时不刷新地址
// http://foo.com/bar?baz=23#bar
// http://foo.com/#!/bar?bar=23#bar
this.checkUrl = function() {
var path = getHash(getFragment(), true);
avalon.Router.navigate(path)
}
if (this.html5mode) { //如果支持pushState
//http://caniuse.com/#search=pushstate
window.addEventListener("popstate", this.checkUrl);
} else if (window.opera || document.documentMode >= 8) {
//http://caniuse.com/#search=pushstate
this.checkUrl = avalon.bind(window, "hashchange", this.checkUrl);
}
},
stop: function() {
//停止事件监听或interval
avalon.unbind(window, "popstate", this.checkUrl).unbind(window, "hashchange", this.checkUrl);
clearInterval(timeoutID);
started = false;
}
});
}
/*********************************************************************
* Router *
**********************************************************************/
new function() {
//表的结构:method+segments.length 普通字段
function _tokenize(pathStr) {
var stack = [''];
for (var i = 0; i < pathStr.length; i++) {
var chr = pathStr.charAt(i);
if (chr === '/') {//用于让后面的字符串相加
stack.push('');
continue;
} else if (chr === '(') {
stack.push('(');
stack.push('');
} else if (chr === ')') {
stack.push(')');
stack.push('');
} else {
stack[stack.length - 1] += chr;
}
}
return stack.filter(function(str) {
return str.length !== 0;
});
}
;
//将( ) 转换为数组的两端,最后构成一个多维数组返回
function _parse(tokens) {
var smallAst = [];
var token;
while ((token = tokens.shift()) !== void 0) {
if (token.length <= 0) {
continue;
}
switch (token) {
case '(':
smallAst.push(_parse(tokens));
break;
case ')':
return smallAst;
default:
smallAst.push(token);
}
}
return smallAst;
}
var combine = function(list, func) {
var first = list.shift();
var second = list.shift();
if (second === undefined) {
return first;
}
var combination = first.map(function(val1) {
return second.map(function(val2) {
return func(val1, val2);
});
}).reduce(function(val1, val2) {
return val1.concat(val2);
});
if (list.length === 0) {
return combination;
} else {
return combine([combination].concat(list), func);
}
};
function parse(rule) {
var tokens = _tokenize(rule);
var ast = _parse(tokens);
return ast;
}
function Router() {
this.routingTable = {};
}
function parseQuery(path) {
var array = path.split("#"), query = {}, tail = array[1];
if (tail) {
var index = tail.indexOf("?");
if (index > 0) {
var seg = tail.slice(index + 1).split('&'),
len = seg.length, i = 0, s;
for (; i < len; i++) {
if (!seg[i]) {
continue;
}
s = seg[i].split('=');
query[decodeURIComponent(s[0])] = decodeURIComponent(s[1]);
}
}
}
return {
pathname: array[0],
query: query
};
}
Router.prototype = {
_set: function(table, query, value) {
var nextKey = query.shift();//构建一个前缀树,用于高速匹对给定的URL
if (nextKey.length <= 0) {
avalon.error('构建失败');
}
if (nextKey.charAt && nextKey.charAt(0) === ':') {//如果碰到参数
var n = nextKey.substring(1);
if (table.hasOwnProperty('^n') && table['^n'] !== n) {
return false;
}
table['^n'] = n;
nextKey = '^v';
}
if (query.length === 0) {
table[nextKey] = value;
return true;
} else {
var nextTable = table.hasOwnProperty(nextKey) ?
table[nextKey] : table[nextKey] = {};
return this._set(nextTable, query, value);
}
},
add: function(method, path, value) {
var ast = parse(path); //转换为抽象语法树
var patterns = this._expandRules(ast);//进行全排列,应对可选的fragment
if (patterns.length === 0) {
var query = [method, 0];
this._set(this.routingTable, query, value);
} else {
var self = this
patterns.every(function(pattern) {
var length = pattern.length,
query = [method, length].concat(pattern);
return self._set(self.routingTable, query, value);
});
}
return value;
},
routeWithQuery: function(method, path) {
var parsedUrl = parseQuery(path),
ret = this.route(method, parsedUrl.pathname);
if (ret) {
ret.query = parsedUrl.query;
return ret;
}
},
route: function(method, path) {//将当前URL与
path = path.trim();
var splitted = path.split('/'),
query = Array(splitted.length),
index = 0,
params = {},
table = [],
args = [],
val, key, j;
for (var i = 0; i < splitted.length; ++i) {
val = splitted[i];
if (val.length !== 0) {
query[index] = val;
index++;
}
}
query.length = index;
table = this.routingTable[method];
if (table === void 0)
return;
table = table[query.length];
if (table === void 0)
return;
for (j = 0; j < query.length; ++j) {
key = query[j];
if (table.hasOwnProperty(key)) {
table = table[key];
} else if (table.hasOwnProperty('^v')) {
params[table['^n']] = key;
args.push(key)
table = table['^v'];
} else {
return;
}
}
return {
query: {},
args: args,
params: params,
value: table
};
},
_expandRules: function(ast) {
if (Array.isArray(ast) && ast.length === 0) {
return [];
}
var self = this;
var result = combine(ast.map(function(val) {
if (typeof val === 'string') {
return [[val]];
} else if (Array.isArray(val)) {
return self._expandRules(val).concat([[]]);
} else {
throw new Error('这里的值只能是字符串或数组 {{' + val + '}}');
}
}), function(a, b) {
return a.concat(b);
});
return result;
}
};
var callbacks = {}, errback, router = new Router();
avalon.Router = {
extend: function(obj) {//定义所有路由规则
if (typeof obj.routes === "object") {
for (var i in obj.routes) {
if (i === "*error") {
errback = obj.routes[i]
} else {
router.add("GET", i, obj.routes[i]);
}
}
}
for (var i in obj) {
if (typeof obj[i] === "function") {
callbacks[i] = obj[i];
}
}
},
navigate: function(url) {//传入一个URL,触发预定义的回调
var match = router.routeWithQuery("GET", url);
if (match) {
var key = match.value;
if (typeof callbacks[key] === "function") {
return callbacks[key].apply(match, match.args);
}
}
if (typeof callbacks[errback] === "function") {
callbacks[errback](url);
}
}
};
}