Skip to content

Commit 8b15899

Browse files
committed
change: convert to TypeScript and move definition to default property.
1 parent 5a73361 commit 8b15899

11 files changed

+560
-397
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
src/**/*.js
2+
test/**/*.js

README.md

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,36 +25,39 @@ var app = angular.module('myApp', ['ng', 'ui.router.default']);
2525
Defining Default Child State
2626
----------------------------
2727

28-
In your state definition for an abstract state, set the `abstract` property to the name of a child state (relative or absolute).
29-
The child state name can be provided statically as a string dynamically as a function callback.
28+
In your state definition for an abstract state, add a `default` property with the name of a child state (relative or absolute).
29+
The child state name can be provided statically as a string or dynamically as a function callback.
3030

3131
When a state transtion targets this abstract state, it will be redirected to the default child state instead.
3232

3333
```javascript
3434
$stateProvider
3535
.state('parent', {
36-
abstract: '.index',
36+
abstract: true,
37+
default: '.index',
3738
template: '<ui-view/>'
3839
})
3940
.state('parent.index', {
40-
...
41+
// ...
4142
})
4243
.state('parent.page2', {
43-
...
44+
// ...
4445
})
4546
.state('another', {
46-
abstract: ['$rootScope', function($rootScope) {
47+
abstract: true,
48+
default: ['$rootScope', function($rootScope) {
4749
return $rootScope.edit ? '.edit' : '.display';
4850
}]
4951
})
5052
.state('another.display', {
51-
...
53+
// ...
5254
})
5355
.state('another.edit', {
54-
...
56+
// ...
5557
})
5658
.state('anotherWithPromise',{
57-
abstract: ['$q',function($q){
59+
abstract: true,
60+
default: ['$q',function($q){
5861
var defer = $q.defer();
5962
asyncFunctionThatReturnsPromise().then(function(){
6063
defer.resolve('anotherWithPromise.details');
@@ -63,10 +66,26 @@ $stateProvider
6366
}]
6467
})
6568
.state('anotherWithPromise.details',{
66-
...
69+
// ...
70+
})
71+
```
72+
73+
#### Older version (< 0.0.5)
74+
75+
Older versions of this module specified the default state by assigning it to the `abstract` property:
76+
77+
```javascript
78+
$stateProvider
79+
.state('parent', {
80+
abstract: '.index',
81+
template: '<ui-view/>'
6782
})
83+
// ...
6884
```
6985

86+
This behavior is still supported, but is **deprecated**, because it causes TypeScript conflicts. It is recommended
87+
that the `{ abstract: true, default: '.index' }` format is used instead.
88+
7089
Using Default Child State
7190
-------------------------
7291

gulpfile.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var git = require('gulp-git');
44
var bump = require('gulp-bump');
55
var filter = require('gulp-filter');
66
var tag_version = require('gulp-tag-version');
7-
var eslint = require("gulp-eslint");
7+
var tslint = require("gulp-tslint");
88
var runSequence = require('run-sequence');
99
var wrap = require("gulp-wrap");
1010
var gutil = require('gulp-util');
@@ -21,7 +21,7 @@ gulp.task('bump-version', function () {
2121
.pipe(gulp.dest('./'));
2222
});
2323

