vaderjs 1.0.9 → 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.
Files changed (3) hide show
  1. package/package.json +1 -1
  2. package/vader.js +5 -0
  3. package/vaderRouter.js +38 -30
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vaderjs",
3
- "version": "1.0.9",
3
+ "version": "1.1.0",
4
4
  "description": "A Reactive Framework for Single-Page Applications (SPA)",
5
5
  "main": "vader.js",
6
6
  "scripts": {
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
@@ -64,39 +64,11 @@ class VaderRouter {
64
64
  */
65
65
  start() {
66
66
 
67
- if (!this.routes[window.location.hash.substring(1)]) {
67
+ if (window.location.hash === "") {
68
68
  window.location.hash = this.starturl;
69
69
  }
70
70
  window.addEventListener("hashchange", () => {
71
- let hash = window.location.hash.substring(1).split("/")
72
- ? window.location.hash.substring(1).split("/")
73
- : window.location.hash.substring(1);
74
- // remove '' from array
75
- hash = hash.filter((item) => item !== "");
76
- let basePath = "";
77
- if (hash.length > 1) {
78
- basePath = hash[0] + "/" + hash[1];
79
- } else {
80
- basePath = hash[0];
81
- }
82
-
83
- if (!this.routes[basePath] && !this.customerror) {
84
- window.location.hash = this.starturl;
85
- } else if (!this.routes[basePath] && this.customerror) {
86
- const errBody = {
87
- status: 404,
88
- message: "Page not found",
89
- };
90
- const res = {
91
- return: function (data) {
92
- this.hooked = false;
93
- },
94
- render: function (selector, data) {
95
- document.querySelector(selector).innerHTML = data;
96
- }
97
- }
98
- this.handleError("404", errBody, res);
99
- }
71
+ this.handleRoute();
100
72
  });
101
73
  }
102
74
 
@@ -116,6 +88,41 @@ class VaderRouter {
116
88
  this.errorHandlers[type] = callback;
117
89
  this.customerror = true;
118
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
+ }
119
126
  /**
120
127
  *
121
128
  * @param {*} type
@@ -215,6 +222,7 @@ class VaderRouter {
215
222
 
216
223
  window.$URL_PARAMS = params;
217
224
  window.$URL_QUERY = query;
225
+ window.$CURRENT_URL = window.location.hash.substring(1);
218
226
 
219
227
  const res = {
220
228
  return: function (data) {