Skip to content
This repository has been archived by the owner on Mar 26, 2018. It is now read-only.

Added support for Angular UI-Router #1331

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ var Generator = module.exports = function Generator(args, options) {
args: ['about']
});
}

if (this.env.options.uiRouter) {
this.invoke('angular:state', {
args: ['about']
});
}
});

this.pkg = require('../package.json');
Expand Down Expand Up @@ -250,6 +256,10 @@ Generator.prototype.askForModules = function askForModules() {
}, {
value: 'routeModule',
name: 'angular-route.js',
checked: false
}, {
value: 'uiRouterModule',
name: 'angular-ui-router.js',
checked: true
}, {
value: 'sanitizeModule',
Expand All @@ -271,6 +281,7 @@ Generator.prototype.askForModules = function askForModules() {
this.messagesModule = hasMod('messagesModule');
this.resourceModule = hasMod('resourceModule');
this.routeModule = hasMod('routeModule');
this.uiRouterModule = hasMod('uiRouterModule');
this.sanitizeModule = hasMod('sanitizeModule');
this.touchModule = hasMod('touchModule');

Expand All @@ -296,6 +307,11 @@ Generator.prototype.askForModules = function askForModules() {
angMods.push("'ngResource'");
}

if (this.uiRouterModule) {
angMods.push("'ui.router'");
this.env.options.uiRouter = true;
}

if (this.routeModule) {
angMods.push("'ngRoute'");
this.env.options.ngRoute = true;
Expand All @@ -319,6 +335,7 @@ Generator.prototype.askForModules = function askForModules() {

Generator.prototype.readIndex = function readIndex() {
this.ngRoute = this.env.options.ngRoute;
this.uiRouter = this.env.options.uiRouter;
this.indexFile = this.engine(this.read('app/index.html'), this);
};

Expand Down Expand Up @@ -349,6 +366,8 @@ Generator.prototype.createIndexHtml = function createIndexHtml() {
Generator.prototype.packageFiles = function packageFiles() {
this.coffee = this.env.options.coffee;
this.typescript = this.env.options.typescript;
this.uiRouter = this.env.options.uiRouter;

this.template('root/_bower.json', 'bower.json');
this.template('root/_bowerrc', '.bowerrc');
this.template('root/_package.json', 'package.json');
Expand Down
1 change: 1 addition & 0 deletions main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ Generator.prototype.createAppFile = function createAppFile() {
this.ngResource = this.env.options.ngResource;
this.ngSanitize = this.env.options.ngSanitize;
this.ngRoute = this.env.options.ngRoute;
this.uiRouter = this.env.options.uiRouter;
this.appTemplate('app', 'scripts/app');
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "generator-angular",
"version": "0.15.1",
"description": "Yeoman generator for AngularJS",
"description": "Yeoman generator for AngularJS (with support for Angular-UI router)",
"keywords": [
"yeoman-generator",
"scaffold",
Expand All @@ -25,6 +25,7 @@
"main",
"provider",
"route",
"state",
"service",
"templates",
"value",
Expand Down
31 changes: 31 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Available generators:
* [angular:directive](#directive)
* [angular:filter](#filter)
* [angular:route](#route)
* [angular:state](#state)
* [angular:service](#service)
* [angular:provider](#service)
* [angular:factory](#service)
Expand Down Expand Up @@ -92,6 +93,36 @@ yo angular:route myRoute --uri=my/route
Produces controller and view as above and adds a route to `app/scripts/app.js`
with URI `my/route`

### State
Generates a controller and view, and configures a state in `app/scripts/app.js` connecting them.

Example:
```bash
yo angular:state mystate
```

Produces `app/scripts/controllers/mystate.js`:
```javascript
angular.module('myMod').controller('MystateCtrl', function ($scope) {
// ...
});
```

Produces `app/views/mystate.html`:
```html
<p>This is the mystate view</p>
```

**Explicitly provide state URI**

Example:
```bash
yo angular:state myState --uri=my/state/uri
```

Produces controller and view as above and adds a state to `app/scripts/app.js`
with URI `my/state/uri`

### Controller
Generates a controller in `app/scripts/controllers`.

Expand Down
11 changes: 11 additions & 0 deletions state/USAGE
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Description:
Creates a new AngularJS State

Example:
yo angular:state thing [--coffee] [--uri=path_to/thing]

This will create:
app/scripts/controllers/thing.js
app/views/thing.html
And add routing to:
app.js
81 changes: 81 additions & 0 deletions state/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
'use strict';
var path = require('path');
var chalk = require('chalk');
var util = require('util');
var ScriptBase = require('../script-base.js');
var angularUtils = require('../util.js');


var Generator = module.exports = function Generator() {
ScriptBase.apply(this, arguments);
this.option('uri', {
desc: 'Allow a custom uri for routing',
type: String,
required: false
});

var coffee = this.env.options.coffee;
var typescript = this.env.options.typescript;
var bower = require(path.join(process.cwd(), 'bower.json'));
var match = require('fs').readFileSync(path.join(
this.env.options.appPath,
'scripts/app.' + (coffee ? 'coffee' : typescript ? 'ts': 'js')
), 'utf-8').match(/\.state/);

if (
bower.dependencies['ui.router'] ||
bower.devDependencies['ui.router'] ||
match !== null
) {
this.foundState = true;
}

this.hookFor('angular:controller');
this.hookFor('angular:view');
};

util.inherits(Generator, ScriptBase);

Generator.prototype.rewriteAppJs = function () {
var coffee = this.env.options.coffee;

if (!this.foundState) {
this.on('end', function () {
this.log(chalk.yellow(
'\nangular-ui-router is not installed. Skipping adding the state to ' +
'scripts/app.' + (coffee ? 'coffee' : 'js')
));
});
return;
}

this.uri = this.name;
if (this.options.uri) {
this.uri = this.options.uri;
}

var typescript = this.env.options.typescript;
var config = {
file: path.join(
this.env.options.appPath,
'scripts/app.' + (coffee ? 'coffee' : typescript ? 'ts': 'js')
),
needle: '.otherwise',
splicable: [
" url: '/" + this.uri + "',",
" templateUrl: 'views/" + this.name.toLowerCase() + ".html'" + (coffee ? "" : "," ),
" controller: '" + this.classedName + "Ctrl'" + (coffee ? "" : ","),
" controllerAs: '" + this.cameledName + "'"
]
};

if (coffee) {
config.splicable.unshift(".state '" + this.uri + "',");
}
else {
config.splicable.unshift(".state('" + this.uri + "', {");
config.splicable.push("})");
}

angularUtils.rewriteFile(config);
};
20 changes: 14 additions & 6 deletions templates/common/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@
<% } %>
<ul class="nav navbar-nav">
<li class="active"><a href="#/">Home</a></li>
<li><a ng-href="<% if (ngRoute) { %>#/about<% } else { %>#/<% } %>">About</a></li>
<% if (ngRoute) { %>
<li><a ng-href="#/about">About</a></li>
<% } else if (uiRouter) { %>
<li><a ui-sref="about">About</a></li>
<% } else { %>
<li><a ng-href="#/">About</a></li>
<% } %>
<li><a ng-href="#/">Contact</a></li>
</ul>
</div>
Expand All @@ -50,11 +56,13 @@
</div>

<div class="container">
<% if (ngRoute) {
%><div ng-view></div><%
} else {
%><div ng-include="'views/main.html'" ng-controller="MainCtrl"></div><%
} %>
<% if (ngRoute) { %>
<div ng-view></div>
<% } else if (uiRouter) { %>
<div ui-view></div>
<% } else { %>
<div ng-include="'views/main.html'" ng-controller="MainCtrl"></div>
<% } %>
</div>

<div class="footer">
Expand Down
3 changes: 2 additions & 1 deletion templates/common/root/_bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"angular-messages": "^<%= ngVer %>"<% } %><% if (resourceModule) { %>,
"angular-resource": "^<%= ngVer %>"<% } %><% if (routeModule) { %>,
"angular-route": "^<%= ngVer %>"<% } %><% if (sanitizeModule) { %>,
"angular-sanitize": "^<%= ngVer %>"<% } %><% if (touchModule) { %>,
"angular-ui-router": "^<%= ngVer %>"<% } %><% if (uiRouter) { %>,
"angular-sanitize": "^0.3.1"<% } %><% if (touchModule) { %>,
"angular-touch": "^<%= ngVer %>"<% } %>
},
"devDependencies": {
Expand Down
29 changes: 21 additions & 8 deletions templates/javascript/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,28 @@
* Main module of the application.
*/
angular
.module('<%= scriptAppName %>', [<%= angularModules %>])<% if (ngRoute) { %>
.config(function ($routeProvider) {
$routeProvider
.when('/', {
.module('<%= scriptAppName %>', [<%= angularModules %>])
<% if (ngRoute) { %>
.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl',
controllerAs: 'main'
})
.otherwise({
redirectTo: '/'
});
})
<% } else if (uiRouter) { %>
.config(function ($urlRouterProvider, $stateProvider) {
$stateProvider
.state('main', {
templateUrl: 'views/main.html',
controller: 'MainCtrl',
controllerAs: 'main'
})
.otherwise({
redirectTo: '/'
});
})<% } %>;

$urlRouterProvider.otherwise('/');
})
<% } %>;