-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathpage.js
378 lines (308 loc) · 10.2 KB
/
page.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
370
371
372
373
374
375
376
377
378
(function(w, undefined) {
var exports = w,
cache = {}, // resourceMap cache
cacheMaxTime = 0, // 缓存时间
appOptions = {}, // app页面管理的options
curPageUrl,
isPushState,
layer, // 事件代理层
urlReg = /^(([^:/?#]+):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/i;
/**
* 启动页面管理
* @param {Object} options 初始化参数
* @param {String} options["selector"] 全局代理元素的选择器匹配,写法同 document.querySeletor 函数
* @param {Number} options["cacheMaxTime"] 页面缓存时间
* @param {Function|RegExp} options["validate"] url验证方法,
* @return {void}
*/
function start(options) {
/**
* 默认参数 {
* selector : <string> // 代理元素的选择器规则
* cacheMaxTime: <integer> //缓存存活时间,默认5min
* }
*/
var defaultOptions = {
selector: "a,[data-href]",
cacheMaxTime: 5 * 60 * 1000,
pushState : true,
layer : document.body
};
appOptions = merge(defaultOptions, options);
cacheMaxTime = appOptions.cacheMaxTime;
isPushState = appOptions.pushState;
layer = getLayer(appOptions.layer);
curPageUrl = getCurrentUrl();
if(isPushState === true){
// 绑定事件
bindEvent();
}
}
/**
* 事件绑定
* @return {void}
*/
function bindEvent() {
// 处理history.back事件
window.addEventListener('popstate', onPopState, false);
// 全局接管指定元素点击事件
layer.addEventListener('click', proxy, true);
// bigpipe回调事件
BigPipe.on('pagerendercomplete', onPagerendered, this); // 执行完页面的ready函数后触发
// 页面数据到达的时候派发事件
BigPipe.on('pagearrived', onPageArrived, this);
// 页面内所有的样式JS都被加载完成后触发
BigPipe.on('onpageloaded', onPageLoaded, this);
}
function getLayer(ele) {
if(typeof ele === "string") {
return document.querySelector(ele);
} else if (ele && ele.nodeType) {
return ele;
} else {
return document.body
}
}
/**
* 处理popstate事件,响应历史记录返回
* @param {PopStateEvent} e popstate事件对象
* @return {void}
*/
function onPopState(e) {
var currentUrl = getCurrentUrl();
if (!curPageUrl || currentUrl === curPageUrl) {
return;
}
trigger('onpagerenderstart');
fetchPage(currentUrl, e.state);
}
/**
* 渲染完成事件函数
* @param {String} obj bigpipe回传事件参数
* @return {void}
*/
function onPagerendered(obj) {
cache[obj.url] = {
resource: obj.resource,
time: Date.now()
};
//page render end
trigger('onpagerendercomplete',{
url : obj.url
});
}
function onPageArrived(options){
trigger('onpagearrived',options);
}
function onPageLoaded() {
trigger('onpageloaded');
}
/**
* 简单merge两个对象
* @param {object} _old
* @param {object} _new
* @returns {*}
*/
function merge(_old, _new) {
for (var i in _new) {
if (_new.hasOwnProperty(i)) {
_old[i] = _new[i];
}
}
return _old;
}
/**
* 事件代理
* @param {MouseEvent} 点击事件对象
*/
function proxy(e) {
var element = e.target,
parent = element,
selector = appOptions.selector;
while (parent !== document.body) {
if (matchSelector(parent, selector)) {
urlAttr = parent.tagName.toLowerCase() === "a" ? "href" : "data-href";
url = parent.getAttribute(urlAttr);
// 验证url, 可以自行配置url验证规则
if (validateUrl(url)) {
e.stopPropagation();
e.preventDefault();
var opt = {
replace: parent.getAttribute("data-replace") || false,
containerId: parent.getAttribute("data-area"),
pagelets: parent.getAttribute("data-area"),
target : parent
}
redirect(url, opt);
}
return;
} else {
parent = parent.parentNode;
}
}
}
/**
* 检查元素是否匹配选择器
* @param {HTMLElement} element
* @param {String} selector 选择器规则
* @return {boolean}
*/
function matchSelector(element, selector) {
if (!element || element.nodeType !== 1) {
return false
}
var parent,
match,
matchesSelector = element.webkitMatchesSelector || element.matchesSelector;
if (matchesSelector) {
match = matchesSelector.call(element, selector)
} else {
parent = element.parentNode;
match = !! parent.querySelector(selector);
}
return match;
}
/**
* 验证URL是否符合validate规则
* @param {string} url
* @return {boolean}
*/
function validateUrl(url) {
var validate = appOptions.validate,
type = Object.prototype.toString.call(validate);
if (type === "[object RegExp]") {
return validate.test(url);
} else if (type === "[object Function]") {
return validate(url);
} else {
return true;
}
}
/**
* 获取url的pathname 和 query部分
* @param {String} url
* @return {String} 返回url的pathname 和 query部分
*/
function getUrl(url) {
if (urlReg.test(url)) {
return RegExp.$5 + (RegExp.$6 ? RegExp.$6 : "");
} else {
"console" in window && console.error("[url error]:", url);
}
}
/**
* 获取当前的url
* @return {String} 获取当前url
*/
function getCurrentUrl() {
return getUrl(window.location.href)
}
/**
* 跳转页面
* @param {String} url 目标页面的url
* @param {Object} options 跳转配置参数
* @param {Array|String} options[pagelets] 请求的pagelets
* @param {String} options[containerId] pagelets渲染容器
* @param {Boolean} options[trigger] 是否触发加载
* @param {Boolean} options[forword] 是否替换URL
* @param {Boolean} options[replace] 是否替换当前历史记录
* @param {HTMLElement} options[target] 触发跳转的DOM元素
*/
function redirect(url, options) {
url = getUrl(url);
// 如果url不变则不加载
if(getCurrentUrl() === url) {
return;
}
var method,
defaultOptions = {
trigger: true,
forword: true,
replace: false
},
eventsOptions = {
url : url
};
options = merge(defaultOptions, options);
eventsOptions.target = options.target || null;
// tirgger 状态不进行页面获取,只切换URL
if(options.trigger === false) {
if(isPushState) {
method = options.replace ? "replaceState" : "pushState";
window.history[method]({}, document.title, url);
}
return;
}
if (!isPushState) {
options.replace ? (location.replace(url)) : (location.href = url);
return;
}
//page render start
trigger('onpagerenderstart' , eventsOptions);
// 之所以放在页面回调中替换历史记录,是因为在移动端低网速下
// 有可能后续页面没有在下一次用户操作前返回,而造成添加无效历史记录的问题
fetchPage(url, options, function(){
if (options.forword) {
method = options.replace ? "replaceState" : "pushState";
window.history[method]({}, document.title, url);
}
});
}
function fetchPage (url, options, callback){
if(!url) {
return;
}
var now = Date.now(),
options = options || {},
pageletsParams = [],
opt = {},
containerId = options.containerId ? options.containerId : appOptions.containerId,
pagelets = options.pagelets ? options.pagelets : appOptions.pagelets;
if(typeof pagelets === "string" ) {
pagelets = [pagelets]
}
curPageUrl = url;
if (pagelets.length > 0) {
for (var i = 0, len = pagelets.length; i < len; i++) {
pageletsParams.push('pagelets[]=' + pagelets[i]);
}
url = (url.indexOf('?') === -1) ? url + '/?' + pageletsParams.join('&') : url + '&' + pageletsParams.join('&');
}
(options.cache === false) && (opt.cache = false);
BigPipe.refresh(url, containerId, opt, function(){
callback && callback();
})
}
// -------------------- 事件队列 --------------------
var SLICE = [].slice;
var events = {};
function trigger(type /* args... */ ) {
var list = events[type];
if (!list) {
return;
}
var arg = SLICE.call(arguments, 1);
for (var i = 0, j = list.length; i < j; i++) {
var cb = list[i];
if (cb.f.apply(cb.o, arg) === false) {
break;
}
}
}
function on(type, listener, context) {
var queue = events[type] || (events[type] = []);
queue.push({
f: listener,
o: context
});
}
exports.appPage = {
start: start,
redirect: redirect,
on: on
};
// 模块化支持
if ("define" in window && typeof module != "undefined") {
module.exports = exports.appPage
}
})(this);