-
Notifications
You must be signed in to change notification settings - Fork 2
/
RootController.js
83 lines (78 loc) · 2.25 KB
/
RootController.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
/**
* Created by reco on 2018/8/13.
*/
MElegant.regist('RootController');
RootController = {
_windowStack : [],
mainContainer : null,
_currentIndex : -1,
init:function(mainContainer){
this.mainContainer = mainContainer;
this.mainContainer.css({
'width':'100%',
'height':"100%",
'overflow':'hidden',
'position':'relative'
});
window.addEventListener("popstate", this._popState.bind(this), false);
},
/**
* 添加一个view进场
* @param viewClassString
* @param animate 是否带动画
*/
addView:function(viewClassString,animate) {
var windowIndex = this._windowStack.length;
if(windowIndex==0) {
animate = false;
}
this._currentIndex = windowIndex;
var window = this._createWindow(windowIndex);
window.animate = animate;
this._windowStack.push(window);
Source.loadView(viewClassString).then(function(viewClass){
window.view = new viewClass();
window.view.viewClass = viewClass;
window.view.showIn(window.dom);
}.bind(this)).then(function(){
window.in();
if(windowIndex>0) {
history.pushState({"viewClassName": viewClassString}, null, "#" + viewClassString + '-' + windowIndex);
}
});
},
/**
* 出栈一个view
* @param animate 是否带动画
*/
popView:function(animate) {
history.back();
},
/**
*
* @param index
* @returns {WindowLayer}
* @private
*/
_createWindow:function(index) {
var window = new WindowLayer();
var newWindow = $('<div class="window"></div>');
newWindow.css({
'width':"100%",
'height':"100%",
'z-index':index,
'background-color':"red"
});
window.dom = newWindow;
window.index = index;
return window;
},
_popState:function(state) {
var hash = location.hash;
var pageInfo = /^#(.+)\-(\d+)/.exec(hash);
var className = pageInfo[1];
var index = pageInfo[2];
var topWindow = this._windowStack[this._currentIndex];
topWindow.out();
}
};