UNPKG

4.54 kBTypeScriptView Raw
1import { StateDeclaration } from '../state';
2import { UrlMatcher } from './urlMatcher';
3import { UIRouter } from '../router';
4import { StateObject } from '../state/stateObject';
5import { UrlRule, UrlRuleMatchFn, UrlRuleHandlerFn, UrlRuleType, MatcherUrlRule, StateRule, RegExpRule } from './interface';
6/**
7 * Creates a [[UrlRule]]
8 *
9 * Creates a [[UrlRule]] from a:
10 *
11 * - `string`
12 * - [[UrlMatcher]]
13 * - `RegExp`
14 * - [[StateObject]]
15 */
16export declare class UrlRuleFactory {
17 router: UIRouter;
18 static isUrlRule: (obj: any) => boolean;
19 constructor(router: UIRouter);
20 compile(str: string): UrlMatcher;
21 create(what: string | UrlMatcher | StateObject | StateDeclaration | RegExp | UrlRuleMatchFn, handler?: string | UrlRuleHandlerFn): UrlRule;
22 /**
23 * A UrlRule which matches based on a UrlMatcher
24 *
25 * The `handler` may be either a `string`, a [[UrlRuleHandlerFn]] or another [[UrlMatcher]]
26 *
27 * ## Handler as a function
28 *
29 * If `handler` is a function, the function is invoked with:
30 *
31 * - matched parameter values ([[RawParams]] from [[UrlMatcher.exec]])
32 * - url: the current Url ([[UrlParts]])
33 * - router: the router object ([[UIRouter]])
34 *
35 * #### Example:
36 * ```js
37 * var urlMatcher = $umf.compile("/foo/:fooId/:barId");
38 * var rule = factory.fromUrlMatcher(urlMatcher, match => "/home/" + match.fooId + "/" + match.barId);
39 * var match = rule.match('/foo/123/456'); // results in { fooId: '123', barId: '456' }
40 * var result = rule.handler(match); // '/home/123/456'
41 * ```
42 *
43 * ## Handler as UrlMatcher
44 *
45 * If `handler` is a UrlMatcher, the handler matcher is used to create the new url.
46 * The `handler` UrlMatcher is formatted using the matched param from the first matcher.
47 * The url is replaced with the result.
48 *
49 * #### Example:
50 * ```js
51 * var urlMatcher = $umf.compile("/foo/:fooId/:barId");
52 * var handler = $umf.compile("/home/:fooId/:barId");
53 * var rule = factory.fromUrlMatcher(urlMatcher, handler);
54 * var match = rule.match('/foo/123/456'); // results in { fooId: '123', barId: '456' }
55 * var result = rule.handler(match); // '/home/123/456'
56 * ```
57 */
58 fromUrlMatcher(urlMatcher: UrlMatcher, handler: string | UrlMatcher | UrlRuleHandlerFn): MatcherUrlRule;
59 /**
60 * A UrlRule which matches a state by its url
61 *
62 * #### Example:
63 * ```js
64 * var rule = factory.fromState($state.get('foo'), router);
65 * var match = rule.match('/foo/123/456'); // results in { fooId: '123', barId: '456' }
66 * var result = rule.handler(match);
67 * // Starts a transition to 'foo' with params: { fooId: '123', barId: '456' }
68 * ```
69 */
70 fromState(stateOrDecl: StateObject | StateDeclaration, router: UIRouter): StateRule;
71 /**
72 * A UrlRule which matches based on a regular expression
73 *
74 * The `handler` may be either a [[UrlRuleHandlerFn]] or a string.
75 *
76 * ## Handler as a function
77 *
78 * If `handler` is a function, the function is invoked with:
79 *
80 * - regexp match array (from `regexp`)
81 * - url: the current Url ([[UrlParts]])
82 * - router: the router object ([[UIRouter]])
83 *
84 * #### Example:
85 * ```js
86 * var rule = factory.fromRegExp(/^\/foo\/(bar|baz)$/, match => "/home/" + match[1])
87 * var match = rule.match('/foo/bar'); // results in [ '/foo/bar', 'bar' ]
88 * var result = rule.handler(match); // '/home/bar'
89 * ```
90 *
91 * ## Handler as string
92 *
93 * If `handler` is a string, the url is *replaced by the string* when the Rule is invoked.
94 * The string is first interpolated using `string.replace()` style pattern.
95 *
96 * #### Example:
97 * ```js
98 * var rule = factory.fromRegExp(/^\/foo\/(bar|baz)$/, "/home/$1")
99 * var match = rule.match('/foo/bar'); // results in [ '/foo/bar', 'bar' ]
100 * var result = rule.handler(match); // '/home/bar'
101 * ```
102 */
103 fromRegExp(regexp: RegExp, handler: string | UrlRuleHandlerFn): RegExpRule;
104}
105/**
106 * A base rule which calls `match`
107 *
108 * The value from the `match` function is passed through to the `handler`.
109 * @internal
110 */
111export declare class BaseUrlRule implements UrlRule {
112 match: UrlRuleMatchFn;
113 $id: number;
114 priority: number;
115 _group: number;
116 type: UrlRuleType;
117 handler: UrlRuleHandlerFn;
118 matchPriority: (match: any) => number;
119 constructor(match: UrlRuleMatchFn, handler?: UrlRuleHandlerFn);
120}
121
\No newline at end of file