ionic-rails-engine 0.9.17 → 0.9.26

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,1769 @@
1
+ /**
2
+ * State-based routing for AngularJS
3
+ * @version v0.2.7
4
+ * @link http://angular-ui.github.com/
5
+ * @license MIT License, http://www.opensource.org/licenses/MIT
6
+ */
7
+
8
+ /* commonjs package manager support (eg componentjs) */
9
+ if (typeof module !== "undefined" && typeof exports !== "undefined" && module.exports === exports){
10
+ module.exports = 'ui.router';
11
+ }
12
+
13
+ (function (window, angular, undefined) {
14
+ /*jshint globalstrict:true*/
15
+ /*global angular:false*/
16
+ 'use strict';
17
+
18
+ var isDefined = angular.isDefined,
19
+ isFunction = angular.isFunction,
20
+ isString = angular.isString,
21
+ isObject = angular.isObject,
22
+ isArray = angular.isArray,
23
+ forEach = angular.forEach,
24
+ extend = angular.extend,
25
+ copy = angular.copy;
26
+
27
+ function inherit(parent, extra) {
28
+ return extend(new (extend(function() {}, { prototype: parent }))(), extra);
29
+ }
30
+
31
+ function merge(dst) {
32
+ forEach(arguments, function(obj) {
33
+ if (obj !== dst) {
34
+ forEach(obj, function(value, key) {
35
+ if (!dst.hasOwnProperty(key)) dst[key] = value;
36
+ });
37
+ }
38
+ });
39
+ return dst;
40
+ }
41
+
42
+ /**
43
+ * Finds the common ancestor path between two states.
44
+ *
45
+ * @param {Object} first The first state.
46
+ * @param {Object} second The second state.
47
+ * @return {Array} Returns an array of state names in descending order, not including the root.
48
+ */
49
+ function ancestors(first, second) {
50
+ var path = [];
51
+
52
+ for (var n in first.path) {
53
+ if (first.path[n] === "") continue;
54
+ if (!second.path[n]) break;
55
+ path.push(first.path[n]);
56
+ }
57
+ return path;
58
+ }
59
+
60
+ /**
61
+ * IE8-safe wrapper for `Object.keys()`.
62
+ *
63
+ * @param {Object} object A JavaScript object.
64
+ * @return {Array} Returns the keys of the object as an array.
65
+ */
66
+ function keys(object) {
67
+ if (Object.keys) {
68
+ return Object.keys(object);
69
+ }
70
+ var result = [];
71
+
72
+ angular.forEach(object, function(val, key) {
73
+ result.push(key);
74
+ });
75
+ return result;
76
+ }
77
+
78
+ /**
79
+ * IE8-safe wrapper for `Array.prototype.indexOf()`.
80
+ *
81
+ * @param {Array} array A JavaScript array.
82
+ * @param {*} value A value to search the array for.
83
+ * @return {Number} Returns the array index value of `value`, or `-1` if not present.
84
+ */
85
+ function arraySearch(array, value) {
86
+ if (Array.prototype.indexOf) {
87
+ return array.indexOf(value, Number(arguments[2]) || 0);
88
+ }
89
+ var len = array.length >>> 0, from = Number(arguments[2]) || 0;
90
+ from = (from < 0) ? Math.ceil(from) : Math.floor(from);
91
+
92
+ if (from < 0) from += len;
93
+
94
+ for (; from < len; from++) {
95
+ if (from in array && array[from] === value) return from;
96
+ }
97
+ return -1;
98
+ }
99
+
100
+ /**
101
+ * Merges a set of parameters with all parameters inherited between the common parents of the
102
+ * current state and a given destination state.
103
+ *
104
+ * @param {Object} currentParams The value of the current state parameters ($stateParams).
105
+ * @param {Object} newParams The set of parameters which will be composited with inherited params.
106
+ * @param {Object} $current Internal definition of object representing the current state.
107
+ * @param {Object} $to Internal definition of object representing state to transition to.
108
+ */
109
+ function inheritParams(currentParams, newParams, $current, $to) {
110
+ var parents = ancestors($current, $to), parentParams, inherited = {}, inheritList = [];
111
+
112
+ for (var i in parents) {
113
+ if (!parents[i].params || !parents[i].params.length) continue;
114
+ parentParams = parents[i].params;
115
+
116
+ for (var j in parentParams) {
117
+ if (arraySearch(inheritList, parentParams[j]) >= 0) continue;
118
+ inheritList.push(parentParams[j]);
119
+ inherited[parentParams[j]] = currentParams[parentParams[j]];
120
+ }
121
+ }
122
+ return extend({}, inherited, newParams);
123
+ }
124
+
125
+ /**
126
+ * Normalizes a set of values to string or `null`, filtering them by a list of keys.
127
+ *
128
+ * @param {Array} keys The list of keys to normalize/return.
129
+ * @param {Object} values An object hash of values to normalize.
130
+ * @return {Object} Returns an object hash of normalized string values.
131
+ */
132
+ function normalize(keys, values) {
133
+ var normalized = {};
134
+
135
+ forEach(keys, function (name) {
136
+ var value = values[name];
137
+ normalized[name] = (value != null) ? String(value) : null;
138
+ });
139
+ return normalized;
140
+ }
141
+
142
+ /**
143
+ * Performs a non-strict comparison of the subset of two objects, defined by a list of keys.
144
+ *
145
+ * @param {Object} a The first object.
146
+ * @param {Object} b The second object.
147
+ * @param {Array} keys The list of keys within each object to compare. If the list is empty or not specified,
148
+ * it defaults to the list of keys in `a`.
149
+ * @return {Boolean} Returns `true` if the keys match, otherwise `false`.
150
+ */
151
+ function equalForKeys(a, b, keys) {
152
+ if (!keys) {
153
+ keys = [];
154
+ for (var n in a) keys.push(n); // Used instead of Object.keys() for IE8 compatibility
155
+ }
156
+
157
+ for (var i=0; i<keys.length; i++) {
158
+ var k = keys[i];
159
+ if (a[k] != b[k]) return false; // Not '===', values aren't necessarily normalized
160
+ }
161
+ return true;
162
+ }
163
+
164
+ /**
165
+ * Returns the subset of an object, based on a list of keys.
166
+ *
167
+ * @param {Array} keys
168
+ * @param {Object} values
169
+ * @return {Boolean} Returns a subset of `values`.
170
+ */
171
+ function filterByKeys(keys, values) {
172
+ var filtered = {};
173
+
174
+ forEach(keys, function (name) {
175
+ filtered[name] = values[name];
176
+ });
177
+ return filtered;
178
+ }
179
+
180
+ angular.module('ui.router.util', ['ng']);
181
+ angular.module('ui.router.router', ['ui.router.util']);
182
+ angular.module('ui.router.state', ['ui.router.router', 'ui.router.util']);
183
+ angular.module('ui.router', ['ui.router.state']);
184
+ angular.module('ui.router.compat', ['ui.router']);
185
+
186
+
187
+ /**
188
+ * Service (`ui-util`). Manages resolution of (acyclic) graphs of promises.
189
+ * @module $resolve
190
+ * @requires $q
191
+ * @requires $injector
192
+ */
193
+ $Resolve.$inject = ['$q', '$injector'];
194
+ function $Resolve( $q, $injector) {
195
+
196
+ var VISIT_IN_PROGRESS = 1,
197
+ VISIT_DONE = 2,
198
+ NOTHING = {},
199
+ NO_DEPENDENCIES = [],
200
+ NO_LOCALS = NOTHING,
201
+ NO_PARENT = extend($q.when(NOTHING), { $$promises: NOTHING, $$values: NOTHING });
202
+
203
+
204
+ /**
205
+ * Studies a set of invocables that are likely to be used multiple times.
206
+ * $resolve.study(invocables)(locals, parent, self)
207
+ * is equivalent to
208
+ * $resolve.resolve(invocables, locals, parent, self)
209
+ * but the former is more efficient (in fact `resolve` just calls `study` internally).
210
+ * See {@link module:$resolve/resolve} for details.
211
+ * @function
212
+ * @param {Object} invocables
213
+ * @return {Function}
214
+ */
215
+ this.study = function (invocables) {
216
+ if (!isObject(invocables)) throw new Error("'invocables' must be an object");
217
+
218
+ // Perform a topological sort of invocables to build an ordered plan
219
+ var plan = [], cycle = [], visited = {};
220
+ function visit(value, key) {
221
+ if (visited[key] === VISIT_DONE) return;
222
+
223
+ cycle.push(key);
224
+ if (visited[key] === VISIT_IN_PROGRESS) {
225
+ cycle.splice(0, cycle.indexOf(key));
226
+ throw new Error("Cyclic dependency: " + cycle.join(" -> "));
227
+ }
228
+ visited[key] = VISIT_IN_PROGRESS;
229
+
230
+ if (isString(value)) {
231
+ plan.push(key, [ function() { return $injector.get(value); }], NO_DEPENDENCIES);
232
+ } else {
233
+ var params = $injector.annotate(value);
234
+ forEach(params, function (param) {
235
+ if (param !== key && invocables.hasOwnProperty(param)) visit(invocables[param], param);
236
+ });
237
+ plan.push(key, value, params);
238
+ }
239
+
240
+ cycle.pop();
241
+ visited[key] = VISIT_DONE;
242
+ }
243
+ forEach(invocables, visit);
244
+ invocables = cycle = visited = null; // plan is all that's required
245
+
246
+ function isResolve(value) {
247
+ return isObject(value) && value.then && value.$$promises;
248
+ }
249
+
250
+ return function (locals, parent, self) {
251
+ if (isResolve(locals) && self === undefined) {
252
+ self = parent; parent = locals; locals = null;
253
+ }
254
+ if (!locals) locals = NO_LOCALS;
255
+ else if (!isObject(locals)) {
256
+ throw new Error("'locals' must be an object");
257
+ }
258
+ if (!parent) parent = NO_PARENT;
259
+ else if (!isResolve(parent)) {
260
+ throw new Error("'parent' must be a promise returned by $resolve.resolve()");
261
+ }
262
+
263
+ // To complete the overall resolution, we have to wait for the parent
264
+ // promise and for the promise for each invokable in our plan.
265
+ var resolution = $q.defer(),
266
+ result = resolution.promise,
267
+ promises = result.$$promises = {},
268
+ values = extend({}, locals),
269
+ wait = 1 + plan.length/3,
270
+ merged = false;
271
+
272
+ function done() {
273
+ // Merge parent values we haven't got yet and publish our own $$values
274
+ if (!--wait) {
275
+ if (!merged) merge(values, parent.$$values);
276
+ result.$$values = values;
277
+ result.$$promises = true; // keep for isResolve()
278
+ resolution.resolve(values);
279
+ }
280
+ }
281
+
282
+ function fail(reason) {
283
+ result.$$failure = reason;
284
+ resolution.reject(reason);
285
+ }
286
+
287
+ // Short-circuit if parent has already failed
288
+ if (isDefined(parent.$$failure)) {
289
+ fail(parent.$$failure);
290
+ return result;
291
+ }
292
+
293
+ // Merge parent values if the parent has already resolved, or merge
294
+ // parent promises and wait if the parent resolve is still in progress.
295
+ if (parent.$$values) {
296
+ merged = merge(values, parent.$$values);
297
+ done();
298
+ } else {
299
+ extend(promises, parent.$$promises);
300
+ parent.then(done, fail);
301
+ }
302
+
303
+ // Process each invocable in the plan, but ignore any where a local of the same name exists.
304
+ for (var i=0, ii=plan.length; i<ii; i+=3) {
305
+ if (locals.hasOwnProperty(plan[i])) done();
306
+ else invoke(plan[i], plan[i+1], plan[i+2]);
307
+ }
308
+
309
+ function invoke(key, invocable, params) {
310
+ // Create a deferred for this invocation. Failures will propagate to the resolution as well.
311
+ var invocation = $q.defer(), waitParams = 0;
312
+ function onfailure(reason) {
313
+ invocation.reject(reason);
314
+ fail(reason);
315
+ }
316
+ // Wait for any parameter that we have a promise for (either from parent or from this
317
+ // resolve; in that case study() will have made sure it's ordered before us in the plan).
318
+ forEach(params, function (dep) {
319
+ if (promises.hasOwnProperty(dep) && !locals.hasOwnProperty(dep)) {
320
+ waitParams++;
321
+ promises[dep].then(function (result) {
322
+ values[dep] = result;
323
+ if (!(--waitParams)) proceed();
324
+ }, onfailure);
325
+ }
326
+ });
327
+ if (!waitParams) proceed();
328
+ function proceed() {
329
+ if (isDefined(result.$$failure)) return;
330
+ try {
331
+ invocation.resolve($injector.invoke(invocable, self, values));
332
+ invocation.promise.then(function (result) {
333
+ values[key] = result;
334
+ done();
335
+ }, onfailure);
336
+ } catch (e) {
337
+ onfailure(e);
338
+ }
339
+ }
340
+ // Publish promise synchronously; invocations further down in the plan may depend on it.
341
+ promises[key] = invocation.promise;
342
+ }
343
+
344
+ return result;
345
+ };
346
+ };
347
+
348
+ /**
349
+ * Resolves a set of invocables. An invocable is a function to be invoked via `$injector.invoke()`,
350
+ * and can have an arbitrary number of dependencies. An invocable can either return a value directly,
351
+ * or a `$q` promise. If a promise is returned it will be resolved and the resulting value will be
352
+ * used instead. Dependencies of invocables are resolved (in this order of precedence)
353
+ *
354
+ * - from the specified `locals`
355
+ * - from another invocable that is part of this `$resolve` call
356
+ * - from an invocable that is inherited from a `parent` call to `$resolve` (or recursively
357
+ * from any ancestor `$resolve` of that parent).
358
+ *
359
+ * The return value of `$resolve` is a promise for an object that contains (in this order of precedence)
360
+ *
361
+ * - any `locals` (if specified)
362
+ * - the resolved return values of all injectables
363
+ * - any values inherited from a `parent` call to `$resolve` (if specified)
364
+ *
365
+ * The promise will resolve after the `parent` promise (if any) and all promises returned by injectables
366
+ * have been resolved. If any invocable (or `$injector.invoke`) throws an exception, or if a promise
367
+ * returned by an invocable is rejected, the `$resolve` promise is immediately rejected with the same error.
368
+ * A rejection of a `parent` promise (if specified) will likewise be propagated immediately. Once the
369
+ * `$resolve` promise has been rejected, no further invocables will be called.
370
+ *
371
+ * Cyclic dependencies between invocables are not permitted and will caues `$resolve` to throw an
372
+ * error. As a special case, an injectable can depend on a parameter with the same name as the injectable,
373
+ * which will be fulfilled from the `parent` injectable of the same name. This allows inherited values
374
+ * to be decorated. Note that in this case any other injectable in the same `$resolve` with the same
375
+ * dependency would see the decorated value, not the inherited value.
376
+ *
377
+ * Note that missing dependencies -- unlike cyclic dependencies -- will cause an (asynchronous) rejection
378
+ * of the `$resolve` promise rather than a (synchronous) exception.
379
+ *
380
+ * Invocables are invoked eagerly as soon as all dependencies are available. This is true even for
381
+ * dependencies inherited from a `parent` call to `$resolve`.
382
+ *
383
+ * As a special case, an invocable can be a string, in which case it is taken to be a service name
384
+ * to be passed to `$injector.get()`. This is supported primarily for backwards-compatibility with the
385
+ * `resolve` property of `$routeProvider` routes.
386
+ *
387
+ * @function
388
+ * @param {Object.<string, Function|string>} invocables functions to invoke or `$injector` services to fetch.
389
+ * @param {Object.<string, *>} [locals] values to make available to the injectables
390
+ * @param {Promise.<Object>} [parent] a promise returned by another call to `$resolve`.
391
+ * @param {Object} [self] the `this` for the invoked methods
392
+ * @return {Promise.<Object>} Promise for an object that contains the resolved return value
393
+ * of all invocables, as well as any inherited and local values.
394
+ */
395
+ this.resolve = function (invocables, locals, parent, self) {
396
+ return this.study(invocables)(locals, parent, self);
397
+ };
398
+ }
399
+
400
+ angular.module('ui.router.util').service('$resolve', $Resolve);
401
+
402
+
403
+ /**
404
+ * Service. Manages loading of templates.
405
+ * @constructor
406
+ * @name $templateFactory
407
+ * @requires $http
408
+ * @requires $templateCache
409
+ * @requires $injector
410
+ */
411
+ $TemplateFactory.$inject = ['$http', '$templateCache', '$injector'];
412
+ function $TemplateFactory( $http, $templateCache, $injector) {
413
+
414
+ /**
415
+ * Creates a template from a configuration object.
416
+ * @function
417
+ * @name $templateFactory#fromConfig
418
+ * @methodOf $templateFactory
419
+ * @param {Object} config Configuration object for which to load a template. The following
420
+ * properties are search in the specified order, and the first one that is defined is
421
+ * used to create the template:
422
+ * @param {string|Function} config.template html string template or function to load via
423
+ * {@link $templateFactory#fromString fromString}.
424
+ * @param {string|Function} config.templateUrl url to load or a function returning the url
425
+ * to load via {@link $templateFactory#fromUrl fromUrl}.
426
+ * @param {Function} config.templateProvider function to invoke via
427
+ * {@link $templateFactory#fromProvider fromProvider}.
428
+ * @param {Object} params Parameters to pass to the template function.
429
+ * @param {Object} [locals] Locals to pass to `invoke` if the template is loaded via a
430
+ * `templateProvider`. Defaults to `{ params: params }`.
431
+ * @return {string|Promise.<string>} The template html as a string, or a promise for that string,
432
+ * or `null` if no template is configured.
433
+ */
434
+ this.fromConfig = function (config, params, locals) {
435
+ return (
436
+ isDefined(config.template) ? this.fromString(config.template, params) :
437
+ isDefined(config.templateUrl) ? this.fromUrl(config.templateUrl, params) :
438
+ isDefined(config.templateProvider) ? this.fromProvider(config.templateProvider, params, locals) :
439
+ null
440
+ );
441
+ };
442
+
443
+ /**
444
+ * Creates a template from a string or a function returning a string.
445
+ * @function
446
+ * @name $templateFactory#fromString
447
+ * @methodOf $templateFactory
448
+ * @param {string|Function} template html template as a string or function that returns an html
449
+ * template as a string.
450
+ * @param {Object} params Parameters to pass to the template function.
451
+ * @return {string|Promise.<string>} The template html as a string, or a promise for that string.
452
+ */
453
+ this.fromString = function (template, params) {
454
+ return isFunction(template) ? template(params) : template;
455
+ };
456
+
457
+ /**
458
+ * Loads a template from the a URL via `$http` and `$templateCache`.
459
+ * @function
460
+ * @name $templateFactory#fromUrl
461
+ * @methodOf $templateFactory
462
+ * @param {string|Function} url url of the template to load, or a function that returns a url.
463
+ * @param {Object} params Parameters to pass to the url function.
464
+ * @return {string|Promise.<string>} The template html as a string, or a promise for that string.
465
+ */
466
+ this.fromUrl = function (url, params) {
467
+ if (isFunction(url)) url = url(params);
468
+ if (url == null) return null;
469
+ else return $http
470
+ .get(url, { cache: $templateCache })
471
+ .then(function(response) { return response.data; });
472
+ };
473
+
474
+ /**
475
+ * Creates a template by invoking an injectable provider function.
476
+ * @function
477
+ * @name $templateFactory#fromUrl
478
+ * @methodOf $templateFactory
479
+ * @param {Function} provider Function to invoke via `$injector.invoke`
480
+ * @param {Object} params Parameters for the template.
481
+ * @param {Object} [locals] Locals to pass to `invoke`. Defaults to `{ params: params }`.
482
+ * @return {string|Promise.<string>} The template html as a string, or a promise for that string.
483
+ */
484
+ this.fromProvider = function (provider, params, locals) {
485
+ return $injector.invoke(provider, null, locals || { params: params });
486
+ };
487
+ }
488
+
489
+ angular.module('ui.router.util').service('$templateFactory', $TemplateFactory);
490
+
491
+ /**
492
+ * Matches URLs against patterns and extracts named parameters from the path or the search
493
+ * part of the URL. A URL pattern consists of a path pattern, optionally followed by '?' and a list
494
+ * of search parameters. Multiple search parameter names are separated by '&'. Search parameters
495
+ * do not influence whether or not a URL is matched, but their values are passed through into
496
+ * the matched parameters returned by {@link UrlMatcher#exec exec}.
497
+ *
498
+ * Path parameter placeholders can be specified using simple colon/catch-all syntax or curly brace
499
+ * syntax, which optionally allows a regular expression for the parameter to be specified:
500
+ *
501
+ * * ':' name - colon placeholder
502
+ * * '*' name - catch-all placeholder
503
+ * * '{' name '}' - curly placeholder
504
+ * * '{' name ':' regexp '}' - curly placeholder with regexp. Should the regexp itself contain
505
+ * curly braces, they must be in matched pairs or escaped with a backslash.
506
+ *
507
+ * Parameter names may contain only word characters (latin letters, digits, and underscore) and
508
+ * must be unique within the pattern (across both path and search parameters). For colon
509
+ * placeholders or curly placeholders without an explicit regexp, a path parameter matches any
510
+ * number of characters other than '/'. For catch-all placeholders the path parameter matches
511
+ * any number of characters.
512
+ *
513
+ * ### Examples
514
+ *
515
+ * * '/hello/' - Matches only if the path is exactly '/hello/'. There is no special treatment for
516
+ * trailing slashes, and patterns have to match the entire path, not just a prefix.
517
+ * * '/user/:id' - Matches '/user/bob' or '/user/1234!!!' or even '/user/' but not '/user' or
518
+ * '/user/bob/details'. The second path segment will be captured as the parameter 'id'.
519
+ * * '/user/{id}' - Same as the previous example, but using curly brace syntax.
520
+ * * '/user/{id:[^/]*}' - Same as the previous example.
521
+ * * '/user/{id:[0-9a-fA-F]{1,8}}' - Similar to the previous example, but only matches if the id
522
+ * parameter consists of 1 to 8 hex digits.
523
+ * * '/files/{path:.*}' - Matches any URL starting with '/files/' and captures the rest of the
524
+ * path into the parameter 'path'.
525
+ * * '/files/*path' - ditto.
526
+ *
527
+ * @constructor
528
+ * @param {string} pattern the pattern to compile into a matcher.
529
+ *
530
+ * @property {string} prefix A static prefix of this pattern. The matcher guarantees that any
531
+ * URL matching this matcher (i.e. any string for which {@link UrlMatcher#exec exec()} returns
532
+ * non-null) will start with this prefix.
533
+ */
534
+ function UrlMatcher(pattern) {
535
+
536
+ // Find all placeholders and create a compiled pattern, using either classic or curly syntax:
537
+ // '*' name
538
+ // ':' name
539
+ // '{' name '}'
540
+ // '{' name ':' regexp '}'
541
+ // The regular expression is somewhat complicated due to the need to allow curly braces
542
+ // inside the regular expression. The placeholder regexp breaks down as follows:
543
+ // ([:*])(\w+) classic placeholder ($1 / $2)
544
+ // \{(\w+)(?:\:( ... ))?\} curly brace placeholder ($3) with optional regexp ... ($4)
545
+ // (?: ... | ... | ... )+ the regexp consists of any number of atoms, an atom being either
546
+ // [^{}\\]+ - anything other than curly braces or backslash
547
+ // \\. - a backslash escape
548
+ // \{(?:[^{}\\]+|\\.)*\} - a matched set of curly braces containing other atoms
549
+ var placeholder = /([:*])(\w+)|\{(\w+)(?:\:((?:[^{}\\]+|\\.|\{(?:[^{}\\]+|\\.)*\})+))?\}/g,
550
+ names = {}, compiled = '^', last = 0, m,
551
+ segments = this.segments = [],
552
+ params = this.params = [];
553
+
554
+ function addParameter(id) {
555
+ if (!/^\w+(-+\w+)*$/.test(id)) throw new Error("Invalid parameter name '" + id + "' in pattern '" + pattern + "'");
556
+ if (names[id]) throw new Error("Duplicate parameter name '" + id + "' in pattern '" + pattern + "'");
557
+ names[id] = true;
558
+ params.push(id);
559
+ }
560
+
561
+ function quoteRegExp(string) {
562
+ return string.replace(/[\\\[\]\^$*+?.()|{}]/g, "\\$&");
563
+ }
564
+
565
+ this.source = pattern;
566
+
567
+ // Split into static segments separated by path parameter placeholders.
568
+ // The number of segments is always 1 more than the number of parameters.
569
+ var id, regexp, segment;
570
+ while ((m = placeholder.exec(pattern))) {
571
+ id = m[2] || m[3]; // IE[78] returns '' for unmatched groups instead of null
572
+ regexp = m[4] || (m[1] == '*' ? '.*' : '[^/]*');
573
+ segment = pattern.substring(last, m.index);
574
+ if (segment.indexOf('?') >= 0) break; // we're into the search part
575
+ compiled += quoteRegExp(segment) + '(' + regexp + ')';
576
+ addParameter(id);
577
+ segments.push(segment);
578
+ last = placeholder.lastIndex;
579
+ }
580
+ segment = pattern.substring(last);
581
+
582
+ // Find any search parameter names and remove them from the last segment
583
+ var i = segment.indexOf('?');
584
+ if (i >= 0) {
585
+ var search = this.sourceSearch = segment.substring(i);
586
+ segment = segment.substring(0, i);
587
+ this.sourcePath = pattern.substring(0, last+i);
588
+
589
+ // Allow parameters to be separated by '?' as well as '&' to make concat() easier
590
+ forEach(search.substring(1).split(/[&?]/), addParameter);
591
+ } else {
592
+ this.sourcePath = pattern;
593
+ this.sourceSearch = '';
594
+ }
595
+
596
+ compiled += quoteRegExp(segment) + '$';
597
+ segments.push(segment);
598
+ this.regexp = new RegExp(compiled);
599
+ this.prefix = segments[0];
600
+ }
601
+
602
+ /**
603
+ * Returns a new matcher for a pattern constructed by appending the path part and adding the
604
+ * search parameters of the specified pattern to this pattern. The current pattern is not
605
+ * modified. This can be understood as creating a pattern for URLs that are relative to (or
606
+ * suffixes of) the current pattern.
607
+ *
608
+ * ### Example
609
+ * The following two matchers are equivalent:
610
+ * ```
611
+ * new UrlMatcher('/user/{id}?q').concat('/details?date');
612
+ * new UrlMatcher('/user/{id}/details?q&date');
613
+ * ```
614
+ *
615
+ * @param {string} pattern The pattern to append.
616
+ * @return {UrlMatcher} A matcher for the concatenated pattern.
617
+ */
618
+ UrlMatcher.prototype.concat = function (pattern) {
619
+ // Because order of search parameters is irrelevant, we can add our own search
620
+ // parameters to the end of the new pattern. Parse the new pattern by itself
621
+ // and then join the bits together, but it's much easier to do this on a string level.
622
+ return new UrlMatcher(this.sourcePath + pattern + this.sourceSearch);
623
+ };
624
+
625
+ UrlMatcher.prototype.toString = function () {
626
+ return this.source;
627
+ };
628
+
629
+ /**
630
+ * Tests the specified path against this matcher, and returns an object containing the captured
631
+ * parameter values, or null if the path does not match. The returned object contains the values
632
+ * of any search parameters that are mentioned in the pattern, but their value may be null if
633
+ * they are not present in `searchParams`. This means that search parameters are always treated
634
+ * as optional.
635
+ *
636
+ * ### Example
637
+ * ```
638
+ * new UrlMatcher('/user/{id}?q&r').exec('/user/bob', { x:'1', q:'hello' });
639
+ * // returns { id:'bob', q:'hello', r:null }
640
+ * ```
641
+ *
642
+ * @param {string} path The URL path to match, e.g. `$location.path()`.
643
+ * @param {Object} searchParams URL search parameters, e.g. `$location.search()`.
644
+ * @return {Object} The captured parameter values.
645
+ */
646
+ UrlMatcher.prototype.exec = function (path, searchParams) {
647
+ var m = this.regexp.exec(path);
648
+ if (!m) return null;
649
+
650
+ var params = this.params, nTotal = params.length,
651
+ nPath = this.segments.length-1,
652
+ values = {}, i;
653
+
654
+ if (nPath !== m.length - 1) throw new Error("Unbalanced capture group in route '" + this.source + "'");
655
+
656
+ for (i=0; i<nPath; i++) values[params[i]] = m[i+1];
657
+ for (/**/; i<nTotal; i++) values[params[i]] = searchParams[params[i]];
658
+
659
+ return values;
660
+ };
661
+
662
+ /**
663
+ * Returns the names of all path and search parameters of this pattern in an unspecified order.
664
+ * @return {Array.<string>} An array of parameter names. Must be treated as read-only. If the
665
+ * pattern has no parameters, an empty array is returned.
666
+ */
667
+ UrlMatcher.prototype.parameters = function () {
668
+ return this.params;
669
+ };
670
+
671
+ /**
672
+ * Creates a URL that matches this pattern by substituting the specified values
673
+ * for the path and search parameters. Null values for path parameters are
674
+ * treated as empty strings.
675
+ *
676
+ * ### Example
677
+ * ```
678
+ * new UrlMatcher('/user/{id}?q').format({ id:'bob', q:'yes' });
679
+ * // returns '/user/bob?q=yes'
680
+ * ```
681
+ *
682
+ * @param {Object} values the values to substitute for the parameters in this pattern.
683
+ * @return {string} the formatted URL (path and optionally search part).
684
+ */
685
+ UrlMatcher.prototype.format = function (values) {
686
+ var segments = this.segments, params = this.params;
687
+ if (!values) return segments.join('');
688
+
689
+ var nPath = segments.length-1, nTotal = params.length,
690
+ result = segments[0], i, search, value;
691
+
692
+ for (i=0; i<nPath; i++) {
693
+ value = values[params[i]];
694
+ // TODO: Maybe we should throw on null here? It's not really good style to use '' and null interchangeabley
695
+ if (value != null) result += encodeURIComponent(value);
696
+ result += segments[i+1];
697
+ }
698
+ for (/**/; i<nTotal; i++) {
699
+ value = values[params[i]];
700
+ if (value != null) {
701
+ result += (search ? '&' : '?') + params[i] + '=' + encodeURIComponent(value);
702
+ search = true;
703
+ }
704
+ }
705
+
706
+ return result;
707
+ };
708
+
709
+ /**
710
+ * Service. Factory for {@link UrlMatcher} instances. The factory is also available to providers
711
+ * under the name `$urlMatcherFactoryProvider`.
712
+ * @constructor
713
+ * @name $urlMatcherFactory
714
+ */
715
+ function $UrlMatcherFactory() {
716
+ /**
717
+ * Creates a {@link UrlMatcher} for the specified pattern.
718
+ * @function
719
+ * @name $urlMatcherFactory#compile
720
+ * @methodOf $urlMatcherFactory
721
+ * @param {string} pattern The URL pattern.
722
+ * @return {UrlMatcher} The UrlMatcher.
723
+ */
724
+ this.compile = function (pattern) {
725
+ return new UrlMatcher(pattern);
726
+ };
727
+
728
+ /**
729
+ * Returns true if the specified object is a UrlMatcher, or false otherwise.
730
+ * @function
731
+ * @name $urlMatcherFactory#isMatcher
732
+ * @methodOf $urlMatcherFactory
733
+ * @param {Object} o
734
+ * @return {boolean}
735
+ */
736
+ this.isMatcher = function (o) {
737
+ return isObject(o) && isFunction(o.exec) && isFunction(o.format) && isFunction(o.concat);
738
+ };
739
+
740
+ this.$get = function () {
741
+ return this;
742
+ };
743
+ }
744
+
745
+ // Register as a provider so it's available to other providers
746
+ angular.module('ui.router.util').provider('$urlMatcherFactory', $UrlMatcherFactory);
747
+
748
+
749
+ $UrlRouterProvider.$inject = ['$urlMatcherFactoryProvider'];
750
+ function $UrlRouterProvider( $urlMatcherFactory) {
751
+ var rules = [],
752
+ otherwise = null;
753
+
754
+ // Returns a string that is a prefix of all strings matching the RegExp
755
+ function regExpPrefix(re) {
756
+ var prefix = /^\^((?:\\[^a-zA-Z0-9]|[^\\\[\]\^$*+?.()|{}]+)*)/.exec(re.source);
757
+ return (prefix != null) ? prefix[1].replace(/\\(.)/g, "$1") : '';
758
+ }
759
+
760
+ // Interpolates matched values into a String.replace()-style pattern
761
+ function interpolate(pattern, match) {
762
+ return pattern.replace(/\$(\$|\d{1,2})/, function (m, what) {
763
+ return match[what === '$' ? 0 : Number(what)];
764
+ });
765
+ }
766
+
767
+ this.rule =
768
+ function (rule) {
769
+ if (!isFunction(rule)) throw new Error("'rule' must be a function");
770
+ rules.push(rule);
771
+ return this;
772
+ };
773
+
774
+ this.otherwise =
775
+ function (rule) {
776
+ if (isString(rule)) {
777
+ var redirect = rule;
778
+ rule = function () { return redirect; };
779
+ }
780
+ else if (!isFunction(rule)) throw new Error("'rule' must be a function");
781
+ otherwise = rule;
782
+ return this;
783
+ };
784
+
785
+
786
+ function handleIfMatch($injector, handler, match) {
787
+ if (!match) return false;
788
+ var result = $injector.invoke(handler, handler, { $match: match });
789
+ return isDefined(result) ? result : true;
790
+ }
791
+
792
+ this.when =
793
+ function (what, handler) {
794
+ var redirect, handlerIsString = isString(handler);
795
+ if (isString(what)) what = $urlMatcherFactory.compile(what);
796
+
797
+ if (!handlerIsString && !isFunction(handler) && !isArray(handler))
798
+ throw new Error("invalid 'handler' in when()");
799
+
800
+ var strategies = {
801
+ matcher: function (what, handler) {
802
+ if (handlerIsString) {
803
+ redirect = $urlMatcherFactory.compile(handler);
804
+ handler = ['$match', function ($match) { return redirect.format($match); }];
805
+ }
806
+ return extend(function ($injector, $location) {
807
+ return handleIfMatch($injector, handler, what.exec($location.path(), $location.search()));
808
+ }, {
809
+ prefix: isString(what.prefix) ? what.prefix : ''
810
+ });
811
+ },
812
+ regex: function (what, handler) {
813
+ if (what.global || what.sticky) throw new Error("when() RegExp must not be global or sticky");
814
+
815
+ if (handlerIsString) {
816
+ redirect = handler;
817
+ handler = ['$match', function ($match) { return interpolate(redirect, $match); }];
818
+ }
819
+ return extend(function ($injector, $location) {
820
+ return handleIfMatch($injector, handler, what.exec($location.path()));
821
+ }, {
822
+ prefix: regExpPrefix(what)
823
+ });
824
+ }
825
+ };
826
+
827
+ var check = { matcher: $urlMatcherFactory.isMatcher(what), regex: what instanceof RegExp };
828
+
829
+ for (var n in check) {
830
+ if (check[n]) {
831
+ return this.rule(strategies[n](what, handler));
832
+ }
833
+ }
834
+
835
+ throw new Error("invalid 'what' in when()");
836
+ };
837
+
838
+ this.$get =
839
+ [ '$location', '$rootScope', '$injector',
840
+ function ($location, $rootScope, $injector) {
841
+ // TODO: Optimize groups of rules with non-empty prefix into some sort of decision tree
842
+ function update(evt) {
843
+ if (evt && evt.defaultPrevented) return;
844
+ function check(rule) {
845
+ var handled = rule($injector, $location);
846
+ if (handled) {
847
+ if (isString(handled)) $location.replace().url(handled);
848
+ return true;
849
+ }
850
+ return false;
851
+ }
852
+ var n=rules.length, i;
853
+ for (i=0; i<n; i++) {
854
+ if (check(rules[i])) return;
855
+ }
856
+ // always check otherwise last to allow dynamic updates to the set of rules
857
+ if (otherwise) check(otherwise);
858
+ }
859
+
860
+ $rootScope.$on('$locationChangeSuccess', update);
861
+
862
+ return {
863
+ sync: function () {
864
+ update();
865
+ }
866
+ };
867
+ }];
868
+ }
869
+
870
+ angular.module('ui.router.router').provider('$urlRouter', $UrlRouterProvider);
871
+
872
+ $StateProvider.$inject = ['$urlRouterProvider', '$urlMatcherFactoryProvider', '$locationProvider'];
873
+ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $locationProvider) {
874
+
875
+ var root, states = {}, $state, queue = {}, abstractKey = 'abstract';
876
+
877
+ // Builds state properties from definition passed to registerState()
878
+ var stateBuilder = {
879
+
880
+ // Derive parent state from a hierarchical name only if 'parent' is not explicitly defined.
881
+ // state.children = [];
882
+ // if (parent) parent.children.push(state);
883
+ parent: function(state) {
884
+ if (isDefined(state.parent) && state.parent) return findState(state.parent);
885
+ // regex matches any valid composite state name
886
+ // would match "contact.list" but not "contacts"
887
+ var compositeName = /^(.+)\.[^.]+$/.exec(state.name);
888
+ return compositeName ? findState(compositeName[1]) : root;
889
+ },
890
+
891
+ // inherit 'data' from parent and override by own values (if any)
892
+ data: function(state) {
893
+ if (state.parent && state.parent.data) {
894
+ state.data = state.self.data = extend({}, state.parent.data, state.data);
895
+ }
896
+ return state.data;
897
+ },
898
+
899
+ // Build a URLMatcher if necessary, either via a relative or absolute URL
900
+ url: function(state) {
901
+ var url = state.url;
902
+
903
+ if (isString(url)) {
904
+ if (url.charAt(0) == '^') {
905
+ return $urlMatcherFactory.compile(url.substring(1));
906
+ }
907
+ return (state.parent.navigable || root).url.concat(url);
908
+ }
909
+
910
+ if ($urlMatcherFactory.isMatcher(url) || url == null) {
911
+ return url;
912
+ }
913
+ throw new Error("Invalid url '" + url + "' in state '" + state + "'");
914
+ },
915
+
916
+ // Keep track of the closest ancestor state that has a URL (i.e. is navigable)
917
+ navigable: function(state) {
918
+ return state.url ? state : (state.parent ? state.parent.navigable : null);
919
+ },
920
+
921
+ // Derive parameters for this state and ensure they're a super-set of parent's parameters
922
+ params: function(state) {
923
+ if (!state.params) {
924
+ return state.url ? state.url.parameters() : state.parent.params;
925
+ }
926
+ if (!isArray(state.params)) throw new Error("Invalid params in state '" + state + "'");
927
+ if (state.url) throw new Error("Both params and url specicified in state '" + state + "'");
928
+ return state.params;
929
+ },
930
+
931
+ // If there is no explicit multi-view configuration, make one up so we don't have
932
+ // to handle both cases in the view directive later. Note that having an explicit
933
+ // 'views' property will mean the default unnamed view properties are ignored. This
934
+ // is also a good time to resolve view names to absolute names, so everything is a
935
+ // straight lookup at link time.
936
+ views: function(state) {
937
+ var views = {};
938
+
939
+ forEach(isDefined(state.views) ? state.views : { '': state }, function (view, name) {
940
+ if (name.indexOf('@') < 0) name += '@' + state.parent.name;
941
+ views[name] = view;
942
+ });
943
+ return views;
944
+ },
945
+
946
+ ownParams: function(state) {
947
+ if (!state.parent) {
948
+ return state.params;
949
+ }
950
+ var paramNames = {}; forEach(state.params, function (p) { paramNames[p] = true; });
951
+
952
+ forEach(state.parent.params, function (p) {
953
+ if (!paramNames[p]) {
954
+ throw new Error("Missing required parameter '" + p + "' in state '" + state.name + "'");
955
+ }
956
+ paramNames[p] = false;
957
+ });
958
+ var ownParams = [];
959
+
960
+ forEach(paramNames, function (own, p) {
961
+ if (own) ownParams.push(p);
962
+ });
963
+ return ownParams;
964
+ },
965
+
966
+ // Keep a full path from the root down to this state as this is needed for state activation.
967
+ path: function(state) {
968
+ return state.parent ? state.parent.path.concat(state) : []; // exclude root from path
969
+ },
970
+
971
+ // Speed up $state.contains() as it's used a lot
972
+ includes: function(state) {
973
+ var includes = state.parent ? extend({}, state.parent.includes) : {};
974
+ includes[state.name] = true;
975
+ return includes;
976
+ },
977
+
978
+ $delegates: {}
979
+ };
980
+
981
+ function isRelative(stateName) {
982
+ return stateName.indexOf(".") === 0 || stateName.indexOf("^") === 0;
983
+ }
984
+
985
+ function findState(stateOrName, base) {
986
+ var isStr = isString(stateOrName),
987
+ name = isStr ? stateOrName : stateOrName.name,
988
+ path = isRelative(name);
989
+
990
+ if (path) {
991
+ if (!base) throw new Error("No reference point given for path '" + name + "'");
992
+ var rel = name.split("."), i = 0, pathLength = rel.length, current = base;
993
+
994
+ for (; i < pathLength; i++) {
995
+ if (rel[i] === "" && i === 0) {
996
+ current = base;
997
+ continue;
998
+ }
999
+ if (rel[i] === "^") {
1000
+ if (!current.parent) throw new Error("Path '" + name + "' not valid for state '" + base.name + "'");
1001
+ current = current.parent;
1002
+ continue;
1003
+ }
1004
+ break;
1005
+ }
1006
+ rel = rel.slice(i).join(".");
1007
+ name = current.name + (current.name && rel ? "." : "") + rel;
1008
+ }
1009
+ var state = states[name];
1010
+
1011
+ if (state && (isStr || (!isStr && (state === stateOrName || state.self === stateOrName)))) {
1012
+ return state;
1013
+ }
1014
+ return undefined;
1015
+ }
1016
+
1017
+ function queueState(parentName, state) {
1018
+ if (!queue[parentName]) {
1019
+ queue[parentName] = [];
1020
+ }
1021
+ queue[parentName].push(state);
1022
+ }
1023
+
1024
+ function registerState(state) {
1025
+ // Wrap a new object around the state so we can store our private details easily.
1026
+ state = inherit(state, {
1027
+ self: state,
1028
+ resolve: state.resolve || {},
1029
+ toString: function() { return this.name; }
1030
+ });
1031
+
1032
+ var name = state.name;
1033
+ if (!isString(name) || name.indexOf('@') >= 0) throw new Error("State must have a valid name");
1034
+ if (states.hasOwnProperty(name)) throw new Error("State '" + name + "'' is already defined");
1035
+
1036
+ // Get parent name
1037
+ var parentName = (name.indexOf('.') !== -1) ? name.substring(0, name.lastIndexOf('.'))
1038
+ : (isString(state.parent)) ? state.parent
1039
+ : '';
1040
+
1041
+ // If parent is not registered yet, add state to queue and register later
1042
+ if (parentName && !states[parentName]) {
1043
+ return queueState(parentName, state.self);
1044
+ }
1045
+
1046
+ for (var key in stateBuilder) {
1047
+ if (isFunction(stateBuilder[key])) state[key] = stateBuilder[key](state, stateBuilder.$delegates[key]);
1048
+ }
1049
+ states[name] = state;
1050
+
1051
+ // Register the state in the global state list and with $urlRouter if necessary.
1052
+ if (!state[abstractKey] && state.url) {
1053
+ $urlRouterProvider.when(state.url, ['$match', '$stateParams', function ($match, $stateParams) {
1054
+ if ($state.$current.navigable != state || !equalForKeys($match, $stateParams)) {
1055
+ $state.transitionTo(state, $match, { location: false });
1056
+ }
1057
+ }]);
1058
+ }
1059
+
1060
+ // Register any queued children
1061
+ if (queue[name]) {
1062
+ for (var i = 0; i < queue[name].length; i++) {
1063
+ registerState(queue[name][i]);
1064
+ }
1065
+ }
1066
+
1067
+ return state;
1068
+ }
1069
+
1070
+
1071
+ // Implicit root state that is always active
1072
+ root = registerState({
1073
+ name: '',
1074
+ url: '^',
1075
+ views: null,
1076
+ 'abstract': true
1077
+ });
1078
+ root.navigable = null;
1079
+
1080
+
1081
+ // .decorator()
1082
+ // .decorator(name)
1083
+ // .decorator(name, function)
1084
+ this.decorator = decorator;
1085
+ function decorator(name, func) {
1086
+ /*jshint validthis: true */
1087
+ if (isString(name) && !isDefined(func)) {
1088
+ return stateBuilder[name];
1089
+ }
1090
+ if (!isFunction(func) || !isString(name)) {
1091
+ return this;
1092
+ }
1093
+ if (stateBuilder[name] && !stateBuilder.$delegates[name]) {
1094
+ stateBuilder.$delegates[name] = stateBuilder[name];
1095
+ }
1096
+ stateBuilder[name] = func;
1097
+ return this;
1098
+ }
1099
+
1100
+ // .state(state)
1101
+ // .state(name, state)
1102
+ this.state = state;
1103
+ function state(name, definition) {
1104
+ /*jshint validthis: true */
1105
+ if (isObject(name)) definition = name;
1106
+ else definition.name = name;
1107
+ registerState(definition);
1108
+ return this;
1109
+ }
1110
+
1111
+ // $urlRouter is injected just to ensure it gets instantiated
1112
+ this.$get = $get;
1113
+ $get.$inject = ['$rootScope', '$q', '$view', '$injector', '$resolve', '$stateParams', '$location', '$urlRouter'];
1114
+ function $get( $rootScope, $q, $view, $injector, $resolve, $stateParams, $location, $urlRouter) {
1115
+
1116
+ var TransitionSuperseded = $q.reject(new Error('transition superseded'));
1117
+ var TransitionPrevented = $q.reject(new Error('transition prevented'));
1118
+ var TransitionAborted = $q.reject(new Error('transition aborted'));
1119
+ var TransitionFailed = $q.reject(new Error('transition failed'));
1120
+ var currentLocation = $location.url();
1121
+
1122
+ function syncUrl() {
1123
+ if ($location.url() !== currentLocation) {
1124
+ $location.url(currentLocation);
1125
+ $location.replace();
1126
+ }
1127
+ }
1128
+
1129
+ root.locals = { resolve: null, globals: { $stateParams: {} } };
1130
+ $state = {
1131
+ params: {},
1132
+ current: root.self,
1133
+ $current: root,
1134
+ transition: null
1135
+ };
1136
+
1137
+ $state.reload = function reload() {
1138
+ $state.transitionTo($state.current, $stateParams, { reload: true, inherit: false, notify: false });
1139
+ };
1140
+
1141
+ $state.go = function go(to, params, options) {
1142
+ return this.transitionTo(to, params, extend({ inherit: true, relative: $state.$current }, options));
1143
+ };
1144
+
1145
+ $state.transitionTo = function transitionTo(to, toParams, options) {
1146
+ toParams = toParams || {};
1147
+ options = extend({
1148
+ location: true, inherit: false, relative: null, notify: true, reload: false, $retry: false
1149
+ }, options || {});
1150
+
1151
+ var from = $state.$current, fromParams = $state.params, fromPath = from.path;
1152
+ var evt, toState = findState(to, options.relative);
1153
+
1154
+ if (!isDefined(toState)) {
1155
+ // Broadcast not found event and abort the transition if prevented
1156
+ var redirect = { to: to, toParams: toParams, options: options };
1157
+ evt = $rootScope.$broadcast('$stateNotFound', redirect, from.self, fromParams);
1158
+ if (evt.defaultPrevented) {
1159
+ syncUrl();
1160
+ return TransitionAborted;
1161
+ }
1162
+
1163
+ // Allow the handler to return a promise to defer state lookup retry
1164
+ if (evt.retry) {
1165
+ if (options.$retry) {
1166
+ syncUrl();
1167
+ return TransitionFailed;
1168
+ }
1169
+ var retryTransition = $state.transition = $q.when(evt.retry);
1170
+ retryTransition.then(function() {
1171
+ if (retryTransition !== $state.transition) return TransitionSuperseded;
1172
+ redirect.options.$retry = true;
1173
+ return $state.transitionTo(redirect.to, redirect.toParams, redirect.options);
1174
+ }, function() {
1175
+ return TransitionAborted;
1176
+ });
1177
+ syncUrl();
1178
+ return retryTransition;
1179
+ }
1180
+
1181
+ // Always retry once if the $stateNotFound was not prevented
1182
+ // (handles either redirect changed or state lazy-definition)
1183
+ to = redirect.to;
1184
+ toParams = redirect.toParams;
1185
+ options = redirect.options;
1186
+ toState = findState(to, options.relative);
1187
+ if (!isDefined(toState)) {
1188
+ if (options.relative) throw new Error("Could not resolve '" + to + "' from state '" + options.relative + "'");
1189
+ throw new Error("No such state '" + to + "'");
1190
+ }
1191
+ }
1192
+ if (toState[abstractKey]) throw new Error("Cannot transition to abstract state '" + to + "'");
1193
+ if (options.inherit) toParams = inheritParams($stateParams, toParams || {}, $state.$current, toState);
1194
+ to = toState;
1195
+
1196
+ var toPath = to.path;
1197
+
1198
+ // Starting from the root of the path, keep all levels that haven't changed
1199
+ var keep, state, locals = root.locals, toLocals = [];
1200
+ for (keep = 0, state = toPath[keep];
1201
+ state && state === fromPath[keep] && equalForKeys(toParams, fromParams, state.ownParams) && !options.reload;
1202
+ keep++, state = toPath[keep]) {
1203
+ locals = toLocals[keep] = state.locals;
1204
+ }
1205
+
1206
+ // If we're going to the same state and all locals are kept, we've got nothing to do.
1207
+ // But clear 'transition', as we still want to cancel any other pending transitions.
1208
+ // TODO: We may not want to bump 'transition' if we're called from a location change that we've initiated ourselves,
1209
+ // because we might accidentally abort a legitimate transition initiated from code?
1210
+ if (shouldTriggerReload(to, from, locals, options) ) {
1211
+ if ( to.self.reloadOnSearch !== false )
1212
+ syncUrl();
1213
+ $state.transition = null;
1214
+ return $q.when($state.current);
1215
+ }
1216
+
1217
+ // Normalize/filter parameters before we pass them to event handlers etc.
1218
+ toParams = normalize(to.params, toParams || {});
1219
+
1220
+ // Broadcast start event and cancel the transition if requested
1221
+ if (options.notify) {
1222
+ evt = $rootScope.$broadcast('$stateChangeStart', to.self, toParams, from.self, fromParams);
1223
+ if (evt.defaultPrevented) {
1224
+ syncUrl();
1225
+ return TransitionPrevented;
1226
+ }
1227
+ }
1228
+
1229
+ // Resolve locals for the remaining states, but don't update any global state just
1230
+ // yet -- if anything fails to resolve the current state needs to remain untouched.
1231
+ // We also set up an inheritance chain for the locals here. This allows the view directive
1232
+ // to quickly look up the correct definition for each view in the current state. Even
1233
+ // though we create the locals object itself outside resolveState(), it is initially
1234
+ // empty and gets filled asynchronously. We need to keep track of the promise for the
1235
+ // (fully resolved) current locals, and pass this down the chain.
1236
+ var resolved = $q.when(locals);
1237
+ for (var l=keep; l<toPath.length; l++, state=toPath[l]) {
1238
+ locals = toLocals[l] = inherit(locals);
1239
+ resolved = resolveState(state, toParams, state===to, resolved, locals);
1240
+ }
1241
+
1242
+ // Once everything is resolved, we are ready to perform the actual transition
1243
+ // and return a promise for the new state. We also keep track of what the
1244
+ // current promise is, so that we can detect overlapping transitions and
1245
+ // keep only the outcome of the last transition.
1246
+ var transition = $state.transition = resolved.then(function () {
1247
+ var l, entering, exiting;
1248
+
1249
+ if ($state.transition !== transition) return TransitionSuperseded;
1250
+
1251
+ // Exit 'from' states not kept
1252
+ for (l=fromPath.length-1; l>=keep; l--) {
1253
+ exiting = fromPath[l];
1254
+ if (exiting.self.onExit) {
1255
+ $injector.invoke(exiting.self.onExit, exiting.self, exiting.locals.globals);
1256
+ }
1257
+ exiting.locals = null;
1258
+ }
1259
+
1260
+ // Enter 'to' states not kept
1261
+ for (l=keep; l<toPath.length; l++) {
1262
+ entering = toPath[l];
1263
+ entering.locals = toLocals[l];
1264
+ if (entering.self.onEnter) {
1265
+ $injector.invoke(entering.self.onEnter, entering.self, entering.locals.globals);
1266
+ }
1267
+ }
1268
+
1269
+ // Run it again, to catch any transitions in callbacks
1270
+ if ($state.transition !== transition) return TransitionSuperseded;
1271
+
1272
+ // Update globals in $state
1273
+ $state.$current = to;
1274
+ $state.current = to.self;
1275
+ $state.params = toParams;
1276
+ copy($state.params, $stateParams);
1277
+ $state.transition = null;
1278
+
1279
+ // Update $location
1280
+ var toNav = to.navigable;
1281
+ if (options.location && toNav) {
1282
+ $location.url(toNav.url.format(toNav.locals.globals.$stateParams));
1283
+
1284
+ if (options.location === 'replace') {
1285
+ $location.replace();
1286
+ }
1287
+ }
1288
+
1289
+ if (options.notify) {
1290
+ $rootScope.$broadcast('$stateChangeSuccess', to.self, toParams, from.self, fromParams);
1291
+ }
1292
+ currentLocation = $location.url();
1293
+
1294
+ return $state.current;
1295
+ }, function (error) {
1296
+ if ($state.transition !== transition) return TransitionSuperseded;
1297
+
1298
+ $state.transition = null;
1299
+ $rootScope.$broadcast('$stateChangeError', to.self, toParams, from.self, fromParams, error);
1300
+ syncUrl();
1301
+
1302
+ return $q.reject(error);
1303
+ });
1304
+
1305
+ return transition;
1306
+ };
1307
+
1308
+ $state.is = function is(stateOrName, params) {
1309
+ var state = findState(stateOrName);
1310
+
1311
+ if (!isDefined(state)) {
1312
+ return undefined;
1313
+ }
1314
+
1315
+ if ($state.$current !== state) {
1316
+ return false;
1317
+ }
1318
+
1319
+ return isDefined(params) ? angular.equals($stateParams, params) : true;
1320
+ };
1321
+
1322
+ $state.includes = function includes(stateOrName, params) {
1323
+ var state = findState(stateOrName);
1324
+ if (!isDefined(state)) {
1325
+ return undefined;
1326
+ }
1327
+
1328
+ if (!isDefined($state.$current.includes[state.name])) {
1329
+ return false;
1330
+ }
1331
+
1332
+ var validParams = true;
1333
+ angular.forEach(params, function(value, key) {
1334
+ if (!isDefined($stateParams[key]) || $stateParams[key] !== value) {
1335
+ validParams = false;
1336
+ }
1337
+ });
1338
+ return validParams;
1339
+ };
1340
+
1341
+ $state.href = function href(stateOrName, params, options) {
1342
+ options = extend({ lossy: true, inherit: false, absolute: false, relative: $state.$current }, options || {});
1343
+ var state = findState(stateOrName, options.relative);
1344
+ if (!isDefined(state)) return null;
1345
+
1346
+ params = inheritParams($stateParams, params || {}, $state.$current, state);
1347
+ var nav = (state && options.lossy) ? state.navigable : state;
1348
+ var url = (nav && nav.url) ? nav.url.format(normalize(state.params, params || {})) : null;
1349
+ if (!$locationProvider.html5Mode() && url) {
1350
+ url = "#" + $locationProvider.hashPrefix() + url;
1351
+ }
1352
+ if (options.absolute && url) {
1353
+ url = $location.protocol() + '://' +
1354
+ $location.host() +
1355
+ ($location.port() == 80 || $location.port() == 443 ? '' : ':' + $location.port()) +
1356
+ (!$locationProvider.html5Mode() && url ? '/' : '') +
1357
+ url;
1358
+ }
1359
+ return url;
1360
+ };
1361
+
1362
+ $state.get = function (stateOrName, context) {
1363
+ if (!isDefined(stateOrName)) {
1364
+ var list = [];
1365
+ forEach(states, function(state) { list.push(state.self); });
1366
+ return list;
1367
+ }
1368
+ var state = findState(stateOrName, context);
1369
+ return (state && state.self) ? state.self : null;
1370
+ };
1371
+
1372
+ function resolveState(state, params, paramsAreFiltered, inherited, dst) {
1373
+ // Make a restricted $stateParams with only the parameters that apply to this state if
1374
+ // necessary. In addition to being available to the controller and onEnter/onExit callbacks,
1375
+ // we also need $stateParams to be available for any $injector calls we make during the
1376
+ // dependency resolution process.
1377
+ var $stateParams = (paramsAreFiltered) ? params : filterByKeys(state.params, params);
1378
+ var locals = { $stateParams: $stateParams };
1379
+
1380
+ // Resolve 'global' dependencies for the state, i.e. those not specific to a view.
1381
+ // We're also including $stateParams in this; that way the parameters are restricted
1382
+ // to the set that should be visible to the state, and are independent of when we update
1383
+ // the global $state and $stateParams values.
1384
+ dst.resolve = $resolve.resolve(state.resolve, locals, dst.resolve, state);
1385
+ var promises = [ dst.resolve.then(function (globals) {
1386
+ dst.globals = globals;
1387
+ }) ];
1388
+ if (inherited) promises.push(inherited);
1389
+
1390
+ // Resolve template and dependencies for all views.
1391
+ forEach(state.views, function (view, name) {
1392
+ var injectables = (view.resolve && view.resolve !== state.resolve ? view.resolve : {});
1393
+ injectables.$template = [ function () {
1394
+ return $view.load(name, { view: view, locals: locals, params: $stateParams, notify: false }) || '';
1395
+ }];
1396
+
1397
+ promises.push($resolve.resolve(injectables, locals, dst.resolve, state).then(function (result) {
1398
+ // References to the controller (only instantiated at link time)
1399
+ if (isFunction(view.controllerProvider) || isArray(view.controllerProvider)) {
1400
+ var injectLocals = angular.extend({}, injectables, locals);
1401
+ result.$$controller = $injector.invoke(view.controllerProvider, null, injectLocals);
1402
+ } else {
1403
+ result.$$controller = view.controller;
1404
+ }
1405
+ // Provide access to the state itself for internal use
1406
+ result.$$state = state;
1407
+ dst[name] = result;
1408
+ }));
1409
+ });
1410
+
1411
+ // Wait for all the promises and then return the activation object
1412
+ return $q.all(promises).then(function (values) {
1413
+ return dst;
1414
+ });
1415
+ }
1416
+
1417
+ return $state;
1418
+ }
1419
+
1420
+ function shouldTriggerReload(to, from, locals, options) {
1421
+ if ( to === from && ((locals === from.locals && !options.reload) || (to.self.reloadOnSearch === false)) ) {
1422
+ return true;
1423
+ }
1424
+ }
1425
+ }
1426
+
1427
+ angular.module('ui.router.state')
1428
+ .value('$stateParams', {})
1429
+ .provider('$state', $StateProvider);
1430
+
1431
+
1432
+ $ViewProvider.$inject = [];
1433
+ function $ViewProvider() {
1434
+
1435
+ this.$get = $get;
1436
+ $get.$inject = ['$rootScope', '$templateFactory'];
1437
+ function $get( $rootScope, $templateFactory) {
1438
+ return {
1439
+ // $view.load('full.viewName', { template: ..., controller: ..., resolve: ..., async: false, params: ... })
1440
+ load: function load(name, options) {
1441
+ var result, defaults = {
1442
+ template: null, controller: null, view: null, locals: null, notify: true, async: true, params: {}
1443
+ };
1444
+ options = extend(defaults, options);
1445
+
1446
+ if (options.view) {
1447
+ result = $templateFactory.fromConfig(options.view, options.params, options.locals);
1448
+ }
1449
+ if (result && options.notify) {
1450
+ $rootScope.$broadcast('$viewContentLoading', options);
1451
+ }
1452
+ return result;
1453
+ }
1454
+ };
1455
+ }
1456
+ }
1457
+
1458
+ angular.module('ui.router.state').provider('$view', $ViewProvider);
1459
+
1460
+
1461
+ $ViewDirective.$inject = ['$state', '$compile', '$controller', '$injector', '$anchorScroll'];
1462
+ function $ViewDirective( $state, $compile, $controller, $injector, $anchorScroll) {
1463
+ var $animator = $injector.has('$animator') ? $injector.get('$animator') : false;
1464
+ var viewIsUpdating = false;
1465
+
1466
+ var directive = {
1467
+ restrict: 'ECA',
1468
+ terminal: true,
1469
+ priority: 1000,
1470
+ transclude: true,
1471
+ compile: function (element, attr, transclude) {
1472
+ return function(scope, element, attr) {
1473
+ var viewScope, viewLocals,
1474
+ name = attr[directive.name] || attr.name || '',
1475
+ onloadExp = attr.onload || '',
1476
+ animate = $animator && $animator(scope, attr),
1477
+ initialView = transclude(scope);
1478
+
1479
+ // Returns a set of DOM manipulation functions based on whether animation
1480
+ // should be performed
1481
+ var renderer = function(doAnimate) {
1482
+ return ({
1483
+ "true": {
1484
+ remove: function(element) { animate.leave(element.contents(), element); },
1485
+ restore: function(compiled, element) { animate.enter(compiled, element); },
1486
+ populate: function(template, element) {
1487
+ var contents = angular.element('<div></div>').html(template).contents();
1488
+ animate.enter(contents, element);
1489
+ return contents;
1490
+ }
1491
+ },
1492
+ "false": {
1493
+ remove: function(element) { element.html(''); },
1494
+ restore: function(compiled, element) { element.append(compiled); },
1495
+ populate: function(template, element) {
1496
+ element.html(template);
1497
+ return element.contents();
1498
+ }
1499
+ }
1500
+ })[doAnimate.toString()];
1501
+ };
1502
+
1503
+ // Put back the compiled initial view
1504
+ element.append(initialView);
1505
+
1506
+ // Find the details of the parent view directive (if any) and use it
1507
+ // to derive our own qualified view name, then hang our own details
1508
+ // off the DOM so child directives can find it.
1509
+ var parent = element.parent().inheritedData('$uiView');
1510
+ if (name.indexOf('@') < 0) name = name + '@' + (parent ? parent.state.name : '');
1511
+ var view = { name: name, state: null };
1512
+ element.data('$uiView', view);
1513
+
1514
+ var eventHook = function() {
1515
+ if (viewIsUpdating) return;
1516
+ viewIsUpdating = true;
1517
+
1518
+ try { updateView(true); } catch (e) {
1519
+ viewIsUpdating = false;
1520
+ throw e;
1521
+ }
1522
+ viewIsUpdating = false;
1523
+ };
1524
+
1525
+ scope.$on('$stateChangeSuccess', eventHook);
1526
+ scope.$on('$viewContentLoading', eventHook);
1527
+ updateView(false);
1528
+
1529
+ function updateView(doAnimate) {
1530
+ var locals = $state.$current && $state.$current.locals[name];
1531
+ if (locals === viewLocals) return; // nothing to do
1532
+ var render = renderer(animate && doAnimate);
1533
+
1534
+ // Remove existing content
1535
+ render.remove(element);
1536
+
1537
+ // Destroy previous view scope
1538
+ if (viewScope) {
1539
+ viewScope.$destroy();
1540
+ viewScope = null;
1541
+ }
1542
+
1543
+ if (!locals) {
1544
+ viewLocals = null;
1545
+ view.state = null;
1546
+
1547
+ // Restore the initial view
1548
+ return render.restore(initialView, element);
1549
+ }
1550
+
1551
+ viewLocals = locals;
1552
+ view.state = locals.$$state;
1553
+
1554
+ var link = $compile(render.populate(locals.$template, element));
1555
+ viewScope = scope.$new();
1556
+
1557
+ if (locals.$$controller) {
1558
+ locals.$scope = viewScope;
1559
+ var controller = $controller(locals.$$controller, locals);
1560
+ element.children().data('$ngControllerController', controller);
1561
+ }
1562
+ link(viewScope);
1563
+ viewScope.$emit('$viewContentLoaded');
1564
+ if (onloadExp) viewScope.$eval(onloadExp);
1565
+
1566
+ // TODO: This seems strange, shouldn't $anchorScroll listen for $viewContentLoaded if necessary?
1567
+ // $anchorScroll might listen on event...
1568
+ $anchorScroll();
1569
+ }
1570
+ };
1571
+ }
1572
+ };
1573
+ return directive;
1574
+ }
1575
+
1576
+ angular.module('ui.router.state').directive('uiView', $ViewDirective);
1577
+
1578
+ function parseStateRef(ref) {
1579
+ var parsed = ref.replace(/\n/g, " ").match(/^([^(]+?)\s*(\((.*)\))?$/);
1580
+ if (!parsed || parsed.length !== 4) throw new Error("Invalid state ref '" + ref + "'");
1581
+ return { state: parsed[1], paramExpr: parsed[3] || null };
1582
+ }
1583
+
1584
+ function stateContext(el) {
1585
+ var stateData = el.parent().inheritedData('$uiView');
1586
+
1587
+ if (stateData && stateData.state && stateData.state.name) {
1588
+ return stateData.state;
1589
+ }
1590
+ }
1591
+
1592
+ $StateRefDirective.$inject = ['$state', '$timeout'];
1593
+ function $StateRefDirective($state, $timeout) {
1594
+ return {
1595
+ restrict: 'A',
1596
+ require: '?^uiSrefActive',
1597
+ link: function(scope, element, attrs, uiSrefActive) {
1598
+ var ref = parseStateRef(attrs.uiSref);
1599
+ var params = null, url = null, base = stateContext(element) || $state.$current;
1600
+ var isForm = element[0].nodeName === "FORM";
1601
+ var attr = isForm ? "action" : "href", nav = true;
1602
+
1603
+ var update = function(newVal) {
1604
+ if (newVal) params = newVal;
1605
+ if (!nav) return;
1606
+
1607
+ var newHref = $state.href(ref.state, params, { relative: base });
1608
+
1609
+ if (!newHref) {
1610
+ nav = false;
1611
+ return false;
1612
+ }
1613
+ element[0][attr] = newHref;
1614
+ if (uiSrefActive) {
1615
+ uiSrefActive.$$setStateInfo(ref.state, params);
1616
+ }
1617
+ };
1618
+
1619
+ if (ref.paramExpr) {
1620
+ scope.$watch(ref.paramExpr, function(newVal, oldVal) {
1621
+ if (newVal !== params) update(newVal);
1622
+ }, true);
1623
+ params = scope.$eval(ref.paramExpr);
1624
+ }
1625
+ update();
1626
+
1627
+ if (isForm) return;
1628
+
1629
+ element.bind("click", function(e) {
1630
+ var button = e.which || e.button;
1631
+
1632
+ if ((button === 0 || button == 1) && !e.ctrlKey && !e.metaKey && !e.shiftKey) {
1633
+ // HACK: This is to allow ng-clicks to be processed before the transition is initiated:
1634
+ $timeout(function() {
1635
+ scope.$apply(function() {
1636
+ $state.go(ref.state, params, { relative: base });
1637
+ });
1638
+ });
1639
+ e.preventDefault();
1640
+ }
1641
+ });
1642
+ }
1643
+ };
1644
+ }
1645
+
1646
+ $StateActiveDirective.$inject = ['$state', '$stateParams', '$interpolate'];
1647
+ function $StateActiveDirective($state, $stateParams, $interpolate) {
1648
+ return {
1649
+ restrict: "A",
1650
+ controller: function($scope, $element, $attrs) {
1651
+ var state, params, activeClass;
1652
+
1653
+ // There probably isn't much point in $observing this
1654
+ activeClass = $interpolate($attrs.uiSrefActive || '', false)($scope);
1655
+
1656
+ // Allow uiSref to communicate with uiSrefActive
1657
+ this.$$setStateInfo = function(newState, newParams) {
1658
+ state = $state.get(newState, stateContext($element));
1659
+ params = newParams;
1660
+ update();
1661
+ };
1662
+
1663
+ $scope.$on('$stateChangeSuccess', update);
1664
+
1665
+ // Update route state
1666
+ function update() {
1667
+ if ($state.$current.self === state && matchesParams()) {
1668
+ $element.addClass(activeClass);
1669
+ } else {
1670
+ $element.removeClass(activeClass);
1671
+ }
1672
+ }
1673
+
1674
+ function matchesParams() {
1675
+ return !params || equalForKeys(params, $stateParams);
1676
+ }
1677
+ }
1678
+ };
1679
+ }
1680
+
1681
+ angular.module('ui.router.state')
1682
+ .directive('uiSref', $StateRefDirective)
1683
+ .directive('uiSrefActive', $StateActiveDirective);
1684
+
1685
+ $RouteProvider.$inject = ['$stateProvider', '$urlRouterProvider'];
1686
+ function $RouteProvider( $stateProvider, $urlRouterProvider) {
1687
+
1688
+ var routes = [];
1689
+
1690
+ onEnterRoute.$inject = ['$$state'];
1691
+ function onEnterRoute( $$state) {
1692
+ /*jshint validthis: true */
1693
+ this.locals = $$state.locals.globals;
1694
+ this.params = this.locals.$stateParams;
1695
+ }
1696
+
1697
+ function onExitRoute() {
1698
+ /*jshint validthis: true */
1699
+ this.locals = null;
1700
+ this.params = null;
1701
+ }
1702
+
1703
+ this.when = when;
1704
+ function when(url, route) {
1705
+ /*jshint validthis: true */
1706
+ if (route.redirectTo != null) {
1707
+ // Redirect, configure directly on $urlRouterProvider
1708
+ var redirect = route.redirectTo, handler;
1709
+ if (isString(redirect)) {
1710
+ handler = redirect; // leave $urlRouterProvider to handle
1711
+ } else if (isFunction(redirect)) {
1712
+ // Adapt to $urlRouterProvider API
1713
+ handler = function (params, $location) {
1714
+ return redirect(params, $location.path(), $location.search());
1715
+ };
1716
+ } else {
1717
+ throw new Error("Invalid 'redirectTo' in when()");
1718
+ }
1719
+ $urlRouterProvider.when(url, handler);
1720
+ } else {
1721
+ // Regular route, configure as state
1722
+ $stateProvider.state(inherit(route, {
1723
+ parent: null,
1724
+ name: 'route:' + encodeURIComponent(url),
1725
+ url: url,
1726
+ onEnter: onEnterRoute,
1727
+ onExit: onExitRoute
1728
+ }));
1729
+ }
1730
+ routes.push(route);
1731
+ return this;
1732
+ }
1733
+
1734
+ this.$get = $get;
1735
+ $get.$inject = ['$state', '$rootScope', '$routeParams'];
1736
+ function $get( $state, $rootScope, $routeParams) {
1737
+
1738
+ var $route = {
1739
+ routes: routes,
1740
+ params: $routeParams,
1741
+ current: undefined
1742
+ };
1743
+
1744
+ function stateAsRoute(state) {
1745
+ return (state.name !== '') ? state : undefined;
1746
+ }
1747
+
1748
+ $rootScope.$on('$stateChangeStart', function (ev, to, toParams, from, fromParams) {
1749
+ $rootScope.$broadcast('$routeChangeStart', stateAsRoute(to), stateAsRoute(from));
1750
+ });
1751
+
1752
+ $rootScope.$on('$stateChangeSuccess', function (ev, to, toParams, from, fromParams) {
1753
+ $route.current = stateAsRoute(to);
1754
+ $rootScope.$broadcast('$routeChangeSuccess', stateAsRoute(to), stateAsRoute(from));
1755
+ copy(toParams, $route.params);
1756
+ });
1757
+
1758
+ $rootScope.$on('$stateChangeError', function (ev, to, toParams, from, fromParams, error) {
1759
+ $rootScope.$broadcast('$routeChangeError', stateAsRoute(to), stateAsRoute(from), error);
1760
+ });
1761
+
1762
+ return $route;
1763
+ }
1764
+ }
1765
+
1766
+ angular.module('ui.router.compat')
1767
+ .provider('$route', $RouteProvider)
1768
+ .directive('ngView', $ViewDirective);
1769
+ })(window, window.angular);