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