Skip to content

Commit

Permalink
Route parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
CMEONE committed Mar 9, 2021
1 parent 5ff88f9 commit e48673c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
14 changes: 14 additions & 0 deletions example/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ tApp.route("#/template", function(request) {
});
});

tApp.route("#/custom/<text>", function(request) {
tApp.render(`
<h1>` + request.data.text + `</h1>
<p>To customize the above text, change the url above to "#/custom/YOUR_TEXT_HERE"<br><br>See <a href="#/custom/` + request.data.text + `/subpage">subpage</a> here.</p>
`);
});

tApp.route("#/custom/<text>/subpage", function(request) {
tApp.render(`
<h1>Subpage For: ` + request.data.text + `</h1>
<p>To customize the above text, change the url above to "#/custom/YOUR_TEXT_HERE/subpage"<br><br>See <a href="#/custom/` + request.data.text + `">main page</a> here.</p>
`);
});

tApp.route("#/404", function(request) {
tApp.render(`
<h1>Error 404</h1>
Expand Down
1 change: 1 addition & 0 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ <h3>Navbar</h3>
<a href="#/about">About</a><br>
<a href="#/text">Sample Text</a><br>
<a href="#/template">Sample Template</a><br>
<a href="#/custom/Custom Text Here">Custom Text</a><br>
<a href="#/forbidden">Forbidden Page</a><br>
<a href="#/nonexistent">Nonexistent Page</a><br>
</nav>
Expand Down
48 changes: 42 additions & 6 deletions tApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class tApp {
static database;
static currentHash = "/";
static get version() {
return "v0.8.1";
return "v0.8.2";
}
static configure(params) {
if(params == null) {
Expand Down Expand Up @@ -387,20 +387,56 @@ class tApp {
if(hash == null || hash == "") {
hash = "/";
}
let splitHash = hash.split("/").filter(s => s != "");
if(tApp.config.ignoreRoutes != null && tApp.config.ignoreRoutes instanceof Array && tApp.config.ignoreRoutes.includes(hash)) {

} else if(tApp.config.forbiddenRoutes != null && tApp.config.forbiddenRoutes instanceof Array && tApp.config.forbiddenRoutes.includes(hash) && tApp.config.errorPages != null && tApp.config.errorPages.forbidden != null) {
tApp.updatePage(tApp.config.errorPages.forbidden);
} else if(tApp.routes[hash] != null) {
tApp.routes[hash]({
type: "GET",
referrer: tApp.currentHash,
data: null
});
} else if(tApp.config.forbiddenRoutes != null && tApp.config.forbiddenRoutes instanceof Array && tApp.config.forbiddenRoutes.includes(hash) && tApp.config.errorPages != null && tApp.config.errorPages.forbidden != null) {
tApp.updatePage(tApp.config.errorPages.forbidden);
} else if(tApp.config.errorPages != null && tApp.config.errorPages.notFound != null) {
tApp.updatePage(tApp.config.errorPages.notFound);
} else {
tApp.render("");
let routeHashes = Object.keys(tApp.routes);
let routeHash;
let routeParams = {};
for(let i = 0; i < routeHashes.length; i++) {
if(routeHash == null) {
let splitRoute = routeHashes[i].split("/").filter(s => s != "");
if(splitHash.length == splitRoute.length) {
let correctRoute = true;
for(let j = 0; j < splitHash.length; j++) {
if(correctRoute) {
if(splitHash[j] == splitRoute[j]) {

} else if(splitRoute[j][0] == "<" && splitRoute[j][splitRoute[j].length - 1] == ">") {
routeParams[splitRoute[j].substring(1, splitRoute[j].length - 1)] = decodeURI(splitHash[j]);
} else {
correctRoute = false;
}
}
}
if(correctRoute) {
routeHash = routeHashes[i];
} else {
routeParams = {};
}
}
}
}
if(routeHash != null) {
tApp.routes[routeHash]({
type: "GET",
referrer: tApp.currentHash,
data: routeParams
});
} else if(tApp.config.errorPages != null && tApp.config.errorPages.notFound != null) {
tApp.updatePage(tApp.config.errorPages.notFound);
} else {
tApp.render("");
}
}
tApp.currentHash = hash;
}
Expand Down

0 comments on commit e48673c

Please sign in to comment.