-
Notifications
You must be signed in to change notification settings - Fork 0
/
kahe.js
398 lines (295 loc) · 10.5 KB
/
kahe.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*! kahe 0.7.4 */
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.kahe = factory());
}(this, (function () { 'use strict';
function extend(target) {
var sources = [], len = arguments.length - 1;
while ( len-- > 0 ) sources[ len ] = arguments[ len + 1 ];
if (Object.assign) {
return Object.assign.apply(Object, [ target ].concat( sources ));
}
sources.forEach(function (source) {
Object.keys(source).forEach(function (value, key) {
if (source.hasOwnProperty(key)) {
target[key] = value;
}
});
});
return target;
}
function isArray(value) {
return Array.isArray(value);
}
function isFunction(value) {
return typeof value === 'function';
}
function isObject(value) {
return typeof value === 'object' && !isArray(value);
}
function isUndefined(value) {
return typeof value === 'undefined';
}
function noop() {}
var Mediator = function Mediator() {
var views = [], len = arguments.length;
while ( len-- ) views[ len ] = arguments[ len ];
if (!isArray(views)) { views = [views]; }
this.views = views.map(function (view) { return isFunction(view) ? new view() : view; });
};
Mediator.prototype.init = function init (req, done) {
this.execute('init', req, done);
};
Mediator.prototype.resize = function resize (w, h) {
this.views.forEach(function(view) {
isFunction(view.resize) && view.resize(w, h);
});
};
Mediator.prototype.animateIn = function animateIn (req, done) {
this.resize(window.innerWidth, window.innerHeight);
this.execute('animateIn', req, done);
};
Mediator.prototype.animateOut = function animateOut (req, done) {
var this$1 = this;
this.execute('animateOut', req, function () {
this$1.destroy(req, noop);
done();
});
};
Mediator.prototype.destroy = function destroy (req, done) {
this.execute('destroy', req, done);
};
Mediator.prototype.execute = function execute (method, req, done) {
var total = 0;
var count = 0;
this.views.forEach(function(view) {
isFunction(view[method]) && total++;
});
if (!total) {
done();
return;
}
function oneDone() {
if (++count === total) { done(); }
}
this.views.forEach(function(view) {
if (isFunction(view[method])) {
view[method].call(view, req, oneDone);
}
});
};
var reserved = /^(hash|initial|keys|path|params|query|regex|view)$/;
var Route = function Route(path, config) {
var this$1 = this;
if (isArray(config)) {
config = {view: config};
}
this.keys = [];
this.view = config.view || config;
this.regex = toRegExp(path, this.keys);
Object.keys(config).forEach(function (key) {
if (!reserved.test(key)) { this$1[key] = config[key]; }
});
};
Route.prototype.match = function match (url) {
var this$1 = this;
var path = url.split(/[?#]/)[0];
var captures = path.match(this.regex);
if (!captures) { return; }
var params = [];
captures.forEach(function (item, i) {
var key = this$1.keys[i];
var value = decodeURIComponent(captures[i + 1]);
if (key) {
params[key] = convert(value);
} else if (value !== 'undefined') {
params.push(convert(value));
}
});
var clone = extend({ url: url, path: path, params: params }, this);
delete clone.regex;
delete clone.keys;
delete clone.view;
return clone;
};
function toRegExp(path, keys) {
if (path[0] !== '/') { path = '/' + path; }
path = path
.concat('/?')
.replace(/\/\(/g, '(?:/')
.replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?|\*/g,
function(match, slash, format, key, capture, optional) {
if (match === '*') {
keys.push(undefined);
return match;
}
keys.push(key);
slash = slash || '';
return ''
+ (optional ? '' : slash)
+ '(?:'
+ (optional ? slash : '')
+ (format || '')
+ (capture || '([^/]+?)')
+ ')'
+ (optional || '');
})
.replace(/([\/.])/g, '\\$1')
.replace(/\*/g, '(.*)');
return new RegExp('^' + path + '$', 'i');
}
function convert(value) {
if (value === 'true') {
value = true;
} else if (value === 'false') {
value = false;
} else if (value === 'null') {
value = null;
} else if (value === 'undefined') {
value = undefined;
} else if (isNaN(value) === false) {
value = Number(value);
}
return value;
}
var routes = [];
var beforeHooks = [];
var afterHooks = [];
var base;
var state;
var pending;
var incoming;
var outgoing;
var transition;
var kahe = {
before: function before(hook) {
if (isFunction(hook)) { hook = [hook]; }
beforeHooks.push.apply(beforeHooks, hook);
},
after: function after(hook) {
if (isFunction(hook)) { hook = [hook]; }
afterHooks.push.apply(afterHooks, hook);
},
route: function route(path, config) {
if (isUndefined(config)) {
navigate(path);
return;
}
routes.push( new Route(path, config) );
},
start: function start(options) {
if ( options === void 0 ) options = {};
base = window.location.protocol + '//' + window.location.host + (options.base || '/');
if (isArray(options.routes)) {
options.routes.forEach(function (route) {
route.path && kahe.route(route.path, route);
});
} else if (isObject(options.routes)) {
Object.keys(options.routes).forEach(function (key) {
kahe.route(key, options.routes[key]);
});
}
options.before && kahe.before(options.before);
options.after && kahe.after(options.after);
window.addEventListener('click', onclick);
window.addEventListener('touchstart', onclick);
window.addEventListener('popstate', onpopstate);
window.addEventListener('resize', onresize);
var href = '/' + window.location.href.replace(base, '');
href = routes.some(function (route) { return route.match(href); }) ? href : (options.fallback || '/');
navigate(href, {replace: true});
}
};
function onclick(event) {
if (event.defaultPrevented ||
event.ctrlKey ||
event.metaKey ||
event.shiftKey ||
event.button !== 0) { return; }
var el = event.target;
while (el && el.nodeName.toUpperCase() !== 'A') { el = el.parentNode; }
if (!el || !el.href) { return; }
if (el.target ||
el.href.indexOf(base) === -1 ||
el.getAttribute('rel') === 'external' ||
el.hasAttribute('download')) { return; }
event.preventDefault();
navigate(el.href);
}
function onpopstate() {
navigate(window.location.href, {replace: true});
}
function onresize() {
var width = window.innerWidth;
var height = window.innerHeight;
incoming && incoming.resize(width, height);
outgoing && outgoing.resize(width, height);
}
function navigate(url, options) {
if ( options === void 0 ) options = {};
url = url.replace(base, '');
if (url.charAt(0) !== '/') { url = '/' + url; }
var route;
var request;
for (var i = 0; i < routes.length; i++) {
route = routes[i];
request = route.match(url);
if (request) { break; }
}
if (isUndefined(request)) { return; }
if (state && state.path === request.path) { return; }
window.history[options.replace ? 'replaceState' : 'pushState']({}, '', url);
execute(route, request);
}
function execute(route, request) {
if (transition) {
pending = {route: route, request: request};
return;
}
if (isUndefined(outgoing)) {
outgoing = new Mediator();
request.initial = true;
}
transition = { from: state, to: request };
beforeHooks.forEach(function (fn) { return fn(transition); });
incoming = new Mediator(route.view);
state = request;
var init = function () {
return new Promise(function (resolve) { return incoming.init(request, resolve); });
};
var animateIn = function () {
return new Promise(function (resolve) { return incoming.animateIn(request, resolve); });
};
var animateOut = function () {
return new Promise(function (resolve) { return outgoing.animateOut(request, resolve); });
};
var done = function () {
afterHooks.forEach(function (fn) { return fn(transition); });
outgoing = incoming;
incoming = null;
transition = null;
if (pending) {
execute(pending.route, pending.request);
pending = null;
}
};
switch (transition.type) {
case 'out-in':
return animateOut()
.then(init)
.then(animateIn)
.then(done);
case 'in-out':
return init()
.then(animateIn)
.then(animateOut)
.then(done);
default:
return init()
.then(function () { return Promise.all([ animateIn(), animateOut() ]); })
.then(done);
}
}
return kahe;
})));