Skip to content

Commit

Permalink
feat: add support for hashPrefix option
Browse files Browse the repository at this point in the history
Add support for hashPrefix option so hashbang paths

Fixes #1
  • Loading branch information
troch committed Jul 15, 2015
1 parent 59e1981 commit cdcd5e3
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 26 deletions.
18 changes: 13 additions & 5 deletions dist/browser/router5.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,8 @@ var makeState = function makeState(name, params, path) {

var Router5 = (function () {
function Router5(routes) {
var _this = this;

var opts = arguments[1] === undefined ? {} : arguments[1];

_classCallCheck(this, Router5);
Expand All @@ -487,7 +489,13 @@ var Router5 = (function () {
this.lastKnownState = null;
this.rootNode = routes instanceof RouteNode ? routes : new RouteNode('', '', routes);
this.activeComponents = {};
this.options = opts;
this.options = {
useHash: false,
hashPrefix: ''
};
Object.keys(opts).forEach(function (opt) {
return _this.options[opt] = opts[opt];
});
this.base = window.location.pathname.replace(/^\/$/, '');

return this;
Expand Down Expand Up @@ -796,7 +804,7 @@ var Router5 = (function () {
* @private
*/
value: function getLocation() {
return this.options.useHash ? window.location.hash.replace(/^#/, '') : window.location.pathname.replace(new RegExp('^' + this.base), '');
return this.options.useHash ? window.location.hash.replace(new RegExp('^#' + this.options.hashPrefix), '') : window.location.pathname.replace(new RegExp('^' + this.base), '');
}
}, {
key: 'buildUrl',
Expand All @@ -809,7 +817,7 @@ var Router5 = (function () {
* @return {String} The built URL
*/
value: function buildUrl(route, params) {
return (this.options.useHash ? window.location.pathname + '#' : this.base) + this.rootNode.buildPath(route, params);
return (this.options.useHash ? window.location.pathname + '#' + this.options.hashPrefix : this.base) + this.rootNode.buildPath(route, params);
}
}, {
key: 'buildPath',
Expand Down Expand Up @@ -843,7 +851,7 @@ var Router5 = (function () {
* @private
*/
value: function _transition(toState, fromState) {
var _this = this;
var _this2 = this;

if (!fromState) {
this.lastKnownState = toState;
Expand All @@ -862,7 +870,7 @@ var Router5 = (function () {
}

cannotDeactivate = fromStateIds.slice(i).reverse().map(function (id) {
return _this.activeComponents[id];
return _this2.activeComponents[id];
}).filter(function (comp) {
return comp && comp.canDeactivate;
}).some(function (comp) {
Expand Down
2 changes: 1 addition & 1 deletion dist/browser/router5.min.js

Large diffs are not rendered by default.

18 changes: 13 additions & 5 deletions dist/commonjs/router5.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ var makeState = function makeState(name, params, path) {

var Router5 = (function () {
function Router5(routes) {
var _this = this;

var opts = arguments[1] === undefined ? {} : arguments[1];

_classCallCheck(this, Router5);
Expand All @@ -45,7 +47,13 @@ var Router5 = (function () {
this.lastKnownState = null;
this.rootNode = routes instanceof _routeNode2['default'] ? routes : new _routeNode2['default']('', '', routes);
this.activeComponents = {};
this.options = opts;
this.options = {
useHash: false,
hashPrefix: ''
};
Object.keys(opts).forEach(function (opt) {
return _this.options[opt] = opts[opt];
});
this.base = window.location.pathname.replace(/^\/$/, '');

return this;
Expand Down Expand Up @@ -354,7 +362,7 @@ var Router5 = (function () {
* @private
*/
value: function getLocation() {
return this.options.useHash ? window.location.hash.replace(/^#/, '') : window.location.pathname.replace(new RegExp('^' + this.base), '');
return this.options.useHash ? window.location.hash.replace(new RegExp('^#' + this.options.hashPrefix), '') : window.location.pathname.replace(new RegExp('^' + this.base), '');
}
}, {
key: 'buildUrl',
Expand All @@ -367,7 +375,7 @@ var Router5 = (function () {
* @return {String} The built URL
*/
value: function buildUrl(route, params) {
return (this.options.useHash ? window.location.pathname + '#' : this.base) + this.rootNode.buildPath(route, params);
return (this.options.useHash ? window.location.pathname + '#' + this.options.hashPrefix : this.base) + this.rootNode.buildPath(route, params);
}
}, {
key: 'buildPath',
Expand Down Expand Up @@ -401,7 +409,7 @@ var Router5 = (function () {
* @private
*/
value: function _transition(toState, fromState) {
var _this = this;
var _this2 = this;

if (!fromState) {
this.lastKnownState = toState;
Expand All @@ -420,7 +428,7 @@ var Router5 = (function () {
}

cannotDeactivate = fromStateIds.slice(i).reverse().map(function (id) {
return _this.activeComponents[id];
return _this2.activeComponents[id];
}).filter(function (comp) {
return comp && comp.canDeactivate;
}).some(function (comp) {
Expand Down
18 changes: 13 additions & 5 deletions dist/umd/router5.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@

var Router5 = (function () {
function Router5(routes) {
var _this = this;

var opts = arguments[1] === undefined ? {} : arguments[1];

_classCallCheck(this, Router5);
Expand All @@ -52,7 +54,13 @@
this.lastKnownState = null;
this.rootNode = routes instanceof _RouteNode['default'] ? routes : new _RouteNode['default']('', '', routes);
this.activeComponents = {};
this.options = opts;
this.options = {
useHash: false,
hashPrefix: ''
};
Object.keys(opts).forEach(function (opt) {
return _this.options[opt] = opts[opt];
});
this.base = window.location.pathname.replace(/^\/$/, '');

return this;
Expand Down Expand Up @@ -361,7 +369,7 @@
* @private
*/
value: function getLocation() {
return this.options.useHash ? window.location.hash.replace(/^#/, '') : window.location.pathname.replace(new RegExp('^' + this.base), '');
return this.options.useHash ? window.location.hash.replace(new RegExp('^#' + this.options.hashPrefix), '') : window.location.pathname.replace(new RegExp('^' + this.base), '');
}
}, {
key: 'buildUrl',
Expand All @@ -374,7 +382,7 @@
* @return {String} The built URL
*/
value: function buildUrl(route, params) {
return (this.options.useHash ? window.location.pathname + '#' : this.base) + this.rootNode.buildPath(route, params);
return (this.options.useHash ? window.location.pathname + '#' + this.options.hashPrefix : this.base) + this.rootNode.buildPath(route, params);
}
}, {
key: 'buildPath',
Expand Down Expand Up @@ -408,7 +416,7 @@
* @private
*/
value: function _transition(toState, fromState) {
var _this = this;
var _this2 = this;

if (!fromState) {
this.lastKnownState = toState;
Expand All @@ -427,7 +435,7 @@
}

cannotDeactivate = fromStateIds.slice(i).reverse().map(function (id) {
return _this.activeComponents[id];
return _this2.activeComponents[id];
}).filter(function (comp) {
return comp && comp.canDeactivate;
}).some(function (comp) {
Expand Down
11 changes: 8 additions & 3 deletions modules/Router5.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ export default class Router5 {
this.lastKnownState = null
this.rootNode = routes instanceof RouteNode ? routes : new RouteNode('', '', routes)
this.activeComponents = {}
this.options = opts
this.options = {
useHash: false,
hashPrefix: ''
}
Object.keys(opts).forEach(opt => this.options[opt] = opts[opt])
this.base = window.location.pathname.replace(/^\/$/, '')

return this
Expand Down Expand Up @@ -280,7 +284,7 @@ export default class Router5 {
*/
getLocation() {
return this.options.useHash
? window.location.hash.replace(/^#/, '')
? window.location.hash.replace(new RegExp('^#' + this.options.hashPrefix), '')
: window.location.pathname.replace(new RegExp('^' + this.base), '')
}

Expand All @@ -292,7 +296,8 @@ export default class Router5 {
* @return {String} The built URL
*/
buildUrl(route, params) {
return (this.options.useHash ? window.location.pathname + '#' : this.base) + this.rootNode.buildPath(route, params)
return (this.options.useHash ? window.location.pathname + '#' + this.options.hashPrefix : this.base) +
this.rootNode.buildPath(route, params)
}

/**
Expand Down
15 changes: 8 additions & 7 deletions tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ router = new Router5([
defaultRoute: 'home'
})
.setOption('useHash', true)
.setOption('hashPrefix', '!')
.add(ordersRoute)
.addNode('home', '/home');

Expand All @@ -49,7 +50,7 @@ describe('router5', function () {
router.start().start();
expect(router.started).toBe(true);

expect(window.location.hash).toBe('#/home');
expect(window.location.hash).toBe('#!/home');
expect(router.getState()).toEqual({name: 'home', params: {}, path: '/home'});
});

Expand All @@ -60,7 +61,7 @@ describe('router5', function () {
it('should be able to navigate to routes', function () {
router.navigate('users.view', {id: 123});

expect(window.location.hash).toBe('#/users/view/123');
expect(window.location.hash).toBe('#!/users/view/123');
});

it('should throw an error if trying to navigate to an unknown route', function () {
Expand All @@ -78,7 +79,7 @@ describe('router5', function () {
router.addListener(listeners.global);

router.navigate('orders.pending');
expect(window.location.hash).toBe('#/orders/pending');
expect(window.location.hash).toBe('#!/orders/pending');
expect(listeners.global).toHaveBeenCalledWith(router.lastKnownState, previousState);
router.removeListener(listeners.global);
});
Expand Down Expand Up @@ -136,14 +137,14 @@ describe('router5', function () {
router.stop();
expect(router.started).toBe(false);
router.navigate('users.list');
expect(window.location.hash).toBe('#/orders/pending');
expect(window.location.hash).toBe('#!/orders/pending');
// Stopping again shouldn't throw an error
router.stop();
});

it('should not start with default route if current path matches an existing route', function () {
router.start();
expect(window.location.hash).toBe('#/orders/pending');
expect(window.location.hash).toBe('#!/orders/pending');
});

it('should invoke node listeners', function () {
Expand Down Expand Up @@ -206,7 +207,7 @@ describe('router5', function () {
}
});
router.navigate('users');
expect(window.location.hash).toBe('#/users/list');
expect(window.location.hash).toBe('#!/users/list');

// Can deactivate
router.deregisterComponent('users.list');
Expand All @@ -216,7 +217,7 @@ describe('router5', function () {
}
});
router.navigate('users');
expect(window.location.hash).toBe('#/users');
expect(window.location.hash).toBe('#!/users');
});

it('should warn when trying to register a component twice', function () {
Expand Down

0 comments on commit cdcd5e3

Please sign in to comment.