-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyspa2.0.1.js
391 lines (349 loc) · 13.7 KB
/
myspa2.0.1.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
379
380
381
382
383
384
385
386
387
388
389
390
391
/**
* myspa2.0升级版 myspa 2.0.1
* @author lowinwu
*/
define(function(require,exports,module){
var mystore = require("./mystore.js"),
myEvent = require("./myevent.js");
//引用整个应用依赖的模块
//国际化
var i18n,
global = window,
myspa = {isStart:false},
qs = "q_mymyspa",
rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]*))$/;
//移除綁定事件
var ZeptEvenNames = ['input','swipe', 'swipeLeft', 'swipeRight', 'swipeUp', 'swipeDown',
'doubleTap', 'tap', 'singleTap', 'longTap','focusin','focusout','focus','blur','load','resize','scroll','unload',
'click','dblclick','mousedown','mouseup','mousemove','mouseover','mouseout','mouseenter','mouseleave',
'change','select','keydown','keypress','keyup','error'].join(" ");
myspa.myEvent = myEvent;
/*
* 默认路由
*/
var defaultUIOptions = {
path: '', //路由名,注入点
script:'',
isAutoShow:true, //是否默认显示出来,默认显示
init: function() {}, //初始化回调函数
beforeinit: function() {}, //打开前回调
afterinit: function() {}, //打开后回调
beforeclose: function() {}, //关闭前回调
afterclose: function() {} //关闭后回调
}
var firemothod = function(module,method,options){
var arg = Array.prototype.slice.call(options);
arg.splice(0,1);
if(module && module[method]){
module[method].apply(module,arg);
}
}
//增加额外逻辑:如页面切换
myEvent.on("beforeinit",function(module){
firemothod(module,"beforeinit",arguments);
});
myEvent.on("init",function(module){
//处理属性问题
myspa.changeClass(module.path,module);
myspa.changeTitle(module.path,module);
myspa.changeView(module.path,module);
});
myEvent.on("afterinit",function(module){
firemothod(module,"afterinit",arguments);
});
myEvent.on("beforeclose",function(module){
firemothod(module,"beforeclose",arguments);
});
myEvent.on("afterclose",function(module){
firemothod(module,"afterclose",arguments);
});
var myspa = $.extend(myspa,{
//注册源
routers:{},
_curentHash:'',//保存当前hash
_saveCurentHash:function(){
this._curentHash = location.hash && location.hash.replace("#","");
},
r :function(options){
var args = Array.prototype.slice.call(arguments, 1)
if(args.length > 1) {
$.each(args, function(i, panel) {
myspa.r(panel)
})
return false
}
if(options.path) {
if(myspa.routers[options.path]){//更新
myspa.routers[options.path] = $.extend(myspa.routers[options.path], options);
}else{
myspa.routers[options.path] = $.extend({}, defaultUIOptions, options);
}
}
},
//
innerBoot:function(path,arg){
var uiModule = this.getBootRouter(path);
var curPage = this._curentHash;
//load module
if(uiModule['script']){
seajs.use([uiModule['script']],function(module){
if(!module) return ;
//更新router
myspa.r($.extend({path:path},module));
//1 注册上view ,2 运行init方法 3、传参数 4 自动隐藏当前节点
myEvent.emit('beforeinit',module,arg);
//处理模块属性问题 title class等
myspa.changeView(path,module);
myspa.changeClass(path,module);
myspa.changeTitle(path,module);
//insert animate
if(uiModule.animate) {
transitPage($("#"+path), $("#"+curPage), uiModule.animate, function(){
module.init.call(module,$("#"+path),arg);
})
}else{
module.init.call(module,$("#"+path),arg);
}
myEvent.emit('afterinit',module,arg);
//保存当前路由
myspa.signLocaton({state:{path:path},type:true});
});
}else{
alert('无对应路由:' + uiModule['script']);
}
//store arg
if(arg && arg.length > 0){
mystore.setItem(qs + "_" + uiModule['script'],arg);
}
},
boot:function(path){
var uiModule = myspa.getBootRouter(path);
arg = Array.prototype.slice.call(arguments);
arg.splice(0,1);
//value from session
if(arg && arg.length == 0){
arg = mystore.getItem(qs+"_"+uiModule['script'])
}
myspa.startRun(uiModule,arg);
},
//1/在本身页面上,2#取页面上元素html 3,取线上html; 隐藏页面其它view
injectView:function(id,html){
if(!html)return;
var matchs = html.match(/^#(\w*)/);
if(matchs && matchs.length == 2){
if(matchs[1] == id){//在本身页面上
this.setHtml(null,$("#"+id).html());
}else if(!rquickExpr.exec(html)){//取页面上元素html
html = $(html).html();
}
}else{
this.setHtml(id,html);
}
},
//去掉缓存的事件以及上面缓存数据
clearData:function(elems){
elems = elems || [];
//去除事件
for ( var i = 0, elem; (elem = elems[i]) != null; i++ ) {
$.event&&$.event.remove&&$.event.remove(elem,ZeptEvenNames);
}
},
getAllNodes:function(view){
return view&&view[0]&&view[0].getElementsByTagName("*")
},
//设置html
setHtml:function(id,html){
//默认在view-container上,统一在一个节点上,利于开发时,在首页加节点d
var viewContainer = $("#"+id);
var view = viewContainer.length == 0 ? $(".view-container") : viewContainer;
//清事件
this.clearData(this.getAllNodes(view));
//注入
view.html(i18n?i18n.processString(html):html);
},
changeView:function(id,module){
this.hideAllView(module);
module&&module.body&&this.injectView(id,module.body);
this.showView(id,module);
},
hideAllView:function(){
var lstRequest = myspa.getRunModule(this._curentHash);
var lstViewDom = $("#"+lstRequest.path);
if(lstRequest && lstViewDom.length > 0){
lstRequest.hideView = typeof(lstRequest.hideView) == "undefined" ? true : !!lstRequest.hideView;
if(!!lstRequest.hideView){
lstViewDom.addClass("hide");
}
}
$(".view-container").addClass("hide");
},
showView :function(id,module){
//默认为false
var isAutoShow = typeof(module.isAutoShow) == "undefined" ? true : !!module.isAutoShow;
if($("#"+id).length == 0){
isAutoShow && $(".view-container").removeClass("hide");
}else{
isAutoShow && $("#"+id).removeClass("hide");
this.clearData(this.getAllNodes($("#"+id)));
}
},
showApp:function(){
$(".view-container").removeClass("hide");
},
changeClass:function(id,module){
module.bodyClass && $("body").removeAttr("class").addClass(module.bodyClass + " " + (i18n?i18n.getApplang():""));
},
changeTitle:function(id,module){
if(module.title){
var $body = $('body');
document.title = module.title;
// hack在微信等webview中无法修改document.title的情况
var $iframe = $('<iframe style="display:none;" src="about:blank" ></iframe>').on('load', function() {
setTimeout(function() {
$iframe.off('load').remove()
}, 10)
}).appendTo($body);
}
},
//注册
regRouter : function(path,type){
//回退首页面
if(path == "index"){
if(this._curentHash){ //刷新url中包含hash
if(path !== this._curentHash){
myspa.hashWrite = true;//不要触发hashchange中事件渲染
}
}
}else if(type){
if(location.hash !== "#"+path){
location.hash = "#"+path;
myspa.hashWrite = true;
}
}
},
// myspa.signLocaton({state:{path:pathName},func:func});
signLocaton : function(obj){
//path为必填
if(! obj.state || ! obj.state.path){
throw 'require state.path';
}
myspa.pushHash(obj.state.path,obj.type);
myspa._saveCurentHash();
},
pushHash : function(path,type){
myspa.regRouter(path,type);
},
getLastRunModule:function(){
return myspa.getRunModule(this._curentHash)
},
getRunModule:function(hash){
hash = hash || location.hash;
var request = myspa.getBootRouter(hash.replace("#",""));
return request;
},
//enter fire
innerRun:function(request,arg){
if($.isFunction(request)){
request()
}else if($.isPlainObject(request)){
myspa.innerBoot(request["path"],arg);
}
},
//outer fier
outerRun:function(lstRequest,curRequest){
if($.isPlainObject(lstRequest) || $.isPlainObject(curRequest)){
myEvent.emit('beforeclose',lstRequest,curRequest);
myEvent.emit('afterclose',lstRequest,curRequest);
}
},
startRun:function(curRequest,arg){
var curRequest = curRequest || myspa.getRunModule();
var lstRequest = myspa.getRunModule(this._curentHash);
//value from session
arg = arg ? arg:mystore.getItem(qs+"_"+curRequest['script']);
if(this._curentHash != curRequest.path){//避免重复请求
this.outerRun(lstRequest,curRequest);
if(myspa.isStart){
this._startRouter(curRequest,arg);
}
}
//第一次启动,刷新处理
if(!myspa.isStart){
myspa.isStart = true;
this._startRouter(curRequest,arg);
}
},
_startRouter:function(curRequest,arg){
if(!curRequest) return ;
curRequest.isclose = typeof curRequest.isclose =='undefined' ? false : !!curRequest.isclose;
if(!curRequest.isclose){
this.innerRun(curRequest,arg);
}
},
//初化化路由
// 1,设置当前url根目录
initRun:function(options,arg){
options = options || {};
myspa.rootPath = options.rootPath;
myspa.defaultRouter = options.defaultRouter;
i18n = options.i18n;
//路由处理
var matchs = location.search.match(/page=([^&]*)(&|$)/);
match = RegExp.$1;
if(matchs && match){
match = match.replace("%2F","/");
location.hash = match;
myspa.defaultRouter = match;
myspa.hashWrite = true;
}else if(location.hash){//刷新处理
myspa.defaultRouter = location.hash.replace("#","");
}else{
location.hash = myspa.defaultRouter;
myspa.hashWrite = true;
}
//注册当前路由
myspa.r({
path: myspa.defaultRouter,//挂载点
script: myspa.rootPath+"/"+myspa.defaultRouter+".js"
});
//加载运行
myspa.startRun();
myspa.isStart = true;
},
//获取要启动的路由
getBootRouter:function(path){
var router = myspa.routers[path];
if(!router) {//注册新路由
myspa.r({
path: path,//挂载点
script: this.rootPath+"/"+path+".js"
});
}
return myspa.routers[path];
},
//back request,save before data and excute,mobile refesh can excute onpopstate
//no support propstate,apply onhashchage
monitorRouter:function(){
//解决调回时,不发生触发
this._saveCurentHash();
//监听触发
window.onhashchange = function(){
if(myspa.hashWrite){//写的操作引起change
myspa.hashWrite = false;
}else{//回退的操作
myspa.startRun();
}
};
}
});
myspa.monitorRouter();
module.exports = {
on : function(){
var arg = Array.prototype.slice.call(arguments);
arg.splice(0,1);
return myEvent.on.apply(myEvent,arg);
},
boot : myspa.boot,
initRun: myspa.initRun
}
});