-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
326 lines (261 loc) · 6.64 KB
/
index.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
var angular = require('angular');
var router = require('angular-ui-router');
var debug = require('debug')('angular-setup');
var Promise = require('bluebird');
module.exports = AngularSetup;
/*
* Creates and configures an angular app, with the
* idea of reducing repeated boilerplate.
*/
function AngularSetup(name) {
if (!(this instanceof AngularSetup)) return new AngularSetup(name);
this.name = name;
this.deps = [router];
this.configs = [];
this.runs = [];
};
/*
* Handy plugin thing with `fn`
*
* @param {Function} fn
* @return {AngularSetup}
* @public
*/
AngularSetup.prototype.use = function(fn) {
fn(this)
return this;
}
/*
* Setup module
*
* @param {String|Array} deps
* @return {AngularSetup}
* @public
*/
AngularSetup.prototype.module = function(deps) {
if (!Array.isArray(deps)) {
deps = Array.prototype.slice.call(arguments);
}
this.deps = this.deps.concat(deps);
return this;
}
/*
* Setup Auth
*
* @param {String|Array} deps
* @return {AngularSetup}
* @public
*/
AngularSetup.prototype.auth = function(getToken) {
this.runs.push(function(module){
module.run(function($rootScope, $state, $interval, $http){
function check(){
debug('checking token');
var token = getToken();
var origToken = $rootScope.token;
// no token - nothing changed
if (!token && !origToken) return;
token = token || {};
origToken = origToken || {};
// tokens expire same time - nothing changed
if (token.expires_at === origToken.expires_at) return;
// token has changed
debug('token changed', token);
$rootScope.token = token;
}
function refresh(){
return $http.post('/auth/refresh').then(check);
}
check();
$interval(refresh, 60 * 1000)
})
})
return this;
}
/*
* Handy plugin thing with `fn`
*
* @param {Function} fn
* @return {AngularSetup}
* @public
*/
AngularSetup.prototype.api = function(obj) {
this.configs.push(function(mod){
mod.factory('$api', function(){
return obj;
})
})
return this;
}
/*
* Pass in `fn` to run on angular module config
*
* @param {Function} fn
* @return {AngularSetup}
* @public
*/
AngularSetup.prototype.config = function(fn) {
this.configs.push(fn);
return this;
}
/*
* Pass in `fn` to run on angular module run
*
* @param {Function} fn
* @return {AngularSetup}
* @public
*/
AngularSetup.prototype.run = function(fn) {
this.runs.push(fn);
return this;
}
/*
* Sets up an angular state with given `name`
* and state `opts`.
*
* @param {String} name
* @param {Object} opts
* @return {AngularSetup} this
* @public
*/
AngularSetup.prototype.state = function(name, opts) {
this.configs.push(function(module) {
debug('%s : state setup "%s"', this.name, name);
module.config(function($stateProvider) {
$stateProvider.state(name, opts);
})
})
return this;
}
/*
* Trigger settings and create angular stuff
*
* @param {Angular Module} module
* @private
*/
AngularSetup.prototype.defaultConfig = function(module) {
module.config(function($locationProvider, $compileProvider) {
$locationProvider.html5Mode({
enabled: true,
requireBase: false
})
$compileProvider.debugInfoEnabled(false);
});
}
/*
* Trigger settings and create angular stuff
*
* @param {Angular Module} module
* @private
*/
AngularSetup.prototype.defaultRun = function(module) {
var self = this;
module.run(function($rootScope, $state) {
debug('run', module)
onStateChange(self.name, $state.current, $rootScope);
// setup bluebird promise with angular.
// http://stackoverflow.com/questions/23984471/how-do-i-use-bluebird-with-angular
Promise.setScheduler(function(cb) {
$rootScope.$evalAsync(cb);
});
$rootScope.$on('$stateChangeStart', function(e, to, toParams, from, fromParams) {
if (to.bounce) {
debug('%s : bounce to "%s"', self.name, to.name);
e.preventDefault();
$state.go(to.name + to.bounce);
}
});
$rootScope.$on('$stateChangeStart', function(e, toState) {
onStateChange(self.name, toState, $rootScope);
authenticateState($rootScope.token, toState, $state, e);
});
})
}
/*
* Trigger the angular setup.
*
* @return {Angular Module}
* @public
*/
AngularSetup.prototype.done = function() {
var module = angular.module(this.name, this.deps);
debug('%s : setting up with deps %o', this.name, this.deps);
var self = this;
this.defaultConfig(module);
// trigger config fns
this.configs.forEach(function(fn){
fn.call(self, module);
})
// trigger run fns
this.runs.forEach(function(fn){
fn.call(self, module);
})
this.defaultRun(module);
return module;
}
/*
* Configuration for state change stuff
*
* @param {String} name
* @param {Object} toState
* @param {$rootScope} $rootScope
* @private
*/
function onStateChange(name, toState, $rootScope) {
if (!toState.name) return;
debug('%s : state change "%s"', name, toState.name);
if (toState && toState.title) {
document.title = toState.title;
}
$rootScope.stateName = (toState.name || '').replace(/\./g, '-');
}
function authenticateState(token, toState, $state, event){
var data = toState.data || {};
var authenticate = data.authenticate;
// no authentication required
if (!authenticate) {
debug('nav to "%s" (unauth)', toState.name);
return;
}
// no auth but route request auth
if (!token) {
event.preventDefault();
debug('nav to "%s" : requires auth - going to login', toState.name);
window.location = '/login?redirect=' + toState.url;
return;
}
var claims = data.claims || [];
var claimFailed = false;
claims.forEach(function(claim){
var found = false;
if (claimFailed) return;
token.claims.forEach(function(c){
if (found) return;
if (claim.type == c.type && claim.value == c.value) {
found = true;
}
})
if (found) {
debug('claim found "%s" with value "%s"', claim.type, claim.value);
return;
}
claimFailed = true;
if (claim.redirect) {
event.preventDefault();
debug('claim NOT found "%s" with value "%s" - redirecting to %s', claim.type, claim.value, claim.redirect);
window.location = claim.redirect;
return;
}
if (claim.state) {
event.preventDefault();
debug('claim NOT found "%s" with value "%s" - redirecting to state %s', claim.type, claim.value, claim.state);
$state.go(claim.state);
return;
}
throw new Error('somebody didnt specify a claim.state or claim.redirect')
})
if (token) {
debug('nav to "%s"', toState.name);
return;
}
}