-
Notifications
You must be signed in to change notification settings - Fork 1
/
shift.js
executable file
·341 lines (297 loc) · 8.98 KB
/
shift.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
/*
Shift - Javascript - Framework
Copyright (C) 2013 Erik Landvall
Dual licensed under the MIT and GPL version 3
*/
/**
* @returns Shift
*/
function Shift()
{
/**
* @returns Shift.Manager
*/
function Manager()
{
var container = {};
/**
* Set a service that can be acceced through the defined namespace.
*
* @param String namespace The desired namespace, will overwrite if
* namespace already exists.
* @param Object service The service
*/
this.set = function(namespace, service)
{
namespace = namespace.toLowerCase();
container[namespace] = service;
};
/**
* Used to retrieve a service from a certain namespace
*
* @param String namespace The namespace
* @returns Object
* @throws Namespace undefined
*/
this.get = function(namespace)
{
namespace = namespace.toLowerCase();
if(!container[namespace])
throw 'Namespace undefined';
return container[namespace];
};
/**
* Used to retrieve all availible services
*
* @returns Object
*/
this.getAll = function()
{
return container;
};
/**
* Is used for removing a service from the manager
*/
this.remove = function(ns)
{
delete container[ns];
};
/**
* Returns if a namespace is set or not
*
* @param String namespace The namespace
* @returns Boolean
*/
this.has = function(namespace)
{
namespace = namespace.toLowerCase();
return !! container[namespace];
};
}
/**
* The router provides matching routes, it doesn't dispatch them.
*
* @returns Shift.Router
*/
function Router()
{
/**
* Determines if the event matches the rout
*
* Expected behavior:
*
* |------------|------------|--------|
* | rout | event | match |
* |============|============|========|
* | foo.bar | foo.bar | true |
* | foo | foo | true |
* | foo.* | foo.bar | true |
* | * | * | true |
* | foo.* | foo.* | true |
* | foo.bar | foo.* | true |
* | foo | foo.bar | false |
* | foo.bar | foo | false |
* | foo | bar | false |
* |------------|------------|--------|
*
* @param String rout The rout to compare to
* @param String event The event to compare with
*
* @returns Boolean
*/
function match(rout, event)
{
var match = true;
rout = rout.split('.');
event = event.split('.');
if(rout.length !== event.length)
match = false;
for(var i = 0; match && i < event.length; i++)
if(rout[i] !== event[i] && '*' !== rout[i] && '*' !== event[i])
match = false;
return match;
}
/**
* Returns all matching routes
*
* @param String event The event that should be matched agains the routes
* in the current scope
* @param Object scope A container with the relevenat modules to search
* for routes in.
*
* @returns Array
* @throws Unrecognized router type
*/
this.getRoutes = function(event, scope)
{
var actions = [];
for(var module in scope)
if(scope[module].router)
for(var rout in scope[module].router)
if(match(rout, event))
(function callback(rout)
{
switch(typeof rout)
{
case 'object':
if(rout instanceof Array)
for(var i = 0; i < rout.length; i++)
callback(rout[i]);
else
for(var ns in rout)
callback(rout[ns]);
break;
case 'string':
actions.push(
{
'module': module,
'rout' : rout
});
break;
default:
throw 'Unrecognized router type';
}
})(scope[module].router[rout]);
return actions;
};
}
/**
* @param Object scope A container of the modules that should be affected
* when an event is triggered
*
* @returns Shift.EventBus
*/
function EventBus(scope)
{
var router = new Router();
/**
* This couses the application to work asynchronously through diffrent
* event. It should also help queuing up rendering work in some browsers.
*
* @param Object route Where the route information is held.
* @param String eventType The type of event.
* @param mix data Anything that is passed from the trigger.
* @param Shift.EventBus eventBus The event bus that we can use to trigger
* an error if so is needed.
*
* @returns Shift.EventBus.Thread
*/
function Thread(route, eventType, data, eventBus)
{
this.run = function()
{
setTimeout(this.dispatch, 0);
};
this.dispatch = function()
{
try
{
// A handle to the module we are about to dispatch from
var module = Shift[route.module];
// If the controller existes, the return value from this will be
// passed on to the view.
// If no controller existes, the data passed through the event will
// be passed on to the view.
data = ( module.controller && module.controller[route.rout] )
? module.controller[route.rout](data)
: data;
// If a view existes it will be rendered with the data passed on.
if(module.view && module.view[route.rout])
module.view[route.rout](data);
}
// Preventing an eception in the dispatch proces to couse a melt-down
catch(exception)
{
// Composes an exception with more information
var e =
{
'Module' : route.module,
'Rout' : route.rout,
'Event' : eventType,
'Exception': exception
};
// Preventing an eternal loop
if(eventType === 'error.dispatch')
throw 'Eternal loop found in Shift.\n' +
'\n' +
'Event: "' + eventType + '"\n' +
'Module: "' + e.Module + '"\n' +
'Rout: "' + e.Rout + '"\n' +
'Exception: "' + exception + '"';
// Trigger a user defined exception handler - preferably a logger
else
eventBus.trigger('error.dispatch', e);
}
};
}
/**
* Triggers an event
*
* @param String eventType The event type name.
* @param mix data What ever that is sent when the event is
* triggered, if anything at all.
*
* @returns Shift.EventBus
* @throws Unrecognized router type
* @throws Eternal loop found in Shift..
*/
this.trigger = function(eventType, data)
{
// Retrives matching routes
var routes = router.getRoutes(eventType, scope);
// Looping through all matched routes
for(var i = 0; i < routes.length; i++)
new Thread(routes[i], eventType, data, this).run();
return this;
};
}
// Declaring a global service manager
var serviceLocator = new Manager;
// Attiching services
serviceLocator.set('event-bus', new EventBus(Shift));
var
// Callback to notice when the document is ready
initiationCallback = function()
{
// Once the document is ready we can initiate
if(document.readyState === 'complete')
{
clearInterval(initiationInterval);
bootstrap();
}
},
// The bootstrap process
bootstrap = function()
{
var exceptions = {};
// Bootstrapping the modules
for(var module in Shift)
if(typeof Shift[module] === 'function')
try
{
Shift[module] = new Shift[module](serviceLocator);
}
// Logging wich modules that where unable to bootstrap and removes it
// from Shift to prevent future use.
catch(exception)
{
exceptions[module] = exception;
delete Shift[module];
}
// If an excpetion occurred during the bootstrap process in any of the
// modules then this will trigger an error event
for(var ns in exceptions)
serviceLocator.get('event-bus').trigger(
'error.bootstrap',
{
'Module' : ns,
'Exception': exceptions[ns]
});
// Once the bootstrap process has finished, an event declaring that
// shift is ready is triggerd
serviceLocator.get('event-bus').trigger('shift.ready');
},
// Initiation
initiationInterval = setInterval(initiationCallback, 10);
}
new Shift;