UNPKG

6.17 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.TargetState = void 0;
4var predicates_1 = require("../common/predicates");
5var strings_1 = require("../common/strings");
6var common_1 = require("../common");
7/**
8 * Encapsulate the target (destination) state/params/options of a [[Transition]].
9 *
10 * This class is frequently used to redirect a transition to a new destination.
11 *
12 * See:
13 *
14 * - [[HookResult]]
15 * - [[TransitionHookFn]]
16 * - [[TransitionService.onStart]]
17 *
18 * To create a `TargetState`, use [[StateService.target]].
19 *
20 * ---
21 *
22 * This class wraps:
23 *
24 * 1) an identifier for a state
25 * 2) a set of parameters
26 * 3) and transition options
27 * 4) the registered state object (the [[StateDeclaration]])
28 *
29 * Many UI-Router APIs such as [[StateService.go]] take a [[StateOrName]] argument which can
30 * either be a *state object* (a [[StateDeclaration]] or [[StateObject]]) or a *state name* (a string).
31 * The `TargetState` class normalizes those options.
32 *
33 * A `TargetState` may be valid (the state being targeted exists in the registry)
34 * or invalid (the state being targeted is not registered).
35 */
36var TargetState = /** @class */ (function () {
37 /**
38 * The TargetState constructor
39 *
40 * Note: Do not construct a `TargetState` manually.
41 * To create a `TargetState`, use the [[StateService.target]] factory method.
42 *
43 * @param _stateRegistry The StateRegistry to use to look up the _definition
44 * @param _identifier An identifier for a state.
45 * Either a fully-qualified state name, or the object used to define the state.
46 * @param _params Parameters for the target state
47 * @param _options Transition options.
48 *
49 * @internal
50 */
51 function TargetState(_stateRegistry, _identifier, _params, _options) {
52 this._stateRegistry = _stateRegistry;
53 this._identifier = _identifier;
54 this._identifier = _identifier;
55 this._params = common_1.extend({}, _params || {});
56 this._options = common_1.extend({}, _options || {});
57 this._definition = _stateRegistry.matcher.find(_identifier, this._options.relative);
58 }
59 /** The name of the state this object targets */
60 TargetState.prototype.name = function () {
61 return (this._definition && this._definition.name) || this._identifier;
62 };
63 /** The identifier used when creating this TargetState */
64 TargetState.prototype.identifier = function () {
65 return this._identifier;
66 };
67 /** The target parameter values */
68 TargetState.prototype.params = function () {
69 return this._params;
70 };
71 /** The internal state object (if it was found) */
72 TargetState.prototype.$state = function () {
73 return this._definition;
74 };
75 /** The internal state declaration (if it was found) */
76 TargetState.prototype.state = function () {
77 return this._definition && this._definition.self;
78 };
79 /** The target options */
80 TargetState.prototype.options = function () {
81 return this._options;
82 };
83 /** True if the target state was found */
84 TargetState.prototype.exists = function () {
85 return !!(this._definition && this._definition.self);
86 };
87 /** True if the object is valid */
88 TargetState.prototype.valid = function () {
89 return !this.error();
90 };
91 /** If the object is invalid, returns the reason why */
92 TargetState.prototype.error = function () {
93 var base = this.options().relative;
94 if (!this._definition && !!base) {
95 var stateName = base.name ? base.name : base;
96 return "Could not resolve '" + this.name() + "' from state '" + stateName + "'";
97 }
98 if (!this._definition)
99 return "No such state '" + this.name() + "'";
100 if (!this._definition.self)
101 return "State '" + this.name() + "' has an invalid definition";
102 };
103 TargetState.prototype.toString = function () {
104 return "'" + this.name() + "'" + strings_1.stringify(this.params());
105 };
106 /**
107 * Returns a copy of this TargetState which targets a different state.
108 * The new TargetState has the same parameter values and transition options.
109 *
110 * @param state The new state that should be targeted
111 */
112 TargetState.prototype.withState = function (state) {
113 return new TargetState(this._stateRegistry, state, this._params, this._options);
114 };
115 /**
116 * Returns a copy of this TargetState, using the specified parameter values.
117 *
118 * @param params the new parameter values to use
119 * @param replace When false (default) the new parameter values will be merged with the current values.
120 * When true the parameter values will be used instead of the current values.
121 */
122 TargetState.prototype.withParams = function (params, replace) {
123 if (replace === void 0) { replace = false; }
124 var newParams = replace ? params : common_1.extend({}, this._params, params);
125 return new TargetState(this._stateRegistry, this._identifier, newParams, this._options);
126 };
127 /**
128 * Returns a copy of this TargetState, using the specified Transition Options.
129 *
130 * @param options the new options to use
131 * @param replace When false (default) the new options will be merged with the current options.
132 * When true the options will be used instead of the current options.
133 */
134 TargetState.prototype.withOptions = function (options, replace) {
135 if (replace === void 0) { replace = false; }
136 var newOpts = replace ? options : common_1.extend({}, this._options, options);
137 return new TargetState(this._stateRegistry, this._identifier, this._params, newOpts);
138 };
139 /** Returns true if the object has a state property that might be a state or state name */
140 TargetState.isDef = function (obj) {
141 return obj && obj.state && (predicates_1.isString(obj.state) || (predicates_1.isObject(obj.state) && predicates_1.isString(obj.state.name)));
142 };
143 return TargetState;
144}());
145exports.TargetState = TargetState;
146//# sourceMappingURL=targetState.js.map
\No newline at end of file