vaderjs 1.0.8 → 1.1.0
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/package.json +1 -1
- package/vader.js +5 -0
- package/vaderRouter.js +49 -32
package/package.json
CHANGED
package/vader.js
CHANGED
|
@@ -443,7 +443,11 @@ export const rf = (name, fn) => {
|
|
|
443
443
|
* @param {string} path
|
|
444
444
|
*/
|
|
445
445
|
|
|
446
|
+
let cache = {}
|
|
446
447
|
export const include = (path) => {
|
|
448
|
+
if(cache[path]){
|
|
449
|
+
return new Function(`return \`${cache[path]}\`;`)()
|
|
450
|
+
}
|
|
447
451
|
return fetch(`./${path}`)
|
|
448
452
|
.then((res) => {
|
|
449
453
|
if(res.status === 404){
|
|
@@ -452,6 +456,7 @@ export const include = (path) => {
|
|
|
452
456
|
return res.text()
|
|
453
457
|
})
|
|
454
458
|
.then((data) => {
|
|
459
|
+
cache[path] = data
|
|
455
460
|
return new Function(`return \`${data}\`;`)()
|
|
456
461
|
})
|
|
457
462
|
};
|
package/vaderRouter.js
CHANGED
|
@@ -63,34 +63,12 @@ class VaderRouter {
|
|
|
63
63
|
* Starts the router.
|
|
64
64
|
*/
|
|
65
65
|
start() {
|
|
66
|
-
|
|
66
|
+
|
|
67
|
+
if (window.location.hash === "") {
|
|
67
68
|
window.location.hash = this.starturl;
|
|
68
69
|
}
|
|
69
70
|
window.addEventListener("hashchange", () => {
|
|
70
|
-
|
|
71
|
-
? window.location.hash.substring(1).split("/")
|
|
72
|
-
: window.location.hash.substring(1);
|
|
73
|
-
// remove '' from array
|
|
74
|
-
hash = hash.filter((item) => item !== "");
|
|
75
|
-
const basePath = "/" + hash[0];
|
|
76
|
-
|
|
77
|
-
if (!this.routes[basePath] && !this.customerror) {
|
|
78
|
-
window.location.hash = this.starturl;
|
|
79
|
-
} else if (!this.routes[basePath] && this.customerror) {
|
|
80
|
-
const errBody = {
|
|
81
|
-
status: 404,
|
|
82
|
-
message: "Page not found",
|
|
83
|
-
};
|
|
84
|
-
const res = {
|
|
85
|
-
return: function (data) {
|
|
86
|
-
this.hooked = false;
|
|
87
|
-
},
|
|
88
|
-
render: function (selector, data) {
|
|
89
|
-
document.querySelector(selector).innerHTML = data;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
this.handleError("404", errBody, res);
|
|
93
|
-
}
|
|
71
|
+
this.handleRoute();
|
|
94
72
|
});
|
|
95
73
|
}
|
|
96
74
|
|
|
@@ -110,6 +88,41 @@ class VaderRouter {
|
|
|
110
88
|
this.errorHandlers[type] = callback;
|
|
111
89
|
this.customerror = true;
|
|
112
90
|
}
|
|
91
|
+
handleRoute() {
|
|
92
|
+
if (this.hooked) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
this.hooked = true;
|
|
96
|
+
const route = window.location.hash.substring(1);
|
|
97
|
+
this.currentUrl = route;
|
|
98
|
+
window.$CURRENT_URL = route;
|
|
99
|
+
window.$URL_PARAMS = {};
|
|
100
|
+
window.$URL_QUERY = {};
|
|
101
|
+
if (this.routes[route]) {
|
|
102
|
+
this.storedroutes.push(route);
|
|
103
|
+
const req = {
|
|
104
|
+
params: {},
|
|
105
|
+
query: {},
|
|
106
|
+
url: route,
|
|
107
|
+
method: "GET",
|
|
108
|
+
};
|
|
109
|
+
const res = {
|
|
110
|
+
return: function (data) {
|
|
111
|
+
this.hooked = false;
|
|
112
|
+
},
|
|
113
|
+
render: function (selector, data) {
|
|
114
|
+
document.querySelector(selector).innerHTML = data;
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
this.routes[route] === Function ? this.routes[route](req, res) : null;
|
|
118
|
+
} else {
|
|
119
|
+
if (this.customerror) {
|
|
120
|
+
this.handleError("404", route);
|
|
121
|
+
} else {
|
|
122
|
+
console.error("404: Route not found");
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
113
126
|
/**
|
|
114
127
|
*
|
|
115
128
|
* @param {*} type
|
|
@@ -209,6 +222,7 @@ class VaderRouter {
|
|
|
209
222
|
|
|
210
223
|
window.$URL_PARAMS = params;
|
|
211
224
|
window.$URL_QUERY = query;
|
|
225
|
+
window.$CURRENT_URL = window.location.hash.substring(1);
|
|
212
226
|
|
|
213
227
|
const res = {
|
|
214
228
|
return: function (data) {
|
|
@@ -313,19 +327,22 @@ class VaderRouter {
|
|
|
313
327
|
.join("/");
|
|
314
328
|
const regex = new RegExp("^" + parsedPath + "(\\?(.*))?$");
|
|
315
329
|
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
330
|
+
let hash = window.location.hash.split("#")[1]
|
|
331
|
+
? window.location.hash.split("#")[1]
|
|
332
|
+
: window.location.hash;
|
|
333
|
+
|
|
334
|
+
let basePath = "";
|
|
335
|
+
if (hash.length > 1) {
|
|
336
|
+
basePath = hash.split("/")[0] + "/" + hash.split("/")[1];
|
|
321
337
|
} else {
|
|
322
|
-
|
|
338
|
+
basePath = hash[0];
|
|
323
339
|
}
|
|
340
|
+
const route = basePath;
|
|
324
341
|
|
|
325
342
|
window.$CURRENT_URL = route;
|
|
326
343
|
window.$URL_PARAMS = {};
|
|
327
344
|
if (
|
|
328
|
-
|
|
345
|
+
window.location.hash.substring(1).match(regex) &&
|
|
329
346
|
this.routes[$CURRENT_URL]
|
|
330
347
|
) {
|
|
331
348
|
this.storedroutes.push(window.location.hash.substring(1));
|