Skip to content

Commit

Permalink
[ROUTER] public methods are now chainable (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyle-west authored Jun 10, 2019
1 parent eb3d073 commit 8d12443
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 🐉 Dragon Router

![](https://travis-ci.com/kyle-west/dragon-router.svg?branch=master)
[![](https://travis-ci.com/kyle-west/dragon-router.svg?branch=master)](https://travis-ci.com/kyle-west/dragon-router/branches)

Dragon Router is an ExpressJS-like client side router built from the ground up
on [debuggability](#debugging) and [simplicity](#derived-subpaths).
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dragon-router",
"version": "1.0.2",
"version": "1.1.0",
"description": "An ExpressJS-like client side router built from the ground up on debuggability and simplicity",
"authors": [
"Kyle West <[email protected]>"
Expand Down
29 changes: 19 additions & 10 deletions dist/dragon-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,21 +184,22 @@ class Router {
if (firstArg instanceof DerivedSubpath) {
this.subPaths[firstArg.name] = firstArg.callback;
} else if (firstArg instanceof RouteHandler) {
this.registerHandlers(firstArg);
this._registerHandlers(firstArg);
} else if (firstArg instanceof Function) { // middleware
this.globalActions = [...this.globalActions, firstArg, ...actions];
} else if (firstArg instanceof Array) {
firstArg.forEach(item => {
this.use(item, ...actions);
});
} else if (typeof firstArg === 'string' || firstArg instanceof RegExp) {
this.registerHandlers(new RouteHandler(firstArg, actions));
this._registerHandlers(new RouteHandler(firstArg, actions));
} else {
console.error(`Router::use - first argument of type not supported:`, firstArg)
}
return this;
}

registerHandlers (routeHandle, isRecursive = false) {
_registerHandlers (routeHandle, isRecursive = false) {
if (routeHandle.isRegex) {
this.registrar.push(routeHandle)
return
Expand All @@ -218,8 +219,8 @@ class Router {
}
});

this.registerHandlers(new RouteHandler(part.join('/'), routeHandle.actions))
this.registerHandlers(new RouteHandler(full.join('/'), routeHandle.actions))
this._registerHandlers(new RouteHandler(part.join('/'), routeHandle.actions))
this._registerHandlers(new RouteHandler(full.join('/'), routeHandle.actions))
return;
}

Expand All @@ -241,7 +242,7 @@ class Router {

let remainingRoute = route.replace('$:', ':');
if (remainingRoute.includes('$:')) {
this.registerHandlers(new RouteHandler(remainingRoute), true)
this._registerHandlers(new RouteHandler(remainingRoute), true)
}
let afterSections = rest.split('/').slice(1)
let routeWithRemovedSection = [before, ...afterSections].join('/');
Expand Down Expand Up @@ -284,6 +285,7 @@ class Router {
} else {
throw new Error(`ClilentRouter::registerOn(): a router is already attached: Router {#${window.attachedRouter.routerId}}`)
}
return this;
}

unregister () {
Expand All @@ -292,6 +294,7 @@ class Router {
window.removeEventListener('popstate', this._onPopState);
this.window.attachedRouter = null;
delete this.window.attachedRouter;
return this;
}

_onClick (e) {
Expand Down Expand Up @@ -336,9 +339,9 @@ class Router {
console.log(this.registrar[i])
}

this.fireGlobalActions(ctx);
this._fireGlobalActions(ctx);
fireMatchingActions(ctx);
this.pushState(ctx, replaceState);
this._pushState(ctx, replaceState);

if (this.debug) {
console.groupEnd(`Router {${this.routerId}}::[route match]: ${ctx.path}`);
Expand All @@ -350,7 +353,7 @@ class Router {
this.window.location = context.url;
}

pushState (context, replace = false) {
_pushState (context, replace = false) {
if (!context.isRecordedHistory) {
context.isRecordedHistory = true;
if (replace) {
Expand All @@ -361,7 +364,7 @@ class Router {
}
}

fireGlobalActions (context) {
_fireGlobalActions (context) {
let idx = 0;
let next = () => {
let fn = this.globalActions[idx++];
Expand All @@ -372,22 +375,28 @@ class Router {

redirect (path) {
this.evaluate(new Context(path, this.routerId), true);
return this;
}

navigate (path) {
this.evaluate(new Context(path, this.routerId));
return this;
}

back () {
this.window.history.back();
return this;
}

forward () {
this.window.history.forward();
return this;
}

start () {
if (!this.window) this.window = window; // assume client window if not specified
this.evaluate(new Context(this.window.location.href, this.routerId));
return this;
}
}

Expand Down
2 changes: 1 addition & 1 deletion dist/dragon-router.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8d12443

Please sign in to comment.