UNPKG

6.07 kBJavaScriptView Raw
1/** @publicapi @module ng1 */ /** */
2import { val, isObject, createProxyFunctions, } from '@uirouter/core';
3/**
4 * The Angular 1 `StateProvider`
5 *
6 * The `$stateProvider` works similar to Angular's v1 router, but it focuses purely
7 * on state.
8 *
9 * A state corresponds to a "place" in the application in terms of the overall UI and
10 * navigation. A state describes (via the controller / template / view properties) what
11 * the UI looks like and does at that place.
12 *
13 * States often have things in common, and the primary way of factoring out these
14 * commonalities in this model is via the state hierarchy, i.e. parent/child states aka
15 * nested states.
16 *
17 * The `$stateProvider` provides interfaces to declare these states for your app.
18 */
19var StateProvider = /** @class */ (function () {
20 function StateProvider(stateRegistry, stateService) {
21 this.stateRegistry = stateRegistry;
22 this.stateService = stateService;
23 createProxyFunctions(val(StateProvider.prototype), this, val(this));
24 }
25 /**
26 * Decorates states when they are registered
27 *
28 * Allows you to extend (carefully) or override (at your own peril) the
29 * `stateBuilder` object used internally by [[StateRegistry]].
30 * This can be used to add custom functionality to ui-router,
31 * for example inferring templateUrl based on the state name.
32 *
33 * When passing only a name, it returns the current (original or decorated) builder
34 * function that matches `name`.
35 *
36 * The builder functions that can be decorated are listed below. Though not all
37 * necessarily have a good use case for decoration, that is up to you to decide.
38 *
39 * In addition, users can attach custom decorators, which will generate new
40 * properties within the state's internal definition. There is currently no clear
41 * use-case for this beyond accessing internal states (i.e. $state.$current),
42 * however, expect this to become increasingly relevant as we introduce additional
43 * meta-programming features.
44 *
45 * **Warning**: Decorators should not be interdependent because the order of
46 * execution of the builder functions in non-deterministic. Builder functions
47 * should only be dependent on the state definition object and super function.
48 *
49 *
50 * Existing builder functions and current return values:
51 *
52 * - **parent** `{object}` - returns the parent state object.
53 * - **data** `{object}` - returns state data, including any inherited data that is not
54 * overridden by own values (if any).
55 * - **url** `{object}` - returns a {@link ui.router.util.type:UrlMatcher UrlMatcher}
56 * or `null`.
57 * - **navigable** `{object}` - returns closest ancestor state that has a URL (aka is
58 * navigable).
59 * - **params** `{object}` - returns an array of state params that are ensured to
60 * be a super-set of parent's params.
61 * - **views** `{object}` - returns a views object where each key is an absolute view
62 * name (i.e. "viewName@stateName") and each value is the config object
63 * (template, controller) for the view. Even when you don't use the views object
64 * explicitly on a state config, one is still created for you internally.
65 * So by decorating this builder function you have access to decorating template
66 * and controller properties.
67 * - **ownParams** `{object}` - returns an array of params that belong to the state,
68 * not including any params defined by ancestor states.
69 * - **path** `{string}` - returns the full path from the root down to this state.
70 * Needed for state activation.
71 * - **includes** `{object}` - returns an object that includes every state that
72 * would pass a `$state.includes()` test.
73 *
74 * #### Example:
75 * Override the internal 'views' builder with a function that takes the state
76 * definition, and a reference to the internal function being overridden:
77 * ```js
78 * $stateProvider.decorator('views', function (state, parent) {
79 * let result = {},
80 * views = parent(state);
81 *
82 * angular.forEach(views, function (config, name) {
83 * let autoName = (state.name + '.' + name).replace('.', '/');
84 * config.templateUrl = config.templateUrl || '/partials/' + autoName + '.html';
85 * result[name] = config;
86 * });
87 * return result;
88 * });
89 *
90 * $stateProvider.state('home', {
91 * views: {
92 * 'contact.list': { controller: 'ListController' },
93 * 'contact.item': { controller: 'ItemController' }
94 * }
95 * });
96 * ```
97 *
98 *
99 * ```js
100 * // Auto-populates list and item views with /partials/home/contact/list.html,
101 * // and /partials/home/contact/item.html, respectively.
102 * $state.go('home');
103 * ```
104 *
105 * @param {string} name The name of the builder function to decorate.
106 * @param {object} func A function that is responsible for decorating the original
107 * builder function. The function receives two parameters:
108 *
109 * - `{object}` - state - The state config object.
110 * - `{object}` - super - The original builder function.
111 *
112 * @return {object} $stateProvider - $stateProvider instance
113 */
114 StateProvider.prototype.decorator = function (name, func) {
115 return this.stateRegistry.decorator(name, func) || this;
116 };
117 StateProvider.prototype.state = function (name, definition) {
118 if (isObject(name)) {
119 definition = name;
120 }
121 else {
122 definition.name = name;
123 }
124 this.stateRegistry.register(definition);
125 return this;
126 };
127 /**
128 * Registers an invalid state handler
129 *
130 * This is a passthrough to [[StateService.onInvalid]] for ng1.
131 */
132 StateProvider.prototype.onInvalid = function (callback) {
133 return this.stateService.onInvalid(callback);
134 };
135 return StateProvider;
136}());
137export { StateProvider };
138//# sourceMappingURL=stateProvider.js.map
\No newline at end of file