UNPKG

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