UNPKG

9.38 kBTypeScriptView Raw
1import { UIRouter } from '../router';
2import { Disposable } from '../interface';
3import { UrlRule, UrlRuleHandlerFn } from './interface';
4import { TargetState, TargetStateDef } from '../state';
5import { UrlMatcher } from './urlMatcher';
6import { UrlRuleFactory } from './urlRule';
7/**
8 * API for managing URL rules
9 *
10 * This API is used to create and manage URL rules.
11 * URL rules are a mechanism to respond to specific URL patterns.
12 *
13 * The most commonly used methods are [[otherwise]] and [[when]].
14 *
15 * This API is found at `router.urlService.rules` (see: [[UIRouter.urlService]], [[URLService.rules]])
16 */
17export declare class UrlRules implements Disposable {
18 private router;
19 /** used to create [[UrlRule]] objects for common cases */
20 urlRuleFactory: UrlRuleFactory;
21 /** @internal */ private _sortFn;
22 /** @internal */ private _otherwiseFn;
23 /** @internal */ private _sorted;
24 /** @internal */ private _rules;
25 /** @internal */ private _id;
26 /** @internal */
27 constructor(/** @internal */ router: UIRouter);
28 /** @internal */
29 dispose(router?: UIRouter): void;
30 /**
31 * Defines the initial state, path, or behavior to use when the app starts.
32 *
33 * This rule defines the initial/starting state for the application.
34 *
35 * This rule is triggered the first time the URL is checked (when the app initially loads).
36 * The rule is triggered only when the url matches either `""` or `"/"`.
37 *
38 * Note: The rule is intended to be used when the root of the application is directly linked to.
39 * When the URL is *not* `""` or `"/"` and doesn't match other rules, the [[otherwise]] rule is triggered.
40 * This allows 404-like behavior when an unknown URL is deep-linked.
41 *
42 * #### Example:
43 * Start app at `home` state.
44 * ```js
45 * .initial({ state: 'home' });
46 * ```
47 *
48 * #### Example:
49 * Start app at `/home` (by url)
50 * ```js
51 * .initial('/home');
52 * ```
53 *
54 * #### Example:
55 * When no other url rule matches, go to `home` state
56 * ```js
57 * .initial((matchValue, url, router) => {
58 * console.log('initial state');
59 * return { state: 'home' };
60 * })
61 * ```
62 *
63 * @param handler The initial state or url path, or a function which returns the state or url path (or performs custom logic).
64 */
65 initial(handler: string | UrlRuleHandlerFn | TargetState | TargetStateDef): void;
66 /**
67 * Defines the state, url, or behavior to use when no other rule matches the URL.
68 *
69 * This rule is matched when *no other rule* matches.
70 * It is generally used to handle unknown URLs (similar to "404" behavior, but on the client side).
71 *
72 * - If `handler` a string, it is treated as a url redirect
73 *
74 * #### Example:
75 * When no other url rule matches, redirect to `/index`
76 * ```js
77 * .otherwise('/index');
78 * ```
79 *
80 * - If `handler` is an object with a `state` property, the state is activated.
81 *
82 * #### Example:
83 * When no other url rule matches, redirect to `home` and provide a `dashboard` parameter value.
84 * ```js
85 * .otherwise({ state: 'home', params: { dashboard: 'default' } });
86 * ```
87 *
88 * - If `handler` is a function, the function receives the current url ([[UrlParts]]) and the [[UIRouter]] object.
89 * The function can perform actions, and/or return a value.
90 *
91 * #### Example:
92 * When no other url rule matches, manually trigger a transition to the `home` state
93 * ```js
94 * .otherwise((matchValue, urlParts, router) => {
95 * router.stateService.go('home');
96 * });
97 * ```
98 *
99 * #### Example:
100 * When no other url rule matches, go to `home` state
101 * ```js
102 * .otherwise((matchValue, urlParts, router) => {
103 * return { state: 'home' };
104 * });
105 * ```
106 *
107 * @param handler The url path to redirect to, or a function which returns the url path (or performs custom logic).
108 */
109 otherwise(handler: string | UrlRuleHandlerFn | TargetState | TargetStateDef): void;
110 /**
111 * Remove a rule previously registered
112 *
113 * @param rule the matcher rule that was previously registered using [[rule]]
114 */
115 removeRule(rule: any): void;
116 /**
117 * Manually adds a URL Rule.
118 *
119 * Usually, a url rule is added using [[StateDeclaration.url]] or [[when]].
120 * This api can be used directly for more control (to register a [[BaseUrlRule]], for example).
121 * Rules can be created using [[urlRuleFactory]], or created manually as simple objects.
122 *
123 * A rule should have a `match` function which returns truthy if the rule matched.
124 * It should also have a `handler` function which is invoked if the rule is the best match.
125 *
126 * @return a function that deregisters the rule
127 */
128 rule(rule: UrlRule): Function;
129 /**
130 * Gets all registered rules
131 *
132 * @returns an array of all the registered rules
133 */
134 rules(): UrlRule[];
135 /**
136 * Defines URL Rule priorities
137 *
138 * More than one rule ([[UrlRule]]) might match a given URL.
139 * This `compareFn` is used to sort the rules by priority.
140 * Higher priority rules should sort earlier.
141 *
142 * The [[defaultRuleSortFn]] is used by default.
143 *
144 * You only need to call this function once.
145 * The `compareFn` will be used to sort the rules as each is registered.
146 *
147 * If called without any parameter, it will re-sort the rules.
148 *
149 * ---
150 *
151 * Url rules may come from multiple sources: states's urls ([[StateDeclaration.url]]), [[when]], and [[rule]].
152 * Each rule has a (user-provided) [[UrlRule.priority]], a [[UrlRule.type]], and a [[UrlRule.$id]]
153 * The `$id` is is the order in which the rule was registered.
154 *
155 * The sort function should use these data, or data found on a specific type
156 * of [[UrlRule]] (such as [[StateRule.state]]), to order the rules as desired.
157 *
158 * #### Example:
159 * This compare function prioritizes rules by the order in which the rules were registered.
160 * A rule registered earlier has higher priority.
161 *
162 * ```js
163 * function compareFn(a, b) {
164 * return a.$id - b.$id;
165 * }
166 * ```
167 *
168 * @param compareFn a function that compares to [[UrlRule]] objects.
169 * The `compareFn` should abide by the `Array.sort` compare function rules.
170 * Given two rules, `a` and `b`, return a negative number if `a` should be higher priority.
171 * Return a positive number if `b` should be higher priority.
172 * Return `0` if the rules are identical.
173 *
174 * See the [mozilla reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#Description)
175 * for details.
176 */
177 sort(compareFn?: (a: UrlRule, b: UrlRule) => number): void;
178 /** @internal */
179 private ensureSorted;
180 /** @internal */
181 private stableSort;
182 /**
183 * Registers a `matcher` and `handler` for custom URLs handling.
184 *
185 * The `matcher` can be:
186 *
187 * - a [[UrlMatcher]]: See: [[UrlMatcherFactory.compile]]
188 * - a `string`: The string is compiled to a [[UrlMatcher]]
189 * - a `RegExp`: The regexp is used to match the url.
190 *
191 * The `handler` can be:
192 *
193 * - a string: The url is redirected to the value of the string.
194 * - a function: The url is redirected to the return value of the function.
195 *
196 * ---
197 *
198 * When the `handler` is a `string` and the `matcher` is a `UrlMatcher` (or string), the redirect
199 * string is interpolated with parameter values.
200 *
201 * #### Example:
202 * When the URL is `/foo/123` the rule will redirect to `/bar/123`.
203 * ```js
204 * .when("/foo/:param1", "/bar/:param1")
205 * ```
206 *
207 * ---
208 *
209 * When the `handler` is a string and the `matcher` is a `RegExp`, the redirect string is
210 * interpolated with capture groups from the RegExp.
211 *
212 * #### Example:
213 * When the URL is `/foo/123` the rule will redirect to `/bar/123`.
214 * ```js
215 * .when(new RegExp("^/foo/(.*)$"), "/bar/$1");
216 * ```
217 *
218 * ---
219 *
220 * When the handler is a function, it receives the matched value, the current URL, and the `UIRouter` object (See [[UrlRuleHandlerFn]]).
221 * The "matched value" differs based on the `matcher`.
222 * For [[UrlMatcher]]s, it will be the matched state params.
223 * For `RegExp`, it will be the match array from `regexp.exec()`.
224 *
225 * If the handler returns a string, the URL is redirected to the string.
226 *
227 * #### Example:
228 * When the URL is `/foo/123` the rule will redirect to `/bar/123`.
229 * ```js
230 * .when(new RegExp("^/foo/(.*)$"), match => "/bar/" + match[1]);
231 * ```
232 *
233 * Note: the `handler` may also invoke arbitrary code, such as `$state.go()`
234 *
235 * @param matcher A pattern `string` to match, compiled as a [[UrlMatcher]], or a `RegExp`.
236 * @param handler The path to redirect to, or a function that returns the path.
237 * @param options `{ priority: number }`
238 *
239 * @return the registered [[UrlRule]]
240 */
241 when(matcher: RegExp | UrlMatcher | string, handler: string | UrlRuleHandlerFn, options?: {
242 priority: number;
243 }): UrlRule;
244}
245
\No newline at end of file