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AAE,OAA0B;QAApE,iBAEC;QAFkB,UAAK,GAAL,KAAK,CAAgB;QAJxC,SAAI,GAAgB,KAAK,CAAC;QAE1B,kBAAa,GAAG,UAAC,KAAK,IAAK,OAAA,CAAC,GAAG,KAAI,CAAC,GAAG,EAAZ,CAAY,CAAC;QAGtC,IAAI,CAAC,OAAO,GAAG,OAAO,IAAI,iBAAQ,CAAC;IACrC,CAAC;IACH,kBAAC;AAAD,CAAC,AAXD,IAWC;AAXY,kCAAW",
10 "sourcesContent": [
11 "import { StateDeclaration } from '../state';\nimport { UrlMatcher } from './urlMatcher';\nimport { isString, isDefined, isFunction } from '../common/predicates';\nimport { UIRouter } from '../router';\nimport { identity, extend } from '../common/common';\nimport { is, or, pattern } from '../common/hof';\nimport { StateObject } from '../state/stateObject';\nimport { RawParams } from '../params/interface';\nimport {\n UrlRule,\n UrlRuleMatchFn,\n UrlRuleHandlerFn,\n UrlRuleType,\n UrlParts,\n MatcherUrlRule,\n StateRule,\n RegExpRule,\n} from './interface';\n\n/**\n * Creates a [[UrlRule]]\n *\n * Creates a [[UrlRule]] from a:\n *\n * - `string`\n * - [[UrlMatcher]]\n * - `RegExp`\n * - [[StateObject]]\n */\nexport class UrlRuleFactory {\n static isUrlRule = (obj) => obj && ['type', 'match', 'handler'].every((key) => isDefined(obj[key]));\n\n constructor(public router: UIRouter) {}\n\n compile(str: string) {\n return this.router.urlMatcherFactory.compile(str);\n }\n\n create(\n what: string | UrlMatcher | StateObject | StateDeclaration | RegExp | UrlRuleMatchFn,\n handler?: string | UrlRuleHandlerFn\n ): UrlRule {\n const { isState, isStateDeclaration } = StateObject;\n const makeRule = pattern([\n [isString, (_what: string) => makeRule(this.compile(_what))],\n [is(UrlMatcher), (_what: UrlMatcher) => this.fromUrlMatcher(_what, handler)],\n [or(isState, isStateDeclaration), (_what: StateObject | StateDeclaration) => this.fromState(_what, this.router)],\n [is(RegExp), (_what: RegExp) => this.fromRegExp(_what, handler)],\n [isFunction, (_what: UrlRuleMatchFn) => new BaseUrlRule(_what, handler as UrlRuleHandlerFn)],\n ]);\n\n const rule = makeRule(what);\n if (!rule) throw new Error(\"invalid 'what' in when()\");\n return rule;\n }\n\n /**\n * A UrlRule which matches based on a UrlMatcher\n *\n * The `handler` may be either a `string`, a [[UrlRuleHandlerFn]] or another [[UrlMatcher]]\n *\n * ## Handler as a function\n *\n * If `handler` is a function, the function is invoked with:\n *\n * - matched parameter values ([[RawParams]] from [[UrlMatcher.exec]])\n * - url: the current Url ([[UrlParts]])\n * - router: the router object ([[UIRouter]])\n *\n * #### Example:\n * ```js\n * var urlMatcher = $umf.compile(\"/foo/:fooId/:barId\");\n * var rule = factory.fromUrlMatcher(urlMatcher, match => \"/home/\" + match.fooId + \"/\" + match.barId);\n * var match = rule.match('/foo/123/456'); // results in { fooId: '123', barId: '456' }\n * var result = rule.handler(match); // '/home/123/456'\n * ```\n *\n * ## Handler as UrlMatcher\n *\n * If `handler` is a UrlMatcher, the handler matcher is used to create the new url.\n * The `handler` UrlMatcher is formatted using the matched param from the first matcher.\n * The url is replaced with the result.\n *\n * #### Example:\n * ```js\n * var urlMatcher = $umf.compile(\"/foo/:fooId/:barId\");\n * var handler = $umf.compile(\"/home/:fooId/:barId\");\n * var rule = factory.fromUrlMatcher(urlMatcher, handler);\n * var match = rule.match('/foo/123/456'); // results in { fooId: '123', barId: '456' }\n * var result = rule.handler(match); // '/home/123/456'\n * ```\n */\n fromUrlMatcher(urlMatcher: UrlMatcher, handler: string | UrlMatcher | UrlRuleHandlerFn): MatcherUrlRule {\n let _handler: UrlRuleHandlerFn = handler as any;\n if (isString(handler)) handler = this.router.urlMatcherFactory.compile(handler);\n if (is(UrlMatcher)(handler)) _handler = (match: RawParams) => (handler as UrlMatcher).format(match);\n\n function matchUrlParamters(url: UrlParts): RawParams {\n const params = urlMatcher.exec(url.path, url.search, url.hash);\n return urlMatcher.validates(params) && params;\n }\n\n // Prioritize URLs, lowest to highest:\n // - Some optional URL parameters, but none matched\n // - No optional parameters in URL\n // - Some optional parameters, some matched\n // - Some optional parameters, all matched\n function matchPriority(params: RawParams): number {\n const optional = urlMatcher.parameters().filter((param) => param.isOptional);\n if (!optional.length) return 0.000001;\n const matched = optional.filter((param) => params[param.id]);\n return matched.length / optional.length;\n }\n\n const details = { urlMatcher, matchPriority, type: 'URLMATCHER' };\n return extend(new BaseUrlRule(matchUrlParamters, _handler), details) as MatcherUrlRule;\n }\n\n /**\n * A UrlRule which matches a state by its url\n *\n * #### Example:\n * ```js\n * var rule = factory.fromState($state.get('foo'), router);\n * var match = rule.match('/foo/123/456'); // results in { fooId: '123', barId: '456' }\n * var result = rule.handler(match);\n * // Starts a transition to 'foo' with params: { fooId: '123', barId: '456' }\n * ```\n */\n fromState(stateOrDecl: StateObject | StateDeclaration, router: UIRouter): StateRule {\n const state = StateObject.isStateDeclaration(stateOrDecl) ? stateOrDecl.$$state() : stateOrDecl;\n\n /**\n * Handles match by transitioning to matched state\n *\n * First checks if the router should start a new transition.\n * A new transition is not required if the current state's URL\n * and the new URL are already identical\n */\n const handler = (match: RawParams) => {\n const $state = router.stateService;\n const globals = router.globals;\n if ($state.href(state, match) !== $state.href(globals.current, globals.params)) {\n $state.transitionTo(state, match, { inherit: true, source: 'url' });\n }\n };\n\n const details = { state, type: 'STATE' };\n return extend(this.fromUrlMatcher(state.url, handler), details) as StateRule;\n }\n\n /**\n * A UrlRule which matches based on a regular expression\n *\n * The `handler` may be either a [[UrlRuleHandlerFn]] or a string.\n *\n * ## Handler as a function\n *\n * If `handler` is a function, the function is invoked with:\n *\n * - regexp match array (from `regexp`)\n * - url: the current Url ([[UrlParts]])\n * - router: the router object ([[UIRouter]])\n *\n * #### Example:\n * ```js\n * var rule = factory.fromRegExp(/^\\/foo\\/(bar|baz)$/, match => \"/home/\" + match[1])\n * var match = rule.match('/foo/bar'); // results in [ '/foo/bar', 'bar' ]\n * var result = rule.handler(match); // '/home/bar'\n * ```\n *\n * ## Handler as string\n *\n * If `handler` is a string, the url is *replaced by the string* when the Rule is invoked.\n * The string is first interpolated using `string.replace()` style pattern.\n *\n * #### Example:\n * ```js\n * var rule = factory.fromRegExp(/^\\/foo\\/(bar|baz)$/, \"/home/$1\")\n * var match = rule.match('/foo/bar'); // results in [ '/foo/bar', 'bar' ]\n * var result = rule.handler(match); // '/home/bar'\n * ```\n */\n fromRegExp(regexp: RegExp, handler: string | UrlRuleHandlerFn): RegExpRule {\n if (regexp.global || regexp.sticky) throw new Error('Rule RegExp must not be global or sticky');\n\n /**\n * If handler is a string, the url will be replaced by the string.\n * If the string has any String.replace() style variables in it (like `$2`),\n * they will be replaced by the captures from [[match]]\n */\n const redirectUrlTo = (match: RegExpExecArray) =>\n // Interpolates matched values into $1 $2, etc using a String.replace()-style pattern\n (handler as string).replace(/\\$(\\$|\\d{1,2})/, (m, what) => match[what === '$' ? 0 : Number(what)]);\n\n const _handler = isString(handler) ? redirectUrlTo : handler;\n\n const matchParamsFromRegexp = (url: UrlParts): RegExpExecArray => regexp.exec(url.path);\n\n const details = { regexp, type: 'REGEXP' };\n return extend(new BaseUrlRule(matchParamsFromRegexp, _handler), details) as RegExpRule;\n }\n}\n\n/**\n * A base rule which calls `match`\n *\n * The value from the `match` function is passed through to the `handler`.\n * @internal\n */\nexport class BaseUrlRule implements UrlRule {\n $id: number;\n priority: number;\n _group: number;\n type: UrlRuleType = 'RAW';\n handler: UrlRuleHandlerFn;\n matchPriority = (match) => 0 - this.$id;\n\n constructor(public match: UrlRuleMatchFn, handler?: UrlRuleHandlerFn) {\n this.handler = handler || identity;\n }\n}\n"
12 ]
13}
\No newline at end of file