Comparing version 1.3.2 to 2.0.0
80
index.js
@@ -1,5 +0,4 @@ | ||
var on = require('dom-event'); | ||
var off = on.off; | ||
var routes = require('routes'); | ||
var EventEmitter = require('events').EventEmitter; | ||
var loc = new (require('location-bar'))(); | ||
var noop = function() {}; | ||
@@ -65,3 +64,5 @@ | ||
if( global.location ) { | ||
on(global, 'hashchange', this.onURL); | ||
loc.start({pushState: this.s.pushState!==undefined ? this.s.pushState : true}); | ||
this.hasPushState = loc.hasPushState(); | ||
loc.onChange(this.onURL); | ||
} | ||
@@ -74,53 +75,6 @@ | ||
p.sub = function(settings) { | ||
// remove all veriable parts from lastRoute | ||
var splitIdx1 = this.lastRoute.indexOf('*'); | ||
var splitIdx2 = this.lastRoute.indexOf(':'); | ||
var splitIdx; | ||
if(splitIdx1 === -1 && splitIdx2 === -1) { | ||
throw new Error('when creating a sub router the parent route should have a variable route using either : or *'); | ||
} else { | ||
splitIdx1 = splitIdx1 !== -1 ? splitIdx1 : this.lastRoute.length; | ||
splitIdx2 = splitIdx2 !== -1 ? splitIdx2 : this.lastRoute.length; | ||
splitIdx = splitIdx1 < splitIdx2 ? splitIdx1 : splitIdx2; | ||
} | ||
this.childFullRoute = this.lastRoute; | ||
this.childBaseRoute = this.lastRoute.substring(0, splitIdx - 1); | ||
settings.postHash = this.s.postHash + this.childBaseRoute; | ||
this.childRouter = new router(settings); | ||
this.emit('sub_create', { | ||
route: this.childFullRoute, | ||
router: this.childRouter | ||
}); | ||
return this.childRouter; | ||
}; | ||
p.destroySub = function(route) { | ||
// this.childBaseRoute | ||
if(this.childRouter && route.indexOf(this.childBaseRoute) !== 0) { | ||
this.childRouter.destroy(); | ||
this.emit('sub_destroy', { | ||
route: this.childFullRoute, | ||
router: this.childRouter | ||
}); | ||
this.childFullRoute = null; | ||
this.childBaseRoute = null; | ||
this.childRouter = null; | ||
} | ||
}; | ||
p.destroy = function() { | ||
if(global.location) { | ||
off(global, 'hashchange', this.onURL); | ||
location.stop(); | ||
} | ||
@@ -138,3 +92,3 @@ }; | ||
p.go = function(routeStr) { | ||
p.go = function(routeStr,options) { | ||
@@ -150,3 +104,3 @@ var routeData; | ||
newURL = this.s.postHash + routeStr; | ||
newURL = (this.hasPushState ? '' : this.s.postHash) + routeStr; | ||
routeData = this.getRouteData(routeStr) || this.getRouteData('404'); | ||
@@ -158,4 +112,5 @@ section = this.getSection(routeData); | ||
if( global.location && doURLChange ) { | ||
if(global.location.hash.replace(/^#/, '') != newURL) { | ||
global.location.hash = newURL; | ||
var url = this.hasPushState ? global.location.pathname : global.location.hash.replace(/^#/, ''); | ||
if(url != newURL) { | ||
loc.update(newURL,{trigger: (options && options.silent) ? false : true}); | ||
} else if(section.duplicate || !section.useURL) { | ||
@@ -191,3 +146,3 @@ // Check if duplicate is set. The check is done here since, onhashchange event triggers | ||
this.emit('route', { | ||
section: section.section || section, | ||
section: section, | ||
route: routeData, | ||
@@ -206,3 +161,2 @@ path: path | ||
this.lastRoute = routeData.route; | ||
this.destroySub(routeData.route); | ||
} | ||
@@ -241,4 +195,3 @@ | ||
p.onURL = function() { | ||
p.onURL = function(url) { | ||
var routeStr = '/'; | ||
@@ -248,11 +201,12 @@ var routeData; | ||
if( global.location && global.location.hash !== '' ) { | ||
if( global.location && url!==undefined ) { | ||
if (url.charAt(0) != '/') url = '/' + url; | ||
// if we've already looked at this url then just get out of this function | ||
if(global.location.hash === this.resolved) { | ||
if(url === this.resolved) { | ||
return; | ||
} | ||
this.resolved = global.location.hash; | ||
routeStr = global.location.hash.substr(1 + this.s.postHash.length); | ||
this.resolved = url; | ||
routeStr = this.hasPushState ? url : url.substr(1 + this.s.postHash.length); | ||
} | ||
@@ -259,0 +213,0 @@ |
{ | ||
"name": "bw-router", | ||
"version": "1.3.2", | ||
"version": "2.0.0", | ||
"description": "associate objects with routes. Used by bigwheel to route sections", | ||
@@ -29,3 +29,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"dom-event": "0.0.3", | ||
"location-bar": "^2.1.0", | ||
"routes": "^1.2.0" | ||
@@ -32,0 +32,0 @@ }, |
@@ -36,3 +36,3 @@ var test = require( 'tape' ); | ||
function(section, req) { | ||
t.equal( section, sectionGallery, 'section is /gallery/:image' ); | ||
t.equal( section.section, sectionGallery, 'section is /gallery/:image' ); | ||
t.equal( req.params.image, 'snake', 'param was correct' ); | ||
@@ -146,80 +146,2 @@ t.equal( req.route, '/gallery/:image', 'route was correct for gallery' ); | ||
test('test sub sections', function(t) { | ||
reset(); | ||
var subTests = [ | ||
function(section, req) { | ||
t.equal(section, '1', 'first section set'); | ||
process.nextTick( function() { | ||
subRouter.go('/somethingThatDoesntExist'); | ||
}); | ||
}, | ||
function(section, req) { | ||
t.equal(section, '404', 'sub 404d'); | ||
process.nextTick( function() { | ||
subRouter.go('/2'); | ||
}); | ||
}, | ||
function(section, req) { | ||
t.equal(section, '2', 'second section set'); | ||
process.nextTick( function() { | ||
router.go('/other'); | ||
}); | ||
}, | ||
function(section, req) { | ||
t.fail('should have not resolved sub section'); | ||
} | ||
]; | ||
var countInGallery = 0; | ||
router = require('../')( { | ||
'/': '/gallery/1', | ||
'/gallery/*': { section: 'gallery' }, | ||
'/other': { section: 'other' }, | ||
'404': { section: '404' } | ||
}); | ||
router.on('route', function(parentInfo) { | ||
var section = parentInfo.section; | ||
var req = parentInfo.route; | ||
if(section === 'gallery') { | ||
subRouter = router.sub( { | ||
'/1': { section: '1' }, | ||
'/2': { section: '2' }, | ||
'404': { section: '404' } | ||
}); | ||
subRouter.on('route', function(info) { | ||
subTests.shift()(info.section, info.route); | ||
}); | ||
t.equal(++countInGallery, 1, 'been in gallery only once'); | ||
t.ok(subRouter, 'received a sub router'); | ||
subRouter.init(); | ||
} else if(section === 'other') { | ||
t.pass('went in other parent section and may have destroyed child'); | ||
t.end(); | ||
} else { | ||
t.fail('resolved another url for parent: ' + section); | ||
t.end(); | ||
} | ||
}); | ||
router.init(); | ||
}); | ||
function reset() { | ||
@@ -226,0 +148,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12800
273
+ Addedlocation-bar@^2.1.0
+ Addedlocation-bar@2.1.0(transitive)
- Removeddom-event@0.0.3
- Removeddom-event@0.0.3(transitive)