Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Lagendijk committed Feb 21, 2014
1 parent 46d6460 commit 9e3da48
Show file tree
Hide file tree
Showing 10 changed files with 300 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
bower_components
node_modules
42 changes: 39 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,40 @@
ui.router.stateHelper
=====================

# ui-router.stateHelper
A helper module for AngularUI Router, which allows you to define your states as an object tree.

## Installation
1. `bower install angular-ui-router.stateHelper`
2. Reference `stateHelper.min.js`.
3. Add a dependency on `ui.router.stateHelper` in your app module.

## Usage
``` javascript
angular.module('myApp', ['ui.router', 'ui.router.stateHelper'])
.config(function(stateHelperProvider){
stateHelperProvider.setNestedState({
name: 'root',
templateUrl: 'root.html',
children: [
{
name: 'contacts',
templateUrl: 'contacts.html',
children: [
{
name: 'list',
templateUrl: 'contacts.list.html'
}
]
},
{
name: 'products',
templateUrl: 'products.html',
children: [
{
name: 'list',
templateUrl: 'products.list.html'
}
]
}
]
});
});
```
36 changes: 36 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "angular-ui-router.stateHelper",
"main": "statehelper.js",
"version": "1.0.0",
"homepage": "https://github.com/marklagendijk/ui-router.stateHelper",
"authors": [
"Mark Lagendijk <[email protected]>"
],
"description": "A helper module for AngularUI Router, which allows you to define your states as an object tree.",
"keywords": [
"angular",
"helper",
"ui.router",
"ui-router",
"ui",
"router"
],
"license": "MIT",
"ignore": [
"**/.*",
"node_modules",
"package.json",
"gulpfile.js",
"bower_components",
"test",
"tests",
"config"
],
"dependencies": {
"angular": "~1.2.13",
"angular-ui-router": "~0.2.7"
},
"devDependencies": {
"angular-mocks": "~1.2.8"
}
}
9 changes: 9 additions & 0 deletions config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
testFiles: [
'bower_components/angular/angular.js',
'bower_components/angular-mocks/angular-mocks.js',
'bower_components/angular-ui-router/release/angular-ui-router.js',
'statehelper.min.js',
'test/**/*.js'
]
};
58 changes: 58 additions & 0 deletions config/karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
var config = require('./config.js');
module.exports = function(karmaConfig){
karmaConfig.set({

// base path, that will be used to resolve files and exclude
basePath: '../',


// frameworks to use
frameworks: ['jasmine'],


// list of files / patterns to load in the browser
files: config.testFiles,


// test results reporter to use
// possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
reporters: ['progress'],


// web server port
port: 9876,


// enable / disable colors in the output (reporters and logs)
colors: true,


// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: karmaConfig.LOG_WARN,


// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,


// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera (has to be installed with `npm install karma-opera-launcher`)
// - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`)
// - PhantomJS
// - IE (only Windows; has to be installed with `npm install karma-ie-launcher`)
browsers: ['Chrome'],


// If browser does not capture in given timeout [ms], kill it
captureTimeout: 60000,


// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false
});
};
23 changes: 23 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var gulp = require("gulp");
var ngmin = require('gulp-ngmin');
var uglify = require("gulp-uglify");
var rename = require("gulp-rename");
var karma = require("gulp-karma");

var config = require('./config/config.js');

gulp.task("test", ["minify"], function(){
gulp.src(config.testFiles)
.pipe(karma({
configFile: 'config/karma.conf.js',
action: 'run'
}));
});

gulp.task("minify", function(){
gulp.src("statehelper.js")
.pipe(ngmin())
.pipe(uglify())
.pipe(rename("statehelper.min.js"))
.pipe(gulp.dest("./"));
});
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"dependencies": {},
"devDependencies": {
"karma": "~0.11.9",
"karma-chrome-launcher": "~0.1.2",
"karma-jasmine": "~0.1.5",
"karma-firefox-launcher": "~0.1.3",
"karma-phantomjs-launcher": "~0.1.1",
"karma-requirejs": "~0.2.1",
"karma-script-launcher": "~0.1.0",
"gulp": "~3.3.0",
"gulp-uglify": "~0.1.0",
"gulp-karma": "0.0.1",
"gulp-rename": "~0.2.1",
"gulp-ngmin": "^0.1.2"
}
}
45 changes: 45 additions & 0 deletions statehelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* A helper module for AngularUI Router, which allows you to define your states as an object tree.
* @author Mark Lagendijk <[email protected]>
* @license MIT
*/
angular.module('ui.router.stateHelper', ['ui.router'])
.provider('stateHelper', function($stateProvider){
var self = this;

/**
* Recursively sets the states using $stateProvider.state.
* Child states are defined via a `children` property.
*
* 1. Recursively calls itself for all descendant states, by traversing the `children` properties.
* 2. Converts all the state names to dot notation, of the form `grandfather.father.state`.
* 3. Sets `parent` property of the descendant states.
*
* @param {Object} state - A regular ui.router state object.
* @param {Array} [state.children] - An optional array of child states.
*/
this.setNestedState = function(state){
fixStateName(state);
$stateProvider.state(state);

if(state.children && state.children.length){
state.children.forEach(function(childState){
childState.parent = state;
self.setNestedState(childState);
});
}
};

self.$get = angular.noop;

/**
* Converts the name of a state to dot notation, of the form `grandfather.father.state`.
* @param state
*/
function fixStateName(state){
if(state.parent){
state.name = state.parent.name + '.' + state.name;
}
}
})
;
1 change: 1 addition & 0 deletions statehelper.min.js

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

69 changes: 69 additions & 0 deletions test/statehelperSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* globals: beforeEach, describe, it, module, inject, expect */
describe('ui-router.stateHelper', function(){
var stateHelperProvider, $stateProvider, rootState, expectedState;

beforeEach(module('ui.router.stateHelper', function(_stateHelperProvider_, _$stateProvider_){
stateHelperProvider = _stateHelperProvider_;
$stateProvider = _$stateProvider_;
}));

beforeEach(inject(function(){
rootState = {
name: 'root',
children: [
{
name: 'login',
templateUrl: '/partials/views/login.html'
},
{
name: 'backup',
children: [
{
name: 'dashboard'
}
]
}
]
};

expectedState = {
name: 'root',
children: [
{
name: 'root.login',
templateUrl: '/partials/views/login.html'
},
{
name: 'root.backup',
children: [
{
name: 'root.backup.dashboard'
}
]
}
]
};

spyOn($stateProvider, 'state');
stateHelperProvider.setNestedState(rootState);
}));

describe('.setNestedState', function(){
it('should set each state', function(){
expect($stateProvider.state.callCount).toBe(4);
});


it('should convert names to dot notation, set parent references', function(){
// Since the states are objects which contain references to each other, we are testing the eventual
// root state object (and not the root state object as it is passed to $stateProvider.$state).
// Because of this we have to test everything at once

expectedState.children[0].parent = expectedState;
expectedState.children[1].parent = expectedState;
expectedState.children[1].children[0].parent = expectedState.children[1];

expect($stateProvider.state.argsForCall[0][0]).toEqual(expectedState);
});
});
});

0 comments on commit 9e3da48

Please sign in to comment.