-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjDamnSmallRouter.js
171 lines (171 loc) · 6.87 KB
/
jDamnSmallRouter.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
'use strict';
var jDamnSmallRouter;
(function (jDamnSmallRouter) {
class Router {
constructor() {
this._regexDuplicatePathId = new RegExp(/\/(:\w+)\[(?:09|AZ)]\/(?:.+\/)?(\1)(?:\[(?:09|AZ)]|\/|$)/g);
this._regexSearchVariables = new RegExp(/(?<=^|\/):(\w+)(?:\[(09|AZ)])?(?=\/|$)/g);
this._routes = [];
this._routeFunction403 = undefined;
this._routeFunction404 = undefined;
this._routing = false;
this._queue = [];
window.addEventListener("hashchange", this.CheckHash.bind(this));
}
RouteSpecialAdd(code, routeFunction) {
let returnValue = false;
switch (code) {
case 403: {
this._routeFunction403 = routeFunction;
returnValue = true;
break;
}
case 404: {
this._routeFunction404 = routeFunction;
returnValue = true;
break;
}
default: {
throw new RangeError();
}
}
return returnValue;
}
RouteAdd(path, routeFunction, available, routeFunction403) {
let returnValue = false;
if (path.match(this._regexDuplicatePathId)) {
throw new SyntaxError('Duplicate path id');
}
else {
let weight = 0;
const paths = path.split('/');
const cFL = paths.length;
for (let iFL = 0; iFL < cFL; iFL++) {
if (!paths[iFL].startsWith(':')) {
weight += 2 ^ (cFL - iFL - 1);
}
}
let regex = new RegExp('^' + path.replace(this._regexSearchVariables, function (_unused, name, type) {
let returnValue = '(?<' + name + '>[';
switch (type) {
case '09': {
returnValue += '\\d';
break;
}
case 'AZ': {
returnValue += 'a-zA-Z';
break;
}
default: {
returnValue += '\\w';
}
}
returnValue += ']+)';
return (returnValue);
}).replace(/\//g, '\\\/') + '$');
const reducedPath = path.replace(this._regexSearchVariables, ':$2');
if (!this._routes.find((route) => (reducedPath == route.path))) {
this._routes.push({
path: reducedPath,
match: regex,
weight: weight,
routeFunction: routeFunction,
available: available,
routeFunction403: routeFunction403
});
this._routes.sort((a, b) => ((a.weight > b.weight) ? -1 : ((b.weight > a.weight) ? 1 : 0)));
returnValue = true;
}
}
return returnValue;
}
RouteDel(path) {
let returnValue = false;
if (!path.match(this._regexDuplicatePathId)) {
throw new SyntaxError('Duplicate path id');
}
else {
const reducedPath = path.replace(this._regexSearchVariables, ':$2');
const index = this._routes.findIndex((route) => (reducedPath == route.path));
if (-1 < index) {
this._routes.splice(index, 1);
returnValue = true;
}
}
return returnValue;
}
Trigger(path) {
if ('#' + path != window.location.hash) {
window.location.hash = '#' + path;
}
}
async Route(path) {
var _a, _b, _c, _d;
this._routing = true;
let routeFunction = undefined;
let routePath = '';
let result = null;
for (const route of this._routes) {
if ((result = route.match.exec(path))) {
routePath = route.path;
let available = true;
if (route.available) {
available = false;
if ('function' === typeof route.available) {
if ('AsyncFunction' === route.available.constructor.name) {
available = await route.available(routePath, path, ((_a = result.groups) !== null && _a !== void 0 ? _a : {}));
}
else {
available = route.available(routePath, path, ((_b = result.groups) !== null && _b !== void 0 ? _b : {}));
}
}
}
if (available) {
routeFunction = route.routeFunction;
}
else if (route.routeFunction403) {
routeFunction = route.routeFunction403;
}
else if (this._routeFunction403) {
routeFunction = this._routeFunction403;
}
break;
}
}
if (!routeFunction || ('function' !== typeof routeFunction)) {
if (this._routeFunction404) {
routeFunction = this._routeFunction404;
}
}
if (routeFunction && ('function' === typeof routeFunction)) {
if ('AsyncFunction' === routeFunction.constructor.name) {
await routeFunction(routePath, path, ((_c = result === null || result === void 0 ? void 0 : result.groups) !== null && _c !== void 0 ? _c : {}));
}
else {
routeFunction(routePath, path, ((_d = result === null || result === void 0 ? void 0 : result.groups) !== null && _d !== void 0 ? _d : {}));
}
}
if (this._queue.length) {
await this.Route(this._queue.shift());
}
else {
this._routing = false;
}
}
async CheckHash() {
let hash = (window.location.hash.startsWith('#') ? window.location.hash.substring(1) : '');
if ('' != hash) {
if (this._routing) {
this._queue.push(hash);
}
else {
await this.Route(hash);
}
}
}
}
function Create() {
return new Router();
}
jDamnSmallRouter.Create = Create;
})(jDamnSmallRouter || (jDamnSmallRouter = {}));