forked from AlloyTeam/Abstract.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultitabmodel.js
252 lines (185 loc) · 7.17 KB
/
multitabmodel.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
;(function(){
var $ = Model.$;
var containerCountInfo = Model.containerCountInfo;
var MultitabModel = Model.Class("MutexModel", {
type: "MultitabModel",
get currModel(){
return this.mutexModel.currChild;
},
constructor: function(opt){
this.callSuper(opt);
this.mutexModel = new MutexModel();
this.selectorMap = {};
this.tabHander = null;
this.selectorSwitchInMap = {};
this.mutexModel.addEventListener('beforeactived', function(e){
var smodel = e.target;
if(/RenderModel|ScrollModel/.test(smodel.type)){
var id = $(smodel.el).attr("id");
if (containerCountInfo[id] > 1) {
$(smodel.el).html("");
smodel.reset();
} else {
// 如果请求过了
if (smodel.isFirstDataRequestRender > 0) {
smodel.show();
e.preventDefault();
} else {
}
}
// 干涉渲染模型的激活态行为
var target = e.target;
}
});
var _this = this;
this.addEventListener("beforeswitched", function(e, data){
var model = _this.mutexModel.currChild;
var switchType = e.name;
var selector = (data && data.selector) || '';
/*
for(var i in _this.selectorMap){
if(_this.selectorMap[i] === model){
selector = i;
break;
}
}
*/
if(selector){
_this.beforeTabHandler && _this.beforeTabHandler.call(_this, selector, switchType);
}
});
this.addEventListener("switched", function(e){
var model = _this.mutexModel.currChild;
var switchType = e.name;
var selector;
for(var i in _this.selectorMap){
if(_this.selectorMap[i] === model){
selector = i;
$(i).addClass('active')
.addClass('selected');
}else{
$(i).removeClass('active')
.removeClass('selected');
}
}
if(selector){
_this.selectorSwitchInMap[selector] && _this.selectorSwitchInMap[selector].call(_this, selector, switchType);
_this.tabHander && _this.tabHander.call(_this, selector, switchType);
}
});
},
_resetPrivateFlag: function(){
this.get("rocked").set(0);
},
active: function(e){
var rocked = this.get("rocked");
if(! rocked.value){
this.dispatchEvent(Model.createEvent({
type: "beforeswitched",
target: this,
name: "init"
}));
}
this.mutexModel.rock();
// 如果当前为一个loadModel则加载回来才触发switch事件
if(this.mutexModel.currChild.type === "LoadModel"){
var _this = this;
this.mutexModel.currChild.addEventListener("completed", function(e, realizeModel){
if(! rocked.value){
_this.dispatchEvent(Model.createEvent({
type: "switched",
target: _this,
name: "init"
}));
rocked.set(1);
}
});
}else{
if(! rocked.value){
this.dispatchEvent(Model.createEvent({
type: "switched",
target: this,
name: "init"
}));
rocked.set(1);
}
}
},
add: function(selector, model, onswitchin){
var fuse = "multitab_" + selector;
var _this = this;
if(typeof model === "string"){
model = new LoadModel({
name: selector,
moduleName: model
});
// loadModel加载完成
// 对于实现的model控制其显示逻辑
model.addEventListener("completed", function(e, realizeModel, onswitchin){
realizeModel.addFuse(fuse);
realizeModel.addEventListener('actived', function(e){
this.show();
});
realizeModel.addEventListener('unactived', function(e){
this.hide();
});
_this.selectorMap[selector] = realizeModel;
if(onswitchin){
_this.selectorSwitchInMap[selector] = onswitchin;
}
});
}else{
model.addEventListener('actived', function(e){
this.show();
});
model.addEventListener('unactived', function(e){
this.hide();
});
}
model.addFuse(fuse);
this.mutexModel.add(model);
this.selectorMap[selector] = model;
if(onswitchin){
this.selectorSwitchInMap[selector] = onswitchin;
}
this.selector
this.bindSwitchEvent(selector);
},
bindSwitchEvent: function(selector){
var _this = this;
var fuse = "multitab_" + selector;
// 切换事件绑定
$("body").on(Model._config.multitab_event, selector, function(){
_this.dispatchEvent(Model.createEvent({
type: "beforeswitched",
target: _this,
name: "switch"
}), {selector: selector});
Model.trigger(fuse, 'switch');
_this.dispatchEvent(Model.createEvent({
type: "switched",
target: _this,
name: "switch"
}));
});
},
init: function(selector){
if(this.selectorMap[selector]){
this.mutexModel.initChild(this.selectorMap[selector]);
}
},
ontabswitch: function(func){
this.tabHander = func;
},
refresh: function(){
this.mutexModel.currChild.refresh();
},
reset: function(){
this.mutexModel.currChild.reset();
},
beforetabswitch: function(func){
this.beforeTabHandler = func;
}
});
Model.external("MultitabModel", MultitabModel);
})();