vaderjs 1.1.4 → 1.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/settings.json +3 -1
- package/images/router.png +0 -0
- package/images/state.png +0 -0
- package/index.js +12 -0
- package/jsconfig.json +14 -11
- package/logo.png +0 -0
- package/package.json +7 -4
- package/readme.md +144 -113
- package/tsconfig.json +18 -0
- package/vader.js +534 -431
- package/vaderRouter.js +93 -97
package/vaderRouter.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
// @ts-ignore
|
|
1
2
|
window.$URL_PARAMS = {};
|
|
3
|
+
// @ts-ignore
|
|
2
4
|
window.$URL_QUERY = {};
|
|
3
5
|
|
|
4
6
|
/**
|
|
@@ -93,23 +95,21 @@ class VaderRouter {
|
|
|
93
95
|
this.customerror = true;
|
|
94
96
|
}
|
|
95
97
|
handleRoute(method) {
|
|
96
|
-
if (this.hooked) {
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
this.hooked = true;
|
|
100
98
|
let route = window.location.hash.substring(1);
|
|
101
|
-
|
|
99
|
+
// @ts-ignore
|
|
102
100
|
window.$CURRENT_URL = route;
|
|
103
101
|
|
|
104
102
|
// remove query params from route
|
|
105
103
|
if (route.includes("?")) {
|
|
106
104
|
route = route.split("?")[0];
|
|
107
105
|
}
|
|
106
|
+
route = route.split("/")[0] + "/" + route.split("/")[1];
|
|
108
107
|
if (this.routes[route]) {
|
|
109
|
-
console.log("route", route);
|
|
110
108
|
this.storedroutes.push(route);
|
|
111
109
|
const req = {
|
|
112
|
-
|
|
110
|
+
// @ts-ignore
|
|
111
|
+
params: $URL_PARAMS ? $URL_PARAMS : {},
|
|
112
|
+
// @ts-ignore
|
|
113
113
|
query: $URL_QUERY ? $URL_QUERY : {},
|
|
114
114
|
url: route,
|
|
115
115
|
method: method ? method : "GET",
|
|
@@ -124,8 +124,6 @@ class VaderRouter {
|
|
|
124
124
|
};
|
|
125
125
|
if (typeof this.routes[route] === "function") {
|
|
126
126
|
this.routes[route](req, res);
|
|
127
|
-
} else {
|
|
128
|
-
console.error("Error: Route is not a function");
|
|
129
127
|
}
|
|
130
128
|
} else {
|
|
131
129
|
if (this.customerror) {
|
|
@@ -134,12 +132,6 @@ class VaderRouter {
|
|
|
134
132
|
} else {
|
|
135
133
|
console.error("404: Route not found");
|
|
136
134
|
}
|
|
137
|
-
|
|
138
|
-
// if route has no content to send return 500
|
|
139
|
-
if (this.routes[route] === undefined || this.hooked === false) {
|
|
140
|
-
this.handleError("500", route);
|
|
141
|
-
console.error("500: Route has no content");
|
|
142
|
-
}
|
|
143
135
|
}
|
|
144
136
|
}
|
|
145
137
|
/**
|
|
@@ -160,9 +152,9 @@ class VaderRouter {
|
|
|
160
152
|
}
|
|
161
153
|
/**
|
|
162
154
|
* @alias get
|
|
163
|
-
* @param {
|
|
164
|
-
* @param {
|
|
165
|
-
* @returns {
|
|
155
|
+
* @param {String} path
|
|
156
|
+
* @param {Function} callback
|
|
157
|
+
* @returns {boolean}
|
|
166
158
|
* @memberof VaderRouter
|
|
167
159
|
* @description Allows you to perform actions when path matches the current Route on visit.
|
|
168
160
|
*/
|
|
@@ -201,20 +193,13 @@ class VaderRouter {
|
|
|
201
193
|
path.includes(":") &&
|
|
202
194
|
window.location.hash.substring(1).split("?")[1]
|
|
203
195
|
) {
|
|
204
|
-
if (debug.enabled) {
|
|
205
|
-
debug.log(
|
|
206
|
-
[
|
|
207
|
-
`
|
|
208
|
-
Cannot use query params with path params ${path} ${
|
|
209
|
-
window.location.hash.substring(1).split("?")[1]
|
|
210
|
-
}`,
|
|
211
|
-
],
|
|
212
|
-
"assert"
|
|
213
|
-
);
|
|
214
|
-
}
|
|
215
|
-
|
|
216
196
|
return false;
|
|
217
197
|
}
|
|
198
|
+
/**
|
|
199
|
+
* @alias query
|
|
200
|
+
* @type {Object}
|
|
201
|
+
* @property {Object} params - The params object.
|
|
202
|
+
*/
|
|
218
203
|
const query = {};
|
|
219
204
|
|
|
220
205
|
const queryString = window.location.hash.substring(1).split("?")[1];
|
|
@@ -238,9 +223,11 @@ class VaderRouter {
|
|
|
238
223
|
url: window.location.hash.substring(1),
|
|
239
224
|
method: "GET",
|
|
240
225
|
};
|
|
241
|
-
|
|
226
|
+
// @ts-ignore
|
|
242
227
|
window.$URL_PARAMS = params;
|
|
228
|
+
// @ts-ignore
|
|
243
229
|
window.$URL_QUERY = query;
|
|
230
|
+
// @ts-ignore
|
|
244
231
|
window.$CURRENT_URL = window.location.hash.substring(1);
|
|
245
232
|
/**
|
|
246
233
|
* @alias render
|
|
@@ -251,23 +238,26 @@ class VaderRouter {
|
|
|
251
238
|
* @description Allows you to perform actions when the currentRoute changes.
|
|
252
239
|
*/
|
|
253
240
|
const res = {
|
|
254
|
-
|
|
255
|
-
this.hooked = false;
|
|
256
|
-
},
|
|
241
|
+
|
|
257
242
|
render: function (selector, data) {
|
|
258
243
|
document.querySelector(selector).innerHTML = data;
|
|
259
244
|
},
|
|
260
245
|
};
|
|
261
246
|
|
|
262
247
|
callback(req, res);
|
|
263
|
-
|
|
248
|
+
// @ts-ignore
|
|
264
249
|
return true;
|
|
265
250
|
}
|
|
266
251
|
|
|
267
252
|
this.hooked = false;
|
|
268
253
|
return false;
|
|
269
254
|
}
|
|
270
|
-
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* @alias kill
|
|
258
|
+
* @description Allows you to kill a route.
|
|
259
|
+
* @param {string} path
|
|
260
|
+
*/
|
|
271
261
|
kill(path) {
|
|
272
262
|
if (this.routes[path]) {
|
|
273
263
|
delete this.routes[path];
|
|
@@ -275,67 +265,38 @@ class VaderRouter {
|
|
|
275
265
|
}
|
|
276
266
|
/**
|
|
277
267
|
* @alias use
|
|
278
|
-
* @param {
|
|
268
|
+
* @param {String} pattern
|
|
269
|
+
* @param {Function} callback
|
|
279
270
|
* @returns {void}
|
|
280
271
|
* @memberof VaderRouter
|
|
281
272
|
* @description Allows you to set routes to be used throughout your spa.
|
|
282
273
|
*/
|
|
283
|
-
use(
|
|
284
|
-
const
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
if (part.startsWith(":")) {
|
|
290
|
-
paramNames.push(part.substring(1));
|
|
291
|
-
return "([^/]+)";
|
|
292
|
-
}
|
|
293
|
-
if (part.startsWith("*")) {
|
|
294
|
-
paramNames.push(part.substring(1));
|
|
295
|
-
return "(.*)";
|
|
296
|
-
}
|
|
297
|
-
if (part.startsWith("?")) {
|
|
298
|
-
queryNames.push(part.substring(1));
|
|
299
|
-
return "([^/]+)";
|
|
300
|
-
}
|
|
301
|
-
return part;
|
|
302
|
-
})
|
|
303
|
-
.join("/");
|
|
304
|
-
const regex = new RegExp("^" + parsedPath + "(\\?(.*))?$");
|
|
274
|
+
use(pattern, callback = null) {
|
|
275
|
+
const regexPattern = pattern
|
|
276
|
+
.replace(/:[^/]+/g, "([^/]+)") // Replace :param with a capturing group
|
|
277
|
+
.replace(/\//g, "\\/"); // Escape forward slashes
|
|
278
|
+
|
|
279
|
+
const regex = new RegExp("^" + regexPattern + "(\\?(.*))?$");
|
|
305
280
|
let params = {};
|
|
306
281
|
let query = {};
|
|
307
|
-
path = parsedPath;
|
|
308
282
|
|
|
309
|
-
//
|
|
310
|
-
|
|
283
|
+
// Get params
|
|
284
|
+
const match = window.location.hash.substring(1).match(regex);
|
|
285
|
+
|
|
286
|
+
if (match) {
|
|
311
287
|
this.storedroutes.push(window.location.hash.substring(1));
|
|
312
|
-
const matches =
|
|
313
|
-
params = {};
|
|
288
|
+
const matches = match.slice(1); // Extract matched groups
|
|
314
289
|
|
|
290
|
+
// Extract named params from the pattern
|
|
291
|
+
const paramNames = pattern.match(/:[^/]+/g) || [];
|
|
315
292
|
for (let i = 0; i < paramNames.length; i++) {
|
|
316
|
-
|
|
293
|
+
const paramName = paramNames[i].substring(1); // Remove the leading ":"
|
|
294
|
+
params[paramName] = matches[i];
|
|
317
295
|
}
|
|
318
|
-
if (
|
|
319
|
-
path.includes(":") &&
|
|
320
|
-
window.location.hash.substring(1).split("?")[1]
|
|
321
|
-
) {
|
|
322
|
-
if (debug.enabled) {
|
|
323
|
-
debug.log(
|
|
324
|
-
[
|
|
325
|
-
`
|
|
326
|
-
Cannot use query params with path params ${path} ${
|
|
327
|
-
window.location.hash.substring(1).split("?")[1]
|
|
328
|
-
}`,
|
|
329
|
-
],
|
|
330
|
-
"assert"
|
|
331
|
-
);
|
|
332
|
-
}
|
|
333
296
|
|
|
334
|
-
return false;
|
|
335
|
-
}
|
|
336
297
|
query = {};
|
|
337
298
|
|
|
338
|
-
const queryString =
|
|
299
|
+
const queryString = matches[paramNames.length]; // The last match is the query string
|
|
339
300
|
if (queryString) {
|
|
340
301
|
const queryParts = queryString.split("&");
|
|
341
302
|
for (let i = 0; i < queryParts.length; i++) {
|
|
@@ -344,20 +305,28 @@ class VaderRouter {
|
|
|
344
305
|
}
|
|
345
306
|
}
|
|
346
307
|
}
|
|
308
|
+
// @ts-ignore
|
|
347
309
|
window.$URL_PARAMS = params;
|
|
310
|
+
// @ts-ignore
|
|
348
311
|
window.$URL_QUERY = query;
|
|
312
|
+
|
|
349
313
|
if (callback) {
|
|
350
|
-
this.routes[
|
|
314
|
+
this.routes[pattern] = callback;
|
|
351
315
|
} else {
|
|
352
|
-
this.
|
|
353
|
-
|
|
316
|
+
this.routes[pattern] = true;
|
|
317
|
+
this.storedroutes.push(window.location.hash.substring(1));
|
|
354
318
|
}
|
|
355
319
|
}
|
|
356
320
|
|
|
321
|
+
/**
|
|
322
|
+
* @alias onload
|
|
323
|
+
* @param {Function} callback
|
|
324
|
+
*/
|
|
357
325
|
onload(callback) {
|
|
358
326
|
// await dom to be done make sure no new elements are added
|
|
359
327
|
if (
|
|
360
328
|
document.readyState === "complete" ||
|
|
329
|
+
// @ts-ignore
|
|
361
330
|
document.readyState === "loaded" ||
|
|
362
331
|
document.readyState === "interactive"
|
|
363
332
|
) {
|
|
@@ -367,8 +336,8 @@ class VaderRouter {
|
|
|
367
336
|
|
|
368
337
|
/**
|
|
369
338
|
* @alias on
|
|
370
|
-
* @param {
|
|
371
|
-
* @param {
|
|
339
|
+
* @param {String} path
|
|
340
|
+
* @param {Function} callback
|
|
372
341
|
* @returns {void}
|
|
373
342
|
* @memberof VaderRouter
|
|
374
343
|
* @description Allows you to perform actions when the currentRoute changes.
|
|
@@ -376,8 +345,16 @@ class VaderRouter {
|
|
|
376
345
|
*/
|
|
377
346
|
on(path, callback) {
|
|
378
347
|
window.addEventListener("hashchange", () => {
|
|
379
|
-
|
|
380
|
-
|
|
348
|
+
/**
|
|
349
|
+
* @alias paramNames
|
|
350
|
+
* @typedef {Array}
|
|
351
|
+
*/
|
|
352
|
+
const paramNames = new Array();
|
|
353
|
+
/**
|
|
354
|
+
* @alias queryNames
|
|
355
|
+
* @typedef {Array}
|
|
356
|
+
*/
|
|
357
|
+
const queryNames = new Array();
|
|
381
358
|
const parsedPath = path
|
|
382
359
|
.split("/")
|
|
383
360
|
.map((part) => {
|
|
@@ -409,18 +386,22 @@ class VaderRouter {
|
|
|
409
386
|
basePath = hash[0];
|
|
410
387
|
}
|
|
411
388
|
const route = basePath;
|
|
412
|
-
|
|
389
|
+
this.currentUrl = route;
|
|
390
|
+
// @ts-ignore
|
|
413
391
|
window.$CURRENT_URL = route;
|
|
392
|
+
// @ts-ignore
|
|
414
393
|
window.$URL_PARAMS = {};
|
|
415
394
|
if (
|
|
416
395
|
window.location.hash.substring(1).match(regex) &&
|
|
417
|
-
|
|
396
|
+
// @ts-ignore
|
|
397
|
+
this.routes[window.$CURRENT_URL]
|
|
418
398
|
) {
|
|
419
399
|
this.storedroutes.push(window.location.hash.substring(1));
|
|
420
400
|
const matches = window.location.hash.substring(1).match(regex);
|
|
421
401
|
const params = {};
|
|
422
402
|
|
|
423
403
|
for (let i = 0; i < paramNames.length; i++) {
|
|
404
|
+
// @ts-ignore
|
|
424
405
|
params[paramNames[i]] = matches[i + 1];
|
|
425
406
|
}
|
|
426
407
|
if (
|
|
@@ -441,6 +422,7 @@ class VaderRouter {
|
|
|
441
422
|
const queryParts = queryString.split("&");
|
|
442
423
|
for (let i = 0; i < queryParts.length; i++) {
|
|
443
424
|
const queryParam = queryParts[i].split("=");
|
|
425
|
+
// @ts-ignore
|
|
444
426
|
query[queryParam[0]] = queryParam[1];
|
|
445
427
|
}
|
|
446
428
|
}
|
|
@@ -451,25 +433,37 @@ class VaderRouter {
|
|
|
451
433
|
method: "POST",
|
|
452
434
|
};
|
|
453
435
|
const res = {
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* @alias send
|
|
439
|
+
* @param {String} selector
|
|
440
|
+
* @param {String} data
|
|
441
|
+
* @returns {void}
|
|
442
|
+
* @memberof VaderRouter
|
|
443
|
+
* @description Allows you to perform actions when the currentRoute changes.
|
|
444
|
+
* @example
|
|
445
|
+
* res.send('#root', '<h1>Hello World</h1>');
|
|
446
|
+
* */
|
|
457
447
|
send: function (selector, data) {
|
|
448
|
+
// @ts-ignore
|
|
458
449
|
document.querySelector(selector).innerHTML = data;
|
|
459
450
|
},
|
|
460
451
|
/**
|
|
461
452
|
* @alias render
|
|
462
|
-
* @param {
|
|
463
|
-
* @param {
|
|
453
|
+
* @param {String} selector
|
|
454
|
+
* @param {String} data
|
|
464
455
|
* @returns {void}
|
|
465
456
|
* @memberof VaderRouter
|
|
466
457
|
* @description Allows you to perform actions when the currentRoute changes.
|
|
467
458
|
*/
|
|
468
459
|
render: function (selector, data) {
|
|
460
|
+
// @ts-ignore
|
|
469
461
|
document.querySelector(selector).innerHTML = data;
|
|
470
462
|
},
|
|
471
463
|
};
|
|
464
|
+
// @ts-ignore
|
|
472
465
|
window.$URL_QUERY = query;
|
|
466
|
+
// @ts-ignore
|
|
473
467
|
window.$URL_PARAMS = params;
|
|
474
468
|
|
|
475
469
|
/**
|
|
@@ -481,6 +475,8 @@ class VaderRouter {
|
|
|
481
475
|
* @description Allows you to perform actions when the currentRoute changes.
|
|
482
476
|
*/
|
|
483
477
|
callback(req, res);
|
|
478
|
+
} else {
|
|
479
|
+
console.log("no route");
|
|
484
480
|
}
|
|
485
481
|
});
|
|
486
482
|
}
|