-
Notifications
You must be signed in to change notification settings - Fork 183
/
test-ui-router-sample.js
174 lines (169 loc) · 7.59 KB
/
test-ui-router-sample.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*jshint undef: false */
/**
* Module with the ui-router sample conf.
* Code is taken from https://github.com/angular-ui/ui-router/tree/master/sample
* and has been merged in this functionnal module in order to test
* the angular-breadcrumb with the sample of ui-router.
*/
angular.module('ncy-ui-router-conf', ['ngMock'])
.factory('utils', function () {
return {
findById: function findById(a, id) {
for (var i = 0; i < a.length; i++) {
if (a[i].id === id) {
return a[i];
}
}
return null;
},
newRandomKey: function newRandomKey(coll, key, currentKey){
var randKey;
do {
randKey = coll[Math.floor(coll.length * Math.random())][key];
} while (randKey === currentKey);
return randKey;
}
};
})
.factory('contacts', ['utils', function (utils) {
var contacts = {
"contacts":[
{"id": 1, "name": "Alice", "items": [
{"id": "a", "type": "phone number", "value": "555-1234-1234"},
{"id": "b", "type": "email", "value": "alice@mailinator.com"}
]},
{"id": 42, "name": "Bob", "items": [
{"id": "a", "type": "blog", "value": "http://bob.blogger.com"},
{"id": "b", "type": "fax", "value": "555-999-9999"}
]},
{"id": 123, "name": "Eve", "items": [
{"id": "a", "type": "full name", "value": "Eve Adamsdottir"}
]}
]
};
var factory = {};
factory.all = function () {
return contacts;
};
factory.get = function (id) {
return contacts.then(function(){
return utils.findById(contacts, id);
});
};
return factory;
}])
.config(function($stateProvider, $breadcrumbProvider) {
$breadcrumbProvider.setOptions({
prefixStateName: 'home'
});
$stateProvider.state("home", {
url: "/",
template: '<p class="lead">Welcome to the UI-Router Demo</p>' +
'<p>Use the menu above to navigate. ' +
'Pay attention to the <code>$state</code> and <code>$stateParams</code> values below.</p>' +
'<p>Click these links—<a href="#/c?id=1">Alice</a> or ' +
'<a href="#/user/42">Bob</a>—to see a url redirect in action.</p>'
})
.state('about', {
url: '/about',
templateProvider: ['$timeout', function ($timeout) {
return $timeout(function () {
return '<p class="lead">UI-Router Resources</p><ul>' +
'<li><a href="https://github.com/angular-ui/ui-router/tree/master/sample">Source for this Sample</a></li>' +
'<li><a href="https://github.com/angular-ui/ui-router">Github Main Page</a></li>' +
'<li><a href="https://github.com/angular-ui/ui-router#quick-start">Quick Start</a></li>' +
'<li><a href="https://github.com/angular-ui/ui-router/wiki">In-Depth Guide</a></li>' +
'<li><a href="https://github.com/angular-ui/ui-router/wiki/Quick-Reference">API Reference</a></li>' +
'</ul>';
}, 100);
}]
})
.state('contacts', {
abstract: true,
url: '/contacts',
templateUrl: 'app/contacts/contacts.html',
resolve: {
contacts: ['contacts',
function( contacts){
return contacts.all();
}]
},
controller: ['$scope', '$state', 'contacts', 'utils',
function ( $scope, $state, contacts, utils) {
$scope.contacts = contacts;
$scope.goToRandom = function () {
var randId = utils.newRandomKey($scope.contacts, "id", $state.params.contactId);
$state.go('contacts.detail', { contactId: randId });
};
}]
})
.state('contacts.list', {
url: '',
templateUrl: 'app/contacts/contacts.list.html'
})
.state('contacts.detail', {
url: '/{contactId:[0-9]{1,4}}',
views: {
'': {
templateUrl: 'app/contacts/contacts.detail.html',
controller: ['$scope', '$stateParams', 'utils',
function ( $scope, $stateParams, utils) {
$scope.contact = utils.findById($scope.contacts, $stateParams.contactId);
}]
},
'hint@': {
template: 'This is contacts.detail populating the "hint" ui-view'
},
'menuTip': {
templateProvider: ['$stateParams',
function ($stateParams) {
return '<hr><small class="muted">Contact ID: ' + $stateParams.contactId + '</small>';
}]
}
},
ncyBreadcrumb: {
parent: 'contacts.list' // Override the parent state (only for the breadcrumb).
}
})
.state('contacts.detail.item', {
url: '/item/:itemId',
views: {
'': {
templateUrl: 'app/contacts/contacts.detail.item.html',
controller: ['$scope', '$stateParams', '$state', 'utils',
function ( $scope, $stateParams, $state, utils) {
$scope.item = utils.findById($scope.contact.items, $stateParams.itemId);
$scope.edit = function () {
$state.go('.edit', $stateParams);
};
}]
},
'hint@': {
template: ' This is contacts.detail.item overriding the "hint" ui-view'
}
}
})
.state('contacts.detail.item.edit', {
views: {
'@contacts.detail': {
templateUrl: 'app/contacts/contacts.detail.item.edit.html',
controller: ['$scope', '$stateParams', '$state', 'utils',
function ( $scope, $stateParams, $state, utils) {
$scope.item = utils.findById($scope.contact.items, $stateParams.itemId);
$scope.done = function () {
$state.go('^', $stateParams);
};
}]
}
}
});
})
.run(function($httpBackend) {
// Due to templateUrl definitions in $state configuration,
// httpBackend will try to load the view for each state asked.
$httpBackend.when('GET', 'app/contacts/contacts.html').respond('dummy contacts view');
$httpBackend.when('GET', 'app/contacts/contacts.list.html').respond('dummy contacts.list view');
$httpBackend.when('GET', 'app/contacts/contacts.detail.html').respond('dummy contacts.detail view');
$httpBackend.when('GET', 'app/contacts/contacts.detail.item.html').respond('dummy contacts.detail.item view');
$httpBackend.when('GET', 'app/contacts/contacts.detail.item.edit.html').respond('dummy contacts.detail.item.edit view');
});