Comparing version 2.0.10 to 2.1.0
173
index.js
/** @module bigwheel */ | ||
var vm = require( 'bw-vm' ), | ||
viewmediator = require( 'bw-viewmediator' ), | ||
router = require( 'bw-router' ), | ||
on = require( 'dom-event' ); | ||
var vm = require('bw-vm'); | ||
var viewmediator = require('bw-viewmediator'); | ||
var router = require('bw-router'); | ||
var on = require('dom-event'); | ||
/** | ||
@@ -26,3 +27,4 @@ * When instantiating bigwheel you must pass in a setup function. | ||
* routes: { | ||
* | ||
* postHash: '#!', // this string is appended before the route. | ||
* // by default it's value is '#!' | ||
* '/': someSection, | ||
@@ -44,4 +46,2 @@ * '/someOther': someOtherSection, | ||
* // height is passed to all instantiated sections | ||
* postHash: '#!', // this string is appended before the route. | ||
* // by default it's value is '#!' | ||
* } | ||
@@ -54,9 +54,9 @@ * ``` | ||
*/ | ||
function bigwheel( settingsFunc ) { | ||
function bigwheel(settingsFunc) { | ||
if( !( this instanceof bigwheel ) ) | ||
return new bigwheel( settingsFunc ); | ||
if(!(this instanceof bigwheel)) | ||
return new bigwheel(settingsFunc); | ||
this.settingsFunc = settingsFunc; | ||
}; | ||
} | ||
@@ -71,11 +71,11 @@ bigwheel.prototype = { | ||
var onSettingComplete = function( settings ) { | ||
var onSettingComplete = function(settings) { | ||
var s = this.s = settings; | ||
if( s === undefined ) | ||
throw new Error( 'Your settings function must return a settings Object' ); | ||
if(s === undefined) | ||
throw new Error('Your settings function must return a settings Object'); | ||
if( s.routes === undefined ) | ||
throw new Error( 'Your settings object must define routes' ); | ||
if(s.routes === undefined) | ||
throw new Error('Your settings object must define routes'); | ||
@@ -85,12 +85,16 @@ s.autoResize = s.autoResize === undefined ? true : s.autoResize; | ||
// setup the router | ||
this.onRouteCallBack = settings.onRoute; | ||
settings.routes.onRoute = this.show.bind( this ); | ||
this.router = router( settings.routes ); | ||
this.router = settings.router || router(settings.routes); | ||
this.router.on('route', this.show.bind(this)); | ||
// handle adding and removing sub routers to the global | ||
// object for easier retrieval | ||
this.subFrameworks = {}; | ||
// setup the view manager | ||
this.vm = vm( this.s ); | ||
this.vm = vm(this.s); | ||
if( s.autoResize && global.innerWidth !== undefined && global.innerHeight !== undefined ) { | ||
// check if | ||
if(s.autoResize && global.innerWidth !== undefined && global.innerHeight !== undefined) { | ||
on( global, 'resize', this.onResize.bind( this ) ); | ||
on(global, 'resize', this.onResize.bind(this)); | ||
@@ -100,17 +104,70 @@ this.onResize(); | ||
if( s.initSection ) | ||
this.show( s.initSection.bind( undefined, this.router.init.bind( this.router ) ) ); | ||
// handle if there is an init section this should be shown even before | ||
// the router resolves | ||
if(s.initSection) | ||
this.show({section: s.initSection.bind(undefined, this.router.init.bind(this.router))}); | ||
else | ||
this.router.init(); | ||
}.bind( this ); | ||
}.bind(this); | ||
var rVal = this.settingsFunc( onSettingComplete ); | ||
var rVal = this.settingsFunc(onSettingComplete); | ||
if( rVal && rVal.then ) | ||
rVal.then( onSettingComplete ); | ||
else if( rVal && rVal.routes ) | ||
onSettingComplete( rVal ); | ||
// check if promises are used instead | ||
// it might be good to remove this since theres no | ||
// need for promises in this case | ||
if(rVal && rVal.then) | ||
rVal.then(onSettingComplete); | ||
// check if just an object was returned which has .routes | ||
else if(rVal && rVal.routes) | ||
onSettingComplete(rVal); | ||
return this; | ||
}, | ||
sub: function(name, routes) { | ||
var subFrameworks = this.subFrameworks; | ||
var sub; | ||
var settings; | ||
// if there's a subframework with this same name just return it | ||
if(subFrameworks[ name ]) { | ||
sub = subFrameworks[ name ]; | ||
// otherwise if we have routes then create a new one | ||
} else if(routes) { | ||
// if there is already a subframework with this name just return it | ||
settings = { | ||
routes: routes | ||
}; | ||
settings.router = this.router.sub(routes); | ||
sub = new bigwheel(function() { | ||
return settings; | ||
}); | ||
// if a name was passed save it for later reference | ||
if(name) { | ||
subFrameworks[ name ] = sub; | ||
// if a sub router gets destroyed we should check if its | ||
// for this sub framework and destroy it | ||
this.router.on('sub_destroy', function(info) { | ||
if(info.router === settings.router) { | ||
subFrameworks[ name ].destroy(); | ||
delete subFrameworks[ name ]; | ||
} | ||
}); | ||
} | ||
sub.init(); | ||
} | ||
return sub; | ||
}, | ||
/** | ||
@@ -123,8 +180,10 @@ * go can be called to go to another section. | ||
* ```javascript | ||
* framework.go( '/landing' ); | ||
* framework.go('/landing'); | ||
* ``` | ||
*/ | ||
go: function( to ) { | ||
go: function(to) { | ||
this.router.go( to ); | ||
this.router.go(to); | ||
return this; | ||
}, | ||
@@ -152,33 +211,39 @@ | ||
*/ | ||
resize: function( w, h ) { | ||
resize: function(w, h) { | ||
this.vm.resize( w, h ); | ||
this.vm.resize(w, h); | ||
}, | ||
show: function( content, data ) { | ||
show: function(info) { | ||
var section = info.section; | ||
var req = info.route; | ||
// this is the original router callback passed in | ||
if( this.onRouteCallBack ) | ||
this.onRouteCallBack( content, data ); | ||
if(this.onRouteCallBack) | ||
this.onRouteCallBack(section, req); | ||
// check if content is an array or function or object | ||
if( Array.isArray( content ) ) { | ||
// check if section is an array or function or object | ||
if(Array.isArray(section)) { | ||
var contents = []; | ||
var sections = []; | ||
for( var i = 0, len = content.length; i < len; i++ ) { | ||
for(var i = 0, len = section.length; i < len; i++) { | ||
if( typeof content[ i ] == 'object' ) | ||
contents[ i ] = content[ i ]; | ||
else if( typeof content[ i ] == 'function' ) | ||
contents[ i ] = new content[ i ]; | ||
if(typeof section[ i ] == 'object') { | ||
sections[ i ] = section[ i ]; | ||
} else if(typeof section[ i ] == 'function') { | ||
sections[ i ] = new section[ i ](); | ||
} | ||
} | ||
this.doShow( viewmediator.apply( undefined, contents ), data ); | ||
} else if( typeof content == 'object' ) { | ||
this.doShow(viewmediator.apply(undefined, sections), req); | ||
} else if(typeof section == 'object') { | ||
this.doShow( content, data ); | ||
} else if( typeof content == 'function' ) { | ||
this.doShow(section, req); | ||
} else if(typeof section == 'function') { | ||
this.doShow( new content, data ); | ||
this.doShow(new section(), req); | ||
} | ||
@@ -188,5 +253,5 @@ | ||
doShow: function( content, data ) { | ||
doShow: function(section, req) { | ||
this.vm.show( content, data ); | ||
this.vm.show(section, req); | ||
}, | ||
@@ -196,3 +261,3 @@ | ||
this.resize( global.innerWidth, global.innerHeight ); | ||
this.resize(global.innerWidth, global.innerHeight); | ||
} | ||
@@ -199,0 +264,0 @@ }; |
{ | ||
"name": "bigwheel", | ||
"version": "2.0.10", | ||
"version": "2.1.0", | ||
"description": "bigwheel is an unopinionated, minimalist frontend framework that manages application state", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "node test/index.js" | ||
}, | ||
@@ -9,0 +9,0 @@ "repository": { |
@@ -0,0 +0,0 @@ # bigwheel |
@@ -1,26 +0,23 @@ | ||
var test = require( 'tape' ); | ||
var bigwheel = require( './..' ); | ||
var test = require('tape'); | ||
var bigwheel = require('./..'); | ||
setTimeout( function() {}, 1000 ); | ||
setTimeout(function() {}, 1000); | ||
test( 'testing framework', function( t ) { | ||
test('testing single routes', function( t) { | ||
t.plan( 6 ); | ||
t.plan(6); | ||
if( global.location ) { | ||
reset(); | ||
global.location.hash = ''; | ||
} | ||
var framework = bigwheel(function(done) { | ||
var framework = bigwheel( function( done ) { | ||
done( { | ||
routes: { | ||
'/': { | ||
init: function( req, done ) { | ||
init: function(req, done) { | ||
t.equal( req.route, '/', '"/" init received "/"' ); | ||
t.equal(req.route, '/', '"/" init received "/"'); | ||
@@ -30,14 +27,14 @@ done(); | ||
animateIn: function( req, done ) { | ||
animateIn: function(req, done) { | ||
t.equal( req.route, '/', '"/" animateIn received "/"' ); | ||
t.equal(req.route, '/', '"/" animateIn received "/"'); | ||
done(); | ||
framework.go( '/about' ); | ||
framework.go('/about'); | ||
}, | ||
animateOut: function( req, done ) { | ||
animateOut: function(req, done) { | ||
t.equal( req.route, '/about', '"/" animateOut received "/about"' ); | ||
t.equal(req.route, '/about', '"/" animateOut received "/about"'); | ||
@@ -47,5 +44,5 @@ done(); | ||
destroy: function( req, done ) { | ||
destroy: function(req, done) { | ||
t.pass( '"/" destroy' ); | ||
t.pass('"/" destroy'); | ||
@@ -60,5 +57,5 @@ done(); | ||
init: function( req, done ) { | ||
init: function(req, done) { | ||
t.equal( req.route, '/about', '"/about" init received "/about"' ); | ||
t.equal(req.route, '/about', '"/about" init received "/about"'); | ||
@@ -68,7 +65,9 @@ done(); | ||
animateIn: function( req, done ) { | ||
animateIn: function(req, done) { | ||
t.equal( req.route, '/about', '"/about" animateIn received "/about"' ); | ||
t.equal(req.route, '/about', '"/about" animateIn received "/about"'); | ||
done(); | ||
done(); | ||
framework.destroy(); | ||
t.end(); | ||
} | ||
@@ -83,1 +82,88 @@ }; | ||
}); | ||
test('testing sub frameworks', function(t) { | ||
reset(); | ||
var SUB_ID = 'test sub'; | ||
var tests = [ | ||
function(req, done) { | ||
t.equal(req.params.id, '1', 'went into section 1'); | ||
process.nextTick( function() { | ||
sub.go('/2'); | ||
done(); | ||
}); | ||
}, | ||
function(req, done) { | ||
t.equal(req.params.id, '2', 'went into section 2'); | ||
sub.go('/something/not/good'); | ||
done(); | ||
}, | ||
function(req, done) { | ||
t.equal(req.route, '404', 'went into section 404'); | ||
framework.go('/end'); | ||
done(); | ||
} | ||
]; | ||
var sectionGallery = function() { | ||
return { | ||
init: function(req, done) { | ||
sub = framework.sub(SUB_ID, { | ||
'/': '/1', | ||
'/:id': { section: sectionSub, duplicate: true }, | ||
'404': { section: sectionSub } | ||
}); | ||
t.ok(sub, 'received a sub framework'); | ||
t.equal(sub, framework.sub(SUB_ID), 'sub returned same sub framework with key'); | ||
done(); | ||
} | ||
}; | ||
}; | ||
var sectionSub = function() { | ||
return { | ||
init: function(req, done) { | ||
tests.shift()(req, done); | ||
} | ||
}; | ||
}; | ||
var framework; | ||
var sub; | ||
framework = bigwheel(function(done) { | ||
return { | ||
routes: { | ||
'/': '/gallery/1', | ||
'/gallery/:id': { section: sectionGallery }, | ||
'/end': { init: function(req, done) { | ||
t.equal(framework.sub(SUB_ID), undefined, 'sub framework got destroyed'); | ||
t.end(); | ||
done(); | ||
}} | ||
} | ||
}; | ||
}); | ||
framework.init(); | ||
}); | ||
function reset() { | ||
if(global.location) { | ||
global.location.hash = ''; | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
16348
308
0
2