-
Notifications
You must be signed in to change notification settings - Fork 1
/
wrench.js
executable file
·357 lines (309 loc) · 11.2 KB
/
wrench.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
(function () {
var wrench = {},
routing = {routes: {}, lastHash: "/"},
evento = {},
w = window,
d = document;
wrench.VERSION = "0.0.1";
// beget object
var begetObject = function (o, properties) {
function F() {};
F.prototype = o;
var n = new F();
if (properties) {
for (var prop in properties) {
if (properties.hasOwnProperty(prop)) {
n[prop] = properties[prop];
}
}
}
return n;
};
var cloneObject = function (o) {
var c = {};
for (var prop in o) {
if (o.hasOwnProperty(prop)) {
c[prop] = o[prop];
}
}
return c;
};
// ## Events
// Cross browser compatible event handling
evento.add = function (element, eventType, handler) {
if (element.addEventListener) {
element.addEventListener(eventType, handler, false);
return true;
}
else if (element.attachEvent) {
if (eventType === 'DOMContentLoaded') {
element = w;
eventType = "load";
}
return element.attachEvent("on" + eventType, handler);
}
else {
return false;
}
};
// ## Routing
// Set a route and some params into the
// window.location.hash. This will build a string for
// window.location.hash into multiple route
// partials if applicable.
routing.route2Hash = function (route, parameters, isPartial) {
var hash = route,
i = 1,
params = cloneObject(parameters);
if (typeof isPartial === "undefined") { var isPartial = false; }
// Set params in new partial string to be added to hash.
// e.g. 'myroute/:param1/:param2?param3=42¶m4=foo
if (typeof params !== 'undefined') {
// First set the 'myroute/:param1/:param2' style paramters
// as specified by the route
if (route.indexOf("/") !== -1 && route.indexOf(":") !== -1) {
var r = route.split("/");
for (var j = 0; j < r.length; j++) {
if (r[j].indexOf(":") === 0) {
if (r[j].substr(1) in params) {
hash = hash.replace(r[j], params[r[j].substr(1)]);
delete params[r[j].substr(1)];
}
}
}
}
// The remaining params are just normal urlencoded params
// e.g. 'myroute?a=42&b=foo'
for (var param in params) {
if (params.hasOwnProperty(param)) {
if (i !== 1) { hash += "&"; }
else { hash += "?"; }
hash += param + "=" + params[param];
i++;
}
}
}
var newHash = "";
// window.location.hash is blank or it's a full route
if (w.location.hash === '' || w.location.hash === '#' || !isPartial) {
newHash = hash;
}
else {
// If the current route is a full route, the partial should be added, but replace it
var currentRoute = w.location.hash.replace("#", "").split(";")[0].split("?")[0];
if (currentRoute in routing.routes && !routing.routes[currentRoute].isPartial) {
newHash = hash;
}
else {
// find out if we should replace a partial in window.location.hash or just add to it
// window.location.hash does not include the partial route. Add the partial
var newRoute = w.location.hash,
routePrefix = route,
namedPartialPos = route.indexOf(":");
if (namedPartialPos !== -1) {
routePrefix = route.substr(0, namedPartialPos - 1);
}
var prefixRegexp = new RegExp(routePrefix + "[^;]*");
newHash = newRoute.replace(prefixRegexp, hash);
// nothing was found or replaced, just add the hash
if (newHash === w.location.hash && !prefixRegexp.test(newHash)) {
newHash = newHash + ";" + hash;
}
}
}
w.location.hash = newHash;
};
// find out if window.location.hash has changed
// make sure this function can handle multiple calls
routing.hashChanged = function () {
var changed = w.location.hash != routing.lastHash && w.location.hash != "#" + routing.lastHash;
return changed;
};
// find out if a specific partial of
// window.location.hash has changed
routing.partialChanged = function (route) {
if (routing.hashChanged()) {
var lastPartials = routing.lastHash.replace("#", "").split(";"),
currentPartials = w.location.hash.replace("#", "").split(";"),
lastPartial = "",
currentPartial = "";
for (var i = 0; i < lastPartials.length; i++) {
if (lastPartials[i].indexOf(route) === 0) {
lastPartial = lastPartials[i];
break;
}
}
for (var j = 0; j < currentPartials.length; j++) {
if (currentPartials[j].indexOf(route) === 0) {
currentPartial = currentPartials[j];
break;
}
}
return lastPartial !== currentPartial;
}
else { return false; }
};
// This function is called on 'onhashchange' and on load of the page
// Inspects the current window.location.hash to identify routes.
// Only handles a route if it has changed since last call to locate.
// If any of the routes are registered then call the matching funcion of that route.
// TODO: make aware of both partial and full routes - proberly just better variable naming
routing.locate = function () {
// Only locate if the hash hash changed
if (routing.hashChanged()) {
// The hash may consist of many routes called partial routes delimited by ';'.
// e.g. 'route1?a=b;route2?uu=32..."
var partials = w.location.hash.replace("#", "").split(";"),
numberOfPartials = partials.length;
if (numberOfPartials > 0 && partials[0] !== "") {
// Handle each partial route
for (var i = 0; i < numberOfPartials; i++) {
// Each route can have params encoded like url paramters using
// e.g "myroute?foo=32&bar=goo"
var query = partials[i].split("?"),
partial = query.shift(),
params = {},
route = "";
// First check if the is a simple route that can be found
// the in the routing registry directly e.g. 'myroute'
if (partial in routing.routes) {
route = partial;
}
// If it is not a simple route it may be on the form 'myroute/
else if (partial.indexOf("/") !== -1) {
for (var r in routing.routes) {
if (r.indexOf(":") !== -1) {
var n = r.indexOf(":") - 1;
if (r.substr(0, n) === partial.substr(0, n)) {
route = r;
break;
}
}
}
}
// Only call route function if the partial has actually changed
if (route !== "" && routing.partialChanged(partial)) {
// Handle ':foo' route params by matching the route and partial
// e.g. 'myroute/:var1/:var2' and 'myroute/42/77'
if (route.indexOf("/") !== -1 && route.indexOf(":") !== -1) {
var subRoutes = route.split("/"),
subPartial = partial.split("/");
for (var j = 0; j < subRoutes.length; j++) {
if (subRoutes[j].indexOf(":") === 0 && subPartial[j]) {
params[subRoutes[j].replace(":", "")] = subPartial[j];
}
}
}
// The partial may provide normal params using
// 'myroute?a=32' syntax.
if (query.length > 0 && typeof query[0] !== "undefined") {
var paramPairs = query[0].split("&"),
paramPairsLen = paramPairs.length;
for (var k = 0; k < paramPairsLen; k++) {
params[paramPairs[k].split("=")[0]] = paramPairs[k].split("=")[1];
}
}
// Call the located route
routing.routes[route].func(params);
}
}
}
else if ("/" in routing.routes) {
// Call the default route if none specified by the hash
routing.routes["/"].func();
}
}
routing.lastHash = w.location.hash;
};
// Remove a partial route from window.location.hash
routing.removePartial = function (partialRoute) {
if (w.location.hash.indexOf(partialRoute) !== -1) {
var partials = w.location.hash.replace("#", "").split(";"),
newHash = "";
for (var i = 0; i < partials.length; i++) {
if (partials[i].substr(0, partialRoute.length) !== partialRoute) {
if (newHash !== "") { newHash += ";"; }
newHash += partials[i];
}
}
w.location.hash = newHash;
}
};
// form elements on the page with '#' as the first
// character in the action attribute gets a
// onsubmit event added
routing.attachFormRoutes = function () {
var forms = d.forms;
var handler = function (event) {
var form = event.target,
elements = form.elements,
params = {};
for (var j = 0; j < elements.length; j++) {
params[elements[j].id] = elements[j].value;
}
routing.route2Hash(form.action.replace("#", ""), params);
event.preventDefault();
};
for (var i = 0; i < forms.length; i++) {
if (forms[i].action.indexOf("#") === 0) {
evento.add(forms[i], "submit", handler);
}
}
};
// the core wrench app object which wrench.appify uses
// to form a new application with the wrench methods
var wrenchApp = {
run: function (force) {
var app = this;
var bootstrap = function () {
routing.locate();
routing.attachFormRoutes();
if (typeof app.init === 'function') { app.init(); }
};
// attach event listeners
evento.add(d, "DOMContentLoaded", bootstrap);
// fall back to calling the locate function
// every 250 miliseconds if the browser
// doesn't have the onhashchange event
if ("onhashchange" in w) { evento.add(w, "hashchange", routing.locate); }
else { setInterval(routing.locate, 200); }
// Use force loading if wrench is loaded way
// after the load event has triggered
if (force) { bootstrap(); }
return app;
},
route2Hash: function (route, params) {
routing.route2Hash(route, params);
},
removePartial: function (partialRoute) {
routing.removePartial(partialRoute);
}
};
// ## public api
// Turn an object into a wrench application
wrench.appify = function (properties) {
return begetObject(wrenchApp, properties);
};
// connects a route to a function
// can be used like so:
// var users = route("users", function () {});
// or: var users = route("users").to(function () {});
// all routed functions will be called with a
// params hash as the only argument
w.route = function (route, isPartial) {
if (typeof isPartial === "undefined") { var isPartial = false; }
return {
to: function (func) {
routing.routes[route] = {func: func, isPartial: isPartial};
return function (params) {
routing.route2Hash(route, params, isPartial);
func.apply(null, arguments);
};
}
};
};
w.routePartial = function (route, func) {
return w.route(route, true);
};
w.wrench = wrench;
}());