d977aa96c3b33bd37711f6f589a64d8220eb02bd
[vnfsdk/refrepo.git] /
1 /**
2  * State-based routing for AngularJS
3  * @version v0.4.2
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     toJson = angular.toJson;
27
28 function inherit(parent, extra) {
29   return extend(new (extend(function() {}, { prototype: parent }))(), extra);
30 }
31
32 function merge(dst) {
33   forEach(arguments, function(obj) {
34     if (obj !== dst) {
35       forEach(obj, function(value, key) {
36         if (!dst.hasOwnProperty(key)) dst[key] = value;
37       });
38     }
39   });
40   return dst;
41 }
42
43 /**
44  * Finds the common ancestor path between two states.
45  *
46  * @param {Object} first The first state.
47  * @param {Object} second The second state.
48  * @return {Array} Returns an array of state names in descending order, not including the root.
49  */
50 function ancestors(first, second) {
51   var path = [];
52
53   for (var n in first.path) {
54     if (first.path[n] !== 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 objectKeys(object) {
67   if (Object.keys) {
68     return Object.keys(object);
69   }
70   var result = [];
71
72   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 indexOf(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] || !parents[i].params) continue;
114     parentParams = objectKeys(parents[i].params);
115     if (!parentParams.length) continue;
116
117     for (var j in parentParams) {
118       if (indexOf(inheritList, parentParams[j]) >= 0) continue;
119       inheritList.push(parentParams[j]);
120       inherited[parentParams[j]] = currentParams[parentParams[j]];
121     }
122   }
123   return extend({}, inherited, newParams);
124 }
125
126 /**
127  * Performs a non-strict comparison of the subset of two objects, defined by a list of keys.
128  *
129  * @param {Object} a The first object.
130  * @param {Object} b The second object.
131  * @param {Array} keys The list of keys within each object to compare. If the list is empty or not specified,
132  *                     it defaults to the list of keys in `a`.
133  * @return {Boolean} Returns `true` if the keys match, otherwise `false`.
134  */
135 function equalForKeys(a, b, keys) {
136   if (!keys) {
137     keys = [];
138     for (var n in a) keys.push(n); // Used instead of Object.keys() for IE8 compatibility
139   }
140
141   for (var i=0; i<keys.length; i++) {
142     var k = keys[i];
143     if (a[k] != b[k]) return false; // Not '===', values aren't necessarily normalized
144   }
145   return true;
146 }
147
148 /**
149  * Returns the subset of an object, based on a list of keys.
150  *
151  * @param {Array} keys
152  * @param {Object} values
153  * @return {Boolean} Returns a subset of `values`.
154  */
155 function filterByKeys(keys, values) {
156   var filtered = {};
157
158   forEach(keys, function (name) {
159     filtered[name] = values[name];
160   });
161   return filtered;
162 }
163
164 // like _.indexBy
165 // when you know that your index values will be unique, or you want last-one-in to win
166 function indexBy(array, propName) {
167   var result = {};
168   forEach(array, function(item) {
169     result[item[propName]] = item;
170   });
171   return result;
172 }
173
174 // extracted from underscore.js
175 // Return a copy of the object only containing the whitelisted properties.
176 function pick(obj) {
177   var copy = {};
178   var keys = Array.prototype.concat.apply(Array.prototype, Array.prototype.slice.call(arguments, 1));
179   forEach(keys, function(key) {
180     if (key in obj) copy[key] = obj[key];
181   });
182   return copy;
183 }
184
185 // extracted from underscore.js
186 // Return a copy of the object omitting the blacklisted properties.
187 function omit(obj) {
188   var copy = {};
189   var keys = Array.prototype.concat.apply(Array.prototype, Array.prototype.slice.call(arguments, 1));
190   for (var key in obj) {
191     if (indexOf(keys, key) == -1) copy[key] = obj[key];
192   }
193   return copy;
194 }
195
196 function pluck(collection, key) {
197   var result = isArray(collection) ? [] : {};
198
199   forEach(collection, function(val, i) {
200     result[i] = isFunction(key) ? key(val) : val[key];
201   });
202   return result;
203 }
204
205 function filter(collection, callback) {
206   var array = isArray(collection);
207   var result = array ? [] : {};
208   forEach(collection, function(val, i) {
209     if (callback(val, i)) {
210       result[array ? result.length : i] = val;
211     }
212   });
213   return result;
214 }
215
216 function map(collection, callback) {
217   var result = isArray(collection) ? [] : {};
218
219   forEach(collection, function(val, i) {
220     result[i] = callback(val, i);
221   });
222   return result;
223 }
224
225 // issue #2676 #2889
226 function silenceUncaughtInPromise (promise) {
227   return promise.then(undefined, function() {}) && promise;
228 }
229
230 /**
231  * @ngdoc overview
232  * @name ui.router.util
233  *
234  * @description
235  * # ui.router.util sub-module
236  *
237  * This module is a dependency of other sub-modules. Do not include this module as a dependency
238  * in your angular app (use {@link ui.router} module instead).
239  *
240  */
241 angular.module('ui.router.util', ['ng']);
242
243 /**
244  * @ngdoc overview
245  * @name ui.router.router
246  * 
247  * @requires ui.router.util
248  *
249  * @description
250  * # ui.router.router sub-module
251  *
252  * This module is a dependency of other sub-modules. Do not include this module as a dependency
253  * in your angular app (use {@link ui.router} module instead).
254  */
255 angular.module('ui.router.router', ['ui.router.util']);
256
257 /**
258  * @ngdoc overview
259  * @name ui.router.state
260  * 
261  * @requires ui.router.router
262  * @requires ui.router.util
263  *
264  * @description
265  * # ui.router.state sub-module
266  *
267  * This module is a dependency of the main ui.router module. Do not include this module as a dependency
268  * in your angular app (use {@link ui.router} module instead).
269  * 
270  */
271 angular.module('ui.router.state', ['ui.router.router', 'ui.router.util']);
272
273 /**
274  * @ngdoc overview
275  * @name ui.router
276  *
277  * @requires ui.router.state
278  *
279  * @description
280  * # ui.router
281  * 
282  * ## The main module for ui.router 
283  * There are several sub-modules included with the ui.router module, however only this module is needed
284  * as a dependency within your angular app. The other modules are for organization purposes. 
285  *
286  * The modules are:
287  * * ui.router - the main "umbrella" module
288  * * ui.router.router - 
289  * 
290  * *You'll need to include **only** this module as the dependency within your angular app.*
291  * 
292  * <pre>
293  * <!doctype html>
294  * <html ng-app="myApp">
295  * <head>
296  *   <script src="js/angular.js"></script>
297  *   <!-- Include the ui-router script -->
298  *   <script src="js/angular-ui-router.min.js"></script>
299  *   <script>
300  *     // ...and add 'ui.router' as a dependency
301  *     var myApp = angular.module('myApp', ['ui.router']);
302  *   </script>
303  * </head>
304  * <body>
305  * </body>
306  * </html>
307  * </pre>
308  */
309 angular.module('ui.router', ['ui.router.state']);
310
311 angular.module('ui.router.compat', ['ui.router']);
312
313 /**
314  * @ngdoc object
315  * @name ui.router.util.$resolve
316  *
317  * @requires $q
318  * @requires $injector
319  *
320  * @description
321  * Manages resolution of (acyclic) graphs of promises.
322  */
323 $Resolve.$inject = ['$q', '$injector'];
324 function $Resolve(  $q,    $injector) {
325   
326   var VISIT_IN_PROGRESS = 1,
327       VISIT_DONE = 2,
328       NOTHING = {},
329       NO_DEPENDENCIES = [],
330       NO_LOCALS = NOTHING,
331       NO_PARENT = extend($q.when(NOTHING), { $$promises: NOTHING, $$values: NOTHING });
332   
333
334   /**
335    * @ngdoc function
336    * @name ui.router.util.$resolve#study
337    * @methodOf ui.router.util.$resolve
338    *
339    * @description
340    * Studies a set of invocables that are likely to be used multiple times.
341    * <pre>
342    * $resolve.study(invocables)(locals, parent, self)
343    * </pre>
344    * is equivalent to
345    * <pre>
346    * $resolve.resolve(invocables, locals, parent, self)
347    * </pre>
348    * but the former is more efficient (in fact `resolve` just calls `study` 
349    * internally).
350    *
351    * @param {object} invocables Invocable objects
352    * @return {function} a function to pass in locals, parent and self
353    */
354   this.study = function (invocables) {
355     if (!isObject(invocables)) throw new Error("'invocables' must be an object");
356     var invocableKeys = objectKeys(invocables || {});
357     
358     // Perform a topological sort of invocables to build an ordered plan
359     var plan = [], cycle = [], visited = {};
360     function visit(value, key) {
361       if (visited[key] === VISIT_DONE) return;
362       
363       cycle.push(key);
364       if (visited[key] === VISIT_IN_PROGRESS) {
365         cycle.splice(0, indexOf(cycle, key));
366         throw new Error("Cyclic dependency: " + cycle.join(" -> "));
367       }
368       visited[key] = VISIT_IN_PROGRESS;
369       
370       if (isString(value)) {
371         plan.push(key, [ function() { return $injector.get(value); }], NO_DEPENDENCIES);
372       } else {
373         var params = $injector.annotate(value);
374         forEach(params, function (param) {
375           if (param !== key && invocables.hasOwnProperty(param)) visit(invocables[param], param);
376         });
377         plan.push(key, value, params);
378       }
379       
380       cycle.pop();
381       visited[key] = VISIT_DONE;
382     }
383     forEach(invocables, visit);
384     invocables = cycle = visited = null; // plan is all that's required
385     
386     function isResolve(value) {
387       return isObject(value) && value.then && value.$$promises;
388     }
389     
390     return function (locals, parent, self) {
391       if (isResolve(locals) && self === undefined) {
392         self = parent; parent = locals; locals = null;
393       }
394       if (!locals) locals = NO_LOCALS;
395       else if (!isObject(locals)) {
396         throw new Error("'locals' must be an object");
397       }       
398       if (!parent) parent = NO_PARENT;
399       else if (!isResolve(parent)) {
400         throw new Error("'parent' must be a promise returned by $resolve.resolve()");
401       }
402       
403       // To complete the overall resolution, we have to wait for the parent
404       // promise and for the promise for each invokable in our plan.
405       var resolution = $q.defer(),
406           result = silenceUncaughtInPromise(resolution.promise),
407           promises = result.$$promises = {},
408           values = extend({}, locals),
409           wait = 1 + plan.length/3,
410           merged = false;
411
412       silenceUncaughtInPromise(result);
413           
414       function done() {
415         // Merge parent values we haven't got yet and publish our own $$values
416         if (!--wait) {
417           if (!merged) merge(values, parent.$$values); 
418           result.$$values = values;
419           result.$$promises = result.$$promises || true; // keep for isResolve()
420           delete result.$$inheritedValues;
421           resolution.resolve(values);
422         }
423       }
424       
425       function fail(reason) {
426         result.$$failure = reason;
427         resolution.reject(reason);
428       }
429
430       // Short-circuit if parent has already failed
431       if (isDefined(parent.$$failure)) {
432         fail(parent.$$failure);
433         return result;
434       }
435       
436       if (parent.$$inheritedValues) {
437         merge(values, omit(parent.$$inheritedValues, invocableKeys));
438       }
439
440       // Merge parent values if the parent has already resolved, or merge
441       // parent promises and wait if the parent resolve is still in progress.
442       extend(promises, parent.$$promises);
443       if (parent.$$values) {
444         merged = merge(values, omit(parent.$$values, invocableKeys));
445         result.$$inheritedValues = omit(parent.$$values, invocableKeys);
446         done();
447       } else {
448         if (parent.$$inheritedValues) {
449           result.$$inheritedValues = omit(parent.$$inheritedValues, invocableKeys);
450         }        
451         parent.then(done, fail);
452       }
453       
454       // Process each invocable in the plan, but ignore any where a local of the same name exists.
455       for (var i=0, ii=plan.length; i<ii; i+=3) {
456         if (locals.hasOwnProperty(plan[i])) done();
457         else invoke(plan[i], plan[i+1], plan[i+2]);
458       }
459       
460       function invoke(key, invocable, params) {
461         // Create a deferred for this invocation. Failures will propagate to the resolution as well.
462         var invocation = $q.defer(), waitParams = 0;
463         function onfailure(reason) {
464           invocation.reject(reason);
465           fail(reason);
466         }
467         // Wait for any parameter that we have a promise for (either from parent or from this
468         // resolve; in that case study() will have made sure it's ordered before us in the plan).
469         forEach(params, function (dep) {
470           if (promises.hasOwnProperty(dep) && !locals.hasOwnProperty(dep)) {
471             waitParams++;
472             promises[dep].then(function (result) {
473               values[dep] = result;
474               if (!(--waitParams)) proceed();
475             }, onfailure);
476           }
477         });
478         if (!waitParams) proceed();
479         function proceed() {
480           if (isDefined(result.$$failure)) return;
481           try {
482             invocation.resolve($injector.invoke(invocable, self, values));
483             invocation.promise.then(function (result) {
484               values[key] = result;
485               done();
486             }, onfailure);
487           } catch (e) {
488             onfailure(e);
489           }
490         }
491         // Publish promise synchronously; invocations further down in the plan may depend on it.
492         promises[key] = silenceUncaughtInPromise(invocation.promise);
493       }
494       
495       return result;
496     };
497   };
498   
499   /**
500    * @ngdoc function
501    * @name ui.router.util.$resolve#resolve
502    * @methodOf ui.router.util.$resolve
503    *
504    * @description
505    * Resolves a set of invocables. An invocable is a function to be invoked via 
506    * `$injector.invoke()`, and can have an arbitrary number of dependencies. 
507    * An invocable can either return a value directly,
508    * or a `$q` promise. If a promise is returned it will be resolved and the 
509    * resulting value will be used instead. Dependencies of invocables are resolved 
510    * (in this order of precedence)
511    *
512    * - from the specified `locals`
513    * - from another invocable that is part of this `$resolve` call
514    * - from an invocable that is inherited from a `parent` call to `$resolve` 
515    *   (or recursively
516    * - from any ancestor `$resolve` of that parent).
517    *
518    * The return value of `$resolve` is a promise for an object that contains 
519    * (in this order of precedence)
520    *
521    * - any `locals` (if specified)
522    * - the resolved return values of all injectables
523    * - any values inherited from a `parent` call to `$resolve` (if specified)
524    *
525    * The promise will resolve after the `parent` promise (if any) and all promises 
526    * returned by injectables have been resolved. If any invocable 
527    * (or `$injector.invoke`) throws an exception, or if a promise returned by an 
528    * invocable is rejected, the `$resolve` promise is immediately rejected with the 
529    * same error. A rejection of a `parent` promise (if specified) will likewise be 
530    * propagated immediately. Once the `$resolve` promise has been rejected, no 
531    * further invocables will be called.
532    * 
533    * Cyclic dependencies between invocables are not permitted and will cause `$resolve`
534    * to throw an error. As a special case, an injectable can depend on a parameter 
535    * with the same name as the injectable, which will be fulfilled from the `parent` 
536    * injectable of the same name. This allows inherited values to be decorated. 
537    * Note that in this case any other injectable in the same `$resolve` with the same
538    * dependency would see the decorated value, not the inherited value.
539    *
540    * Note that missing dependencies -- unlike cyclic dependencies -- will cause an 
541    * (asynchronous) rejection of the `$resolve` promise rather than a (synchronous) 
542    * exception.
543    *
544    * Invocables are invoked eagerly as soon as all dependencies are available. 
545    * This is true even for dependencies inherited from a `parent` call to `$resolve`.
546    *
547    * As a special case, an invocable can be a string, in which case it is taken to 
548    * be a service name to be passed to `$injector.get()`. This is supported primarily 
549    * for backwards-compatibility with the `resolve` property of `$routeProvider` 
550    * routes.
551    *
552    * @param {object} invocables functions to invoke or 
553    * `$injector` services to fetch.
554    * @param {object} locals  values to make available to the injectables
555    * @param {object} parent  a promise returned by another call to `$resolve`.
556    * @param {object} self  the `this` for the invoked methods
557    * @return {object} Promise for an object that contains the resolved return value
558    * of all invocables, as well as any inherited and local values.
559    */
560   this.resolve = function (invocables, locals, parent, self) {
561     return this.study(invocables)(locals, parent, self);
562   };
563 }
564
565 angular.module('ui.router.util').service('$resolve', $Resolve);
566
567
568
569 /**
570  * @ngdoc object
571  * @name ui.router.util.$templateFactoryProvider
572  *
573  * @description
574  * Provider for $templateFactory. Manages which template-loading mechanism to
575  * use, and will default to the most recent one ($templateRequest on Angular
576  * versions starting from 1.3, $http otherwise).
577  */
578 function TemplateFactoryProvider() {
579   var shouldUnsafelyUseHttp = angular.version.minor < 3;
580
581   /**
582    * @ngdoc function
583    * @name ui.router.util.$templateFactoryProvider#shouldUnsafelyUseHttp
584    * @methodOf ui.router.util.$templateFactoryProvider
585    *
586    * @description
587    * Forces $templateFactory to use $http instead of $templateRequest. This
588    * might cause XSS, as $http doesn't enforce the regular security checks for
589    * templates that have been introduced in Angular 1.3. Note that setting this
590    * to false on Angular older than 1.3.x will crash, as the $templateRequest
591    * service (and the security checks) are not implemented on these versions.
592    *
593    * See the $sce documentation, section
594    * <a href="https://docs.angularjs.org/api/ng/service/$sce#impact-on-loading-templates">
595    * Impact on loading templates</a> for more details about this mechanism.
596    *
597    * @param {boolean} value
598    */
599   this.shouldUnsafelyUseHttp = function(value) {
600     shouldUnsafelyUseHttp = !!value;
601   };
602
603   /**
604    * @ngdoc object
605    * @name ui.router.util.$templateFactory
606    *
607    * @requires $http
608    * @requires $templateCache
609    * @requires $injector
610    *
611    * @description
612    * Service. Manages loading of templates.
613    */
614   this.$get = ['$http', '$templateCache', '$injector', function($http, $templateCache, $injector){
615     return new TemplateFactory($http, $templateCache, $injector, shouldUnsafelyUseHttp);}];
616 }
617
618
619 /**
620  * @ngdoc object
621  * @name ui.router.util.$templateFactory
622  *
623  * @requires $http
624  * @requires $templateCache
625  * @requires $injector
626  *
627  * @description
628  * Service. Manages loading of templates.
629  */
630 function TemplateFactory($http, $templateCache, $injector, shouldUnsafelyUseHttp) {
631
632   /**
633    * @ngdoc function
634    * @name ui.router.util.$templateFactory#fromConfig
635    * @methodOf ui.router.util.$templateFactory
636    *
637    * @description
638    * Creates a template from a configuration object. 
639    *
640    * @param {object} config Configuration object for which to load a template. 
641    * The following properties are search in the specified order, and the first one 
642    * that is defined is used to create the template:
643    *
644    * @param {string|object} config.template html string template or function to 
645    * load via {@link ui.router.util.$templateFactory#fromString fromString}.
646    * @param {string|object} config.templateUrl url to load or a function returning 
647    * the url to load via {@link ui.router.util.$templateFactory#fromUrl fromUrl}.
648    * @param {Function} config.templateProvider function to invoke via 
649    * {@link ui.router.util.$templateFactory#fromProvider fromProvider}.
650    * @param {object} params  Parameters to pass to the template function.
651    * @param {object} locals Locals to pass to `invoke` if the template is loaded 
652    * via a `templateProvider`. Defaults to `{ params: params }`.
653    *
654    * @return {string|object}  The template html as a string, or a promise for 
655    * that string,or `null` if no template is configured.
656    */
657   this.fromConfig = function (config, params, locals) {
658     return (
659       isDefined(config.template) ? this.fromString(config.template, params) :
660       isDefined(config.templateUrl) ? this.fromUrl(config.templateUrl, params) :
661       isDefined(config.templateProvider) ? this.fromProvider(config.templateProvider, params, locals) :
662       null
663     );
664   };
665
666   /**
667    * @ngdoc function
668    * @name ui.router.util.$templateFactory#fromString
669    * @methodOf ui.router.util.$templateFactory
670    *
671    * @description
672    * Creates a template from a string or a function returning a string.
673    *
674    * @param {string|object} template html template as a string or function that 
675    * returns an html template as a string.
676    * @param {object} params Parameters to pass to the template function.
677    *
678    * @return {string|object} The template html as a string, or a promise for that 
679    * string.
680    */
681   this.fromString = function (template, params) {
682     return isFunction(template) ? template(params) : template;
683   };
684
685   /**
686    * @ngdoc function
687    * @name ui.router.util.$templateFactory#fromUrl
688    * @methodOf ui.router.util.$templateFactory
689    * 
690    * @description
691    * Loads a template from the a URL via `$http` and `$templateCache`.
692    *
693    * @param {string|Function} url url of the template to load, or a function 
694    * that returns a url.
695    * @param {Object} params Parameters to pass to the url function.
696    * @return {string|Promise.<string>} The template html as a string, or a promise 
697    * for that string.
698    */
699   this.fromUrl = function (url, params) {
700     if (isFunction(url)) url = url(params);
701     if (url == null) return null;
702     else {
703       if(!shouldUnsafelyUseHttp) {
704         return $injector.get('$templateRequest')(url);
705       } else {
706         return $http
707           .get(url, { cache: $templateCache, headers: { Accept: 'text/html' }})
708           .then(function(response) { return response.data; });
709       }
710     }
711   };
712
713   /**
714    * @ngdoc function
715    * @name ui.router.util.$templateFactory#fromProvider
716    * @methodOf ui.router.util.$templateFactory
717    *
718    * @description
719    * Creates a template by invoking an injectable provider function.
720    *
721    * @param {Function} provider Function to invoke via `$injector.invoke`
722    * @param {Object} params Parameters for the template.
723    * @param {Object} locals Locals to pass to `invoke`. Defaults to 
724    * `{ params: params }`.
725    * @return {string|Promise.<string>} The template html as a string, or a promise 
726    * for that string.
727    */
728   this.fromProvider = function (provider, params, locals) {
729     return $injector.invoke(provider, null, locals || { params: params });
730   };
731 }
732
733 angular.module('ui.router.util').provider('$templateFactory', TemplateFactoryProvider);
734
735 var $$UMFP; // reference to $UrlMatcherFactoryProvider
736
737 /**
738  * @ngdoc object
739  * @name ui.router.util.type:UrlMatcher
740  *
741  * @description
742  * Matches URLs against patterns and extracts named parameters from the path or the search
743  * part of the URL. A URL pattern consists of a path pattern, optionally followed by '?' and a list
744  * of search parameters. Multiple search parameter names are separated by '&'. Search parameters
745  * do not influence whether or not a URL is matched, but their values are passed through into
746  * the matched parameters returned by {@link ui.router.util.type:UrlMatcher#methods_exec exec}.
747  *
748  * Path parameter placeholders can be specified using simple colon/catch-all syntax or curly brace
749  * syntax, which optionally allows a regular expression for the parameter to be specified:
750  *
751  * * `':'` name - colon placeholder
752  * * `'*'` name - catch-all placeholder
753  * * `'{' name '}'` - curly placeholder
754  * * `'{' name ':' regexp|type '}'` - curly placeholder with regexp or type name. Should the
755  *   regexp itself contain curly braces, they must be in matched pairs or escaped with a backslash.
756  *
757  * Parameter names may contain only word characters (latin letters, digits, and underscore) and
758  * must be unique within the pattern (across both path and search parameters). For colon
759  * placeholders or curly placeholders without an explicit regexp, a path parameter matches any
760  * number of characters other than '/'. For catch-all placeholders the path parameter matches
761  * any number of characters.
762  *
763  * Examples:
764  *
765  * * `'/hello/'` - Matches only if the path is exactly '/hello/'. There is no special treatment for
766  *   trailing slashes, and patterns have to match the entire path, not just a prefix.
767  * * `'/user/:id'` - Matches '/user/bob' or '/user/1234!!!' or even '/user/' but not '/user' or
768  *   '/user/bob/details'. The second path segment will be captured as the parameter 'id'.
769  * * `'/user/{id}'` - Same as the previous example, but using curly brace syntax.
770  * * `'/user/{id:[^/]*}'` - Same as the previous example.
771  * * `'/user/{id:[0-9a-fA-F]{1,8}}'` - Similar to the previous example, but only matches if the id
772  *   parameter consists of 1 to 8 hex digits.
773  * * `'/files/{path:.*}'` - Matches any URL starting with '/files/' and captures the rest of the
774  *   path into the parameter 'path'.
775  * * `'/files/*path'` - ditto.
776  * * `'/calendar/{start:date}'` - Matches "/calendar/2014-11-12" (because the pattern defined
777  *   in the built-in  `date` Type matches `2014-11-12`) and provides a Date object in $stateParams.start
778  *
779  * @param {string} pattern  The pattern to compile into a matcher.
780  * @param {Object} config  A configuration object hash:
781  * @param {Object=} parentMatcher Used to concatenate the pattern/config onto
782  *   an existing UrlMatcher
783  *
784  * * `caseInsensitive` - `true` if URL matching should be case insensitive, otherwise `false`, the default value (for backward compatibility) is `false`.
785  * * `strict` - `false` if matching against a URL with a trailing slash should be treated as equivalent to a URL without a trailing slash, the default value is `true`.
786  *
787  * @property {string} prefix  A static prefix of this pattern. The matcher guarantees that any
788  *   URL matching this matcher (i.e. any string for which {@link ui.router.util.type:UrlMatcher#methods_exec exec()} returns
789  *   non-null) will start with this prefix.
790  *
791  * @property {string} source  The pattern that was passed into the constructor
792  *
793  * @property {string} sourcePath  The path portion of the source property
794  *
795  * @property {string} sourceSearch  The search portion of the source property
796  *
797  * @property {string} regex  The constructed regex that will be used to match against the url when
798  *   it is time to determine which url will match.
799  *
800  * @returns {Object}  New `UrlMatcher` object
801  */
802 function UrlMatcher(pattern, config, parentMatcher) {
803   config = extend({ params: {} }, isObject(config) ? config : {});
804
805   // Find all placeholders and create a compiled pattern, using either classic or curly syntax:
806   //   '*' name
807   //   ':' name
808   //   '{' name '}'
809   //   '{' name ':' regexp '}'
810   // The regular expression is somewhat complicated due to the need to allow curly braces
811   // inside the regular expression. The placeholder regexp breaks down as follows:
812   //    ([:*])([\w\[\]]+)              - classic placeholder ($1 / $2) (search version has - for snake-case)
813   //    \{([\w\[\]]+)(?:\:\s*( ... ))?\}  - curly brace placeholder ($3) with optional regexp/type ... ($4) (search version has - for snake-case
814   //    (?: ... | ... | ... )+         - the regexp consists of any number of atoms, an atom being either
815   //    [^{}\\]+                       - anything other than curly braces or backslash
816   //    \\.                            - a backslash escape
817   //    \{(?:[^{}\\]+|\\.)*\}          - a matched set of curly braces containing other atoms
818   var placeholder       = /([:*])([\w\[\]]+)|\{([\w\[\]]+)(?:\:\s*((?:[^{}\\]+|\\.|\{(?:[^{}\\]+|\\.)*\})+))?\}/g,
819       searchPlaceholder = /([:]?)([\w\[\].-]+)|\{([\w\[\].-]+)(?:\:\s*((?:[^{}\\]+|\\.|\{(?:[^{}\\]+|\\.)*\})+))?\}/g,
820       compiled = '^', last = 0, m,
821       segments = this.segments = [],
822       parentParams = parentMatcher ? parentMatcher.params : {},
823       params = this.params = parentMatcher ? parentMatcher.params.$$new() : new $$UMFP.ParamSet(),
824       paramNames = [];
825
826   function addParameter(id, type, config, location) {
827     paramNames.push(id);
828     if (parentParams[id]) return parentParams[id];
829     if (!/^\w+([-.]+\w+)*(?:\[\])?$/.test(id)) throw new Error("Invalid parameter name '" + id + "' in pattern '" + pattern + "'");
830     if (params[id]) throw new Error("Duplicate parameter name '" + id + "' in pattern '" + pattern + "'");
831     params[id] = new $$UMFP.Param(id, type, config, location);
832     return params[id];
833   }
834
835   function quoteRegExp(string, pattern, squash, optional) {
836     var surroundPattern = ['',''], result = string.replace(/[\\\[\]\^$*+?.()|{}]/g, "\\$&");
837     if (!pattern) return result;
838     switch(squash) {
839       case false: surroundPattern = ['(', ')' + (optional ? "?" : "")]; break;
840       case true:
841         result = result.replace(/\/$/, '');
842         surroundPattern = ['(?:\/(', ')|\/)?'];
843       break;
844       default:    surroundPattern = ['(' + squash + "|", ')?']; break;
845     }
846     return result + surroundPattern[0] + pattern + surroundPattern[1];
847   }
848
849   this.source = pattern;
850
851   // Split into static segments separated by path parameter placeholders.
852   // The number of segments is always 1 more than the number of parameters.
853   function matchDetails(m, isSearch) {
854     var id, regexp, segment, type, cfg, arrayMode;
855     id          = m[2] || m[3]; // IE[78] returns '' for unmatched groups instead of null
856     cfg         = config.params[id];
857     segment     = pattern.substring(last, m.index);
858     regexp      = isSearch ? m[4] : m[4] || (m[1] == '*' ? '.*' : null);
859
860     if (regexp) {
861       type      = $$UMFP.type(regexp) || inherit($$UMFP.type("string"), { pattern: new RegExp(regexp, config.caseInsensitive ? 'i' : undefined) });
862     }
863
864     return {
865       id: id, regexp: regexp, segment: segment, type: type, cfg: cfg
866     };
867   }
868
869   var p, param, segment;
870   while ((m = placeholder.exec(pattern))) {
871     p = matchDetails(m, false);
872     if (p.segment.indexOf('?') >= 0) break; // we're into the search part
873
874     param = addParameter(p.id, p.type, p.cfg, "path");
875     compiled += quoteRegExp(p.segment, param.type.pattern.source, param.squash, param.isOptional);
876     segments.push(p.segment);
877     last = placeholder.lastIndex;
878   }
879   segment = pattern.substring(last);
880
881   // Find any search parameter names and remove them from the last segment
882   var i = segment.indexOf('?');
883
884   if (i >= 0) {
885     var search = this.sourceSearch = segment.substring(i);
886     segment = segment.substring(0, i);
887     this.sourcePath = pattern.substring(0, last + i);
888
889     if (search.length > 0) {
890       last = 0;
891       while ((m = searchPlaceholder.exec(search))) {
892         p = matchDetails(m, true);
893         param = addParameter(p.id, p.type, p.cfg, "search");
894         last = placeholder.lastIndex;
895         // check if ?&
896       }
897     }
898   } else {
899     this.sourcePath = pattern;
900     this.sourceSearch = '';
901   }
902
903   compiled += quoteRegExp(segment) + (config.strict === false ? '\/?' : '') + '$';
904   segments.push(segment);
905
906   this.regexp = new RegExp(compiled, config.caseInsensitive ? 'i' : undefined);
907   this.prefix = segments[0];
908   this.$$paramNames = paramNames;
909 }
910
911 /**
912  * @ngdoc function
913  * @name ui.router.util.type:UrlMatcher#concat
914  * @methodOf ui.router.util.type:UrlMatcher
915  *
916  * @description
917  * Returns a new matcher for a pattern constructed by appending the path part and adding the
918  * search parameters of the specified pattern to this pattern. The current pattern is not
919  * modified. This can be understood as creating a pattern for URLs that are relative to (or
920  * suffixes of) the current pattern.
921  *
922  * @example
923  * The following two matchers are equivalent:
924  * <pre>
925  * new UrlMatcher('/user/{id}?q').concat('/details?date');
926  * new UrlMatcher('/user/{id}/details?q&date');
927  * </pre>
928  *
929  * @param {string} pattern  The pattern to append.
930  * @param {Object} config  An object hash of the configuration for the matcher.
931  * @returns {UrlMatcher}  A matcher for the concatenated pattern.
932  */
933 UrlMatcher.prototype.concat = function (pattern, config) {
934   // Because order of search parameters is irrelevant, we can add our own search
935   // parameters to the end of the new pattern. Parse the new pattern by itself
936   // and then join the bits together, but it's much easier to do this on a string level.
937   var defaultConfig = {
938     caseInsensitive: $$UMFP.caseInsensitive(),
939     strict: $$UMFP.strictMode(),
940     squash: $$UMFP.defaultSquashPolicy()
941   };
942   return new UrlMatcher(this.sourcePath + pattern + this.sourceSearch, extend(defaultConfig, config), this);
943 };
944
945 UrlMatcher.prototype.toString = function () {
946   return this.source;
947 };
948
949 /**
950  * @ngdoc function
951  * @name ui.router.util.type:UrlMatcher#exec
952  * @methodOf ui.router.util.type:UrlMatcher
953  *
954  * @description
955  * Tests the specified path against this matcher, and returns an object containing the captured
956  * parameter values, or null if the path does not match. The returned object contains the values
957  * of any search parameters that are mentioned in the pattern, but their value may be null if
958  * they are not present in `searchParams`. This means that search parameters are always treated
959  * as optional.
960  *
961  * @example
962  * <pre>
963  * new UrlMatcher('/user/{id}?q&r').exec('/user/bob', {
964  *   x: '1', q: 'hello'
965  * });
966  * // returns { id: 'bob', q: 'hello', r: null }
967  * </pre>
968  *
969  * @param {string} path  The URL path to match, e.g. `$location.path()`.
970  * @param {Object} searchParams  URL search parameters, e.g. `$location.search()`.
971  * @returns {Object}  The captured parameter values.
972  */
973 UrlMatcher.prototype.exec = function (path, searchParams) {
974   var m = this.regexp.exec(path);
975   if (!m) return null;
976   searchParams = searchParams || {};
977
978   var paramNames = this.parameters(), nTotal = paramNames.length,
979     nPath = this.segments.length - 1,
980     values = {}, i, j, cfg, paramName;
981
982   if (nPath !== m.length - 1) throw new Error("Unbalanced capture group in route '" + this.source + "'");
983
984   function decodePathArray(string) {
985     function reverseString(str) { return str.split("").reverse().join(""); }
986     function unquoteDashes(str) { return str.replace(/\\-/g, "-"); }
987
988     var split = reverseString(string).split(/-(?!\\)/);
989     var allReversed = map(split, reverseString);
990     return map(allReversed, unquoteDashes).reverse();
991   }
992
993   var param, paramVal;
994   for (i = 0; i < nPath; i++) {
995     paramName = paramNames[i];
996     param = this.params[paramName];
997     paramVal = m[i+1];
998     // if the param value matches a pre-replace pair, replace the value before decoding.
999     for (j = 0; j < param.replace.length; j++) {
1000       if (param.replace[j].from === paramVal) paramVal = param.replace[j].to;
1001     }
1002     if (paramVal && param.array === true) paramVal = decodePathArray(paramVal);
1003     if (isDefined(paramVal)) paramVal = param.type.decode(paramVal);
1004     values[paramName] = param.value(paramVal);
1005   }
1006   for (/**/; i < nTotal; i++) {
1007     paramName = paramNames[i];
1008     values[paramName] = this.params[paramName].value(searchParams[paramName]);
1009     param = this.params[paramName];
1010     paramVal = searchParams[paramName];
1011     for (j = 0; j < param.replace.length; j++) {
1012       if (param.replace[j].from === paramVal) paramVal = param.replace[j].to;
1013     }
1014     if (isDefined(paramVal)) paramVal = param.type.decode(paramVal);
1015     values[paramName] = param.value(paramVal);
1016   }
1017
1018   return values;
1019 };
1020
1021 /**
1022  * @ngdoc function
1023  * @name ui.router.util.type:UrlMatcher#parameters
1024  * @methodOf ui.router.util.type:UrlMatcher
1025  *
1026  * @description
1027  * Returns the names of all path and search parameters of this pattern in an unspecified order.
1028  *
1029  * @returns {Array.<string>}  An array of parameter names. Must be treated as read-only. If the
1030  *    pattern has no parameters, an empty array is returned.
1031  */
1032 UrlMatcher.prototype.parameters = function (param) {
1033   if (!isDefined(param)) return this.$$paramNames;
1034   return this.params[param] || null;
1035 };
1036
1037 /**
1038  * @ngdoc function
1039  * @name ui.router.util.type:UrlMatcher#validates
1040  * @methodOf ui.router.util.type:UrlMatcher
1041  *
1042  * @description
1043  * Checks an object hash of parameters to validate their correctness according to the parameter
1044  * types of this `UrlMatcher`.
1045  *
1046  * @param {Object} params The object hash of parameters to validate.
1047  * @returns {boolean} Returns `true` if `params` validates, otherwise `false`.
1048  */
1049 UrlMatcher.prototype.validates = function (params) {
1050   return this.params.$$validates(params);
1051 };
1052
1053 /**
1054  * @ngdoc function
1055  * @name ui.router.util.type:UrlMatcher#format
1056  * @methodOf ui.router.util.type:UrlMatcher
1057  *
1058  * @description
1059  * Creates a URL that matches this pattern by substituting the specified values
1060  * for the path and search parameters. Null values for path parameters are
1061  * treated as empty strings.
1062  *
1063  * @example
1064  * <pre>
1065  * new UrlMatcher('/user/{id}?q').format({ id:'bob', q:'yes' });
1066  * // returns '/user/bob?q=yes'
1067  * </pre>
1068  *
1069  * @param {Object} values  the values to substitute for the parameters in this pattern.
1070  * @returns {string}  the formatted URL (path and optionally search part).
1071  */
1072 UrlMatcher.prototype.format = function (values) {
1073   values = values || {};
1074   var segments = this.segments, params = this.parameters(), paramset = this.params;
1075   if (!this.validates(values)) return null;
1076
1077   var i, search = false, nPath = segments.length - 1, nTotal = params.length, result = segments[0];
1078
1079   function encodeDashes(str) { // Replace dashes with encoded "\-"
1080     return encodeURIComponent(str).replace(/-/g, function(c) { return '%5C%' + c.charCodeAt(0).toString(16).toUpperCase(); });
1081   }
1082
1083   for (i = 0; i < nTotal; i++) {
1084     var isPathParam = i < nPath;
1085     var name = params[i], param = paramset[name], value = param.value(values[name]);
1086     var isDefaultValue = param.isOptional && param.type.equals(param.value(), value);
1087     var squash = isDefaultValue ? param.squash : false;
1088     var encoded = param.type.encode(value);
1089
1090     if (isPathParam) {
1091       var nextSegment = segments[i + 1];
1092       var isFinalPathParam = i + 1 === nPath;
1093
1094       if (squash === false) {
1095         if (encoded != null) {
1096           if (isArray(encoded)) {
1097             result += map(encoded, encodeDashes).join("-");
1098           } else {
1099             result += encodeURIComponent(encoded);
1100           }
1101         }
1102         result += nextSegment;
1103       } else if (squash === true) {
1104         var capture = result.match(/\/$/) ? /\/?(.*)/ : /(.*)/;
1105         result += nextSegment.match(capture)[1];
1106       } else if (isString(squash)) {
1107         result += squash + nextSegment;
1108       }
1109
1110       if (isFinalPathParam && param.squash === true && result.slice(-1) === '/') result = result.slice(0, -1);
1111     } else {
1112       if (encoded == null || (isDefaultValue && squash !== false)) continue;
1113       if (!isArray(encoded)) encoded = [ encoded ];
1114       if (encoded.length === 0) continue;
1115       encoded = map(encoded, encodeURIComponent).join('&' + name + '=');
1116       result += (search ? '&' : '?') + (name + '=' + encoded);
1117       search = true;
1118     }
1119   }
1120
1121   return result;
1122 };
1123
1124 /**
1125  * @ngdoc object
1126  * @name ui.router.util.type:Type
1127  *
1128  * @description
1129  * Implements an interface to define custom parameter types that can be decoded from and encoded to
1130  * string parameters matched in a URL. Used by {@link ui.router.util.type:UrlMatcher `UrlMatcher`}
1131  * objects when matching or formatting URLs, or comparing or validating parameter values.
1132  *
1133  * See {@link ui.router.util.$urlMatcherFactory#methods_type `$urlMatcherFactory#type()`} for more
1134  * information on registering custom types.
1135  *
1136  * @param {Object} config  A configuration object which contains the custom type definition.  The object's
1137  *        properties will override the default methods and/or pattern in `Type`'s public interface.
1138  * @example
1139  * <pre>
1140  * {
1141  *   decode: function(val) { return parseInt(val, 10); },
1142  *   encode: function(val) { return val && val.toString(); },
1143  *   equals: function(a, b) { return this.is(a) && a === b; },
1144  *   is: function(val) { return angular.isNumber(val) isFinite(val) && val % 1 === 0; },
1145  *   pattern: /\d+/
1146  * }
1147  * </pre>
1148  *
1149  * @property {RegExp} pattern The regular expression pattern used to match values of this type when
1150  *           coming from a substring of a URL.
1151  *
1152  * @returns {Object}  Returns a new `Type` object.
1153  */
1154 function Type(config) {
1155   extend(this, config);
1156 }
1157
1158 /**
1159  * @ngdoc function
1160  * @name ui.router.util.type:Type#is
1161  * @methodOf ui.router.util.type:Type
1162  *
1163  * @description
1164  * Detects whether a value is of a particular type. Accepts a native (decoded) value
1165  * and determines whether it matches the current `Type` object.
1166  *
1167  * @param {*} val  The value to check.
1168  * @param {string} key  Optional. If the type check is happening in the context of a specific
1169  *        {@link ui.router.util.type:UrlMatcher `UrlMatcher`} object, this is the name of the
1170  *        parameter in which `val` is stored. Can be used for meta-programming of `Type` objects.
1171  * @returns {Boolean}  Returns `true` if the value matches the type, otherwise `false`.
1172  */
1173 Type.prototype.is = function(val, key) {
1174   return true;
1175 };
1176
1177 /**
1178  * @ngdoc function
1179  * @name ui.router.util.type:Type#encode
1180  * @methodOf ui.router.util.type:Type
1181  *
1182  * @description
1183  * Encodes a custom/native type value to a string that can be embedded in a URL. Note that the
1184  * return value does *not* need to be URL-safe (i.e. passed through `encodeURIComponent()`), it
1185  * only needs to be a representation of `val` that has been coerced to a string.
1186  *
1187  * @param {*} val  The value to encode.
1188  * @param {string} key  The name of the parameter in which `val` is stored. Can be used for
1189  *        meta-programming of `Type` objects.
1190  * @returns {string}  Returns a string representation of `val` that can be encoded in a URL.
1191  */
1192 Type.prototype.encode = function(val, key) {
1193   return val;
1194 };
1195
1196 /**
1197  * @ngdoc function
1198  * @name ui.router.util.type:Type#decode
1199  * @methodOf ui.router.util.type:Type
1200  *
1201  * @description
1202  * Converts a parameter value (from URL string or transition param) to a custom/native value.
1203  *
1204  * @param {string} val  The URL parameter value to decode.
1205  * @param {string} key  The name of the parameter in which `val` is stored. Can be used for
1206  *        meta-programming of `Type` objects.
1207  * @returns {*}  Returns a custom representation of the URL parameter value.
1208  */
1209 Type.prototype.decode = function(val, key) {
1210   return val;
1211 };
1212
1213 /**
1214  * @ngdoc function
1215  * @name ui.router.util.type:Type#equals
1216  * @methodOf ui.router.util.type:Type
1217  *
1218  * @description
1219  * Determines whether two decoded values are equivalent.
1220  *
1221  * @param {*} a  A value to compare against.
1222  * @param {*} b  A value to compare against.
1223  * @returns {Boolean}  Returns `true` if the values are equivalent/equal, otherwise `false`.
1224  */
1225 Type.prototype.equals = function(a, b) {
1226   return a == b;
1227 };
1228
1229 Type.prototype.$subPattern = function() {
1230   var sub = this.pattern.toString();
1231   return sub.substr(1, sub.length - 2);
1232 };
1233
1234 Type.prototype.pattern = /.*/;
1235
1236 Type.prototype.toString = function() { return "{Type:" + this.name + "}"; };
1237
1238 /** Given an encoded string, or a decoded object, returns a decoded object */
1239 Type.prototype.$normalize = function(val) {
1240   return this.is(val) ? val : this.decode(val);
1241 };
1242
1243 /*
1244  * Wraps an existing custom Type as an array of Type, depending on 'mode'.
1245  * e.g.:
1246  * - urlmatcher pattern "/path?{queryParam[]:int}"
1247  * - url: "/path?queryParam=1&queryParam=2
1248  * - $stateParams.queryParam will be [1, 2]
1249  * if `mode` is "auto", then
1250  * - url: "/path?queryParam=1 will create $stateParams.queryParam: 1
1251  * - url: "/path?queryParam=1&queryParam=2 will create $stateParams.queryParam: [1, 2]
1252  */
1253 Type.prototype.$asArray = function(mode, isSearch) {
1254   if (!mode) return this;
1255   if (mode === "auto" && !isSearch) throw new Error("'auto' array mode is for query parameters only");
1256
1257   function ArrayType(type, mode) {
1258     function bindTo(type, callbackName) {
1259       return function() {
1260         return type[callbackName].apply(type, arguments);
1261       };
1262     }
1263
1264     // Wrap non-array value as array
1265     function arrayWrap(val) { return isArray(val) ? val : (isDefined(val) ? [ val ] : []); }
1266     // Unwrap array value for "auto" mode. Return undefined for empty array.
1267     function arrayUnwrap(val) {
1268       switch(val.length) {
1269         case 0: return undefined;
1270         case 1: return mode === "auto" ? val[0] : val;
1271         default: return val;
1272       }
1273     }
1274     function falsey(val) { return !val; }
1275
1276     // Wraps type (.is/.encode/.decode) functions to operate on each value of an array
1277     function arrayHandler(callback, allTruthyMode) {
1278       return function handleArray(val) {
1279         if (isArray(val) && val.length === 0) return val;
1280         val = arrayWrap(val);
1281         var result = map(val, callback);
1282         if (allTruthyMode === true)
1283           return filter(result, falsey).length === 0;
1284         return arrayUnwrap(result);
1285       };
1286     }
1287
1288     // Wraps type (.equals) functions to operate on each value of an array
1289     function arrayEqualsHandler(callback) {
1290       return function handleArray(val1, val2) {
1291         var left = arrayWrap(val1), right = arrayWrap(val2);
1292         if (left.length !== right.length) return false;
1293         for (var i = 0; i < left.length; i++) {
1294           if (!callback(left[i], right[i])) return false;
1295         }
1296         return true;
1297       };
1298     }
1299
1300     this.encode = arrayHandler(bindTo(type, 'encode'));
1301     this.decode = arrayHandler(bindTo(type, 'decode'));
1302     this.is     = arrayHandler(bindTo(type, 'is'), true);
1303     this.equals = arrayEqualsHandler(bindTo(type, 'equals'));
1304     this.pattern = type.pattern;
1305     this.$normalize = arrayHandler(bindTo(type, '$normalize'));
1306     this.name = type.name;
1307     this.$arrayMode = mode;
1308   }
1309
1310   return new ArrayType(this, mode);
1311 };
1312
1313
1314
1315 /**
1316  * @ngdoc object
1317  * @name ui.router.util.$urlMatcherFactory
1318  *
1319  * @description
1320  * Factory for {@link ui.router.util.type:UrlMatcher `UrlMatcher`} instances. The factory
1321  * is also available to providers under the name `$urlMatcherFactoryProvider`.
1322  */
1323 function $UrlMatcherFactory() {
1324   $$UMFP = this;
1325
1326   var isCaseInsensitive = false, isStrictMode = true, defaultSquashPolicy = false;
1327
1328   // Use tildes to pre-encode slashes.
1329   // If the slashes are simply URLEncoded, the browser can choose to pre-decode them,
1330   // and bidirectional encoding/decoding fails.
1331   // Tilde was chosen because it's not a RFC 3986 section 2.2 Reserved Character
1332   function valToString(val) { return val != null ? val.toString().replace(/(~|\/)/g, function (m) { return {'~':'~~', '/':'~2F'}[m]; }) : val; }
1333   function valFromString(val) { return val != null ? val.toString().replace(/(~~|~2F)/g, function (m) { return {'~~':'~', '~2F':'/'}[m]; }) : val; }
1334
1335   var $types = {}, enqueue = true, typeQueue = [], injector, defaultTypes = {
1336     "string": {
1337       encode: valToString,
1338       decode: valFromString,
1339       // TODO: in 1.0, make string .is() return false if value is undefined/null by default.
1340       // In 0.2.x, string params are optional by default for backwards compat
1341       is: function(val) { return val == null || !isDefined(val) || typeof val === "string"; },
1342       pattern: /[^/]*/
1343     },
1344     "int": {
1345       encode: valToString,
1346       decode: function(val) { return parseInt(val, 10); },
1347       is: function(val) { return val !== undefined && val !== null && this.decode(val.toString()) === val; },
1348       pattern: /\d+/
1349     },
1350     "bool": {
1351       encode: function(val) { return val ? 1 : 0; },
1352       decode: function(val) { return parseInt(val, 10) !== 0; },
1353       is: function(val) { return val === true || val === false; },
1354       pattern: /0|1/
1355     },
1356     "date": {
1357       encode: function (val) {
1358         if (!this.is(val))
1359           return undefined;
1360         return [ val.getFullYear(),
1361           ('0' + (val.getMonth() + 1)).slice(-2),
1362           ('0' + val.getDate()).slice(-2)
1363         ].join("-");
1364       },
1365       decode: function (val) {
1366         if (this.is(val)) return val;
1367         var match = this.capture.exec(val);
1368         return match ? new Date(match[1], match[2] - 1, match[3]) : undefined;
1369       },
1370       is: function(val) { return val instanceof Date && !isNaN(val.valueOf()); },
1371       equals: function (a, b) { return this.is(a) && this.is(b) && a.toISOString() === b.toISOString(); },
1372       pattern: /[0-9]{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[1-2][0-9]|3[0-1])/,
1373       capture: /([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])/
1374     },
1375     "json": {
1376       encode: angular.toJson,
1377       decode: angular.fromJson,
1378       is: angular.isObject,
1379       equals: angular.equals,
1380       pattern: /[^/]*/
1381     },
1382     "any": { // does not encode/decode
1383       encode: angular.identity,
1384       decode: angular.identity,
1385       equals: angular.equals,
1386       pattern: /.*/
1387     }
1388   };
1389
1390   function getDefaultConfig() {
1391     return {
1392       strict: isStrictMode,
1393       caseInsensitive: isCaseInsensitive
1394     };
1395   }
1396
1397   function isInjectable(value) {
1398     return (isFunction(value) || (isArray(value) && isFunction(value[value.length - 1])));
1399   }
1400
1401   /**
1402    * [Internal] Get the default value of a parameter, which may be an injectable function.
1403    */
1404   $UrlMatcherFactory.$$getDefaultValue = function(config) {
1405     if (!isInjectable(config.value)) return config.value;
1406     if (!injector) throw new Error("Injectable functions cannot be called at configuration time");
1407     return injector.invoke(config.value);
1408   };
1409
1410   /**
1411    * @ngdoc function
1412    * @name ui.router.util.$urlMatcherFactory#caseInsensitive
1413    * @methodOf ui.router.util.$urlMatcherFactory
1414    *
1415    * @description
1416    * Defines whether URL matching should be case sensitive (the default behavior), or not.
1417    *
1418    * @param {boolean} value `false` to match URL in a case sensitive manner; otherwise `true`;
1419    * @returns {boolean} the current value of caseInsensitive
1420    */
1421   this.caseInsensitive = function(value) {
1422     if (isDefined(value))
1423       isCaseInsensitive = value;
1424     return isCaseInsensitive;
1425   };
1426
1427   /**
1428    * @ngdoc function
1429    * @name ui.router.util.$urlMatcherFactory#strictMode
1430    * @methodOf ui.router.util.$urlMatcherFactory
1431    *
1432    * @description
1433    * Defines whether URLs should match trailing slashes, or not (the default behavior).
1434    *
1435    * @param {boolean=} value `false` to match trailing slashes in URLs, otherwise `true`.
1436    * @returns {boolean} the current value of strictMode
1437    */
1438   this.strictMode = function(value) {
1439     if (isDefined(value))
1440       isStrictMode = value;
1441     return isStrictMode;
1442   };
1443
1444   /**
1445    * @ngdoc function
1446    * @name ui.router.util.$urlMatcherFactory#defaultSquashPolicy
1447    * @methodOf ui.router.util.$urlMatcherFactory
1448    *
1449    * @description
1450    * Sets the default behavior when generating or matching URLs with default parameter values.
1451    *
1452    * @param {string} value A string that defines the default parameter URL squashing behavior.
1453    *    `nosquash`: When generating an href with a default parameter value, do not squash the parameter value from the URL
1454    *    `slash`: When generating an href with a default parameter value, squash (remove) the parameter value, and, if the
1455    *             parameter is surrounded by slashes, squash (remove) one slash from the URL
1456    *    any other string, e.g. "~": When generating an href with a default parameter value, squash (remove)
1457    *             the parameter value from the URL and replace it with this string.
1458    */
1459   this.defaultSquashPolicy = function(value) {
1460     if (!isDefined(value)) return defaultSquashPolicy;
1461     if (value !== true && value !== false && !isString(value))
1462       throw new Error("Invalid squash policy: " + value + ". Valid policies: false, true, arbitrary-string");
1463     defaultSquashPolicy = value;
1464     return value;
1465   };
1466
1467   /**
1468    * @ngdoc function
1469    * @name ui.router.util.$urlMatcherFactory#compile
1470    * @methodOf ui.router.util.$urlMatcherFactory
1471    *
1472    * @description
1473    * Creates a {@link ui.router.util.type:UrlMatcher `UrlMatcher`} for the specified pattern.
1474    *
1475    * @param {string} pattern  The URL pattern.
1476    * @param {Object} config  The config object hash.
1477    * @returns {UrlMatcher}  The UrlMatcher.
1478    */
1479   this.compile = function (pattern, config) {
1480     return new UrlMatcher(pattern, extend(getDefaultConfig(), config));
1481   };
1482
1483   /**
1484    * @ngdoc function
1485    * @name ui.router.util.$urlMatcherFactory#isMatcher
1486    * @methodOf ui.router.util.$urlMatcherFactory
1487    *
1488    * @description
1489    * Returns true if the specified object is a `UrlMatcher`, or false otherwise.
1490    *
1491    * @param {Object} object  The object to perform the type check against.
1492    * @returns {Boolean}  Returns `true` if the object matches the `UrlMatcher` interface, by
1493    *          implementing all the same methods.
1494    */
1495   this.isMatcher = function (o) {
1496     if (!isObject(o)) return false;
1497     var result = true;
1498
1499     forEach(UrlMatcher.prototype, function(val, name) {
1500       if (isFunction(val)) {
1501         result = result && (isDefined(o[name]) && isFunction(o[name]));
1502       }
1503     });
1504     return result;
1505   };
1506
1507   /**
1508    * @ngdoc function
1509    * @name ui.router.util.$urlMatcherFactory#type
1510    * @methodOf ui.router.util.$urlMatcherFactory
1511    *
1512    * @description
1513    * Registers a custom {@link ui.router.util.type:Type `Type`} object that can be used to
1514    * generate URLs with typed parameters.
1515    *
1516    * @param {string} name  The type name.
1517    * @param {Object|Function} definition   The type definition. See
1518    *        {@link ui.router.util.type:Type `Type`} for information on the values accepted.
1519    * @param {Object|Function} definitionFn (optional) A function that is injected before the app
1520    *        runtime starts.  The result of this function is merged into the existing `definition`.
1521    *        See {@link ui.router.util.type:Type `Type`} for information on the values accepted.
1522    *
1523    * @returns {Object}  Returns `$urlMatcherFactoryProvider`.
1524    *
1525    * @example
1526    * This is a simple example of a custom type that encodes and decodes items from an
1527    * array, using the array index as the URL-encoded value:
1528    *
1529    * <pre>
1530    * var list = ['John', 'Paul', 'George', 'Ringo'];
1531    *
1532    * $urlMatcherFactoryProvider.type('listItem', {
1533    *   encode: function(item) {
1534    *     // Represent the list item in the URL using its corresponding index
1535    *     return list.indexOf(item);
1536    *   },
1537    *   decode: function(item) {
1538    *     // Look up the list item by index
1539    *     return list[parseInt(item, 10)];
1540    *   },
1541    *   is: function(item) {
1542    *     // Ensure the item is valid by checking to see that it appears
1543    *     // in the list
1544    *     return list.indexOf(item) > -1;
1545    *   }
1546    * });
1547    *
1548    * $stateProvider.state('list', {
1549    *   url: "/list/{item:listItem}",
1550    *   controller: function($scope, $stateParams) {
1551    *     console.log($stateParams.item);
1552    *   }
1553    * });
1554    *
1555    * // ...
1556    *
1557    * // Changes URL to '/list/3', logs "Ringo" to the console
1558    * $state.go('list', { item: "Ringo" });
1559    * </pre>
1560    *
1561    * This is a more complex example of a type that relies on dependency injection to
1562    * interact with services, and uses the parameter name from the URL to infer how to
1563    * handle encoding and decoding parameter values:
1564    *
1565    * <pre>
1566    * // Defines a custom type that gets a value from a service,
1567    * // where each service gets different types of values from
1568    * // a backend API:
1569    * $urlMatcherFactoryProvider.type('dbObject', {}, function(Users, Posts) {
1570    *
1571    *   // Matches up services to URL parameter names
1572    *   var services = {
1573    *     user: Users,
1574    *     post: Posts
1575    *   };
1576    *
1577    *   return {
1578    *     encode: function(object) {
1579    *       // Represent the object in the URL using its unique ID
1580    *       return object.id;
1581    *     },
1582    *     decode: function(value, key) {
1583    *       // Look up the object by ID, using the parameter
1584    *       // name (key) to call the correct service
1585    *       return services[key].findById(value);
1586    *     },
1587    *     is: function(object, key) {
1588    *       // Check that object is a valid dbObject
1589    *       return angular.isObject(object) && object.id && services[key];
1590    *     }
1591    *     equals: function(a, b) {
1592    *       // Check the equality of decoded objects by comparing
1593    *       // their unique IDs
1594    *       return a.id === b.id;
1595    *     }
1596    *   };
1597    * });
1598    *
1599    * // In a config() block, you can then attach URLs with
1600    * // type-annotated parameters:
1601    * $stateProvider.state('users', {
1602    *   url: "/users",
1603    *   // ...
1604    * }).state('users.item', {
1605    *   url: "/{user:dbObject}",
1606    *   controller: function($scope, $stateParams) {
1607    *     // $stateParams.user will now be an object returned from
1608    *     // the Users service
1609    *   },
1610    *   // ...
1611    * });
1612    * </pre>
1613    */
1614   this.type = function (name, definition, definitionFn) {
1615     if (!isDefined(definition)) return $types[name];
1616     if ($types.hasOwnProperty(name)) throw new Error("A type named '" + name + "' has already been defined.");
1617
1618     $types[name] = new Type(extend({ name: name }, definition));
1619     if (definitionFn) {
1620       typeQueue.push({ name: name, def: definitionFn });
1621       if (!enqueue) flushTypeQueue();
1622     }
1623     return this;
1624   };
1625
1626   // `flushTypeQueue()` waits until `$urlMatcherFactory` is injected before invoking the queued `definitionFn`s
1627   function flushTypeQueue() {
1628     while(typeQueue.length) {
1629       var type = typeQueue.shift();
1630       if (type.pattern) throw new Error("You cannot override a type's .pattern at runtime.");
1631       angular.extend($types[type.name], injector.invoke(type.def));
1632     }
1633   }
1634
1635   // Register default types. Store them in the prototype of $types.
1636   forEach(defaultTypes, function(type, name) { $types[name] = new Type(extend({name: name}, type)); });
1637   $types = inherit($types, {});
1638
1639   /* No need to document $get, since it returns this */
1640   this.$get = ['$injector', function ($injector) {
1641     injector = $injector;
1642     enqueue = false;
1643     flushTypeQueue();
1644
1645     forEach(defaultTypes, function(type, name) {
1646       if (!$types[name]) $types[name] = new Type(type);
1647     });
1648     return this;
1649   }];
1650
1651   this.Param = function Param(id, type, config, location) {
1652     var self = this;
1653     config = unwrapShorthand(config);
1654     type = getType(config, type, location);
1655     var arrayMode = getArrayMode();
1656     type = arrayMode ? type.$asArray(arrayMode, location === "search") : type;
1657     if (type.name === "string" && !arrayMode && location === "path" && config.value === undefined)
1658       config.value = ""; // for 0.2.x; in 0.3.0+ do not automatically default to ""
1659     var isOptional = config.value !== undefined;
1660     var squash = getSquashPolicy(config, isOptional);
1661     var replace = getReplace(config, arrayMode, isOptional, squash);
1662
1663     function unwrapShorthand(config) {
1664       var keys = isObject(config) ? objectKeys(config) : [];
1665       var isShorthand = indexOf(keys, "value") === -1 && indexOf(keys, "type") === -1 &&
1666                         indexOf(keys, "squash") === -1 && indexOf(keys, "array") === -1;
1667       if (isShorthand) config = { value: config };
1668       config.$$fn = isInjectable(config.value) ? config.value : function () { return config.value; };
1669       return config;
1670     }
1671
1672     function getType(config, urlType, location) {
1673       if (config.type && urlType) throw new Error("Param '"+id+"' has two type configurations.");
1674       if (urlType) return urlType;
1675       if (!config.type) return (location === "config" ? $types.any : $types.string);
1676
1677       if (angular.isString(config.type))
1678         return $types[config.type];
1679       if (config.type instanceof Type)
1680         return config.type;
1681       return new Type(config.type);
1682     }
1683
1684     // array config: param name (param[]) overrides default settings.  explicit config overrides param name.
1685     function getArrayMode() {
1686       var arrayDefaults = { array: (location === "search" ? "auto" : false) };
1687       var arrayParamNomenclature = id.match(/\[\]$/) ? { array: true } : {};
1688       return extend(arrayDefaults, arrayParamNomenclature, config).array;
1689     }
1690
1691     /**
1692      * returns false, true, or the squash value to indicate the "default parameter url squash policy".
1693      */
1694     function getSquashPolicy(config, isOptional) {
1695       var squash = config.squash;
1696       if (!isOptional || squash === false) return false;
1697       if (!isDefined(squash) || squash == null) return defaultSquashPolicy;
1698       if (squash === true || isString(squash)) return squash;
1699       throw new Error("Invalid squash policy: '" + squash + "'. Valid policies: false, true, or arbitrary string");
1700     }
1701
1702     function getReplace(config, arrayMode, isOptional, squash) {
1703       var replace, configuredKeys, defaultPolicy = [
1704         { from: "",   to: (isOptional || arrayMode ? undefined : "") },
1705         { from: null, to: (isOptional || arrayMode ? undefined : "") }
1706       ];
1707       replace = isArray(config.replace) ? config.replace : [];
1708       if (isString(squash))
1709         replace.push({ from: squash, to: undefined });
1710       configuredKeys = map(replace, function(item) { return item.from; } );
1711       return filter(defaultPolicy, function(item) { return indexOf(configuredKeys, item.from) === -1; }).concat(replace);
1712     }
1713
1714     /**
1715      * [Internal] Get the default value of a parameter, which may be an injectable function.
1716      */
1717     function $$getDefaultValue() {
1718       if (!injector) throw new Error("Injectable functions cannot be called at configuration time");
1719       var defaultValue = injector.invoke(config.$$fn);
1720       if (defaultValue !== null && defaultValue !== undefined && !self.type.is(defaultValue))
1721         throw new Error("Default value (" + defaultValue + ") for parameter '" + self.id + "' is not an instance of Type (" + self.type.name + ")");
1722       return defaultValue;
1723     }
1724
1725     /**
1726      * [Internal] Gets the decoded representation of a value if the value is defined, otherwise, returns the
1727      * default value, which may be the result of an injectable function.
1728      */
1729     function $value(value) {
1730       function hasReplaceVal(val) { return function(obj) { return obj.from === val; }; }
1731       function $replace(value) {
1732         var replacement = map(filter(self.replace, hasReplaceVal(value)), function(obj) { return obj.to; });
1733         return replacement.length ? replacement[0] : value;
1734       }
1735       value = $replace(value);
1736       return !isDefined(value) ? $$getDefaultValue() : self.type.$normalize(value);
1737     }
1738
1739     function toString() { return "{Param:" + id + " " + type + " squash: '" + squash + "' optional: " + isOptional + "}"; }
1740
1741     extend(this, {
1742       id: id,
1743       type: type,
1744       location: location,
1745       array: arrayMode,
1746       squash: squash,
1747       replace: replace,
1748       isOptional: isOptional,
1749       value: $value,
1750       dynamic: undefined,
1751       config: config,
1752       toString: toString
1753     });
1754   };
1755
1756   function ParamSet(params) {
1757     extend(this, params || {});
1758   }
1759
1760   ParamSet.prototype = {
1761     $$new: function() {
1762       return inherit(this, extend(new ParamSet(), { $$parent: this}));
1763     },
1764     $$keys: function () {
1765       var keys = [], chain = [], parent = this,
1766         ignore = objectKeys(ParamSet.prototype);
1767       while (parent) { chain.push(parent); parent = parent.$$parent; }
1768       chain.reverse();
1769       forEach(chain, function(paramset) {
1770         forEach(objectKeys(paramset), function(key) {
1771             if (indexOf(keys, key) === -1 && indexOf(ignore, key) === -1) keys.push(key);
1772         });
1773       });
1774       return keys;
1775     },
1776     $$values: function(paramValues) {
1777       var values = {}, self = this;
1778       forEach(self.$$keys(), function(key) {
1779         values[key] = self[key].value(paramValues && paramValues[key]);
1780       });
1781       return values;
1782     },
1783     $$equals: function(paramValues1, paramValues2) {
1784       var equal = true, self = this;
1785       forEach(self.$$keys(), function(key) {
1786         var left = paramValues1 && paramValues1[key], right = paramValues2 && paramValues2[key];
1787         if (!self[key].type.equals(left, right)) equal = false;
1788       });
1789       return equal;
1790     },
1791     $$validates: function $$validate(paramValues) {
1792       var keys = this.$$keys(), i, param, rawVal, normalized, encoded;
1793       for (i = 0; i < keys.length; i++) {
1794         param = this[keys[i]];
1795         rawVal = paramValues[keys[i]];
1796         if ((rawVal === undefined || rawVal === null) && param.isOptional)
1797           break; // There was no parameter value, but the param is optional
1798         normalized = param.type.$normalize(rawVal);
1799         if (!param.type.is(normalized))
1800           return false; // The value was not of the correct Type, and could not be decoded to the correct Type
1801         encoded = param.type.encode(normalized);
1802         if (angular.isString(encoded) && !param.type.pattern.exec(encoded))
1803           return false; // The value was of the correct type, but when encoded, did not match the Type's regexp
1804       }
1805       return true;
1806     },
1807     $$parent: undefined
1808   };
1809
1810   this.ParamSet = ParamSet;
1811 }
1812
1813 // Register as a provider so it's available to other providers
1814 angular.module('ui.router.util').provider('$urlMatcherFactory', $UrlMatcherFactory);
1815 angular.module('ui.router.util').run(['$urlMatcherFactory', function($urlMatcherFactory) { }]);
1816
1817 /**
1818  * @ngdoc object
1819  * @name ui.router.router.$urlRouterProvider
1820  *
1821  * @requires ui.router.util.$urlMatcherFactoryProvider
1822  * @requires $locationProvider
1823  *
1824  * @description
1825  * `$urlRouterProvider` has the responsibility of watching `$location`. 
1826  * When `$location` changes it runs through a list of rules one by one until a 
1827  * match is found. `$urlRouterProvider` is used behind the scenes anytime you specify 
1828  * a url in a state configuration. All urls are compiled into a UrlMatcher object.
1829  *
1830  * There are several methods on `$urlRouterProvider` that make it useful to use directly
1831  * in your module config.
1832  */
1833 $UrlRouterProvider.$inject = ['$locationProvider', '$urlMatcherFactoryProvider'];
1834 function $UrlRouterProvider(   $locationProvider,   $urlMatcherFactory) {
1835   var rules = [], otherwise = null, interceptDeferred = false, listener;
1836
1837   // Returns a string that is a prefix of all strings matching the RegExp
1838   function regExpPrefix(re) {
1839     var prefix = /^\^((?:\\[^a-zA-Z0-9]|[^\\\[\]\^$*+?.()|{}]+)*)/.exec(re.source);
1840     return (prefix != null) ? prefix[1].replace(/\\(.)/g, "$1") : '';
1841   }
1842
1843   // Interpolates matched values into a String.replace()-style pattern
1844   function interpolate(pattern, match) {
1845     return pattern.replace(/\$(\$|\d{1,2})/, function (m, what) {
1846       return match[what === '$' ? 0 : Number(what)];
1847     });
1848   }
1849
1850   /**
1851    * @ngdoc function
1852    * @name ui.router.router.$urlRouterProvider#rule
1853    * @methodOf ui.router.router.$urlRouterProvider
1854    *
1855    * @description
1856    * Defines rules that are used by `$urlRouterProvider` to find matches for
1857    * specific URLs.
1858    *
1859    * @example
1860    * <pre>
1861    * var app = angular.module('app', ['ui.router.router']);
1862    *
1863    * app.config(function ($urlRouterProvider) {
1864    *   // Here's an example of how you might allow case insensitive urls
1865    *   $urlRouterProvider.rule(function ($injector, $location) {
1866    *     var path = $location.path(),
1867    *         normalized = path.toLowerCase();
1868    *
1869    *     if (path !== normalized) {
1870    *       return normalized;
1871    *     }
1872    *   });
1873    * });
1874    * </pre>
1875    *
1876    * @param {function} rule Handler function that takes `$injector` and `$location`
1877    * services as arguments. You can use them to return a valid path as a string.
1878    *
1879    * @return {object} `$urlRouterProvider` - `$urlRouterProvider` instance
1880    */
1881   this.rule = function (rule) {
1882     if (!isFunction(rule)) throw new Error("'rule' must be a function");
1883     rules.push(rule);
1884     return this;
1885   };
1886
1887   /**
1888    * @ngdoc object
1889    * @name ui.router.router.$urlRouterProvider#otherwise
1890    * @methodOf ui.router.router.$urlRouterProvider
1891    *
1892    * @description
1893    * Defines a path that is used when an invalid route is requested.
1894    *
1895    * @example
1896    * <pre>
1897    * var app = angular.module('app', ['ui.router.router']);
1898    *
1899    * app.config(function ($urlRouterProvider) {
1900    *   // if the path doesn't match any of the urls you configured
1901    *   // otherwise will take care of routing the user to the
1902    *   // specified url
1903    *   $urlRouterProvider.otherwise('/index');
1904    *
1905    *   // Example of using function rule as param
1906    *   $urlRouterProvider.otherwise(function ($injector, $location) {
1907    *     return '/a/valid/url';
1908    *   });
1909    * });
1910    * </pre>
1911    *
1912    * @param {string|function} rule The url path you want to redirect to or a function 
1913    * rule that returns the url path. The function version is passed two params: 
1914    * `$injector` and `$location` services, and must return a url string.
1915    *
1916    * @return {object} `$urlRouterProvider` - `$urlRouterProvider` instance
1917    */
1918   this.otherwise = function (rule) {
1919     if (isString(rule)) {
1920       var redirect = rule;
1921       rule = function () { return redirect; };
1922     }
1923     else if (!isFunction(rule)) throw new Error("'rule' must be a function");
1924     otherwise = rule;
1925     return this;
1926   };
1927
1928
1929   function handleIfMatch($injector, handler, match) {
1930     if (!match) return false;
1931     var result = $injector.invoke(handler, handler, { $match: match });
1932     return isDefined(result) ? result : true;
1933   }
1934
1935   /**
1936    * @ngdoc function
1937    * @name ui.router.router.$urlRouterProvider#when
1938    * @methodOf ui.router.router.$urlRouterProvider
1939    *
1940    * @description
1941    * Registers a handler for a given url matching. 
1942    * 
1943    * If the handler is a string, it is
1944    * treated as a redirect, and is interpolated according to the syntax of match
1945    * (i.e. like `String.replace()` for `RegExp`, or like a `UrlMatcher` pattern otherwise).
1946    *
1947    * If the handler is a function, it is injectable. It gets invoked if `$location`
1948    * matches. You have the option of inject the match object as `$match`.
1949    *
1950    * The handler can return
1951    *
1952    * - **falsy** to indicate that the rule didn't match after all, then `$urlRouter`
1953    *   will continue trying to find another one that matches.
1954    * - **string** which is treated as a redirect and passed to `$location.url()`
1955    * - **void** or any **truthy** value tells `$urlRouter` that the url was handled.
1956    *
1957    * @example
1958    * <pre>
1959    * var app = angular.module('app', ['ui.router.router']);
1960    *
1961    * app.config(function ($urlRouterProvider) {
1962    *   $urlRouterProvider.when($state.url, function ($match, $stateParams) {
1963    *     if ($state.$current.navigable !== state ||
1964    *         !equalForKeys($match, $stateParams) {
1965    *      $state.transitionTo(state, $match, false);
1966    *     }
1967    *   });
1968    * });
1969    * </pre>
1970    *
1971    * @param {string|object} what The incoming path that you want to redirect.
1972    * @param {string|function} handler The path you want to redirect your user to.
1973    */
1974   this.when = function (what, handler) {
1975     var redirect, handlerIsString = isString(handler);
1976     if (isString(what)) what = $urlMatcherFactory.compile(what);
1977
1978     if (!handlerIsString && !isFunction(handler) && !isArray(handler))
1979       throw new Error("invalid 'handler' in when()");
1980
1981     var strategies = {
1982       matcher: function (what, handler) {
1983         if (handlerIsString) {
1984           redirect = $urlMatcherFactory.compile(handler);
1985           handler = ['$match', function ($match) { return redirect.format($match); }];
1986         }
1987         return extend(function ($injector, $location) {
1988           return handleIfMatch($injector, handler, what.exec($location.path(), $location.search()));
1989         }, {
1990           prefix: isString(what.prefix) ? what.prefix : ''
1991         });
1992       },
1993       regex: function (what, handler) {
1994         if (what.global || what.sticky) throw new Error("when() RegExp must not be global or sticky");
1995
1996         if (handlerIsString) {
1997           redirect = handler;
1998           handler = ['$match', function ($match) { return interpolate(redirect, $match); }];
1999         }
2000         return extend(function ($injector, $location) {
2001           return handleIfMatch($injector, handler, what.exec($location.path()));
2002         }, {
2003           prefix: regExpPrefix(what)
2004         });
2005       }
2006     };
2007
2008     var check = { matcher: $urlMatcherFactory.isMatcher(what), regex: what instanceof RegExp };
2009
2010     for (var n in check) {
2011       if (check[n]) return this.rule(strategies[n](what, handler));
2012     }
2013
2014     throw new Error("invalid 'what' in when()");
2015   };
2016
2017   /**
2018    * @ngdoc function
2019    * @name ui.router.router.$urlRouterProvider#deferIntercept
2020    * @methodOf ui.router.router.$urlRouterProvider
2021    *
2022    * @description
2023    * Disables (or enables) deferring location change interception.
2024    *
2025    * If you wish to customize the behavior of syncing the URL (for example, if you wish to
2026    * defer a transition but maintain the current URL), call this method at configuration time.
2027    * Then, at run time, call `$urlRouter.listen()` after you have configured your own
2028    * `$locationChangeSuccess` event handler.
2029    *
2030    * @example
2031    * <pre>
2032    * var app = angular.module('app', ['ui.router.router']);
2033    *
2034    * app.config(function ($urlRouterProvider) {
2035    *
2036    *   // Prevent $urlRouter from automatically intercepting URL changes;
2037    *   // this allows you to configure custom behavior in between
2038    *   // location changes and route synchronization:
2039    *   $urlRouterProvider.deferIntercept();
2040    *
2041    * }).run(function ($rootScope, $urlRouter, UserService) {
2042    *
2043    *   $rootScope.$on('$locationChangeSuccess', function(e) {
2044    *     // UserService is an example service for managing user state
2045    *     if (UserService.isLoggedIn()) return;
2046    *
2047    *     // Prevent $urlRouter's default handler from firing
2048    *     e.preventDefault();
2049    *
2050    *     UserService.handleLogin().then(function() {
2051    *       // Once the user has logged in, sync the current URL
2052    *       // to the router:
2053    *       $urlRouter.sync();
2054    *     });
2055    *   });
2056    *
2057    *   // Configures $urlRouter's listener *after* your custom listener
2058    *   $urlRouter.listen();
2059    * });
2060    * </pre>
2061    *
2062    * @param {boolean} defer Indicates whether to defer location change interception. Passing
2063             no parameter is equivalent to `true`.
2064    */
2065   this.deferIntercept = function (defer) {
2066     if (defer === undefined) defer = true;
2067     interceptDeferred = defer;
2068   };
2069
2070   /**
2071    * @ngdoc object
2072    * @name ui.router.router.$urlRouter
2073    *
2074    * @requires $location
2075    * @requires $rootScope
2076    * @requires $injector
2077    * @requires $browser
2078    *
2079    * @description
2080    *
2081    */
2082   this.$get = $get;
2083   $get.$inject = ['$location', '$rootScope', '$injector', '$browser', '$sniffer'];
2084   function $get(   $location,   $rootScope,   $injector,   $browser,   $sniffer) {
2085
2086     var baseHref = $browser.baseHref(), location = $location.url(), lastPushedUrl;
2087
2088     function appendBasePath(url, isHtml5, absolute) {
2089       if (baseHref === '/') return url;
2090       if (isHtml5) return baseHref.slice(0, -1) + url;
2091       if (absolute) return baseHref.slice(1) + url;
2092       return url;
2093     }
2094
2095     // TODO: Optimize groups of rules with non-empty prefix into some sort of decision tree
2096     function update(evt) {
2097       if (evt && evt.defaultPrevented) return;
2098       var ignoreUpdate = lastPushedUrl && $location.url() === lastPushedUrl;
2099       lastPushedUrl = undefined;
2100       // TODO: Re-implement this in 1.0 for https://github.com/angular-ui/ui-router/issues/1573
2101       //if (ignoreUpdate) return true;
2102
2103       function check(rule) {
2104         var handled = rule($injector, $location);
2105
2106         if (!handled) return false;
2107         if (isString(handled)) $location.replace().url(handled);
2108         return true;
2109       }
2110       var n = rules.length, i;
2111
2112       for (i = 0; i < n; i++) {
2113         if (check(rules[i])) return;
2114       }
2115       // always check otherwise last to allow dynamic updates to the set of rules
2116       if (otherwise) check(otherwise);
2117     }
2118
2119     function listen() {
2120       listener = listener || $rootScope.$on('$locationChangeSuccess', update);
2121       return listener;
2122     }
2123
2124     if (!interceptDeferred) listen();
2125
2126     return {
2127       /**
2128        * @ngdoc function
2129        * @name ui.router.router.$urlRouter#sync
2130        * @methodOf ui.router.router.$urlRouter
2131        *
2132        * @description
2133        * Triggers an update; the same update that happens when the address bar url changes, aka `$locationChangeSuccess`.
2134        * This method is useful when you need to use `preventDefault()` on the `$locationChangeSuccess` event,
2135        * perform some custom logic (route protection, auth, config, redirection, etc) and then finally proceed
2136        * with the transition by calling `$urlRouter.sync()`.
2137        *
2138        * @example
2139        * <pre>
2140        * angular.module('app', ['ui.router'])
2141        *   .run(function($rootScope, $urlRouter) {
2142        *     $rootScope.$on('$locationChangeSuccess', function(evt) {
2143        *       // Halt state change from even starting
2144        *       evt.preventDefault();
2145        *       // Perform custom logic
2146        *       var meetsRequirement = ...
2147        *       // Continue with the update and state transition if logic allows
2148        *       if (meetsRequirement) $urlRouter.sync();
2149        *     });
2150        * });
2151        * </pre>
2152        */
2153       sync: function() {
2154         update();
2155       },
2156
2157       listen: function() {
2158         return listen();
2159       },
2160
2161       update: function(read) {
2162         if (read) {
2163           location = $location.url();
2164           return;
2165         }
2166         if ($location.url() === location) return;
2167
2168         $location.url(location);
2169         $location.replace();
2170       },
2171
2172       push: function(urlMatcher, params, options) {
2173          var url = urlMatcher.format(params || {});
2174
2175         // Handle the special hash param, if needed
2176         if (url !== null && params && params['#']) {
2177             url += '#' + params['#'];
2178         }
2179
2180         $location.url(url);
2181         lastPushedUrl = options && options.$$avoidResync ? $location.url() : undefined;
2182         if (options && options.replace) $location.replace();
2183       },
2184
2185       /**
2186        * @ngdoc function
2187        * @name ui.router.router.$urlRouter#href
2188        * @methodOf ui.router.router.$urlRouter
2189        *
2190        * @description
2191        * A URL generation method that returns the compiled URL for a given
2192        * {@link ui.router.util.type:UrlMatcher `UrlMatcher`}, populated with the provided parameters.
2193        *
2194        * @example
2195        * <pre>
2196        * $bob = $urlRouter.href(new UrlMatcher("/about/:person"), {
2197        *   person: "bob"
2198        * });
2199        * // $bob == "/about/bob";
2200        * </pre>
2201        *
2202        * @param {UrlMatcher} urlMatcher The `UrlMatcher` object which is used as the template of the URL to generate.
2203        * @param {object=} params An object of parameter values to fill the matcher's required parameters.
2204        * @param {object=} options Options object. The options are:
2205        *
2206        * - **`absolute`** - {boolean=false},  If true will generate an absolute url, e.g. "http://www.example.com/fullurl".
2207        *
2208        * @returns {string} Returns the fully compiled URL, or `null` if `params` fail validation against `urlMatcher`
2209        */
2210       href: function(urlMatcher, params, options) {
2211         if (!urlMatcher.validates(params)) return null;
2212
2213         var isHtml5 = $locationProvider.html5Mode();
2214         if (angular.isObject(isHtml5)) {
2215           isHtml5 = isHtml5.enabled;
2216         }
2217
2218         isHtml5 = isHtml5 && $sniffer.history;
2219         
2220         var url = urlMatcher.format(params);
2221         options = options || {};
2222
2223         if (!isHtml5 && url !== null) {
2224           url = "#" + $locationProvider.hashPrefix() + url;
2225         }
2226
2227         // Handle special hash param, if needed
2228         if (url !== null && params && params['#']) {
2229           url += '#' + params['#'];
2230         }
2231
2232         url = appendBasePath(url, isHtml5, options.absolute);
2233
2234         if (!options.absolute || !url) {
2235           return url;
2236         }
2237
2238         var slash = (!isHtml5 && url ? '/' : ''), port = $location.port();
2239         port = (port === 80 || port === 443 ? '' : ':' + port);
2240
2241         return [$location.protocol(), '://', $location.host(), port, slash, url].join('');
2242       }
2243     };
2244   }
2245 }
2246
2247 angular.module('ui.router.router').provider('$urlRouter', $UrlRouterProvider);
2248
2249 /**
2250  * @ngdoc object
2251  * @name ui.router.state.$stateProvider
2252  *
2253  * @requires ui.router.router.$urlRouterProvider
2254  * @requires ui.router.util.$urlMatcherFactoryProvider
2255  *
2256  * @description
2257  * The new `$stateProvider` works similar to Angular's v1 router, but it focuses purely
2258  * on state.
2259  *
2260  * A state corresponds to a "place" in the application in terms of the overall UI and
2261  * navigation. A state describes (via the controller / template / view properties) what
2262  * the UI looks like and does at that place.
2263  *
2264  * States often have things in common, and the primary way of factoring out these
2265  * commonalities in this model is via the state hierarchy, i.e. parent/child states aka
2266  * nested states.
2267  *
2268  * The `$stateProvider` provides interfaces to declare these states for your app.
2269  */
2270 $StateProvider.$inject = ['$urlRouterProvider', '$urlMatcherFactoryProvider'];
2271 function $StateProvider(   $urlRouterProvider,   $urlMatcherFactory) {
2272
2273   var root, states = {}, $state, queue = {}, abstractKey = 'abstract';
2274
2275   // Builds state properties from definition passed to registerState()
2276   var stateBuilder = {
2277
2278     // Derive parent state from a hierarchical name only if 'parent' is not explicitly defined.
2279     // state.children = [];
2280     // if (parent) parent.children.push(state);
2281     parent: function(state) {
2282       if (isDefined(state.parent) && state.parent) return findState(state.parent);
2283       // regex matches any valid composite state name
2284       // would match "contact.list" but not "contacts"
2285       var compositeName = /^(.+)\.[^.]+$/.exec(state.name);
2286       return compositeName ? findState(compositeName[1]) : root;
2287     },
2288
2289     // inherit 'data' from parent and override by own values (if any)
2290     data: function(state) {
2291       if (state.parent && state.parent.data) {
2292         state.data = state.self.data = inherit(state.parent.data, state.data);
2293       }
2294       return state.data;
2295     },
2296
2297     // Build a URLMatcher if necessary, either via a relative or absolute URL
2298     url: function(state) {
2299       var url = state.url, config = { params: state.params || {} };
2300
2301       if (isString(url)) {
2302         if (url.charAt(0) == '^') return $urlMatcherFactory.compile(url.substring(1), config);
2303         return (state.parent.navigable || root).url.concat(url, config);
2304       }
2305
2306       if (!url || $urlMatcherFactory.isMatcher(url)) return url;
2307       throw new Error("Invalid url '" + url + "' in state '" + state + "'");
2308     },
2309
2310     // Keep track of the closest ancestor state that has a URL (i.e. is navigable)
2311     navigable: function(state) {
2312       return state.url ? state : (state.parent ? state.parent.navigable : null);
2313     },
2314
2315     // Own parameters for this state. state.url.params is already built at this point. Create and add non-url params
2316     ownParams: function(state) {
2317       var params = state.url && state.url.params || new $$UMFP.ParamSet();
2318       forEach(state.params || {}, function(config, id) {
2319         if (!params[id]) params[id] = new $$UMFP.Param(id, null, config, "config");
2320       });
2321       return params;
2322     },
2323
2324     // Derive parameters for this state and ensure they're a super-set of parent's parameters
2325     params: function(state) {
2326       var ownParams = pick(state.ownParams, state.ownParams.$$keys());
2327       return state.parent && state.parent.params ? extend(state.parent.params.$$new(), ownParams) : new $$UMFP.ParamSet();
2328     },
2329
2330     // If there is no explicit multi-view configuration, make one up so we don't have
2331     // to handle both cases in the view directive later. Note that having an explicit
2332     // 'views' property will mean the default unnamed view properties are ignored. This
2333     // is also a good time to resolve view names to absolute names, so everything is a
2334     // straight lookup at link time.
2335     views: function(state) {
2336       var views = {};
2337
2338       forEach(isDefined(state.views) ? state.views : { '': state }, function (view, name) {
2339         if (name.indexOf('@') < 0) name += '@' + state.parent.name;
2340         view.resolveAs = view.resolveAs || state.resolveAs || '$resolve';
2341         views[name] = view;
2342       });
2343       return views;
2344     },
2345
2346     // Keep a full path from the root down to this state as this is needed for state activation.
2347     path: function(state) {
2348       return state.parent ? state.parent.path.concat(state) : []; // exclude root from path
2349     },
2350
2351     // Speed up $state.contains() as it's used a lot
2352     includes: function(state) {
2353       var includes = state.parent ? extend({}, state.parent.includes) : {};
2354       includes[state.name] = true;
2355       return includes;
2356     },
2357
2358     $delegates: {}
2359   };
2360
2361   function isRelative(stateName) {
2362     return stateName.indexOf(".") === 0 || stateName.indexOf("^") === 0;
2363   }
2364
2365   function findState(stateOrName, base) {
2366     if (!stateOrName) return undefined;
2367
2368     var isStr = isString(stateOrName),
2369         name  = isStr ? stateOrName : stateOrName.name,
2370         path  = isRelative(name);
2371
2372     if (path) {
2373       if (!base) throw new Error("No reference point given for path '"  + name + "'");
2374       base = findState(base);
2375       
2376       var rel = name.split("."), i = 0, pathLength = rel.length, current = base;
2377
2378       for (; i < pathLength; i++) {
2379         if (rel[i] === "" && i === 0) {
2380           current = base;
2381           continue;
2382         }
2383         if (rel[i] === "^") {
2384           if (!current.parent) throw new Error("Path '" + name + "' not valid for state '" + base.name + "'");
2385           current = current.parent;
2386           continue;
2387         }
2388         break;
2389       }
2390       rel = rel.slice(i).join(".");
2391       name = current.name + (current.name && rel ? "." : "") + rel;
2392     }
2393     var state = states[name];
2394
2395     if (state && (isStr || (!isStr && (state === stateOrName || state.self === stateOrName)))) {
2396       return state;
2397     }
2398     return undefined;
2399   }
2400
2401   function queueState(parentName, state) {
2402     if (!queue[parentName]) {
2403       queue[parentName] = [];
2404     }
2405     queue[parentName].push(state);
2406   }
2407
2408   function flushQueuedChildren(parentName) {
2409     var queued = queue[parentName] || [];
2410     while(queued.length) {
2411       registerState(queued.shift());
2412     }
2413   }
2414
2415   function registerState(state) {
2416     // Wrap a new object around the state so we can store our private details easily.
2417     state = inherit(state, {
2418       self: state,
2419       resolve: state.resolve || {},
2420       toString: function() { return this.name; }
2421     });
2422
2423     var name = state.name;
2424     if (!isString(name) || name.indexOf('@') >= 0) throw new Error("State must have a valid name");
2425     if (states.hasOwnProperty(name)) throw new Error("State '" + name + "' is already defined");
2426
2427     // Get parent name
2428     var parentName = (name.indexOf('.') !== -1) ? name.substring(0, name.lastIndexOf('.'))
2429         : (isString(state.parent)) ? state.parent
2430         : (isObject(state.parent) && isString(state.parent.name)) ? state.parent.name
2431         : '';
2432
2433     // If parent is not registered yet, add state to queue and register later
2434     if (parentName && !states[parentName]) {
2435       return queueState(parentName, state.self);
2436     }
2437
2438     for (var key in stateBuilder) {
2439       if (isFunction(stateBuilder[key])) state[key] = stateBuilder[key](state, stateBuilder.$delegates[key]);
2440     }
2441     states[name] = state;
2442
2443     // Register the state in the global state list and with $urlRouter if necessary.
2444     if (!state[abstractKey] && state.url) {
2445       $urlRouterProvider.when(state.url, ['$match', '$stateParams', function ($match, $stateParams) {
2446         if ($state.$current.navigable != state || !equalForKeys($match, $stateParams)) {
2447           $state.transitionTo(state, $match, { inherit: true, location: false });
2448         }
2449       }]);
2450     }
2451
2452     // Register any queued children
2453     flushQueuedChildren(name);
2454
2455     return state;
2456   }
2457
2458   // Checks text to see if it looks like a glob.
2459   function isGlob (text) {
2460     return text.indexOf('*') > -1;
2461   }
2462
2463   // Returns true if glob matches current $state name.
2464   function doesStateMatchGlob (glob) {
2465     var globSegments = glob.split('.'),
2466         segments = $state.$current.name.split('.');
2467
2468     //match single stars
2469     for (var i = 0, l = globSegments.length; i < l; i++) {
2470       if (globSegments[i] === '*') {
2471         segments[i] = '*';
2472       }
2473     }
2474
2475     //match greedy starts
2476     if (globSegments[0] === '**') {
2477        segments = segments.slice(indexOf(segments, globSegments[1]));
2478        segments.unshift('**');
2479     }
2480     //match greedy ends
2481     if (globSegments[globSegments.length - 1] === '**') {
2482        segments.splice(indexOf(segments, globSegments[globSegments.length - 2]) + 1, Number.MAX_VALUE);
2483        segments.push('**');
2484     }
2485
2486     if (globSegments.length != segments.length) {
2487       return false;
2488     }
2489
2490     return segments.join('') === globSegments.join('');
2491   }
2492
2493
2494   // Implicit root state that is always active
2495   root = registerState({
2496     name: '',
2497     url: '^',
2498     views: null,
2499     'abstract': true
2500   });
2501   root.navigable = null;
2502
2503
2504   /**
2505    * @ngdoc function
2506    * @name ui.router.state.$stateProvider#decorator
2507    * @methodOf ui.router.state.$stateProvider
2508    *
2509    * @description
2510    * Allows you to extend (carefully) or override (at your own peril) the 
2511    * `stateBuilder` object used internally by `$stateProvider`. This can be used 
2512    * to add custom functionality to ui-router, for example inferring templateUrl 
2513    * based on the state name.
2514    *
2515    * When passing only a name, it returns the current (original or decorated) builder
2516    * function that matches `name`.
2517    *
2518    * The builder functions that can be decorated are listed below. Though not all
2519    * necessarily have a good use case for decoration, that is up to you to decide.
2520    *
2521    * In addition, users can attach custom decorators, which will generate new 
2522    * properties within the state's internal definition. There is currently no clear 
2523    * use-case for this beyond accessing internal states (i.e. $state.$current), 
2524    * however, expect this to become increasingly relevant as we introduce additional 
2525    * meta-programming features.
2526    *
2527    * **Warning**: Decorators should not be interdependent because the order of 
2528    * execution of the builder functions in non-deterministic. Builder functions 
2529    * should only be dependent on the state definition object and super function.
2530    *
2531    *
2532    * Existing builder functions and current return values:
2533    *
2534    * - **parent** `{object}` - returns the parent state object.
2535    * - **data** `{object}` - returns state data, including any inherited data that is not
2536    *   overridden by own values (if any).
2537    * - **url** `{object}` - returns a {@link ui.router.util.type:UrlMatcher UrlMatcher}
2538    *   or `null`.
2539    * - **navigable** `{object}` - returns closest ancestor state that has a URL (aka is 
2540    *   navigable).
2541    * - **params** `{object}` - returns an array of state params that are ensured to 
2542    *   be a super-set of parent's params.
2543    * - **views** `{object}` - returns a views object where each key is an absolute view 
2544    *   name (i.e. "viewName@stateName") and each value is the config object 
2545    *   (template, controller) for the view. Even when you don't use the views object 
2546    *   explicitly on a state config, one is still created for you internally.
2547    *   So by decorating this builder function you have access to decorating template 
2548    *   and controller properties.
2549    * - **ownParams** `{object}` - returns an array of params that belong to the state, 
2550    *   not including any params defined by ancestor states.
2551    * - **path** `{string}` - returns the full path from the root down to this state. 
2552    *   Needed for state activation.
2553    * - **includes** `{object}` - returns an object that includes every state that 
2554    *   would pass a `$state.includes()` test.
2555    *
2556    * @example
2557    * <pre>
2558    * // Override the internal 'views' builder with a function that takes the state
2559    * // definition, and a reference to the internal function being overridden:
2560    * $stateProvider.decorator('views', function (state, parent) {
2561    *   var result = {},
2562    *       views = parent(state);
2563    *
2564    *   angular.forEach(views, function (config, name) {
2565    *     var autoName = (state.name + '.' + name).replace('.', '/');
2566    *     config.templateUrl = config.templateUrl || '/partials/' + autoName + '.html';
2567    *     result[name] = config;
2568    *   });
2569    *   return result;
2570    * });
2571    *
2572    * $stateProvider.state('home', {
2573    *   views: {
2574    *     'contact.list': { controller: 'ListController' },
2575    *     'contact.item': { controller: 'ItemController' }
2576    *   }
2577    * });
2578    *
2579    * // ...
2580    *
2581    * $state.go('home');
2582    * // Auto-populates list and item views with /partials/home/contact/list.html,
2583    * // and /partials/home/contact/item.html, respectively.
2584    * </pre>
2585    *
2586    * @param {string} name The name of the builder function to decorate. 
2587    * @param {object} func A function that is responsible for decorating the original 
2588    * builder function. The function receives two parameters:
2589    *
2590    *   - `{object}` - state - The state config object.
2591    *   - `{object}` - super - The original builder function.
2592    *
2593    * @return {object} $stateProvider - $stateProvider instance
2594    */
2595   this.decorator = decorator;
2596   function decorator(name, func) {
2597     /*jshint validthis: true */
2598     if (isString(name) && !isDefined(func)) {
2599       return stateBuilder[name];
2600     }
2601     if (!isFunction(func) || !isString(name)) {
2602       return this;
2603     }
2604     if (stateBuilder[name] && !stateBuilder.$delegates[name]) {
2605       stateBuilder.$delegates[name] = stateBuilder[name];
2606     }
2607     stateBuilder[name] = func;
2608     return this;
2609   }
2610
2611   /**
2612    * @ngdoc function
2613    * @name ui.router.state.$stateProvider#state
2614    * @methodOf ui.router.state.$stateProvider
2615    *
2616    * @description
2617    * Registers a state configuration under a given state name. The stateConfig object
2618    * has the following acceptable properties.
2619    *
2620    * @param {string} name A unique state name, e.g. "home", "about", "contacts".
2621    * To create a parent/child state use a dot, e.g. "about.sales", "home.newest".
2622    * @param {object} stateConfig State configuration object.
2623    * @param {string|function=} stateConfig.template
2624    * <a id='template'></a>
2625    *   html template as a string or a function that returns
2626    *   an html template as a string which should be used by the uiView directives. This property 
2627    *   takes precedence over templateUrl.
2628    *   
2629    *   If `template` is a function, it will be called with the following parameters:
2630    *
2631    *   - {array.&lt;object&gt;} - state parameters extracted from the current $location.path() by
2632    *     applying the current state
2633    *
2634    * <pre>template:
2635    *   "<h1>inline template definition</h1>" +
2636    *   "<div ui-view></div>"</pre>
2637    * <pre>template: function(params) {
2638    *       return "<h1>generated template</h1>"; }</pre>
2639    * </div>
2640    *
2641    * @param {string|function=} stateConfig.templateUrl
2642    * <a id='templateUrl'></a>
2643    *
2644    *   path or function that returns a path to an html
2645    *   template that should be used by uiView.
2646    *   
2647    *   If `templateUrl` is a function, it will be called with the following parameters:
2648    *
2649    *   - {array.&lt;object&gt;} - state parameters extracted from the current $location.path() by 
2650    *     applying the current state
2651    *
2652    * <pre>templateUrl: "home.html"</pre>
2653    * <pre>templateUrl: function(params) {
2654    *     return myTemplates[params.pageId]; }</pre>
2655    *
2656    * @param {function=} stateConfig.templateProvider
2657    * <a id='templateProvider'></a>
2658    *    Provider function that returns HTML content string.
2659    * <pre> templateProvider:
2660    *       function(MyTemplateService, params) {
2661    *         return MyTemplateService.getTemplate(params.pageId);
2662    *       }</pre>
2663    *
2664    * @param {string|function=} stateConfig.controller
2665    * <a id='controller'></a>
2666    *
2667    *  Controller fn that should be associated with newly
2668    *   related scope or the name of a registered controller if passed as a string.
2669    *   Optionally, the ControllerAs may be declared here.
2670    * <pre>controller: "MyRegisteredController"</pre>
2671    * <pre>controller:
2672    *     "MyRegisteredController as fooCtrl"}</pre>
2673    * <pre>controller: function($scope, MyService) {
2674    *     $scope.data = MyService.getData(); }</pre>
2675    *
2676    * @param {function=} stateConfig.controllerProvider
2677    * <a id='controllerProvider'></a>
2678    *
2679    * Injectable provider function that returns the actual controller or string.
2680    * <pre>controllerProvider:
2681    *   function(MyResolveData) {
2682    *     if (MyResolveData.foo)
2683    *       return "FooCtrl"
2684    *     else if (MyResolveData.bar)
2685    *       return "BarCtrl";
2686    *     else return function($scope) {
2687    *       $scope.baz = "Qux";
2688    *     }
2689    *   }</pre>
2690    *
2691    * @param {string=} stateConfig.controllerAs
2692    * <a id='controllerAs'></a>
2693    * 
2694    * A controller alias name. If present the controller will be
2695    *   published to scope under the controllerAs name.
2696    * <pre>controllerAs: "myCtrl"</pre>
2697    *
2698    * @param {string|object=} stateConfig.parent
2699    * <a id='parent'></a>
2700    * Optionally specifies the parent state of this state.
2701    *
2702    * <pre>parent: 'parentState'</pre>
2703    * <pre>parent: parentState // JS variable</pre>
2704    *
2705    * @param {object=} stateConfig.resolve
2706    * <a id='resolve'></a>
2707    *
2708    * An optional map&lt;string, function&gt; of dependencies which
2709    *   should be injected into the controller. If any of these dependencies are promises, 
2710    *   the router will wait for them all to be resolved before the controller is instantiated.
2711    *   If all the promises are resolved successfully, the $stateChangeSuccess event is fired
2712    *   and the values of the resolved promises are injected into any controllers that reference them.
2713    *   If any  of the promises are rejected the $stateChangeError event is fired.
2714    *
2715    *   The map object is:
2716    *   
2717    *   - key - {string}: name of dependency to be injected into controller
2718    *   - factory - {string|function}: If string then it is alias for service. Otherwise if function, 
2719    *     it is injected and return value it treated as dependency. If result is a promise, it is 
2720    *     resolved before its value is injected into controller.
2721    *
2722    * <pre>resolve: {
2723    *     myResolve1:
2724    *       function($http, $stateParams) {
2725    *         return $http.get("/api/foos/"+stateParams.fooID);
2726    *       }
2727    *     }</pre>
2728    *
2729    * @param {string=} stateConfig.url
2730    * <a id='url'></a>
2731    *
2732    *   A url fragment with optional parameters. When a state is navigated or
2733    *   transitioned to, the `$stateParams` service will be populated with any 
2734    *   parameters that were passed.
2735    *
2736    *   (See {@link ui.router.util.type:UrlMatcher UrlMatcher} `UrlMatcher`} for
2737    *   more details on acceptable patterns )
2738    *
2739    * examples:
2740    * <pre>url: "/home"
2741    * url: "/users/:userid"
2742    * url: "/books/{bookid:[a-zA-Z_-]}"
2743    * url: "/books/{categoryid:int}"
2744    * url: "/books/{publishername:string}/{categoryid:int}"
2745    * url: "/messages?before&after"
2746    * url: "/messages?{before:date}&{after:date}"
2747    * url: "/messages/:mailboxid?{before:date}&{after:date}"
2748    * </pre>
2749    *
2750    * @param {object=} stateConfig.views
2751    * <a id='views'></a>
2752    * an optional map&lt;string, object&gt; which defined multiple views, or targets views
2753    * manually/explicitly.
2754    *
2755    * Examples:
2756    *
2757    * Targets three named `ui-view`s in the parent state's template
2758    * <pre>views: {
2759    *     header: {
2760    *       controller: "headerCtrl",
2761    *       templateUrl: "header.html"
2762    *     }, body: {
2763    *       controller: "bodyCtrl",
2764    *       templateUrl: "body.html"
2765    *     }, footer: {
2766    *       controller: "footCtrl",
2767    *       templateUrl: "footer.html"
2768    *     }
2769    *   }</pre>
2770    *
2771    * Targets named `ui-view="header"` from grandparent state 'top''s template, and named `ui-view="body" from parent state's template.
2772    * <pre>views: {
2773    *     'header@top': {
2774    *       controller: "msgHeaderCtrl",
2775    *       templateUrl: "msgHeader.html"
2776    *     }, 'body': {
2777    *       controller: "messagesCtrl",
2778    *       templateUrl: "messages.html"
2779    *     }
2780    *   }</pre>
2781    *
2782    * @param {boolean=} [stateConfig.abstract=false]
2783    * <a id='abstract'></a>
2784    * An abstract state will never be directly activated,
2785    *   but can provide inherited properties to its common children states.
2786    * <pre>abstract: true</pre>
2787    *
2788    * @param {function=} stateConfig.onEnter
2789    * <a id='onEnter'></a>
2790    *
2791    * Callback function for when a state is entered. Good way
2792    *   to trigger an action or dispatch an event, such as opening a dialog.
2793    * If minifying your scripts, make sure to explicitly annotate this function,
2794    * because it won't be automatically annotated by your build tools.
2795    *
2796    * <pre>onEnter: function(MyService, $stateParams) {
2797    *     MyService.foo($stateParams.myParam);
2798    * }</pre>
2799    *
2800    * @param {function=} stateConfig.onExit
2801    * <a id='onExit'></a>
2802    *
2803    * Callback function for when a state is exited. Good way to
2804    *   trigger an action or dispatch an event, such as opening a dialog.
2805    * If minifying your scripts, make sure to explicitly annotate this function,
2806    * because it won't be automatically annotated by your build tools.
2807    *
2808    * <pre>onExit: function(MyService, $stateParams) {
2809    *     MyService.cleanup($stateParams.myParam);
2810    * }</pre>
2811    *
2812    * @param {boolean=} [stateConfig.reloadOnSearch=true]
2813    * <a id='reloadOnSearch'></a>
2814    *
2815    * If `false`, will not retrigger the same state
2816    *   just because a search/query parameter has changed (via $location.search() or $location.hash()). 
2817    *   Useful for when you'd like to modify $location.search() without triggering a reload.
2818    * <pre>reloadOnSearch: false</pre>
2819    *
2820    * @param {object=} stateConfig.data
2821    * <a id='data'></a>
2822    *
2823    * Arbitrary data object, useful for custom configuration.  The parent state's `data` is
2824    *   prototypally inherited.  In other words, adding a data property to a state adds it to
2825    *   the entire subtree via prototypal inheritance.
2826    *
2827    * <pre>data: {
2828    *     requiredRole: 'foo'
2829    * } </pre>
2830    *
2831    * @param {object=} stateConfig.params
2832    * <a id='params'></a>
2833    *
2834    * A map which optionally configures parameters declared in the `url`, or
2835    *   defines additional non-url parameters.  For each parameter being
2836    *   configured, add a configuration object keyed to the name of the parameter.
2837    *
2838    *   Each parameter configuration object may contain the following properties:
2839    *
2840    *   - ** value ** - {object|function=}: specifies the default value for this
2841    *     parameter.  This implicitly sets this parameter as optional.
2842    *
2843    *     When UI-Router routes to a state and no value is
2844    *     specified for this parameter in the URL or transition, the
2845    *     default value will be used instead.  If `value` is a function,
2846    *     it will be injected and invoked, and the return value used.
2847    *
2848    *     *Note*: `undefined` is treated as "no default value" while `null`
2849    *     is treated as "the default value is `null`".
2850    *
2851    *     *Shorthand*: If you only need to configure the default value of the
2852    *     parameter, you may use a shorthand syntax.   In the **`params`**
2853    *     map, instead mapping the param name to a full parameter configuration
2854    *     object, simply set map it to the default parameter value, e.g.:
2855    *
2856    * <pre>// define a parameter's default value
2857    * params: {
2858    *     param1: { value: "defaultValue" }
2859    * }
2860    * // shorthand default values
2861    * params: {
2862    *     param1: "defaultValue",
2863    *     param2: "param2Default"
2864    * }</pre>
2865    *
2866    *   - ** array ** - {boolean=}: *(default: false)* If true, the param value will be
2867    *     treated as an array of values.  If you specified a Type, the value will be
2868    *     treated as an array of the specified Type.  Note: query parameter values
2869    *     default to a special `"auto"` mode.
2870    *
2871    *     For query parameters in `"auto"` mode, if multiple  values for a single parameter
2872    *     are present in the URL (e.g.: `/foo?bar=1&bar=2&bar=3`) then the values
2873    *     are mapped to an array (e.g.: `{ foo: [ '1', '2', '3' ] }`).  However, if
2874    *     only one value is present (e.g.: `/foo?bar=1`) then the value is treated as single
2875    *     value (e.g.: `{ foo: '1' }`).
2876    *
2877    * <pre>params: {
2878    *     param1: { array: true }
2879    * }</pre>
2880    *
2881    *   - ** squash ** - {bool|string=}: `squash` configures how a default parameter value is represented in the URL when
2882    *     the current parameter value is the same as the default value. If `squash` is not set, it uses the
2883    *     configured default squash policy.
2884    *     (See {@link ui.router.util.$urlMatcherFactory#methods_defaultSquashPolicy `defaultSquashPolicy()`})
2885    *
2886    *   There are three squash settings:
2887    *
2888    *     - false: The parameter's default value is not squashed.  It is encoded and included in the URL
2889    *     - true: The parameter's default value is omitted from the URL.  If the parameter is preceeded and followed
2890    *       by slashes in the state's `url` declaration, then one of those slashes are omitted.
2891    *       This can allow for cleaner looking URLs.
2892    *     - `"<arbitrary string>"`: The parameter's default value is replaced with an arbitrary placeholder of  your choice.
2893    *
2894    * <pre>params: {
2895    *     param1: {
2896    *       value: "defaultId",
2897    *       squash: true
2898    * } }
2899    * // squash "defaultValue" to "~"
2900    * params: {
2901    *     param1: {
2902    *       value: "defaultValue",
2903    *       squash: "~"
2904    * } }
2905    * </pre>
2906    *
2907    *
2908    * @example
2909    * <pre>
2910    * // Some state name examples
2911    *
2912    * // stateName can be a single top-level name (must be unique).
2913    * $stateProvider.state("home", {});
2914    *
2915    * // Or it can be a nested state name. This state is a child of the
2916    * // above "home" state.
2917    * $stateProvider.state("home.newest", {});
2918    *
2919    * // Nest states as deeply as needed.
2920    * $stateProvider.state("home.newest.abc.xyz.inception", {});
2921    *
2922    * // state() returns $stateProvider, so you can chain state declarations.
2923    * $stateProvider
2924    *   .state("home", {})
2925    *   .state("about", {})
2926    *   .state("contacts", {});
2927    * </pre>
2928    *
2929    */
2930   this.state = state;
2931   function state(name, definition) {
2932     /*jshint validthis: true */
2933     if (isObject(name)) definition = name;
2934     else definition.name = name;
2935     registerState(definition);
2936     return this;
2937   }
2938
2939   /**
2940    * @ngdoc object
2941    * @name ui.router.state.$state
2942    *
2943    * @requires $rootScope
2944    * @requires $q
2945    * @requires ui.router.state.$view
2946    * @requires $injector
2947    * @requires ui.router.util.$resolve
2948    * @requires ui.router.state.$stateParams
2949    * @requires ui.router.router.$urlRouter
2950    *
2951    * @property {object} params A param object, e.g. {sectionId: section.id)}, that 
2952    * you'd like to test against the current active state.
2953    * @property {object} current A reference to the state's config object. However 
2954    * you passed it in. Useful for accessing custom data.
2955    * @property {object} transition Currently pending transition. A promise that'll 
2956    * resolve or reject.
2957    *
2958    * @description
2959    * `$state` service is responsible for representing states as well as transitioning
2960    * between them. It also provides interfaces to ask for current state or even states
2961    * you're coming from.
2962    */
2963   this.$get = $get;
2964   $get.$inject = ['$rootScope', '$q', '$view', '$injector', '$resolve', '$stateParams', '$urlRouter', '$location', '$urlMatcherFactory'];
2965   function $get(   $rootScope,   $q,   $view,   $injector,   $resolve,   $stateParams,   $urlRouter,   $location,   $urlMatcherFactory) {
2966
2967     var TransitionSupersededError = new Error('transition superseded');
2968
2969     var TransitionSuperseded = silenceUncaughtInPromise($q.reject(TransitionSupersededError));
2970     var TransitionPrevented = silenceUncaughtInPromise($q.reject(new Error('transition prevented')));
2971     var TransitionAborted = silenceUncaughtInPromise($q.reject(new Error('transition aborted')));
2972     var TransitionFailed = silenceUncaughtInPromise($q.reject(new Error('transition failed')));
2973
2974     // Handles the case where a state which is the target of a transition is not found, and the user
2975     // can optionally retry or defer the transition
2976     function handleRedirect(redirect, state, params, options) {
2977       /**
2978        * @ngdoc event
2979        * @name ui.router.state.$state#$stateNotFound
2980        * @eventOf ui.router.state.$state
2981        * @eventType broadcast on root scope
2982        * @description
2983        * Fired when a requested state **cannot be found** using the provided state name during transition.
2984        * The event is broadcast allowing any handlers a single chance to deal with the error (usually by
2985        * lazy-loading the unfound state). A special `unfoundState` object is passed to the listener handler,
2986        * you can see its three properties in the example. You can use `event.preventDefault()` to abort the
2987        * transition and the promise returned from `go` will be rejected with a `'transition aborted'` value.
2988        *
2989        * @param {Object} event Event object.
2990        * @param {Object} unfoundState Unfound State information. Contains: `to, toParams, options` properties.
2991        * @param {State} fromState Current state object.
2992        * @param {Object} fromParams Current state params.
2993        *
2994        * @example
2995        *
2996        * <pre>
2997        * // somewhere, assume lazy.state has not been defined
2998        * $state.go("lazy.state", {a:1, b:2}, {inherit:false});
2999        *
3000        * // somewhere else
3001        * $scope.$on('$stateNotFound',
3002        * function(event, unfoundState, fromState, fromParams){
3003        *     console.log(unfoundState.to); // "lazy.state"
3004        *     console.log(unfoundState.toParams); // {a:1, b:2}
3005        *     console.log(unfoundState.options); // {inherit:false} + default options
3006        * })
3007        * </pre>
3008        */
3009       var evt = $rootScope.$broadcast('$stateNotFound', redirect, state, params);
3010
3011       if (evt.defaultPrevented) {
3012         $urlRouter.update();
3013         return TransitionAborted;
3014       }
3015
3016       if (!evt.retry) {
3017         return null;
3018       }
3019
3020       // Allow the handler to return a promise to defer state lookup retry
3021       if (options.$retry) {
3022         $urlRouter.update();
3023         return TransitionFailed;
3024       }
3025       var retryTransition = $state.transition = $q.when(evt.retry);
3026
3027       retryTransition.then(function() {
3028         if (retryTransition !== $state.transition) {
3029           $rootScope.$broadcast('$stateChangeCancel', redirect.to, redirect.toParams, state, params);
3030           return TransitionSuperseded;
3031         }
3032         redirect.options.$retry = true;
3033         return $state.transitionTo(redirect.to, redirect.toParams, redirect.options);
3034       }, function() {
3035         return TransitionAborted;
3036       });
3037       $urlRouter.update();
3038
3039       return retryTransition;
3040     }
3041
3042     root.locals = { resolve: null, globals: { $stateParams: {} } };
3043
3044     $state = {
3045       params: {},
3046       current: root.self,
3047       $current: root,
3048       transition: null
3049     };
3050
3051     /**
3052      * @ngdoc function
3053      * @name ui.router.state.$state#reload
3054      * @methodOf ui.router.state.$state
3055      *
3056      * @description
3057      * A method that force reloads the current state. All resolves are re-resolved,
3058      * controllers reinstantiated, and events re-fired.
3059      *
3060      * @example
3061      * <pre>
3062      * var app angular.module('app', ['ui.router']);
3063      *
3064      * app.controller('ctrl', function ($scope, $state) {
3065      *   $scope.reload = function(){
3066      *     $state.reload();
3067      *   }
3068      * });
3069      * </pre>
3070      *
3071      * `reload()` is just an alias for:
3072      * <pre>
3073      * $state.transitionTo($state.current, $stateParams, { 
3074      *   reload: true, inherit: false, notify: true
3075      * });
3076      * </pre>
3077      *
3078      * @param {string=|object=} state - A state name or a state object, which is the root of the resolves to be re-resolved.
3079      * @example
3080      * <pre>
3081      * //assuming app application consists of 3 states: 'contacts', 'contacts.detail', 'contacts.detail.item' 
3082      * //and current state is 'contacts.detail.item'
3083      * var app angular.module('app', ['ui.router']);
3084      *
3085      * app.controller('ctrl', function ($scope, $state) {
3086      *   $scope.reload = function(){
3087      *     //will reload 'contact.detail' and 'contact.detail.item' states
3088      *     $state.reload('contact.detail');
3089      *   }
3090      * });
3091      * </pre>
3092      *
3093      * `reload()` is just an alias for:
3094      * <pre>
3095      * $state.transitionTo($state.current, $stateParams, { 
3096      *   reload: true, inherit: false, notify: true
3097      * });
3098      * </pre>
3099
3100      * @returns {promise} A promise representing the state of the new transition. See
3101      * {@link ui.router.state.$state#methods_go $state.go}.
3102      */
3103     $state.reload = function reload(state) {
3104       return $state.transitionTo($state.current, $stateParams, { reload: state || true, inherit: false, notify: true});
3105     };
3106
3107     /**
3108      * @ngdoc function
3109      * @name ui.router.state.$state#go
3110      * @methodOf ui.router.state.$state
3111      *
3112      * @description
3113      * Convenience method for transitioning to a new state. `$state.go` calls 
3114      * `$state.transitionTo` internally but automatically sets options to 
3115      * `{ location: true, inherit: true, relative: $state.$current, notify: true }`. 
3116      * This allows you to easily use an absolute or relative to path and specify 
3117      * only the parameters you'd like to update (while letting unspecified parameters 
3118      * inherit from the currently active ancestor states).
3119      *
3120      * @example
3121      * <pre>
3122      * var app = angular.module('app', ['ui.router']);
3123      *
3124      * app.controller('ctrl', function ($scope, $state) {
3125      *   $scope.changeState = function () {
3126      *     $state.go('contact.detail');
3127      *   };
3128      * });
3129      * </pre>
3130      * <img src='../ngdoc_assets/StateGoExamples.png'/>
3131      *
3132      * @param {string} to Absolute state name or relative state path. Some examples:
3133      *
3134      * - `$state.go('contact.detail')` - will go to the `contact.detail` state
3135      * - `$state.go('^')` - will go to a parent state
3136      * - `$state.go('^.sibling')` - will go to a sibling state
3137      * - `$state.go('.child.grandchild')` - will go to grandchild state
3138      *
3139      * @param {object=} params A map of the parameters that will be sent to the state, 
3140      * will populate $stateParams. Any parameters that are not specified will be inherited from currently 
3141      * defined parameters. Only parameters specified in the state definition can be overridden, new 
3142      * parameters will be ignored. This allows, for example, going to a sibling state that shares parameters
3143      * specified in a parent state. Parameter inheritance only works between common ancestor states, I.e.
3144      * transitioning to a sibling will get you the parameters for all parents, transitioning to a child
3145      * will get you all current parameters, etc.
3146      * @param {object=} options Options object. The options are:
3147      *
3148      * - **`location`** - {boolean=true|string=} - If `true` will update the url in the location bar, if `false`
3149      *    will not. If string, must be `"replace"`, which will update url and also replace last history record.
3150      * - **`inherit`** - {boolean=true}, If `true` will inherit url parameters from current url.
3151      * - **`relative`** - {object=$state.$current}, When transitioning with relative path (e.g '^'), 
3152      *    defines which state to be relative from.
3153      * - **`notify`** - {boolean=true}, If `true` will broadcast $stateChangeStart and $stateChangeSuccess events.
3154      * - **`reload`** (v0.2.5) - {boolean=false|string|object}, If `true` will force transition even if no state or params
3155      *    have changed.  It will reload the resolves and views of the current state and parent states.
3156      *    If `reload` is a string (or state object), the state object is fetched (by name, or object reference); and \
3157      *    the transition reloads the resolves and views for that matched state, and all its children states.
3158      *
3159      * @returns {promise} A promise representing the state of the new transition.
3160      *
3161      * Possible success values:
3162      *
3163      * - $state.current
3164      *
3165      * <br/>Possible rejection values:
3166      *
3167      * - 'transition superseded' - when a newer transition has been started after this one
3168      * - 'transition prevented' - when `event.preventDefault()` has been called in a `$stateChangeStart` listener
3169      * - 'transition aborted' - when `event.preventDefault()` has been called in a `$stateNotFound` listener or
3170      *   when a `$stateNotFound` `event.retry` promise errors.
3171      * - 'transition failed' - when a state has been unsuccessfully found after 2 tries.
3172      * - *resolve error* - when an error has occurred with a `resolve`
3173      *
3174      */
3175     $state.go = function go(to, params, options) {
3176       return $state.transitionTo(to, params, extend({ inherit: true, relative: $state.$current }, options));
3177     };
3178
3179     /**
3180      * @ngdoc function
3181      * @name ui.router.state.$state#transitionTo
3182      * @methodOf ui.router.state.$state
3183      *
3184      * @description
3185      * Low-level method for transitioning to a new state. {@link ui.router.state.$state#methods_go $state.go}
3186      * uses `transitionTo` internally. `$state.go` is recommended in most situations.
3187      *
3188      * @example
3189      * <pre>
3190      * var app = angular.module('app', ['ui.router']);
3191      *
3192      * app.controller('ctrl', function ($scope, $state) {
3193      *   $scope.changeState = function () {
3194      *     $state.transitionTo('contact.detail');
3195      *   };
3196      * });
3197      * </pre>
3198      *
3199      * @param {string} to State name.
3200      * @param {object=} toParams A map of the parameters that will be sent to the state,
3201      * will populate $stateParams.
3202      * @param {object=} options Options object. The options are:
3203      *
3204      * - **`location`** - {boolean=true|string=} - If `true` will update the url in the location bar, if `false`
3205      *    will not. If string, must be `"replace"`, which will update url and also replace last history record.
3206      * - **`inherit`** - {boolean=false}, If `true` will inherit url parameters from current url.
3207      * - **`relative`** - {object=}, When transitioning with relative path (e.g '^'), 
3208      *    defines which state to be relative from.
3209      * - **`notify`** - {boolean=true}, If `true` will broadcast $stateChangeStart and $stateChangeSuccess events.
3210      * - **`reload`** (v0.2.5) - {boolean=false|string=|object=}, If `true` will force transition even if the state or params 
3211      *    have not changed, aka a reload of the same state. It differs from reloadOnSearch because you'd
3212      *    use this when you want to force a reload when *everything* is the same, including search params.
3213      *    if String, then will reload the state with the name given in reload, and any children.
3214      *    if Object, then a stateObj is expected, will reload the state found in stateObj, and any children.
3215      *
3216      * @returns {promise} A promise representing the state of the new transition. See
3217      * {@link ui.router.state.$state#methods_go $state.go}.
3218      */
3219     $state.transitionTo = function transitionTo(to, toParams, options) {
3220       toParams = toParams || {};
3221       options = extend({
3222         location: true, inherit: false, relative: null, notify: true, reload: false, $retry: false
3223       }, options || {});
3224
3225       var from = $state.$current, fromParams = $state.params, fromPath = from.path;
3226       var evt, toState = findState(to, options.relative);
3227
3228       // Store the hash param for later (since it will be stripped out by various methods)
3229       var hash = toParams['#'];
3230
3231       if (!isDefined(toState)) {
3232         var redirect = { to: to, toParams: toParams, options: options };
3233         var redirectResult = handleRedirect(redirect, from.self, fromParams, options);
3234
3235         if (redirectResult) {
3236           return redirectResult;
3237         }
3238
3239         // Always retry once if the $stateNotFound was not prevented
3240         // (handles either redirect changed or state lazy-definition)
3241         to = redirect.to;
3242         toParams = redirect.toParams;
3243         options = redirect.options;
3244         toState = findState(to, options.relative);
3245
3246         if (!isDefined(toState)) {
3247           if (!options.relative) throw new Error("No such state '" + to + "'");
3248           throw new Error("Could not resolve '" + to + "' from state '" + options.relative + "'");
3249         }
3250       }
3251       if (toState[abstractKey]) throw new Error("Cannot transition to abstract state '" + to + "'");
3252       if (options.inherit) toParams = inheritParams($stateParams, toParams || {}, $state.$current, toState);
3253       if (!toState.params.$$validates(toParams)) return TransitionFailed;
3254
3255       toParams = toState.params.$$values(toParams);
3256       to = toState;
3257
3258       var toPath = to.path;
3259
3260       // Starting from the root of the path, keep all levels that haven't changed
3261       var keep = 0, state = toPath[keep], locals = root.locals, toLocals = [];
3262
3263       if (!options.reload) {
3264         while (state && state === fromPath[keep] && state.ownParams.$$equals(toParams, fromParams)) {
3265           locals = toLocals[keep] = state.locals;
3266           keep++;
3267           state = toPath[keep];
3268         }
3269       } else if (isString(options.reload) || isObject(options.reload)) {
3270         if (isObject(options.reload) && !options.reload.name) {
3271           throw new Error('Invalid reload state object');
3272         }
3273         
3274         var reloadState = options.reload === true ? fromPath[0] : findState(options.reload);
3275         if (options.reload && !reloadState) {
3276           throw new Error("No such reload state '" + (isString(options.reload) ? options.reload : options.reload.name) + "'");
3277         }
3278
3279         while (state && state === fromPath[keep] && state !== reloadState) {
3280           locals = toLocals[keep] = state.locals;
3281           keep++;
3282           state = toPath[keep];
3283         }
3284       }
3285
3286       // If we're going to the same state and all locals are kept, we've got nothing to do.
3287       // But clear 'transition', as we still want to cancel any other pending transitions.
3288       // TODO: We may not want to bump 'transition' if we're called from a location change
3289       // that we've initiated ourselves, because we might accidentally abort a legitimate
3290       // transition initiated from code?
3291       if (shouldSkipReload(to, toParams, from, fromParams, locals, options)) {
3292         if (hash) toParams['#'] = hash;
3293         $state.params = toParams;
3294         copy($state.params, $stateParams);
3295         copy(filterByKeys(to.params.$$keys(), $stateParams), to.locals.globals.$stateParams);
3296         if (options.location && to.navigable && to.navigable.url) {
3297           $urlRouter.push(to.navigable.url, toParams, {
3298             $$avoidResync: true, replace: options.location === 'replace'
3299           });
3300           $urlRouter.update(true);
3301         }
3302         $state.transition = null;
3303         return $q.when($state.current);
3304       }
3305
3306       // Filter parameters before we pass them to event handlers etc.
3307       toParams = filterByKeys(to.params.$$keys(), toParams || {});
3308       
3309       // Re-add the saved hash before we start returning things or broadcasting $stateChangeStart
3310       if (hash) toParams['#'] = hash;
3311       
3312       // Broadcast start event and cancel the transition if requested
3313       if (options.notify) {
3314         /**
3315          * @ngdoc event
3316          * @name ui.router.state.$state#$stateChangeStart
3317          * @eventOf ui.router.state.$state
3318          * @eventType broadcast on root scope
3319          * @description
3320          * Fired when the state transition **begins**. You can use `event.preventDefault()`
3321          * to prevent the transition from happening and then the transition promise will be
3322          * rejected with a `'transition prevented'` value.
3323          *
3324          * @param {Object} event Event object.
3325          * @param {State} toState The state being transitioned to.
3326          * @param {Object} toParams The params supplied to the `toState`.
3327          * @param {State} fromState The current state, pre-transition.
3328          * @param {Object} fromParams The params supplied to the `fromState`.
3329          *
3330          * @example
3331          *
3332          * <pre>
3333          * $rootScope.$on('$stateChangeStart',
3334          * function(event, toState, toParams, fromState, fromParams){
3335          *     event.preventDefault();
3336          *     // transitionTo() promise will be rejected with
3337          *     // a 'transition prevented' error
3338          * })
3339          * </pre>
3340          */
3341         if ($rootScope.$broadcast('$stateChangeStart', to.self, toParams, from.self, fromParams, options).defaultPrevented) {
3342           $rootScope.$broadcast('$stateChangeCancel', to.self, toParams, from.self, fromParams);
3343           //Don't update and resync url if there's been a new transition started. see issue #2238, #600
3344           if ($state.transition == null) $urlRouter.update();
3345           return TransitionPrevented;
3346         }
3347       }
3348
3349       // Resolve locals for the remaining states, but don't update any global state just
3350       // yet -- if anything fails to resolve the current state needs to remain untouched.
3351       // We also set up an inheritance chain for the locals here. This allows the view directive
3352       // to quickly look up the correct definition for each view in the current state. Even
3353       // though we create the locals object itself outside resolveState(), it is initially
3354       // empty and gets filled asynchronously. We need to keep track of the promise for the
3355       // (fully resolved) current locals, and pass this down the chain.
3356       var resolved = $q.when(locals);
3357
3358       for (var l = keep; l < toPath.length; l++, state = toPath[l]) {
3359         locals = toLocals[l] = inherit(locals);
3360         resolved = resolveState(state, toParams, state === to, resolved, locals, options);
3361       }
3362
3363       // Once everything is resolved, we are ready to perform the actual transition
3364       // and return a promise for the new state. We also keep track of what the
3365       // current promise is, so that we can detect overlapping transitions and
3366       // keep only the outcome of the last transition.
3367       var transition = $state.transition = resolved.then(function () {
3368         var l, entering, exiting;
3369
3370         if ($state.transition !== transition) {
3371           $rootScope.$broadcast('$stateChangeCancel', to.self, toParams, from.self, fromParams);
3372           return TransitionSuperseded;
3373         }
3374
3375         // Exit 'from' states not kept
3376         for (l = fromPath.length - 1; l >= keep; l--) {
3377           exiting = fromPath[l];
3378           if (exiting.self.onExit) {
3379             $injector.invoke(exiting.self.onExit, exiting.self, exiting.locals.globals);
3380           }
3381           exiting.locals = null;
3382         }
3383
3384         // Enter 'to' states not kept
3385         for (l = keep; l < toPath.length; l++) {
3386           entering = toPath[l];
3387           entering.locals = toLocals[l];
3388           if (entering.self.onEnter) {
3389             $injector.invoke(entering.self.onEnter, entering.self, entering.locals.globals);
3390           }
3391         }
3392
3393         // Run it again, to catch any transitions in callbacks
3394         if ($state.transition !== transition) {
3395           $rootScope.$broadcast('$stateChangeCancel', to.self, toParams, from.self, fromParams);
3396           return TransitionSuperseded;
3397         }
3398
3399         // Update globals in $state
3400         $state.$current = to;
3401         $state.current = to.self;
3402         $state.params = toParams;
3403         copy($state.params, $stateParams);
3404         $state.transition = null;
3405
3406         if (options.location && to.navigable) {
3407           $urlRouter.push(to.navigable.url, to.navigable.locals.globals.$stateParams, {
3408             $$avoidResync: true, replace: options.location === 'replace'
3409           });
3410         }
3411
3412         if (options.notify) {
3413         /**
3414          * @ngdoc event
3415          * @name ui.router.state.$state#$stateChangeSuccess
3416          * @eventOf ui.router.state.$state
3417          * @eventType broadcast on root scope
3418          * @description
3419          * Fired once the state transition is **complete**.
3420          *
3421          * @param {Object} event Event object.
3422          * @param {State} toState The state being transitioned to.
3423          * @param {Object} toParams The params supplied to the `toState`.
3424          * @param {State} fromState The current state, pre-transition.
3425          * @param {Object} fromParams The params supplied to the `fromState`.
3426          */
3427           $rootScope.$broadcast('$stateChangeSuccess', to.self, toParams, from.self, fromParams);
3428         }
3429         $urlRouter.update(true);
3430
3431         return $state.current;
3432       }).then(null, function (error) {
3433         // propagate TransitionSuperseded error without emitting $stateChangeCancel
3434         // as it was already emitted in the success handler above
3435         if (error === TransitionSupersededError) return TransitionSuperseded;
3436
3437         if ($state.transition !== transition) {
3438           $rootScope.$broadcast('$stateChangeCancel', to.self, toParams, from.self, fromParams);
3439           return TransitionSuperseded;
3440         }
3441
3442         $state.transition = null;
3443         /**
3444          * @ngdoc event
3445          * @name ui.router.state.$state#$stateChangeError
3446          * @eventOf ui.router.state.$state
3447          * @eventType broadcast on root scope
3448          * @description
3449          * Fired when an **error occurs** during transition. It's important to note that if you
3450          * have any errors in your resolve functions (javascript errors, non-existent services, etc)
3451          * they will not throw traditionally. You must listen for this $stateChangeError event to
3452          * catch **ALL** errors.
3453          *
3454          * @param {Object} event Event object.
3455          * @param {State} toState The state being transitioned to.
3456          * @param {Object} toParams The params supplied to the `toState`.
3457          * @param {State} fromState The current state, pre-transition.
3458          * @param {Object} fromParams The params supplied to the `fromState`.
3459          * @param {Error} error The resolve error object.
3460          */
3461         evt = $rootScope.$broadcast('$stateChangeError', to.self, toParams, from.self, fromParams, error);
3462
3463         if (!evt.defaultPrevented) {
3464           $urlRouter.update();
3465         }
3466
3467         return $q.reject(error);
3468       });
3469
3470       silenceUncaughtInPromise(transition);
3471       return transition;
3472     };
3473
3474     /**
3475      * @ngdoc function
3476      * @name ui.router.state.$state#is
3477      * @methodOf ui.router.state.$state
3478      *
3479      * @description
3480      * Similar to {@link ui.router.state.$state#methods_includes $state.includes},
3481      * but only checks for the full state name. If params is supplied then it will be
3482      * tested for strict equality against the current active params object, so all params
3483      * must match with none missing and no extras.
3484      *
3485      * @example
3486      * <pre>
3487      * $state.$current.name = 'contacts.details.item';
3488      *
3489      * // absolute name
3490      * $state.is('contact.details.item'); // returns true
3491      * $state.is(contactDetailItemStateObject); // returns true
3492      *
3493      * // relative name (. and ^), typically from a template
3494      * // E.g. from the 'contacts.details' template
3495      * <div ng-class="{highlighted: $state.is('.item')}">Item</div>
3496      * </pre>
3497      *
3498      * @param {string|object} stateOrName The state name (absolute or relative) or state object you'd like to check.
3499      * @param {object=} params A param object, e.g. `{sectionId: section.id}`, that you'd like
3500      * to test against the current active state.
3501      * @param {object=} options An options object.  The options are:
3502      *
3503      * - **`relative`** - {string|object} -  If `stateOrName` is a relative state name and `options.relative` is set, .is will
3504      * test relative to `options.relative` state (or name).
3505      *
3506      * @returns {boolean} Returns true if it is the state.
3507      */
3508     $state.is = function is(stateOrName, params, options) {
3509       options = extend({ relative: $state.$current }, options || {});
3510       var state = findState(stateOrName, options.relative);
3511
3512       if (!isDefined(state)) { return undefined; }
3513       if ($state.$current !== state) { return false; }
3514
3515       return !params || objectKeys(params).reduce(function(acc, key) {
3516         var paramDef = state.params[key];
3517         return acc && !paramDef || paramDef.type.equals($stateParams[key], params[key]);
3518       }, true);
3519     };
3520
3521     /**
3522      * @ngdoc function
3523      * @name ui.router.state.$state#includes
3524      * @methodOf ui.router.state.$state
3525      *
3526      * @description
3527      * A method to determine if the current active state is equal to or is the child of the
3528      * state stateName. If any params are passed then they will be tested for a match as well.
3529      * Not all the parameters need to be passed, just the ones you'd like to test for equality.
3530      *
3531      * @example
3532      * Partial and relative names
3533      * <pre>
3534      * $state.$current.name = 'contacts.details.item';
3535      *
3536      * // Using partial names
3537      * $state.includes("contacts"); // returns true
3538      * $state.includes("contacts.details"); // returns true
3539      * $state.includes("contacts.details.item"); // returns true
3540      * $state.includes("contacts.list"); // returns false
3541      * $state.includes("about"); // returns false
3542      *
3543      * // Using relative names (. and ^), typically from a template
3544      * // E.g. from the 'contacts.details' template
3545      * <div ng-class="{highlighted: $state.includes('.item')}">Item</div>
3546      * </pre>
3547      *
3548      * Basic globbing patterns
3549      * <pre>
3550      * $state.$current.name = 'contacts.details.item.url';
3551      *
3552      * $state.includes("*.details.*.*"); // returns true
3553      * $state.includes("*.details.**"); // returns true
3554      * $state.includes("**.item.**"); // returns true
3555      * $state.includes("*.details.item.url"); // returns true
3556      * $state.includes("*.details.*.url"); // returns true
3557      * $state.includes("*.details.*"); // returns false
3558      * $state.includes("item.**"); // returns false
3559      * </pre>
3560      *
3561      * @param {string} stateOrName A partial name, relative name, or glob pattern
3562      * to be searched for within the current state name.
3563      * @param {object=} params A param object, e.g. `{sectionId: section.id}`,
3564      * that you'd like to test against the current active state.
3565      * @param {object=} options An options object.  The options are:
3566      *
3567      * - **`relative`** - {string|object=} -  If `stateOrName` is a relative state reference and `options.relative` is set,
3568      * .includes will test relative to `options.relative` state (or name).
3569      *
3570      * @returns {boolean} Returns true if it does include the state
3571      */
3572     $state.includes = function includes(stateOrName, params, options) {
3573       options = extend({ relative: $state.$current }, options || {});
3574       if (isString(stateOrName) && isGlob(stateOrName)) {
3575         if (!doesStateMatchGlob(stateOrName)) {
3576           return false;
3577         }
3578         stateOrName = $state.$current.name;
3579       }
3580
3581       var state = findState(stateOrName, options.relative);
3582       if (!isDefined(state)) { return undefined; }
3583       if (!isDefined($state.$current.includes[state.name])) { return false; }
3584       if (!params) { return true; }
3585
3586       var keys = objectKeys(params);
3587       for (var i = 0; i < keys.length; i++) {
3588         var key = keys[i], paramDef = state.params[key];
3589         if (paramDef && !paramDef.type.equals($stateParams[key], params[key])) {
3590           return false;
3591         }
3592       }
3593
3594       return objectKeys(params).reduce(function(acc, key) {
3595         var paramDef = state.params[key];
3596         return acc && !paramDef || paramDef.type.equals($stateParams[key], params[key]);
3597       }, true);
3598     };
3599
3600
3601     /**
3602      * @ngdoc function
3603      * @name ui.router.state.$state#href
3604      * @methodOf ui.router.state.$state
3605      *
3606      * @description
3607      * A url generation method that returns the compiled url for the given state populated with the given params.
3608      *
3609      * @example
3610      * <pre>
3611      * expect($state.href("about.person", { person: "bob" })).toEqual("/about/bob");
3612      * </pre>
3613      *
3614      * @param {string|object} stateOrName The state name or state object you'd like to generate a url from.
3615      * @param {object=} params An object of parameter values to fill the state's required parameters.
3616      * @param {object=} options Options object. The options are:
3617      *
3618      * - **`lossy`** - {boolean=true} -  If true, and if there is no url associated with the state provided in the
3619      *    first parameter, then the constructed href url will be built from the first navigable ancestor (aka
3620      *    ancestor with a valid url).
3621      * - **`inherit`** - {boolean=true}, If `true` will inherit url parameters from current url.
3622      * - **`relative`** - {object=$state.$current}, When transitioning with relative path (e.g '^'), 
3623      *    defines which state to be relative from.
3624      * - **`absolute`** - {boolean=false},  If true will generate an absolute url, e.g. "http://www.example.com/fullurl".
3625      * 
3626      * @returns {string} compiled state url
3627      */
3628     $state.href = function href(stateOrName, params, options) {
3629       options = extend({
3630         lossy:    true,
3631         inherit:  true,
3632         absolute: false,
3633         relative: $state.$current
3634       }, options || {});
3635
3636       var state = findState(stateOrName, options.relative);
3637
3638       if (!isDefined(state)) return null;
3639       if (options.inherit) params = inheritParams($stateParams, params || {}, $state.$current, state);
3640       
3641       var nav = (state && options.lossy) ? state.navigable : state;
3642
3643       if (!nav || nav.url === undefined || nav.url === null) {
3644         return null;
3645       }
3646       return $urlRouter.href(nav.url, filterByKeys(state.params.$$keys().concat('#'), params || {}), {
3647         absolute: options.absolute
3648       });
3649     };
3650
3651     /**
3652      * @ngdoc function
3653      * @name ui.router.state.$state#get
3654      * @methodOf ui.router.state.$state
3655      *
3656      * @description
3657      * Returns the state configuration object for any specific state or all states.
3658      *
3659      * @param {string|object=} stateOrName (absolute or relative) If provided, will only get the config for
3660      * the requested state. If not provided, returns an array of ALL state configs.
3661      * @param {string|object=} context When stateOrName is a relative state reference, the state will be retrieved relative to context.
3662      * @returns {Object|Array} State configuration object or array of all objects.
3663      */
3664     $state.get = function (stateOrName, context) {
3665       if (arguments.length === 0) return map(objectKeys(states), function(name) { return states[name].self; });
3666       var state = findState(stateOrName, context || $state.$current);
3667       return (state && state.self) ? state.self : null;
3668     };
3669
3670     function resolveState(state, params, paramsAreFiltered, inherited, dst, options) {
3671       // Make a restricted $stateParams with only the parameters that apply to this state if
3672       // necessary. In addition to being available to the controller and onEnter/onExit callbacks,
3673       // we also need $stateParams to be available for any $injector calls we make during the
3674       // dependency resolution process.
3675       var $stateParams = (paramsAreFiltered) ? params : filterByKeys(state.params.$$keys(), params);
3676       var locals = { $stateParams: $stateParams };
3677
3678       // Resolve 'global' dependencies for the state, i.e. those not specific to a view.
3679       // We're also including $stateParams in this; that way the parameters are restricted
3680       // to the set that should be visible to the state, and are independent of when we update
3681       // the global $state and $stateParams values.
3682       dst.resolve = $resolve.resolve(state.resolve, locals, dst.resolve, state);
3683       var promises = [dst.resolve.then(function (globals) {
3684         dst.globals = globals;
3685       })];
3686       if (inherited) promises.push(inherited);
3687
3688       function resolveViews() {
3689         var viewsPromises = [];
3690
3691         // Resolve template and dependencies for all views.
3692         forEach(state.views, function (view, name) {
3693           var injectables = (view.resolve && view.resolve !== state.resolve ? view.resolve : {});
3694           injectables.$template = [ function () {
3695             return $view.load(name, { view: view, locals: dst.globals, params: $stateParams, notify: options.notify }) || '';
3696           }];
3697
3698           viewsPromises.push($resolve.resolve(injectables, dst.globals, dst.resolve, state).then(function (result) {
3699             // References to the controller (only instantiated at link time)
3700             if (isFunction(view.controllerProvider) || isArray(view.controllerProvider)) {
3701               var injectLocals = angular.extend({}, injectables, dst.globals);
3702               result.$$controller = $injector.invoke(view.controllerProvider, null, injectLocals);
3703             } else {
3704               result.$$controller = view.controller;
3705             }
3706             // Provide access to the state itself for internal use
3707             result.$$state = state;
3708             result.$$controllerAs = view.controllerAs;
3709             result.$$resolveAs = view.resolveAs;
3710             dst[name] = result;
3711           }));
3712         });
3713
3714         return $q.all(viewsPromises).then(function(){
3715           return dst.globals;
3716         });
3717       }
3718
3719       // Wait for all the promises and then return the activation object
3720       return $q.all(promises).then(resolveViews).then(function (values) {
3721         return dst;
3722       });
3723     }
3724
3725     return $state;
3726   }
3727
3728   function shouldSkipReload(to, toParams, from, fromParams, locals, options) {
3729     // Return true if there are no differences in non-search (path/object) params, false if there are differences
3730     function nonSearchParamsEqual(fromAndToState, fromParams, toParams) {
3731       // Identify whether all the parameters that differ between `fromParams` and `toParams` were search params.
3732       function notSearchParam(key) {
3733         return fromAndToState.params[key].location != "search";
3734       }
3735       var nonQueryParamKeys = fromAndToState.params.$$keys().filter(notSearchParam);
3736       var nonQueryParams = pick.apply({}, [fromAndToState.params].concat(nonQueryParamKeys));
3737       var nonQueryParamSet = new $$UMFP.ParamSet(nonQueryParams);
3738       return nonQueryParamSet.$$equals(fromParams, toParams);
3739     }
3740
3741     // If reload was not explicitly requested
3742     // and we're transitioning to the same state we're already in
3743     // and    the locals didn't change
3744     //     or they changed in a way that doesn't merit reloading
3745     //        (reloadOnParams:false, or reloadOnSearch.false and only search params changed)
3746     // Then return true.
3747     if (!options.reload && to === from &&
3748       (locals === from.locals || (to.self.reloadOnSearch === false && nonSearchParamsEqual(from, fromParams, toParams)))) {
3749       return true;
3750     }
3751   }
3752 }
3753
3754 angular.module('ui.router.state')
3755   .factory('$stateParams', function () { return {}; })
3756   .constant("$state.runtime", { autoinject: true })
3757   .provider('$state', $StateProvider)
3758   // Inject $state to initialize when entering runtime. #2574
3759   .run(['$injector', function ($injector) {
3760     // Allow tests (stateSpec.js) to turn this off by defining this constant
3761     if ($injector.get("$state.runtime").autoinject) {
3762       $injector.get('$state');
3763     }
3764   }]);
3765
3766
3767 $ViewProvider.$inject = [];
3768 function $ViewProvider() {
3769
3770   this.$get = $get;
3771   /**
3772    * @ngdoc object
3773    * @name ui.router.state.$view
3774    *
3775    * @requires ui.router.util.$templateFactory
3776    * @requires $rootScope
3777    *
3778    * @description
3779    *
3780    */
3781   $get.$inject = ['$rootScope', '$templateFactory'];
3782   function $get(   $rootScope,   $templateFactory) {
3783     return {
3784       // $view.load('full.viewName', { template: ..., controller: ..., resolve: ..., async: false, params: ... })
3785       /**
3786        * @ngdoc function
3787        * @name ui.router.state.$view#load
3788        * @methodOf ui.router.state.$view
3789        *
3790        * @description
3791        *
3792        * @param {string} name name
3793        * @param {object} options option object.
3794        */
3795       load: function load(name, options) {
3796         var result, defaults = {
3797           template: null, controller: null, view: null, locals: null, notify: true, async: true, params: {}
3798         };
3799         options = extend(defaults, options);
3800
3801         if (options.view) {
3802           result = $templateFactory.fromConfig(options.view, options.params, options.locals);
3803         }
3804         return result;
3805       }
3806     };
3807   }
3808 }
3809
3810 angular.module('ui.router.state').provider('$view', $ViewProvider);
3811
3812 /**
3813  * @ngdoc object
3814  * @name ui.router.state.$uiViewScrollProvider
3815  *
3816  * @description
3817  * Provider that returns the {@link ui.router.state.$uiViewScroll} service function.
3818  */
3819 function $ViewScrollProvider() {
3820
3821   var useAnchorScroll = false;
3822
3823   /**
3824    * @ngdoc function
3825    * @name ui.router.state.$uiViewScrollProvider#useAnchorScroll
3826    * @methodOf ui.router.state.$uiViewScrollProvider
3827    *
3828    * @description
3829    * Reverts back to using the core [`$anchorScroll`](http://docs.angularjs.org/api/ng.$anchorScroll) service for
3830    * scrolling based on the url anchor.
3831    */
3832   this.useAnchorScroll = function () {
3833     useAnchorScroll = true;
3834   };
3835
3836   /**
3837    * @ngdoc object
3838    * @name ui.router.state.$uiViewScroll
3839    *
3840    * @requires $anchorScroll
3841    * @requires $timeout
3842    *
3843    * @description
3844    * When called with a jqLite element, it scrolls the element into view (after a
3845    * `$timeout` so the DOM has time to refresh).
3846    *
3847    * If you prefer to rely on `$anchorScroll` to scroll the view to the anchor,
3848    * this can be enabled by calling {@link ui.router.state.$uiViewScrollProvider#methods_useAnchorScroll `$uiViewScrollProvider.useAnchorScroll()`}.
3849    */
3850   this.$get = ['$anchorScroll', '$timeout', function ($anchorScroll, $timeout) {
3851     if (useAnchorScroll) {
3852       return $anchorScroll;
3853     }
3854
3855     return function ($element) {
3856       return $timeout(function () {
3857         $element[0].scrollIntoView();
3858       }, 0, false);
3859     };
3860   }];
3861 }
3862
3863 angular.module('ui.router.state').provider('$uiViewScroll', $ViewScrollProvider);
3864
3865 /**
3866  * @ngdoc directive
3867  * @name ui.router.state.directive:ui-view
3868  *
3869  * @requires ui.router.state.$state
3870  * @requires $compile
3871  * @requires $controller
3872  * @requires $injector
3873  * @requires ui.router.state.$uiViewScroll
3874  * @requires $document
3875  *
3876  * @restrict ECA
3877  *
3878  * @description
3879  * The ui-view directive tells $state where to place your templates.
3880  *
3881  * @param {string=} name A view name. The name should be unique amongst the other views in the
3882  * same state. You can have views of the same name that live in different states.
3883  *
3884  * @param {string=} autoscroll It allows you to set the scroll behavior of the browser window
3885  * when a view is populated. By default, $anchorScroll is overridden by ui-router's custom scroll
3886  * service, {@link ui.router.state.$uiViewScroll}. This custom service let's you
3887  * scroll ui-view elements into view when they are populated during a state activation.
3888  *
3889  * *Note: To revert back to old [`$anchorScroll`](http://docs.angularjs.org/api/ng.$anchorScroll)
3890  * functionality, call `$uiViewScrollProvider.useAnchorScroll()`.*
3891  *
3892  * @param {string=} onload Expression to evaluate whenever the view updates.
3893  *
3894  * @example
3895  * A view can be unnamed or named.
3896  * <pre>
3897  * <!-- Unnamed -->
3898  * <div ui-view></div>
3899  *
3900  * <!-- Named -->
3901  * <div ui-view="viewName"></div>
3902  * </pre>
3903  *
3904  * You can only have one unnamed view within any template (or root html). If you are only using a
3905  * single view and it is unnamed then you can populate it like so:
3906  * <pre>
3907  * <div ui-view></div>
3908  * $stateProvider.state("home", {
3909  *   template: "<h1>HELLO!</h1>"
3910  * })
3911  * </pre>
3912  *
3913  * The above is a convenient shortcut equivalent to specifying your view explicitly with the {@link ui.router.state.$stateProvider#methods_state `views`}
3914  * config property, by name, in this case an empty name:
3915  * <pre>
3916  * $stateProvider.state("home", {
3917  *   views: {
3918  *     "": {
3919  *       template: "<h1>HELLO!</h1>"
3920  *     }
3921  *   }    
3922  * })
3923  * </pre>
3924  *
3925  * But typically you'll only use the views property if you name your view or have more than one view
3926  * in the same template. There's not really a compelling reason to name a view if its the only one,
3927  * but you could if you wanted, like so:
3928  * <pre>
3929  * <div ui-view="main"></div>
3930  * </pre>
3931  * <pre>
3932  * $stateProvider.state("home", {
3933  *   views: {
3934  *     "main": {
3935  *       template: "<h1>HELLO!</h1>"
3936  *     }
3937  *   }    
3938  * })
3939  * </pre>
3940  *
3941  * Really though, you'll use views to set up multiple views:
3942  * <pre>
3943  * <div ui-view></div>
3944  * <div ui-view="chart"></div>
3945  * <div ui-view="data"></div>
3946  * </pre>
3947  *
3948  * <pre>
3949  * $stateProvider.state("home", {
3950  *   views: {
3951  *     "": {
3952  *       template: "<h1>HELLO!</h1>"
3953  *     },
3954  *     "chart": {
3955  *       template: "<chart_thing/>"
3956  *     },
3957  *     "data": {
3958  *       template: "<data_thing/>"
3959  *     }
3960  *   }    
3961  * })
3962  * </pre>
3963  *
3964  * Examples for `autoscroll`:
3965  *
3966  * <pre>
3967  * <!-- If autoscroll present with no expression,
3968  *      then scroll ui-view into view -->
3969  * <ui-view autoscroll/>
3970  *
3971  * <!-- If autoscroll present with valid expression,
3972  *      then scroll ui-view into view if expression evaluates to true -->
3973  * <ui-view autoscroll='true'/>
3974  * <ui-view autoscroll='false'/>
3975  * <ui-view autoscroll='scopeVariable'/>
3976  * </pre>
3977  *
3978  * Resolve data:
3979  *
3980  * The resolved data from the state's `resolve` block is placed on the scope as `$resolve` (this
3981  * can be customized using [[ViewDeclaration.resolveAs]]).  This can be then accessed from the template.
3982  *
3983  * Note that when `controllerAs` is being used, `$resolve` is set on the controller instance *after* the
3984  * controller is instantiated.  The `$onInit()` hook can be used to perform initialization code which
3985  * depends on `$resolve` data.
3986  *
3987  * Example usage of $resolve in a view template
3988  * <pre>
3989  * $stateProvider.state('home', {
3990  *   template: '<my-component user="$resolve.user"></my-component>',
3991  *   resolve: {
3992  *     user: function(UserService) { return UserService.fetchUser(); }
3993  *   }
3994  * });
3995  * </pre>
3996  */
3997 $ViewDirective.$inject = ['$state', '$injector', '$uiViewScroll', '$interpolate', '$q'];
3998 function $ViewDirective(   $state,   $injector,   $uiViewScroll,   $interpolate,   $q) {
3999
4000   function getService() {
4001     return ($injector.has) ? function(service) {
4002       return $injector.has(service) ? $injector.get(service) : null;
4003     } : function(service) {
4004       try {
4005         return $injector.get(service);
4006       } catch (e) {
4007         return null;
4008       }
4009     };
4010   }
4011
4012   var service = getService(),
4013       $animator = service('$animator'),
4014       $animate = service('$animate');
4015
4016   // Returns a set of DOM manipulation functions based on which Angular version
4017   // it should use
4018   function getRenderer(attrs, scope) {
4019     var statics = function() {
4020       return {
4021         enter: function (element, target, cb) { target.after(element); cb(); },
4022         leave: function (element, cb) { element.remove(); cb(); }
4023       };
4024     };
4025
4026     if ($animate) {
4027       return {
4028         enter: function(element, target, cb) {
4029           if (angular.version.minor > 2) {
4030             $animate.enter(element, null, target).then(cb);
4031           } else {
4032             $animate.enter(element, null, target, cb);
4033           }
4034         },
4035         leave: function(element, cb) {
4036           if (angular.version.minor > 2) {
4037             $animate.leave(element).then(cb);
4038           } else {
4039             $animate.leave(element, cb);
4040           }
4041         }
4042       };
4043     }
4044
4045     if ($animator) {
4046       var animate = $animator && $animator(scope, attrs);
4047
4048       return {
4049         enter: function(element, target, cb) {animate.enter(element, null, target); cb(); },
4050         leave: function(element, cb) { animate.leave(element); cb(); }
4051       };
4052     }
4053
4054     return statics();
4055   }
4056
4057   var directive = {
4058     restrict: 'ECA',
4059     terminal: true,
4060     priority: 400,
4061     transclude: 'element',
4062     compile: function (tElement, tAttrs, $transclude) {
4063       return function (scope, $element, attrs) {
4064         var previousEl, currentEl, currentScope, latestLocals,
4065             onloadExp     = attrs.onload || '',
4066             autoScrollExp = attrs.autoscroll,
4067             renderer      = getRenderer(attrs, scope),
4068             inherited     = $element.inheritedData('$uiView');
4069
4070         scope.$on('$stateChangeSuccess', function() {
4071           updateView(false);
4072         });
4073
4074         updateView(true);
4075
4076         function cleanupLastView() {
4077           if (previousEl) {
4078             previousEl.remove();
4079             previousEl = null;
4080           }
4081
4082           if (currentScope) {
4083             currentScope.$destroy();
4084             currentScope = null;
4085           }
4086
4087           if (currentEl) {
4088             var $uiViewData = currentEl.data('$uiViewAnim');
4089             renderer.leave(currentEl, function() {
4090               $uiViewData.$$animLeave.resolve();
4091               previousEl = null;
4092             });
4093
4094             previousEl = currentEl;
4095             currentEl = null;
4096           }
4097         }
4098
4099         function updateView(firstTime) {
4100           var newScope,
4101               name            = getUiViewName(scope, attrs, $element, $interpolate),
4102               previousLocals  = name && $state.$current && $state.$current.locals[name];
4103
4104           if (!firstTime && previousLocals === latestLocals) return; // nothing to do
4105           newScope = scope.$new();
4106           latestLocals = $state.$current.locals[name];
4107
4108           /**
4109            * @ngdoc event
4110            * @name ui.router.state.directive:ui-view#$viewContentLoading
4111            * @eventOf ui.router.state.directive:ui-view
4112            * @eventType emits on ui-view directive scope
4113            * @description
4114            *
4115            * Fired once the view **begins loading**, *before* the DOM is rendered.
4116            *
4117            * @param {Object} event Event object.
4118            * @param {string} viewName Name of the view.
4119            */
4120           newScope.$emit('$viewContentLoading', name);
4121
4122           var clone = $transclude(newScope, function(clone) {
4123             var animEnter = $q.defer(), animLeave = $q.defer();
4124             var viewAnimData = {
4125               $animEnter: animEnter.promise,
4126               $animLeave: animLeave.promise,
4127               $$animLeave: animLeave
4128             };
4129
4130             clone.data('$uiViewAnim', viewAnimData);
4131             renderer.enter(clone, $element, function onUiViewEnter() {
4132               animEnter.resolve();
4133               if(currentScope) {
4134                 currentScope.$emit('$viewContentAnimationEnded');
4135               }
4136
4137               if (angular.isDefined(autoScrollExp) && !autoScrollExp || scope.$eval(autoScrollExp)) {
4138                 $uiViewScroll(clone);
4139               }
4140             });
4141             cleanupLastView();
4142           });
4143
4144           currentEl = clone;
4145           currentScope = newScope;
4146           /**
4147            * @ngdoc event
4148            * @name ui.router.state.directive:ui-view#$viewContentLoaded
4149            * @eventOf ui.router.state.directive:ui-view
4150            * @eventType emits on ui-view directive scope
4151            * @description
4152            * Fired once the view is **loaded**, *after* the DOM is rendered.
4153            *
4154            * @param {Object} event Event object.
4155            * @param {string} viewName Name of the view.
4156            */
4157           currentScope.$emit('$viewContentLoaded', name);
4158           currentScope.$eval(onloadExp);
4159         }
4160       };
4161     }
4162   };
4163
4164   return directive;
4165 }
4166
4167 $ViewDirectiveFill.$inject = ['$compile', '$controller', '$state', '$interpolate'];
4168 function $ViewDirectiveFill (  $compile,   $controller,   $state,   $interpolate) {
4169   return {
4170     restrict: 'ECA',
4171     priority: -400,
4172     compile: function (tElement) {
4173       var initial = tElement.html();
4174       if (tElement.empty) {
4175         tElement.empty();
4176       } else {
4177         // ng 1.0.0 doesn't have empty(), which cleans up data and handlers
4178         tElement[0].innerHTML = null;
4179       }
4180
4181       return function (scope, $element, attrs) {
4182         var current = $state.$current,
4183             name = getUiViewName(scope, attrs, $element, $interpolate),
4184             locals  = current && current.locals[name];
4185
4186         if (! locals) {
4187           $element.html(initial);
4188           $compile($element.contents())(scope);
4189           return;
4190         }
4191
4192         $element.data('$uiView', { name: name, state: locals.$$state });
4193         $element.html(locals.$template ? locals.$template : initial);
4194
4195         var resolveData = angular.extend({}, locals);
4196         scope[locals.$$resolveAs] = resolveData;
4197
4198         var link = $compile($element.contents());
4199
4200         if (locals.$$controller) {
4201           locals.$scope = scope;
4202           locals.$element = $element;
4203           var controller = $controller(locals.$$controller, locals);
4204           if (locals.$$controllerAs) {
4205             scope[locals.$$controllerAs] = controller;
4206             scope[locals.$$controllerAs][locals.$$resolveAs] = resolveData;
4207           }
4208           if (isFunction(controller.$onInit)) controller.$onInit();
4209           $element.data('$ngControllerController', controller);
4210           $element.children().data('$ngControllerController', controller);
4211         }
4212
4213         link(scope);
4214       };
4215     }
4216   };
4217 }
4218
4219 /**
4220  * Shared ui-view code for both directives:
4221  * Given scope, element, and its attributes, return the view's name
4222  */
4223 function getUiViewName(scope, attrs, element, $interpolate) {
4224   var name = $interpolate(attrs.uiView || attrs.name || '')(scope);
4225   var uiViewCreatedBy = element.inheritedData('$uiView');
4226   return name.indexOf('@') >= 0 ?  name :  (name + '@' + (uiViewCreatedBy ? uiViewCreatedBy.state.name : ''));
4227 }
4228
4229 angular.module('ui.router.state').directive('uiView', $ViewDirective);
4230 angular.module('ui.router.state').directive('uiView', $ViewDirectiveFill);
4231
4232 function parseStateRef(ref, current) {
4233   var preparsed = ref.match(/^\s*({[^}]*})\s*$/), parsed;
4234   if (preparsed) ref = current + '(' + preparsed[1] + ')';
4235   parsed = ref.replace(/\n/g, " ").match(/^([^(]+?)\s*(\((.*)\))?$/);
4236   if (!parsed || parsed.length !== 4) throw new Error("Invalid state ref '" + ref + "'");
4237   return { state: parsed[1], paramExpr: parsed[3] || null };
4238 }
4239
4240 function stateContext(el) {
4241   var stateData = el.parent().inheritedData('$uiView');
4242
4243   if (stateData && stateData.state && stateData.state.name) {
4244     return stateData.state;
4245   }
4246 }
4247
4248 function getTypeInfo(el) {
4249   // SVGAElement does not use the href attribute, but rather the 'xlinkHref' attribute.
4250   var isSvg = Object.prototype.toString.call(el.prop('href')) === '[object SVGAnimatedString]';
4251   var isForm = el[0].nodeName === "FORM";
4252
4253   return {
4254     attr: isForm ? "action" : (isSvg ? 'xlink:href' : 'href'),
4255     isAnchor: el.prop("tagName").toUpperCase() === "A",
4256     clickable: !isForm
4257   };
4258 }
4259
4260 function clickHook(el, $state, $timeout, type, current) {
4261   return function(e) {
4262     var button = e.which || e.button, target = current();
4263
4264     if (!(button > 1 || e.ctrlKey || e.metaKey || e.shiftKey || el.attr('target'))) {
4265       // HACK: This is to allow ng-clicks to be processed before the transition is initiated:
4266       var transition = $timeout(function() {
4267         $state.go(target.state, target.params, target.options);
4268       });
4269       e.preventDefault();
4270
4271       // if the state has no URL, ignore one preventDefault from the <a> directive.
4272       var ignorePreventDefaultCount = type.isAnchor && !target.href ? 1: 0;
4273
4274       e.preventDefault = function() {
4275         if (ignorePreventDefaultCount-- <= 0) $timeout.cancel(transition);
4276       };
4277     }
4278   };
4279 }
4280
4281 function defaultOpts(el, $state) {
4282   return { relative: stateContext(el) || $state.$current, inherit: true };
4283 }
4284
4285 /**
4286  * @ngdoc directive
4287  * @name ui.router.state.directive:ui-sref
4288  *
4289  * @requires ui.router.state.$state
4290  * @requires $timeout
4291  *
4292  * @restrict A
4293  *
4294  * @description
4295  * A directive that binds a link (`<a>` tag) to a state. If the state has an associated
4296  * URL, the directive will automatically generate & update the `href` attribute via
4297  * the {@link ui.router.state.$state#methods_href $state.href()} method. Clicking
4298  * the link will trigger a state transition with optional parameters.
4299  *
4300  * Also middle-clicking, right-clicking, and ctrl-clicking on the link will be
4301  * handled natively by the browser.
4302  *
4303  * You can also use relative state paths within ui-sref, just like the relative
4304  * paths passed to `$state.go()`. You just need to be aware that the path is relative
4305  * to the state that the link lives in, in other words the state that loaded the
4306  * template containing the link.
4307  *
4308  * You can specify options to pass to {@link ui.router.state.$state#methods_go $state.go()}
4309  * using the `ui-sref-opts` attribute. Options are restricted to `location`, `inherit`,
4310  * and `reload`.
4311  *
4312  * @example
4313  * Here's an example of how you'd use ui-sref and how it would compile. If you have the
4314  * following template:
4315  * <pre>
4316  * <a ui-sref="home">Home</a> | <a ui-sref="about">About</a> | <a ui-sref="{page: 2}">Next page</a>
4317  *
4318  * <ul>
4319  *     <li ng-repeat="contact in contacts">
4320  *         <a ui-sref="contacts.detail({ id: contact.id })">{{ contact.name }}</a>
4321  *     </li>
4322  * </ul>
4323  * </pre>
4324  *
4325  * Then the compiled html would be (assuming Html5Mode is off and current state is contacts):
4326  * <pre>
4327  * <a href="#/home" ui-sref="home">Home</a> | <a href="#/about" ui-sref="about">About</a> | <a href="#/contacts?page=2" ui-sref="{page: 2}">Next page</a>
4328  *
4329  * <ul>
4330  *     <li ng-repeat="contact in contacts">
4331  *         <a href="#/contacts/1" ui-sref="contacts.detail({ id: contact.id })">Joe</a>
4332  *     </li>
4333  *     <li ng-repeat="contact in contacts">
4334  *         <a href="#/contacts/2" ui-sref="contacts.detail({ id: contact.id })">Alice</a>
4335  *     </li>
4336  *     <li ng-repeat="contact in contacts">
4337  *         <a href="#/contacts/3" ui-sref="contacts.detail({ id: contact.id })">Bob</a>
4338  *     </li>
4339  * </ul>
4340  *
4341  * <a ui-sref="home" ui-sref-opts="{reload: true}">Home</a>
4342  * </pre>
4343  *
4344  * @param {string} ui-sref 'stateName' can be any valid absolute or relative state
4345  * @param {Object} ui-sref-opts options to pass to {@link ui.router.state.$state#methods_go $state.go()}
4346  */
4347 $StateRefDirective.$inject = ['$state', '$timeout'];
4348 function $StateRefDirective($state, $timeout) {
4349   return {
4350     restrict: 'A',
4351     require: ['?^uiSrefActive', '?^uiSrefActiveEq'],
4352     link: function(scope, element, attrs, uiSrefActive) {
4353       var ref    = parseStateRef(attrs.uiSref, $state.current.name);
4354       var def    = { state: ref.state, href: null, params: null };
4355       var type   = getTypeInfo(element);
4356       var active = uiSrefActive[1] || uiSrefActive[0];
4357       var unlinkInfoFn = null;
4358       var hookFn;
4359
4360       def.options = extend(defaultOpts(element, $state), attrs.uiSrefOpts ? scope.$eval(attrs.uiSrefOpts) : {});
4361
4362       var update = function(val) {
4363         if (val) def.params = angular.copy(val);
4364         def.href = $state.href(ref.state, def.params, def.options);
4365
4366         if (unlinkInfoFn) unlinkInfoFn();
4367         if (active) unlinkInfoFn = active.$$addStateInfo(ref.state, def.params);
4368         if (def.href !== null) attrs.$set(type.attr, def.href);
4369       };
4370
4371       if (ref.paramExpr) {
4372         scope.$watch(ref.paramExpr, function(val) { if (val !== def.params) update(val); }, true);
4373         def.params = angular.copy(scope.$eval(ref.paramExpr));
4374       }
4375       update();
4376
4377       if (!type.clickable) return;
4378       hookFn = clickHook(element, $state, $timeout, type, function() { return def; });
4379       element[element.on ? 'on' : 'bind']("click", hookFn);
4380       scope.$on('$destroy', function() {
4381         element[element.off ? 'off' : 'unbind']("click", hookFn);
4382       });
4383     }
4384   };
4385 }
4386
4387 /**
4388  * @ngdoc directive
4389  * @name ui.router.state.directive:ui-state
4390  *
4391  * @requires ui.router.state.uiSref
4392  *
4393  * @restrict A
4394  *
4395  * @description
4396  * Much like ui-sref, but will accept named $scope properties to evaluate for a state definition,
4397  * params and override options.
4398  *
4399  * @param {string} ui-state 'stateName' can be any valid absolute or relative state
4400  * @param {Object} ui-state-params params to pass to {@link ui.router.state.$state#methods_href $state.href()}
4401  * @param {Object} ui-state-opts options to pass to {@link ui.router.state.$state#methods_go $state.go()}
4402  */
4403 $StateRefDynamicDirective.$inject = ['$state', '$timeout'];
4404 function $StateRefDynamicDirective($state, $timeout) {
4405   return {
4406     restrict: 'A',
4407     require: ['?^uiSrefActive', '?^uiSrefActiveEq'],
4408     link: function(scope, element, attrs, uiSrefActive) {
4409       var type   = getTypeInfo(element);
4410       var active = uiSrefActive[1] || uiSrefActive[0];
4411       var group  = [attrs.uiState, attrs.uiStateParams || null, attrs.uiStateOpts || null];
4412       var watch  = '[' + group.map(function(val) { return val || 'null'; }).join(', ') + ']';
4413       var def    = { state: null, params: null, options: null, href: null };
4414       var unlinkInfoFn = null;
4415       var hookFn;
4416
4417       function runStateRefLink (group) {
4418         def.state = group[0]; def.params = group[1]; def.options = group[2];
4419         def.href = $state.href(def.state, def.params, def.options);
4420
4421         if (unlinkInfoFn) unlinkInfoFn();
4422         if (active) unlinkInfoFn = active.$$addStateInfo(def.state, def.params);
4423         if (def.href) attrs.$set(type.attr, def.href);
4424       }
4425
4426       scope.$watch(watch, runStateRefLink, true);
4427       runStateRefLink(scope.$eval(watch));
4428
4429       if (!type.clickable) return;
4430       hookFn = clickHook(element, $state, $timeout, type, function() { return def; });
4431       element[element.on ? 'on' : 'bind']("click", hookFn);
4432       scope.$on('$destroy', function() {
4433         element[element.off ? 'off' : 'unbind']("click", hookFn);
4434       });
4435     }
4436   };
4437 }
4438
4439
4440 /**
4441  * @ngdoc directive
4442  * @name ui.router.state.directive:ui-sref-active
4443  *
4444  * @requires ui.router.state.$state
4445  * @requires ui.router.state.$stateParams
4446  * @requires $interpolate
4447  *
4448  * @restrict A
4449  *
4450  * @description
4451  * A directive working alongside ui-sref to add classes to an element when the
4452  * related ui-sref directive's state is active, and removing them when it is inactive.
4453  * The primary use-case is to simplify the special appearance of navigation menus
4454  * relying on `ui-sref`, by having the "active" state's menu button appear different,
4455  * distinguishing it from the inactive menu items.
4456  *
4457  * ui-sref-active can live on the same element as ui-sref or on a parent element. The first
4458  * ui-sref-active found at the same level or above the ui-sref will be used.
4459  *
4460  * Will activate when the ui-sref's target state or any child state is active. If you
4461  * need to activate only when the ui-sref target state is active and *not* any of
4462  * it's children, then you will use
4463  * {@link ui.router.state.directive:ui-sref-active-eq ui-sref-active-eq}
4464  *
4465  * @example
4466  * Given the following template:
4467  * <pre>
4468  * <ul>
4469  *   <li ui-sref-active="active" class="item">
4470  *     <a href ui-sref="app.user({user: 'bilbobaggins'})">@bilbobaggins</a>
4471  *   </li>
4472  * </ul>
4473  * </pre>
4474  *
4475  *
4476  * When the app state is "app.user" (or any children states), and contains the state parameter "user" with value "bilbobaggins",
4477  * the resulting HTML will appear as (note the 'active' class):
4478  * <pre>
4479  * <ul>
4480  *   <li ui-sref-active="active" class="item active">
4481  *     <a ui-sref="app.user({user: 'bilbobaggins'})" href="/users/bilbobaggins">@bilbobaggins</a>
4482  *   </li>
4483  * </ul>
4484  * </pre>
4485  *
4486  * The class name is interpolated **once** during the directives link time (any further changes to the
4487  * interpolated value are ignored).
4488  *
4489  * Multiple classes may be specified in a space-separated format:
4490  * <pre>
4491  * <ul>
4492  *   <li ui-sref-active='class1 class2 class3'>
4493  *     <a ui-sref="app.user">link</a>
4494  *   </li>
4495  * </ul>
4496  * </pre>
4497  *
4498  * It is also possible to pass ui-sref-active an expression that evaluates
4499  * to an object hash, whose keys represent active class names and whose
4500  * values represent the respective state names/globs.
4501  * ui-sref-active will match if the current active state **includes** any of
4502  * the specified state names/globs, even the abstract ones.
4503  *
4504  * @Example
4505  * Given the following template, with "admin" being an abstract state:
4506  * <pre>
4507  * <div ui-sref-active="{'active': 'admin.*'}">
4508  *   <a ui-sref-active="active" ui-sref="admin.roles">Roles</a>
4509  * </div>
4510  * </pre>
4511  *
4512  * When the current state is "admin.roles" the "active" class will be applied
4513  * to both the <div> and <a> elements. It is important to note that the state
4514  * names/globs passed to ui-sref-active shadow the state provided by ui-sref.
4515  */
4516
4517 /**
4518  * @ngdoc directive
4519  * @name ui.router.state.directive:ui-sref-active-eq
4520  *
4521  * @requires ui.router.state.$state
4522  * @requires ui.router.state.$stateParams
4523  * @requires $interpolate
4524  *
4525  * @restrict A
4526  *
4527  * @description
4528  * The same as {@link ui.router.state.directive:ui-sref-active ui-sref-active} but will only activate
4529  * when the exact target state used in the `ui-sref` is active; no child states.
4530  *
4531  */
4532 $StateRefActiveDirective.$inject = ['$state', '$stateParams', '$interpolate'];
4533 function $StateRefActiveDirective($state, $stateParams, $interpolate) {
4534   return  {
4535     restrict: "A",
4536     controller: ['$scope', '$element', '$attrs', '$timeout', function ($scope, $element, $attrs, $timeout) {
4537       var states = [], activeClasses = {}, activeEqClass, uiSrefActive;
4538
4539       // There probably isn't much point in $observing this
4540       // uiSrefActive and uiSrefActiveEq share the same directive object with some
4541       // slight difference in logic routing
4542       activeEqClass = $interpolate($attrs.uiSrefActiveEq || '', false)($scope);
4543
4544       try {
4545         uiSrefActive = $scope.$eval($attrs.uiSrefActive);
4546       } catch (e) {
4547         // Do nothing. uiSrefActive is not a valid expression.
4548         // Fall back to using $interpolate below
4549       }
4550       uiSrefActive = uiSrefActive || $interpolate($attrs.uiSrefActive || '', false)($scope);
4551       if (isObject(uiSrefActive)) {
4552         forEach(uiSrefActive, function(stateOrName, activeClass) {
4553           if (isString(stateOrName)) {
4554             var ref = parseStateRef(stateOrName, $state.current.name);
4555             addState(ref.state, $scope.$eval(ref.paramExpr), activeClass);
4556           }
4557         });
4558       }
4559
4560       // Allow uiSref to communicate with uiSrefActive[Equals]
4561       this.$$addStateInfo = function (newState, newParams) {
4562         // we already got an explicit state provided by ui-sref-active, so we
4563         // shadow the one that comes from ui-sref
4564         if (isObject(uiSrefActive) && states.length > 0) {
4565           return;
4566         }
4567         var deregister = addState(newState, newParams, uiSrefActive);
4568         update();
4569         return deregister;
4570       };
4571
4572       $scope.$on('$stateChangeSuccess', update);
4573
4574       function addState(stateName, stateParams, activeClass) {
4575         var state = $state.get(stateName, stateContext($element));
4576         var stateHash = createStateHash(stateName, stateParams);
4577
4578         var stateInfo = {
4579           state: state || { name: stateName },
4580           params: stateParams,
4581           hash: stateHash
4582         };
4583
4584         states.push(stateInfo);
4585         activeClasses[stateHash] = activeClass;
4586
4587         return function removeState() {
4588           var idx = states.indexOf(stateInfo);
4589           if (idx !== -1) states.splice(idx, 1);
4590         };
4591       }
4592
4593       /**
4594        * @param {string} state
4595        * @param {Object|string} [params]
4596        * @return {string}
4597        */
4598       function createStateHash(state, params) {
4599         if (!isString(state)) {
4600           throw new Error('state should be a string');
4601         }
4602         if (isObject(params)) {
4603           return state + toJson(params);
4604         }
4605         params = $scope.$eval(params);
4606         if (isObject(params)) {
4607           return state + toJson(params);
4608         }
4609         return state;
4610       }
4611
4612       // Update route state
4613       function update() {
4614         for (var i = 0; i < states.length; i++) {
4615           if (anyMatch(states[i].state, states[i].params)) {
4616             addClass($element, activeClasses[states[i].hash]);
4617           } else {
4618             removeClass($element, activeClasses[states[i].hash]);
4619           }
4620
4621           if (exactMatch(states[i].state, states[i].params)) {
4622             addClass($element, activeEqClass);
4623           } else {
4624             removeClass($element, activeEqClass);
4625           }
4626         }
4627       }
4628
4629       function addClass(el, className) { $timeout(function () { el.addClass(className); }); }
4630       function removeClass(el, className) { el.removeClass(className); }
4631       function anyMatch(state, params) { return $state.includes(state.name, params); }
4632       function exactMatch(state, params) { return $state.is(state.name, params); }
4633
4634       update();
4635     }]
4636   };
4637 }
4638
4639 angular.module('ui.router.state')
4640   .directive('uiSref', $StateRefDirective)
4641   .directive('uiSrefActive', $StateRefActiveDirective)
4642   .directive('uiSrefActiveEq', $StateRefActiveDirective)
4643   .directive('uiState', $StateRefDynamicDirective);
4644
4645 /**
4646  * @ngdoc filter
4647  * @name ui.router.state.filter:isState
4648  *
4649  * @requires ui.router.state.$state
4650  *
4651  * @description
4652  * Translates to {@link ui.router.state.$state#methods_is $state.is("stateName")}.
4653  */
4654 $IsStateFilter.$inject = ['$state'];
4655 function $IsStateFilter($state) {
4656   var isFilter = function (state, params) {
4657     return $state.is(state, params);
4658   };
4659   isFilter.$stateful = true;
4660   return isFilter;
4661 }
4662
4663 /**
4664  * @ngdoc filter
4665  * @name ui.router.state.filter:includedByState
4666  *
4667  * @requires ui.router.state.$state
4668  *
4669  * @description
4670  * Translates to {@link ui.router.state.$state#methods_includes $state.includes('fullOrPartialStateName')}.
4671  */
4672 $IncludedByStateFilter.$inject = ['$state'];
4673 function $IncludedByStateFilter($state) {
4674   var includesFilter = function (state, params, options) {
4675     return $state.includes(state, params, options);
4676   };
4677   includesFilter.$stateful = true;
4678   return  includesFilter;
4679 }
4680
4681 angular.module('ui.router.state')
4682   .filter('isState', $IsStateFilter)
4683   .filter('includedByState', $IncludedByStateFilter);
4684 })(window, window.angular);