-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.js
236 lines (175 loc) · 5.66 KB
/
core.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
var miApp =(function(window,undefined){
var Events = function(sender){
this.sender = sender;
this.listeners = [];
}
Events.prototype = {
constructor : Event,
attach : function(listener){
this.listeners.push(listener)
},
notify:function(args){
var len = this.listeners.length;
for(var i = 0; i< len;i+=1){
this.listeners[i](this.sendar,args);
}
}
};
var Resources = {
'controllers' : {},
'dependancies': {},
'routeMap' : [],
'models' : {},
'views' : {}
};
var routeobj = function(controller,routerName,url){
this.controller = controller;
this.routeName = routerName;
this.url = url;
}
var Router = {
'routeMap' : {},
'defaultRoute' : null,
'container' : null,
'config' : function(controllerName,routeName,url){
this.routeMap[routeName] = new routeobj(controllerName,routeName,url);
return this;
},
'links' : function(){
var links = document.querySelectorAll('*[data-route]'),
links_len = links.length;
document.body.onclick = function(event){
var target = event.target;
name = target.nodeName.toLowerCase();
if(name === 'a'){
if(!!target.getAttribute('data-route'))
window.location.hash = encodeURI(target.getAttribute('data-route'));
}
};
return this;
},
'getRouteMap' : function(){ return this.routeMap},
'listener' : function(){
var decodeurl = decodeURI(window.location.hash),
pageHash = decodeurl.replace('#',''),
routename = pageHash.replace('/','').replace(/\?(.*)$/,''),
routeobj = this.routeMap[routename],
controller = null,
dependancies = null;
(!!routeobj) == false && (routeobj = this.defaultRoute);
controller = Resources.controllers[routeobj.controller];
dependancies = Resources.dependancies[routeobj.controller];
dependancies = api.loadDependancy(dependancies);
controller(dependancies); // 执行控制器
},
initialize : function(){
this.links();
var self = this;
try{
this.defaultRoute = this.routeMap[Object.getOwnPropertyNames(this.routeMap)[0]];
}catch(err){
var propertyNames = [],i;
for(i in this.routeMap){ propertyNames.push(i) }
this.defaultRoute =this.routeMap[propertyNames[0]];
}
window.onhashchange = this.listener.bind(this);
this.listener(); // 初始化页面路由
}
};
var api = {
'controller' : function(controllerName,handler){
if(typeof handler === 'function'){
Resources.controllers[controllerName] = handler;
}else{
if(handler.length <= 1) throw new Error('如果没有依赖,可直接使用函数作为参数!');
var lastIndex = handler.length - 1;
var dependancies = handler.slice(0,-1);
if(typeof handler[lastIndex] === 'function'){
Resources.controllers[controllerName] = handler[lastIndex];
Resources.dependancies[controllerName] = dependancies;
}else{
throw new Error('无法创建 controller');
}
}
return this;
},
'model' : function(modelName,model){
if(typeof model == 'function'){
Resources.models[modelName] = model;
}else{
throw new Error('无法创建 model ');
}
return this;
},
'view' : function(viewName,view){
if(typeof view === 'function'){
Resources.controllers[viewName] = view;
}else{
throw new Error('无法创建 view ');
}
return this;
},
'loadDependancy' : function(arrayArg){
var dependancies = [];
var len = arrayArg.length,
i;
for(i=0;i<len;i+=1){
if(typeof arrayArg[i] === 'string'){
if(Resources.controllers.hasOwnProperty(arrayArg[i])){
dependancies.push(Resources.controllers[arrayArg[i]]);
}else{
if(Resources.models.hasOwnProperty(arrayArg[i])){
dependancies.push(Resources.models[arrayArg[i]])
}else{
if(Resources.views.hasOwnProperty(arrayArg[i])){
dependancies.push(Resources.views[arrayArg[i]]);
}else{
throw new Error('无法找到依赖模块 “'+ arrayArg[i] +'”');
}
}
}
}
}
return dependancies;
}
};
function loadDependancy(){
api.loadDependancy(arguments[0]);
return this;
}
function controller(){
api.controller(arguments[0],arguments[1]);
return this;
}
function config(){
Router.config(arguments[0],arguments[1],arguments[2]);
return this;
}
function router(){
return Router;
}
function model(){
api.model(arguments[0],arguments[1]);
return this;
}
function view(){
api.view(arguments[0],arguments[1]);
return this;
}
function initialize(){
Router.initialize()
}
function getEvents(){
return Events;
}
return {
'loadDependancy' : loadDependancy,
'controller' : controller,
'config' : config,
'router' : router,
'model' : model,
'view' : view,
'initialize' : initialize,
'getEvents' : getEvents
}
});