1 | ;
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.UrlRouterProvider = void 0;
|
4 | /** @publicapi @module url */ /** */
|
5 | var core_1 = require("@uirouter/core");
|
6 | var core_2 = require("@uirouter/core");
|
7 | /**
|
8 | * Manages rules for client-side URL
|
9 | *
|
10 | * ### Deprecation warning:
|
11 | * This class is now considered to be an internal API
|
12 | * Use the [[UrlService]] instead.
|
13 | * For configuring URL rules, use the [[UrlRulesApi]] which can be found as [[UrlService.rules]].
|
14 | *
|
15 | * This class manages the router rules for what to do when the URL changes.
|
16 | *
|
17 | * This provider remains for backwards compatibility.
|
18 | *
|
19 | * @internalapi
|
20 | * @deprecated
|
21 | */
|
22 | var UrlRouterProvider = /** @class */ (function () {
|
23 | /** @hidden */
|
24 | function UrlRouterProvider(/** @hidden */ router) {
|
25 | this.router = router;
|
26 | }
|
27 | UrlRouterProvider.injectableHandler = function (router, handler) {
|
28 | return function (match) { return core_2.services.$injector.invoke(handler, null, { $match: match, $stateParams: router.globals.params }); };
|
29 | };
|
30 | /** @hidden */
|
31 | UrlRouterProvider.prototype.$get = function () {
|
32 | var urlService = this.router.urlService;
|
33 | this.router.urlRouter.update(true);
|
34 | if (!urlService.interceptDeferred)
|
35 | urlService.listen();
|
36 | return this.router.urlRouter;
|
37 | };
|
38 | /**
|
39 | * Registers a url handler function.
|
40 | *
|
41 | * Registers a low level url handler (a `rule`).
|
42 | * A rule detects specific URL patterns and returns a redirect, or performs some action.
|
43 | *
|
44 | * If a rule returns a string, the URL is replaced with the string, and all rules are fired again.
|
45 | *
|
46 | * #### Example:
|
47 | * ```js
|
48 | * var app = angular.module('app', ['ui.router.router']);
|
49 | *
|
50 | * app.config(function ($urlRouterProvider) {
|
51 | * // Here's an example of how you might allow case insensitive urls
|
52 | * $urlRouterProvider.rule(function ($injector, $location) {
|
53 | * var path = $location.path(),
|
54 | * normalized = path.toLowerCase();
|
55 | *
|
56 | * if (path !== normalized) {
|
57 | * return normalized;
|
58 | * }
|
59 | * });
|
60 | * });
|
61 | * ```
|
62 | *
|
63 | * @param ruleFn
|
64 | * Handler function that takes `$injector` and `$location` services as arguments.
|
65 | * You can use them to detect a url and return a different url as a string.
|
66 | *
|
67 | * @return [[UrlRouterProvider]] (`this`)
|
68 | */
|
69 | UrlRouterProvider.prototype.rule = function (ruleFn) {
|
70 | var _this = this;
|
71 | if (!core_2.isFunction(ruleFn))
|
72 | throw new Error("'rule' must be a function");
|
73 | var match = function () { return ruleFn(core_2.services.$injector, _this.router.locationService); };
|
74 | var rule = new core_1.BaseUrlRule(match, core_2.identity);
|
75 | this.router.urlService.rules.rule(rule);
|
76 | return this;
|
77 | };
|
78 | /**
|
79 | * Defines the path or behavior to use when no url can be matched.
|
80 | *
|
81 | * #### Example:
|
82 | * ```js
|
83 | * var app = angular.module('app', ['ui.router.router']);
|
84 | *
|
85 | * app.config(function ($urlRouterProvider) {
|
86 | * // if the path doesn't match any of the urls you configured
|
87 | * // otherwise will take care of routing the user to the
|
88 | * // specified url
|
89 | * $urlRouterProvider.otherwise('/index');
|
90 | *
|
91 | * // Example of using function rule as param
|
92 | * $urlRouterProvider.otherwise(function ($injector, $location) {
|
93 | * return '/a/valid/url';
|
94 | * });
|
95 | * });
|
96 | * ```
|
97 | *
|
98 | * @param rule
|
99 | * The url path you want to redirect to or a function rule that returns the url path or performs a `$state.go()`.
|
100 | * The function version is passed two params: `$injector` and `$location` services, and should return a url string.
|
101 | *
|
102 | * @return {object} `$urlRouterProvider` - `$urlRouterProvider` instance
|
103 | */
|
104 | UrlRouterProvider.prototype.otherwise = function (rule) {
|
105 | var _this = this;
|
106 | var urlRules = this.router.urlService.rules;
|
107 | if (core_2.isString(rule)) {
|
108 | urlRules.otherwise(rule);
|
109 | }
|
110 | else if (core_2.isFunction(rule)) {
|
111 | urlRules.otherwise(function () { return rule(core_2.services.$injector, _this.router.locationService); });
|
112 | }
|
113 | else {
|
114 | throw new Error("'rule' must be a string or function");
|
115 | }
|
116 | return this;
|
117 | };
|
118 | /**
|
119 | * Registers a handler for a given url matching.
|
120 | *
|
121 | * If the handler is a string, it is
|
122 | * treated as a redirect, and is interpolated according to the syntax of match
|
123 | * (i.e. like `String.replace()` for `RegExp`, or like a `UrlMatcher` pattern otherwise).
|
124 | *
|
125 | * If the handler is a function, it is injectable.
|
126 | * It gets invoked if `$location` matches.
|
127 | * You have the option of inject the match object as `$match`.
|
128 | *
|
129 | * The handler can return
|
130 | *
|
131 | * - **falsy** to indicate that the rule didn't match after all, then `$urlRouter`
|
132 | * will continue trying to find another one that matches.
|
133 | * - **string** which is treated as a redirect and passed to `$location.url()`
|
134 | * - **void** or any **truthy** value tells `$urlRouter` that the url was handled.
|
135 | *
|
136 | * #### Example:
|
137 | * ```js
|
138 | * var app = angular.module('app', ['ui.router.router']);
|
139 | *
|
140 | * app.config(function ($urlRouterProvider) {
|
141 | * $urlRouterProvider.when($state.url, function ($match, $stateParams) {
|
142 | * if ($state.$current.navigable !== state ||
|
143 | * !equalForKeys($match, $stateParams) {
|
144 | * $state.transitionTo(state, $match, false);
|
145 | * }
|
146 | * });
|
147 | * });
|
148 | * ```
|
149 | *
|
150 | * @param what A pattern string to match, compiled as a [[UrlMatcher]].
|
151 | * @param handler The path (or function that returns a path) that you want to redirect your user to.
|
152 | * @param ruleCallback [optional] A callback that receives the `rule` registered with [[UrlMatcher.rule]]
|
153 | *
|
154 | * Note: the handler may also invoke arbitrary code, such as `$state.go()`
|
155 | */
|
156 | UrlRouterProvider.prototype.when = function (what, handler) {
|
157 | if (core_2.isArray(handler) || core_2.isFunction(handler)) {
|
158 | handler = UrlRouterProvider.injectableHandler(this.router, handler);
|
159 | }
|
160 | this.router.urlService.rules.when(what, handler);
|
161 | return this;
|
162 | };
|
163 | /**
|
164 | * Disables monitoring of the URL.
|
165 | *
|
166 | * Call this method before UI-Router has bootstrapped.
|
167 | * It will stop UI-Router from performing the initial url sync.
|
168 | *
|
169 | * This can be useful to perform some asynchronous initialization before the router starts.
|
170 | * Once the initialization is complete, call [[listen]] to tell UI-Router to start watching and synchronizing the URL.
|
171 | *
|
172 | * #### Example:
|
173 | * ```js
|
174 | * var app = angular.module('app', ['ui.router']);
|
175 | *
|
176 | * app.config(function ($urlRouterProvider) {
|
177 | * // Prevent $urlRouter from automatically intercepting URL changes;
|
178 | * $urlRouterProvider.deferIntercept();
|
179 | * })
|
180 | *
|
181 | * app.run(function (MyService, $urlRouter, $http) {
|
182 | * $http.get("/stuff").then(function(resp) {
|
183 | * MyService.doStuff(resp.data);
|
184 | * $urlRouter.listen();
|
185 | * $urlRouter.sync();
|
186 | * });
|
187 | * });
|
188 | * ```
|
189 | *
|
190 | * @param defer Indicates whether to defer location change interception.
|
191 | * Passing no parameter is equivalent to `true`.
|
192 | */
|
193 | UrlRouterProvider.prototype.deferIntercept = function (defer) {
|
194 | this.router.urlService.deferIntercept(defer);
|
195 | };
|
196 | return UrlRouterProvider;
|
197 | }());
|
198 | exports.UrlRouterProvider = UrlRouterProvider;
|
199 | //# sourceMappingURL=urlRouterProvider.js.map |
\ | No newline at end of file |