@uirouter/core
Version:
UI-Router Core: Framework agnostic, State-based routing for JavaScript Single Page Apps
121 lines (120 loc) • 4.54 kB
TypeScript
import { StateDeclaration } from '../state';
import { UrlMatcher } from './urlMatcher';
import { UIRouter } from '../router';
import { StateObject } from '../state/stateObject';
import { UrlRule, UrlRuleMatchFn, UrlRuleHandlerFn, UrlRuleType, MatcherUrlRule, StateRule, RegExpRule } from './interface';
/**
* Creates a [[UrlRule]]
*
* Creates a [[UrlRule]] from a:
*
* - `string`
* - [[UrlMatcher]]
* - `RegExp`
* - [[StateObject]]
*/
export declare class UrlRuleFactory {
router: UIRouter;
static isUrlRule: (obj: any) => boolean;
constructor(router: UIRouter);
compile(str: string): UrlMatcher;
create(what: string | UrlMatcher | StateObject | StateDeclaration | RegExp | UrlRuleMatchFn, handler?: string | UrlRuleHandlerFn): UrlRule;
/**
* A UrlRule which matches based on a UrlMatcher
*
* The `handler` may be either a `string`, a [[UrlRuleHandlerFn]] or another [[UrlMatcher]]
*
* ## Handler as a function
*
* If `handler` is a function, the function is invoked with:
*
* - matched parameter values ([[RawParams]] from [[UrlMatcher.exec]])
* - url: the current Url ([[UrlParts]])
* - router: the router object ([[UIRouter]])
*
* #### Example:
* ```js
* var urlMatcher = $umf.compile("/foo/:fooId/:barId");
* var rule = factory.fromUrlMatcher(urlMatcher, match => "/home/" + match.fooId + "/" + match.barId);
* var match = rule.match('/foo/123/456'); // results in { fooId: '123', barId: '456' }
* var result = rule.handler(match); // '/home/123/456'
* ```
*
* ## Handler as UrlMatcher
*
* If `handler` is a UrlMatcher, the handler matcher is used to create the new url.
* The `handler` UrlMatcher is formatted using the matched param from the first matcher.
* The url is replaced with the result.
*
* #### Example:
* ```js
* var urlMatcher = $umf.compile("/foo/:fooId/:barId");
* var handler = $umf.compile("/home/:fooId/:barId");
* var rule = factory.fromUrlMatcher(urlMatcher, handler);
* var match = rule.match('/foo/123/456'); // results in { fooId: '123', barId: '456' }
* var result = rule.handler(match); // '/home/123/456'
* ```
*/
fromUrlMatcher(urlMatcher: UrlMatcher, handler: string | UrlMatcher | UrlRuleHandlerFn): MatcherUrlRule;
/**
* A UrlRule which matches a state by its url
*
* #### Example:
* ```js
* var rule = factory.fromState($state.get('foo'), router);
* var match = rule.match('/foo/123/456'); // results in { fooId: '123', barId: '456' }
* var result = rule.handler(match);
* // Starts a transition to 'foo' with params: { fooId: '123', barId: '456' }
* ```
*/
fromState(stateOrDecl: StateObject | StateDeclaration, router: UIRouter): StateRule;
/**
* A UrlRule which matches based on a regular expression
*
* The `handler` may be either a [[UrlRuleHandlerFn]] or a string.
*
* ## Handler as a function
*
* If `handler` is a function, the function is invoked with:
*
* - regexp match array (from `regexp`)
* - url: the current Url ([[UrlParts]])
* - router: the router object ([[UIRouter]])
*
* #### Example:
* ```js
* var rule = factory.fromRegExp(/^\/foo\/(bar|baz)$/, match => "/home/" + match[1])
* var match = rule.match('/foo/bar'); // results in [ '/foo/bar', 'bar' ]
* var result = rule.handler(match); // '/home/bar'
* ```
*
* ## Handler as string
*
* If `handler` is a string, the url is *replaced by the string* when the Rule is invoked.
* The string is first interpolated using `string.replace()` style pattern.
*
* #### Example:
* ```js
* var rule = factory.fromRegExp(/^\/foo\/(bar|baz)$/, "/home/$1")
* var match = rule.match('/foo/bar'); // results in [ '/foo/bar', 'bar' ]
* var result = rule.handler(match); // '/home/bar'
* ```
*/
fromRegExp(regexp: RegExp, handler: string | UrlRuleHandlerFn): RegExpRule;
}
/**
* A base rule which calls `match`
*
* The value from the `match` function is passed through to the `handler`.
* @internal
*/
export declare class BaseUrlRule implements UrlRule {
match: UrlRuleMatchFn;
$id: number;
priority: number;
_group: number;
type: UrlRuleType;
handler: UrlRuleHandlerFn;
matchPriority: (match: any) => number;
constructor(match: UrlRuleMatchFn, handler?: UrlRuleHandlerFn);
}