24-
gulp.task('commit-changes', function () {
24+
gulp.task('commit-changes', ['test'], function () {
2525
return gulp.src('.')
2626
.pipe(git.commit('Bumped version number', {args: '-a'}));
2727
});
@@ -35,7 +35,7 @@ gulp.task('push-changes', function (cb) {
3535
git.push('origin', 'master', cb);
3636
});
3737

38-
gulp.task('release', ['test'], function (callback) {
38+
gulp.task('release', ['ts-compile', 'test'], function (callback) {
3939
runSequence(
4040
'bump-version',
4141
'build',
@@ -56,12 +56,20 @@ gulp.task('tag-version', function() {
5656
.pipe(tag_version());
5757
});
5858

59-
gulp.task('build', function() {
59+
gulp.task('build', ['ts-compile'], function() {
6060
return gulp.src("src/angular-ui-router-default.js")
6161
.pipe(wrap({ src: './build.txt' }, { info: require('./package.json') }))
6262
.pipe(gulp.dest('.'));
6363
});
6464

65+
gulp.task('ts-compile', function() {
66+
var ts = require('gulp-typescript');
67+
var tsProject = ts.createProject('tsconfig.json');
68+
return tsProject.src(['src/**/*.ts', 'test/**/*.ts'])
69+
.pipe(tsProject()).js
70+
.pipe(gulp.dest('.'));
71+
});
72+
6573
gulp.task('serve', serve({
6674
root: __dirname,
6775
port: port,
@@ -105,14 +113,11 @@ gulp.task('watch', function() {
105113

106114
gulp.task('lint', function () {
107115
return gulp.src([
108-
"./src/**/*.js",
109-
"./test/**/*.js"
116+
"./src/**/*.ts",
117+
"./test/**/*.ts"
110118
])
111-
.pipe(eslint())
112-
// eslint.format() outputs the lint results to the console.
113-
// Alternatively use eslint.formatEach() (see Docs).
114-
.pipe(eslint.format())
115-
// To have the process exit with an error code (1) on
116-
// lint error, return the stream and pipe to failAfterError last.
117-
.pipe(eslint.failAfterError());
119+
.pipe(tslint({
120+
formatter: "verbose"
121+
}))
122+
.pipe(tslint.report());
118123
});

index.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import angular = require("angular");
2+
3+
declare module 'angular' {
4+
export namespace ui {
5+
export type DefaultSpecifier = string | (() => string) | (() => ng.IPromise<string>);
6+
interface IState {
7+
default?: DefaultSpecifier
8+
}
9+
}
10+
}

package.json

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33
"version": "0.0.4",
44
"description": "AngularJS module that adds support for specifying default child views for abstract states when using ui-router.",
55
"main": "angular-ui-router-default.js",
6+
"types": "./index.d.ts",
7+
"directories": {
8+
"test": "test"
9+
},
610
"scripts": {
7-
"test": "./node_modules/karma/bin/karma start --browsers Firefox --single-run"
11+
"ts-compile": "./node_modules/gulp/bin/gulp.js ts-compile",
12+
"karma-test": "./node_modules/karma/bin/karma start --browsers Firefox --single-run",
13+
"test": "npm run ts-compile && npm run karma-test"
814
},
915
"repository": {
1016
"type": "git",
@@ -17,25 +23,34 @@
1723
},
1824
"homepage": "https://github.com/nonplus/angular-ui-router-default",
1925
"devDependencies": {
20-
"angular": "^1.3.15",
21-
"angular-mocks": "^1.3.15",
22-
"angular-ui-router": "^0.2.14",
26+
"@types/angular-mocks": "^1.5",
27+
"@types/jasmine": "^2.5.35",
28+
"angular": "^1.5",
29+
"angular-mocks": "^1.5",
30+
"angular-ui-router": "^0.2.18",
2331
"gulp": "^3.8.11",
2432
"gulp-bump": "^0.3.0",
25-
"gulp-eslint": "^3.0.1",
2633
"gulp-filter": "^2.0.2",
27-
"gulp-git": "^1.2.1",
34+
"gulp-git": "^1.11.3",
2835
"gulp-karma": "0.0.4",
2936
"gulp-serve": "^0.3.1",
3037
"gulp-tag-version": "^1.2.1",
38+
"gulp-tslint": "^6.1.2",
39+
"gulp-typescript": "^3.0.2",
3140
"gulp-util": "^3.0.4",
3241
"gulp-wrap": "^0.11.0",
33-
"jasmine-core": "^2.3.2",
42+
"jasmine-core": "^2.5.2",
3443
"karma": "^0.12.31",
3544
"karma-chrome-launcher": "^0.1.8",
3645
"karma-firefox-launcher": "^0.1.7",
37-
"karma-jasmine": "^0.3.5",
46+
"karma-jasmine": "^0.3.8",
3847
"lodash": "^3.8.0",
39-
"run-sequence": "^1.1.0"
48+
"run-sequence": "^1.2.2",
49+
"tslint": "^3.15.1",
50+
"typescript": "^2.0.3"
51+
},
52+
"dependencies": {
53+
"@types/angular": "^1.5",
54+
"@types/angular-ui-router": "^1.1"
4055
}
4156
}

sample/app/contacts/contacts.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ angular.module('uiRouterSample.contacts', [
1313

1414
// By specifying the name of a child state, when the abstract state is activated, it
1515
// will activate the specified child state instead.
16-
abstract: '.list',
16+
abstract: true,
17+
default: '.list',
1718

1819
// This abstract state will prepend '/contacts' onto the urls of all its children.
1920
url: '/contacts',
Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
1-
/* global angular */
21
"use strict";
2+
33
var moduleName = 'ui.router.default';
4+
45
/* commonjs package manager support (eg componentjs) */
5-
if (typeof module !== "undefined" && typeof exports !== "undefined" && module.exports === exports){
6+
declare var module, exports;
7+
if (typeof module !== "undefined" && typeof exports !== "undefined" && module.exports === exports) {
68
module.exports = moduleName;
79
}
10+
811
var max_redirects = 10;
912
angular.module(moduleName, ['ui.router'])
10-
.config(['$provide', function($provide) {
11-
$provide.decorator('$state', ['$delegate', '$injector', '$q', function($delegate, $injector, $q) {
13+
.config(['$provide', function($provide: ng.auto.IProvideService) {
14+
15+
$provide.decorator('$state', ['$delegate', '$injector', '$q', function(
16+
$delegate: ng.ui.IStateService,
17+
$injector: ng.auto.IInjectorService,
18+
$q: ng.IQService
19+
) {
1220
var transitionTo = $delegate.transitionTo;
1321
var pendingPromise;
14-
$delegate.transitionTo = function(to, toParams, options) {
22+
$delegate.transitionTo = function(to, toParams, options): ng.IPromise<any> {
1523
var numRedirects = 0;
1624
var $state = this;
1725
var nextState = to.name || to;
@@ -20,16 +28,17 @@ angular.module(moduleName, ['ui.router'])
2028

2129
return fetchTarget();
2230

23-
function fetchTarget() {
31+
function fetchTarget(): ng.IPromise<any> {
2432
var target = $state.get(nextState, $state.$current);
25-
nextState = (target|| {}).name;
26-
33+
nextState = (target || {}).name;
34+
2735
var absRedirectPromise = getAbstractRedirect(target);
2836
pendingPromise = absRedirectPromise;
29-
return $q.when(absRedirectPromise, abstractTargetResolved);
37+
return $q.when(absRedirectPromise)
38+
.then(abstractTargetResolved);
3039

3140
function abstractTargetResolved(abstractTarget) {
32-
if(absRedirectPromise !== pendingPromise) {
41+
if (absRedirectPromise !== pendingPromise) {
3342
return $q.reject(new Error('transition superseded'));
3443
}
3544
// we didn't get anything from the abstract target
@@ -48,12 +57,14 @@ angular.module(moduleName, ['ui.router'])
4857
numRedirects += 1;
4958
}
5059
}
51-
function getAbstractRedirect(state) {
52-
if (!state || !state.abstract || state.abstract === true) {
60+
61+
function getAbstractRedirect(state: ng.ui.IState) {
62+
if (!state || !state.abstract || (state.abstract === true && !state.default)) {
5363
return null;
5464
}
55-
return invokeAbstract(state.abstract).then(abstractInvoked);
56-
function abstractInvoked(newState) {
65+
return invokeAbstract(state).then(abstractInvoked);
66+
67+
function abstractInvoked(newState): string {
5768
if (newState[0] === '.') {
5869
return nextState + newState;
5970
} else {
@@ -62,15 +73,25 @@ angular.module(moduleName, ['ui.router'])
6273
}
6374

6475
}
65-
function invokeAbstract(abstract) {
66-
if (!angular.isString(abstract)) {
67-
return $q.when($injector.invoke(abstract));
76+
77+
function invokeAbstract(state: ng.ui.IState) {
78+
var defaultState: ng.ui.DefaultSpecifier;
79+
80+
if (state.default) {
81+
defaultState = state.default;
82+
} else {
83+
defaultState = state.abstract as any;
84+
}
85+
86+
if (!angular.isString(defaultState)) {
87+
return $q.when($injector.invoke(defaultState));
6888
} else {
69-
return $q.when(abstract);
89+
return $q.when(defaultState);
7090
}
7191
}
7292

7393
};
94+
7495
return $delegate;
7596
}]);
7697
}]);

0 commit comments

Comments
 (0)