-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
247 lines (206 loc) · 6.65 KB
/
index.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
/** @module bigwheel */
var vm = require('bw-vm');
var viewmediator = require('bw-viewmediator');
var router = require('bw-router');
var on = require('dom-event');
var EventEmitter = require('events').EventEmitter;
/**
* When instantiating bigwheel you must pass in a setup function.
*
* In this function you may do any preparation that must be done for your
* application such as creating a global Canvas element or something else.
*
* The setup function must either return a settings object for bigwheel or
* this function must receive a callback which you will call with the settings
* object. Furthermore you can pass back a promise from this settings function
* which will receive the settings object.
*
* The following documents what can be passed in the settings object:
* ```javascript
* {
* ///// REQUIRED /////
*
* // routes defines all the routes for your website it can also define a
* // 404 section which will be opened if the route is incorrect
* routes: {
* postHash: '#!', // this string is appended before the route.
* // by default it's value is '#!'
* '/': someSection,
* '/someOther': someOtherSection,
* '404': sectionFourOhFour
* },
*
* ///// OPTIONAL /////
* initSection: preSection, // this could be a section that is run always
* // before routes are even evaluated. This is
* // usefulf for site preloaders or landing pages
* // such as age verification (something the user
* // must see)
*
* autoResize: true, // by default this value is true. When this value is
* // true a resize listener is added to the window
* // whenever the window changes size it's width and
* // height is passed to all instantiated sections
* }
* ```
*
* @class bigwheel
* @param {Function} settingsFunc This settings function will be used to
* initialize bigwheel.
*/
function bigwheel(settingsFunc) {
if(!(this instanceof bigwheel))
return new bigwheel(settingsFunc);
this.settingsFunc = settingsFunc;
EventEmitter.call(this);
}
bigwheel.prototype = Object.create(EventEmitter.prototype);
/**
* init must be called to start the framework. This was done to allow for
* a developer to have full control of when bigwheel starts doing it's thing.
*/
bigwheel.prototype.init = function() {
var onSettingComplete = function(settings) {
var s = this.s = settings;
if(s === undefined)
throw new Error('Your settings function must return a settings Object');
if(s.routes === undefined)
throw new Error('Your settings object must define routes');
s.autoResize = s.autoResize === undefined ? true : s.autoResize;
this.previousRoute = undefined;
this.depth = [];
this.vms = [];
this.routes = {};
this.parseRoutes(settings.routes);
// setup the router
this.router = router(this.routes);
this.router.on('route', this.show.bind(this));
// Re-dispatch routes
this.router.on('route',this.emit.bind(this,'route'));
// check if
if(s.autoResize && global.innerWidth !== undefined && global.innerHeight !== undefined) {
on(global, 'resize', this.onResize.bind(this));
this.onResize();
}
// handle if there is an init section this should be shown even before
// the router resolves
if(s.initSection)
this.show({section: s.initSection.bind(undefined, this.router.init.bind(this.router))});
else
this.router.init();
}.bind(this);
var rVal = this.settingsFunc(onSettingComplete);
// check if promises are used instead
// it might be good to remove this since theres no
// need for promises in this case
if(rVal && rVal.then)
rVal.then(onSettingComplete);
// check if just an object was returned which has .routes
else if(rVal && rVal.routes)
onSettingComplete(rVal);
return this;
};
bigwheel.prototype.parseRoutes = function(routes,prefix) {
var depth = (prefix || '').split('/').length;
if (this.vms.length<depth) this.vms.push(vm(this.s));
prefix = prefix || "";
for (var key in routes) {
if (key.charAt(0)==='/') {
if (prefix) routes[key].parent = prefix;
this.routes[prefix+key] = routes[key];
if (routes[key].routes) {
this.parseRoutes(routes[key].routes,prefix+key);
delete routes[key].routes;
}
} else {
this.routes[key] = routes[key];
}
}
};
/**
* go can be called to go to another section.
*
* @param {String} to This is the route you want to go to.
*
* @example
* ```javascript
* framework.go('/landing');
* ```
*/
bigwheel.prototype.go = function(to,options) {
this.router.go(to,options);
return this;
};
/**
* Destroys bighweel
*/
bigwheel.prototype.destroy = function() {
this.router.removeAllListeners('route');
this.router.destroy();
};
/**
* Resize can be called at any time. The values passed in for
* width and height will be passed to the currently instantiated
* sections.
*
* If `autoResize` was not passed in or it was true then resize
* will automatically be called when the window of the browser
* resizes.
*
* @param {Number} w width value you'd like to pass to the sections
* @param {Number} h height value you'd like to pass to the sections
*/
bigwheel.prototype.resize = function(w, h) {
for (var i=0; i<this.vms.length; i++) {
this.vms[i].resize(w,h);
}
};
bigwheel.prototype.show = function(info) {
var section = info.section;
var req = info.route || {};
req.previous = this.previousRoute;
req.framework = this;
if (req.route) {
var depth = [this.rebuildRoute(req.route,info.path)];
var views = [section.section || section];
while (section.parent) {
depth.unshift(this.rebuildRoute(section.parent,info.path));
section = this.routes[section.parent];
views.unshift(section.section || section);
}
var prevDepth = this.depth;
this.depth = depth;
var total = Math.max(prevDepth.length,depth.length);
for (var i=0; i<total; i++) {
if (i>depth.length-1) {
this.vms[i].clear(req);
} else if (prevDepth[i]!=depth[i]) {
this.vms[i].show(this.parseSection(views[i]),req);
}
}
} else {
this.vms[0].show(this.parseSection(section.section || section),req);
}
this.previousRoute = info.route;
};
bigwheel.prototype.rebuildRoute = function(route,path) {
var path = path.split('/')
path.length = route.split('/').length;
return path.join('/');
};
bigwheel.prototype.parseSection = function(section) {
if (Array.isArray(section)) {
for (var i=0; i<section.length; i++) {
if (typeof section[i] == 'function') section[i] = new section[i]();
}
return viewmediator.apply(undefined,section);
} else if (typeof section == 'function') {
return new section();
} else {
return section;
}
};
bigwheel.prototype.onResize = function() {
this.resize(global.innerWidth, global.innerHeight);
};
module.exports = bigwheel;