-
Notifications
You must be signed in to change notification settings - Fork 2
/
router.js
135 lines (130 loc) · 4.31 KB
/
router.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
/*!
* RouterJS v1.5.0 (https://www.interart.com/)
* Copyright 2018-2023 Silvio Delgado (https://github.com/silviodelgado)
* Licensed under MIT (https://opensource.org/licenses/MIT)
* https://github.com/silviodelgado/routerjs
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory(root));
} else if (typeof exports === 'object') {
module.exports = factory(root);
} else {
root.Router = factory(root);
}
})(typeof global !== "undefined" ? global : this.window || this.global, function (root) {
'use strict';
let internal = {
current: null,
routes: [],
history: [],
run_before: null,
run_after: null
};
const run_before = (route) => {
if (typeof internal.run_before == 'function' && internal.run_before) {
internal.run_before.call(this);
}
if (route && route.run_before) {
if (typeof route.run_before == 'function' && route.run_before) {
route.run_before.call(this);
}
}
};
const run_after = (route) => {
if (route && route.run_after) {
if (typeof route.run_after == 'function' && route.run_after) {
route.run_after.call(this);
}
}
if (typeof internal.run_after == 'function' && internal.run_after) {
internal.run_after.call(this);
}
};
const router = {
getFragment: () => {
return window.location.hash.replace(/\/$/, '');
},
add: (route, handler, run_before, run_after) => {
if (typeof route == 'function') {
handler = route;
route = '';
}
internal.routes.push({
handler: handler,
route: route,
run_before,
run_after
});
return router;
},
beforeAll: (handler) => {
internal.run_before = handler;
return router;
},
afterAll: (handler) => {
internal.run_after = handler;
return router;
},
apply: (frg) => {
let fragment = frg || router.getFragment();
if (internal.current) return router;
for (let i = 0; i < internal.routes.length; i++) {
let matches = fragment.match(internal.routes[i].route);
if (matches) {
matches.shift();
internal.current = fragment;
if (!internal.history[fragment])
internal.history.push(fragment);
run_before(internal.routes[i]);
internal.routes[i].handler.apply({}, matches);
run_after(internal.routes[i]);
return router;
}
}
return router;
},
start: () => {
let current = router.getFragment();
window.onhashchange = function () {
let frg = router.getFragment();
if (!frg) {
internal.current = null;
current = null;
}
if (internal.current != frg) {
internal.current = null;
router.apply(frg);
}
}
if (current && !internal.current) {
router.apply();
}
return router;
},
navigate: (path, title) => {
document.title = title || document.title;
path = path.replace(/##/g, '#') || '';
window.location.hash = path ? '#' + path : '';
return router;
},
clearHash: () => {
window.location.hash = '#';
history.pushState(null, document.title, window.location.pathname + window.location.search);
},
back: () => {
internal.history.pop();
let path = internal.history.pop();
path = path || '';
window.location.hash = path;
return router;
},
checkFragment: (current) => {
return router.getFragment().indexOf(current) >= 0;
},
routes: () => {
return internal.routes;
}
};
return router;
});