1 | /**
|
2 | * State-based routing for AngularJS 1.x
|
3 | * @version v1.0.30
|
4 | * @link https://ui-router.github.io
|
5 | * @license MIT License, http://www.opensource.org/licenses/MIT
|
6 | */
|
7 | (function (global, factory) {
|
8 | typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('angular')) :
|
9 | typeof define === 'function' && define.amd ? define(['exports', 'angular'], factory) :
|
10 | (global = global || self, factory(global['@uirouter/angularjs-state-events'] = {}, global.angular));
|
11 | }(this, (function (exports, ng_from_import) { 'use strict';
|
12 |
|
13 | /** @publicapi @module ng1 */ /** */
|
14 | /** @hidden */ var ng_from_global = angular;
|
15 | /** @hidden */ var ng = ng_from_import && ng_from_import.module ? ng_from_import : ng_from_global;
|
16 |
|
17 | /**
|
18 | * # Legacy state events
|
19 | *
|
20 | * Polyfill implementation of the UI-Router 0.2.x state events.
|
21 | *
|
22 | * The 0.2.x state events are deprecated. We recommend moving to Transition Hooks instead, as they
|
23 | * provide much more flexibility, support async, and provide the context (the Transition, etc) necessary
|
24 | * to implement meaningful application behaviors.
|
25 | *
|
26 | * To enable these state events, include the `stateEvents.js` file in your project, e.g.,
|
27 | * ```
|
28 | * <script src="stateEvents.js"></script>
|
29 | * ```
|
30 | * and also make sure you depend on the `ui.router.state.events` angular module, e.g.,
|
31 | * ```
|
32 | * angular.module("myApplication", ['ui.router', 'ui.router.state.events']
|
33 | * ```
|
34 | *
|
35 | * @publicapi @module ng1_state_events
|
36 | */ /** */
|
37 | /**
|
38 | * An event broadcast on `$rootScope` when the state transition **begins**.
|
39 | *
|
40 | * ### Deprecation warning: use [[TransitionService.onStart]] instead
|
41 | *
|
42 | * You can use `event.preventDefault()`
|
43 | * to prevent the transition from happening and then the transition promise will be
|
44 | * rejected with a `'transition prevented'` value.
|
45 | *
|
46 | * Additional arguments to the event handler are provided:
|
47 | * - `toState`: the Transition Target state
|
48 | * - `toParams`: the Transition Target Params
|
49 | * - `fromState`: the state the transition is coming from
|
50 | * - `fromParams`: the parameters from the state the transition is coming from
|
51 | * - `options`: any Transition Options
|
52 | * - `$transition$`: the [[Transition]]
|
53 | *
|
54 | * #### Example:
|
55 | * ```js
|
56 | * $rootScope.$on('$stateChangeStart', function(event, transition) {
|
57 | * event.preventDefault();
|
58 | * // transitionTo() promise will be rejected with
|
59 | * // a 'transition prevented' error
|
60 | * })
|
61 | * ```
|
62 | *
|
63 | * @event $stateChangeStart
|
64 | * @deprecated
|
65 | */
|
66 | var $stateChangeStart;
|
67 | /**
|
68 | * An event broadcast on `$rootScope` if a transition is **cancelled**.
|
69 | *
|
70 | * ### Deprecation warning: use [[TransitionService.onStart]] instead
|
71 | *
|
72 | * Additional arguments to the event handler are provided:
|
73 | * - `toState`: the Transition Target state
|
74 | * - `toParams`: the Transition Target Params
|
75 | * - `fromState`: the state the transition is coming from
|
76 | * - `fromParams`: the parameters from the state the transition is coming from
|
77 | * - `options`: any Transition Options
|
78 | * - `$transition$`: the [[Transition]] that was cancelled
|
79 | *
|
80 | * @event $stateChangeCancel
|
81 | * @deprecated
|
82 | */
|
83 | var $stateChangeCancel;
|
84 | /**
|
85 | * An event broadcast on `$rootScope` once the state transition is **complete**.
|
86 | *
|
87 | * ### Deprecation warning: use [[TransitionService.onStart]] and [[Transition.promise]], or [[Transition.onSuccess]]
|
88 | *
|
89 | * Additional arguments to the event handler are provided:
|
90 | * - `toState`: the Transition Target state
|
91 | * - `toParams`: the Transition Target Params
|
92 | * - `fromState`: the state the transition is coming from
|
93 | * - `fromParams`: the parameters from the state the transition is coming from
|
94 | * - `options`: any Transition Options
|
95 | * - `$transition$`: the [[Transition]] that just succeeded
|
96 | *
|
97 | * @event $stateChangeSuccess
|
98 | * @deprecated
|
99 | */
|
100 | var $stateChangeSuccess;
|
101 | /**
|
102 | * An event broadcast on `$rootScope` when an **error occurs** during transition.
|
103 | *
|
104 | * ### Deprecation warning: use [[TransitionService.onStart]] and [[Transition.promise]], or [[Transition.onError]]
|
105 | *
|
106 | * It's important to note that if you
|
107 | * have any errors in your resolve functions (javascript errors, non-existent services, etc)
|
108 | * they will not throw traditionally. You must listen for this $stateChangeError event to
|
109 | * catch **ALL** errors.
|
110 | *
|
111 | * Additional arguments to the event handler are provided:
|
112 | * - `toState`: the Transition Target state
|
113 | * - `toParams`: the Transition Target Params
|
114 | * - `fromState`: the state the transition is coming from
|
115 | * - `fromParams`: the parameters from the state the transition is coming from
|
116 | * - `error`: The reason the transition errored.
|
117 | * - `options`: any Transition Options
|
118 | * - `$transition$`: the [[Transition]] that errored
|
119 | *
|
120 | * @event $stateChangeError
|
121 | * @deprecated
|
122 | */
|
123 | var $stateChangeError;
|
124 | /**
|
125 | * An event broadcast on `$rootScope` when a requested state **cannot be found** using the provided state name.
|
126 | *
|
127 | * ### Deprecation warning: use [[StateService.onInvalid]] instead
|
128 | *
|
129 | * The event is broadcast allowing any handlers a single chance to deal with the error (usually by
|
130 | * lazy-loading the unfound state). A `TargetState` object is passed to the listener handler,
|
131 | * you can see its properties in the example. You can use `event.preventDefault()` to abort the
|
132 | * transition and the promise returned from `transitionTo()` will be rejected with a
|
133 | * `'transition aborted'` error.
|
134 | *
|
135 | * Additional arguments to the event handler are provided:
|
136 | * - `unfoundState` Unfound State information. Contains: `to, toParams, options` properties.
|
137 | * - `fromState`: the state the transition is coming from
|
138 | * - `fromParams`: the parameters from the state the transition is coming from
|
139 | * - `options`: any Transition Options
|
140 | *
|
141 | * #### Example:
|
142 | * ```js
|
143 | * // somewhere, assume lazy.state has not been defined
|
144 | * $state.go("lazy.state", { a: 1, b: 2 }, { inherit: false });
|
145 | *
|
146 | * // somewhere else
|
147 | * $scope.$on('$stateNotFound', function(event, transition) {
|
148 | * function(event, unfoundState, fromState, fromParams){
|
149 | * console.log(unfoundState.to); // "lazy.state"
|
150 | * console.log(unfoundState.toParams); // {a:1, b:2}
|
151 | * console.log(unfoundState.options); // {inherit:false} + default options
|
152 | * });
|
153 | * ```
|
154 | *
|
155 | * @event $stateNotFound
|
156 | * @deprecated
|
157 | */
|
158 | var $stateNotFound;
|
159 | (function () {
|
160 | var isFunction = ng.isFunction, isString = ng.isString;
|
161 | function applyPairs(memo, keyValTuple) {
|
162 | var key, value;
|
163 | if (Array.isArray(keyValTuple))
|
164 | key = keyValTuple[0], value = keyValTuple[1];
|
165 | if (!isString(key))
|
166 | throw new Error('invalid parameters to applyPairs');
|
167 | memo[key] = value;
|
168 | return memo;
|
169 | }
|
170 | function stateChangeStartHandler($transition$) {
|
171 | if (!$transition$.options().notify || !$transition$.valid() || $transition$.ignored())
|
172 | return;
|
173 | var $injector = $transition$.injector();
|
174 | var $stateEvents = $injector.get('$stateEvents');
|
175 | var $rootScope = $injector.get('$rootScope');
|
176 | var $state = $injector.get('$state');
|
177 | var $urlRouter = $injector.get('$urlRouter');
|
178 | var enabledEvents = $stateEvents.provider.enabled();
|
179 | var toParams = $transition$.params('to');
|
180 | var fromParams = $transition$.params('from');
|
181 | if (enabledEvents.$stateChangeSuccess) {
|
182 | var startEvent = $rootScope.$broadcast('$stateChangeStart', $transition$.to(), toParams, $transition$.from(), fromParams, $transition$.options(), $transition$);
|
183 | if (startEvent.defaultPrevented) {
|
184 | if (enabledEvents.$stateChangeCancel) {
|
185 | $rootScope.$broadcast('$stateChangeCancel', $transition$.to(), toParams, $transition$.from(), fromParams, $transition$.options(), $transition$);
|
186 | }
|
187 | // Don't update and resync url if there's been a new transition started. see issue #2238, #600
|
188 | if ($state.transition == null)
|
189 | $urlRouter.update();
|
190 | return false;
|
191 | }
|
192 | // right after global state is updated
|
193 | var successOpts = { priority: 9999 };
|
194 | $transition$.onSuccess({}, function () {
|
195 | $rootScope.$broadcast('$stateChangeSuccess', $transition$.to(), toParams, $transition$.from(), fromParams, $transition$.options(), $transition$);
|
196 | }, successOpts);
|
197 | }
|
198 | if (enabledEvents.$stateChangeError) {
|
199 | $transition$.promise['catch'](function (error) {
|
200 | if (error && (error.type === 2 /* RejectType.SUPERSEDED */ || error.type === 3) /* RejectType.ABORTED */)
|
201 | return;
|
202 | var evt = $rootScope.$broadcast('$stateChangeError', $transition$.to(), toParams, $transition$.from(), fromParams, error, $transition$.options(), $transition$);
|
203 | if (!evt.defaultPrevented) {
|
204 | $urlRouter.update();
|
205 | }
|
206 | });
|
207 | }
|
208 | }
|
209 | stateNotFoundHandler.$inject = ['$to$', '$from$', '$state', '$rootScope', '$urlRouter'];
|
210 | function stateNotFoundHandler($to$, $from$, injector) {
|
211 | var $state = injector.get('$state');
|
212 | var $rootScope = injector.get('$rootScope');
|
213 | var $urlRouter = injector.get('$urlRouter');
|
214 | var redirect = { to: $to$.identifier(), toParams: $to$.params(), options: $to$.options() };
|
215 | var e = $rootScope.$broadcast('$stateNotFound', redirect, $from$.state(), $from$.params());
|
216 | if (e.defaultPrevented || e.retry)
|
217 | $urlRouter.update();
|
218 | function redirectFn() {
|
219 | return $state.target(redirect.to, redirect.toParams, redirect.options);
|
220 | }
|
221 | if (e.defaultPrevented) {
|
222 | return false;
|
223 | }
|
224 | else if (e.retry || !!$state.get(redirect.to)) {
|
225 | return e.retry && isFunction(e.retry.then) ? e.retry.then(redirectFn) : redirectFn();
|
226 | }
|
227 | }
|
228 | $StateEventsProvider.$inject = ['$stateProvider'];
|
229 | function $StateEventsProvider($stateProvider) {
|
230 | $StateEventsProvider.prototype.instance = this;
|
231 | var runtime = false;
|
232 | var allEvents = ['$stateChangeStart', '$stateNotFound', '$stateChangeSuccess', '$stateChangeError'];
|
233 | var enabledStateEvents = allEvents.map(function (e) { return [e, true]; }).reduce(applyPairs, {});
|
234 | function assertNotRuntime() {
|
235 | if (runtime)
|
236 | throw new Error('Cannot enable events at runtime (use $stateEventsProvider');
|
237 | }
|
238 | /**
|
239 | * Enables the deprecated UI-Router 0.2.x State Events
|
240 | * [ '$stateChangeStart', '$stateNotFound', '$stateChangeSuccess', '$stateChangeError' ]
|
241 | */
|
242 | this.enable = function () {
|
243 | var events = [];
|
244 | for (var _i = 0; _i < arguments.length; _i++) {
|
245 | events[_i] = arguments[_i];
|
246 | }
|
247 | assertNotRuntime();
|
248 | if (!events || !events.length)
|
249 | events = allEvents;
|
250 | events.forEach(function (event) { return (enabledStateEvents[event] = true); });
|
251 | };
|
252 | /**
|
253 | * Disables the deprecated UI-Router 0.2.x State Events
|
254 | * [ '$stateChangeStart', '$stateNotFound', '$stateChangeSuccess', '$stateChangeError' ]
|
255 | */
|
256 | this.disable = function () {
|
257 | var events = [];
|
258 | for (var _i = 0; _i < arguments.length; _i++) {
|
259 | events[_i] = arguments[_i];
|
260 | }
|
261 | assertNotRuntime();
|
262 | if (!events || !events.length)
|
263 | events = allEvents;
|
264 | events.forEach(function (event) { return delete enabledStateEvents[event]; });
|
265 | };
|
266 | this.enabled = function () { return enabledStateEvents; };
|
267 | this.$get = $get;
|
268 | $get.$inject = ['$transitions'];
|
269 | function $get($transitions) {
|
270 | runtime = true;
|
271 | if (enabledStateEvents['$stateNotFound'])
|
272 | $stateProvider.onInvalid(stateNotFoundHandler);
|
273 | if (enabledStateEvents.$stateChangeStart)
|
274 | $transitions.onBefore({}, stateChangeStartHandler, { priority: 1000 });
|
275 | return {
|
276 | provider: $StateEventsProvider.prototype.instance,
|
277 | };
|
278 | }
|
279 | }
|
280 | ng
|
281 | .module('ui.router.state.events', ['ui.router.state'])
|
282 | .provider('$stateEvents', $StateEventsProvider)
|
283 | .run([
|
284 | '$stateEvents',
|
285 | // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
286 | function ($stateEvents) {
|
287 | /* Invokes $get() */
|
288 | },
|
289 | ]);
|
290 | })();
|
291 |
|
292 | exports.$stateChangeCancel = $stateChangeCancel;
|
293 | exports.$stateChangeError = $stateChangeError;
|
294 | exports.$stateChangeStart = $stateChangeStart;
|
295 | exports.$stateChangeSuccess = $stateChangeSuccess;
|
296 | exports.$stateNotFound = $stateNotFound;
|
297 |
|
298 | Object.defineProperty(exports, '__esModule', { value: true });
|
299 |
|
300 | })));
|
301 | //# sourceMappingURL=stateEvents.js.map
|