Skip to content

Commit 44c71d6

Browse files
committed
added router proper functionality
1 parent 07c4ab5 commit 44c71d6

File tree

2 files changed

+122
-34
lines changed

2 files changed

+122
-34
lines changed

OpenScript.js

Lines changed: 69 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var OpenScript = {
1313
* Current Prefix
1414
* @type {string}
1515
*/
16-
__prefix;
16+
__prefix = '';
1717

1818
/**
1919
* The routes Map
@@ -38,6 +38,19 @@ var OpenScript = {
3838
*/
3939
reset;
4040

41+
/**
42+
* The default path
43+
*/
44+
path = "";
45+
46+
/**
47+
* Default Action
48+
* @type {function}
49+
*/
50+
defaultAction = () => {
51+
alert('404 File Not Found')
52+
}
53+
4154
/**
4255
*
4356
*/
@@ -51,14 +64,32 @@ var OpenScript = {
5164
});
5265
}
5366

67+
/**
68+
* Sets the default path
69+
* @param {string} path
70+
* @returns
71+
*/
72+
basePath(path) {
73+
this.path = path;
74+
return this;
75+
}
76+
77+
/**
78+
* Sets the default action if a route is not found
79+
* @param {function} action
80+
*/
81+
default(action) {
82+
this.defaultAction = action;
83+
}
84+
5485
/**
5586
* Adds an action on URL path
5687
* @param {string} path
5788
* @param {function} action action to perform
5889
*/
5990
on(path, action) {
6091

61-
const paths =`${this.__prefix}/${path}`.split('/');
92+
const paths =`${this.path}/${this.__prefix}/${path}`.split('/');
6293

6394
let key = null;
6495
let map = this.map;
@@ -81,6 +112,20 @@ var OpenScript = {
81112
return this;
82113
}
83114

115+
/**
116+
* Used to add multiple routes to the same action
117+
* @param {Array<string>} paths
118+
* @param {function} action
119+
*/
120+
orOn(paths, action){
121+
122+
for(let path of paths) {
123+
this.on(path, action);
124+
}
125+
126+
return this;
127+
}
128+
84129
/**
85130
* Creates a prefix for a group of routes
86131
* @param {string} name
@@ -96,6 +141,7 @@ var OpenScript = {
96141
* Executes the actions based on the url
97142
*/
98143
listen(){
144+
99145
let url = new URL(window.location.href);
100146
this.params = {};
101147

@@ -116,6 +162,7 @@ var OpenScript = {
116162

117163
if(!next) {
118164
console.error(`${url.pathname} was not found`);
165+
this.defaultAction();
119166
return this;
120167
}
121168

@@ -138,6 +185,8 @@ var OpenScript = {
138185
*/
139186
to(path, qs = {}){
140187

188+
if(path[0] != '/') path = '/' + path;
189+
141190
let s = '';
142191

143192
for(let k in qs) {
@@ -159,7 +208,8 @@ var OpenScript = {
159208
* @returns string
160209
*/
161210
baseUrl(path = ''){
162-
return (new URL(window.location.href)).origin + '/' + path;
211+
return (new URL(window.location.href)).origin +
212+
( (this.path.length > 0) ? '/' + this.path: '' ) + '/' + path;
163213
}
164214

165215
/**
@@ -1373,6 +1423,11 @@ var OpenScript = {
13731423
*/
13741424
AutoLoader: class ClassLoader {
13751425

1426+
/**
1427+
* Keeps track of the files that have been loaded
1428+
*/
1429+
history = new Map();
1430+
13761431
/**
13771432
* The Directory or URL in which all JS files are located
13781433
*/
@@ -1412,32 +1467,15 @@ var OpenScript = {
14121467

14131468
/**
14141469
*
1415-
* @param {string} className script name without the .js.
1470+
* @param {string} fileName script name without the .js.
14161471
*/
1417-
async req(className){
1472+
async req(fileName){
14181473

1419-
let names = className.split(/\./);
1420-
let obj;
1421-
1422-
// check if the object already exists
1423-
for(let n of names){
1424-
1425-
if(!obj) {
1426-
obj = window[n];
1427-
continue;
1428-
}
1429-
1430-
if(!obj) {
1431-
obj = null;
1432-
break;
1433-
}
1434-
1435-
obj = obj[n];
1436-
}
1437-
1438-
if(obj) return obj;
1439-
1440-
let response = await fetch(`${this.dir}/${this.normalize(className)}${this.extension}?v=${this.version}`);
1474+
let names = fileName.split(/\./);
1475+
1476+
if(this.history.has(`${this.dir}.${fileName}`)) return this.history.get(`${this.dir}.${fileName}`);
1477+
1478+
let response = await fetch(`${this.dir}/${this.normalize(fileName)}${this.extension}?v=${this.version}`);
14411479

14421480
let classes = await response.text();
14431481

@@ -1453,7 +1491,6 @@ var OpenScript = {
14531491
let codeMap = new Map();
14541492

14551493
let prefixArray = [...names];
1456-
prefixArray.pop();
14571494

14581495
let prefix = prefixArray.join('.');
14591496
if(prefix.length > 0 && !/^\s+$/.test(prefix)) prefix += '.';
@@ -1507,7 +1544,7 @@ var OpenScript = {
15071544
}
15081545
else {
15091546
let replacement = inheritance[0].replace(original, parent);
1510-
console.log(k, arr[1]);
1547+
//console.log(k, arr[1]);
15111548

15121549
let c = arr[1].replace(inheritance[0], replacement);
15131550
arr[1] = c;
@@ -1525,14 +1562,15 @@ var OpenScript = {
15251562
}
15261563
}
15271564

1565+
this.history.set(`${this.dir}.${fileName}`, codeMap);
15281566

15291567
return codeMap;
15301568
}
15311569

1532-
async include(className){
1570+
async include(fileName){
15331571

15341572
try{
1535-
return await this.req(this.normalize(className));
1573+
return await this.req(fileName);
15361574
} catch(e) {}
15371575

15381576
return null;

docs/js/OpenScript.js

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var OpenScript = {
1313
* Current Prefix
1414
* @type {string}
1515
*/
16-
__prefix;
16+
__prefix = '';
1717

1818
/**
1919
* The routes Map
@@ -38,6 +38,19 @@ var OpenScript = {
3838
*/
3939
reset;
4040

41+
/**
42+
* The default path
43+
*/
44+
path = "";
45+
46+
/**
47+
* Default Action
48+
* @type {function}
49+
*/
50+
defaultAction = () => {
51+
alert('404 File Not Found')
52+
}
53+
4154
/**
4255
*
4356
*/
@@ -51,14 +64,32 @@ var OpenScript = {
5164
});
5265
}
5366

67+
/**
68+
* Sets the default path
69+
* @param {string} path
70+
* @returns
71+
*/
72+
basePath(path) {
73+
this.path = path;
74+
return this;
75+
}
76+
77+
/**
78+
* Sets the default action if a route is not found
79+
* @param {function} action
80+
*/
81+
default(action) {
82+
this.defaultAction = action;
83+
}
84+
5485
/**
5586
* Adds an action on URL path
5687
* @param {string} path
5788
* @param {function} action action to perform
5889
*/
5990
on(path, action) {
6091

61-
const paths =`${this.__prefix}/${path}`.split('/');
92+
const paths =`${this.path}/${this.__prefix}/${path}`.split('/');
6293

6394
let key = null;
6495
let map = this.map;
@@ -81,6 +112,20 @@ var OpenScript = {
81112
return this;
82113
}
83114

115+
/**
116+
* Used to add multiple routes to the same action
117+
* @param {Array<string>} paths
118+
* @param {function} action
119+
*/
120+
orOn(paths, action){
121+
122+
for(let path of paths) {
123+
this.on(path, action);
124+
}
125+
126+
return this;
127+
}
128+
84129
/**
85130
* Creates a prefix for a group of routes
86131
* @param {string} name
@@ -96,6 +141,7 @@ var OpenScript = {
96141
* Executes the actions based on the url
97142
*/
98143
listen(){
144+
99145
let url = new URL(window.location.href);
100146
this.params = {};
101147

@@ -116,6 +162,7 @@ var OpenScript = {
116162

117163
if(!next) {
118164
console.error(`${url.pathname} was not found`);
165+
this.defaultAction();
119166
return this;
120167
}
121168

@@ -138,6 +185,8 @@ var OpenScript = {
138185
*/
139186
to(path, qs = {}){
140187

188+
if(path[0] != '/') path = '/' + path;
189+
141190
let s = '';
142191

143192
for(let k in qs) {
@@ -159,7 +208,8 @@ var OpenScript = {
159208
* @returns string
160209
*/
161210
baseUrl(path = ''){
162-
return (new URL(window.location.href)).origin + '/' + path;
211+
return (new URL(window.location.href)).origin +
212+
( (this.path.length > 0) ? '/' + this.path: '' ) + '/' + path;
163213
}
164214

165215
/**

0 commit comments

Comments
 (0)