mirror of
https://github.com/discourse/discourse.git
synced 2024-11-26 00:23:40 +08:00
1 line
46 KiB
Plaintext
1 line
46 KiB
Plaintext
|
{"version":3,"file":"route-recognizer.js","sources":["route-recognizer/util.ts","route-recognizer/dsl.ts","route-recognizer/normalizer.ts","route-recognizer.ts"],"sourcesContent":["const createObject = Object.create;\nexport function createMap<T>() {\n const map: { [key: string]: T | undefined } = createObject(null);\n map[\"__\"] = undefined;\n delete map[\"__\"];\n return map;\n}\n","import { createMap } from \"./util\";\n\nexport interface Delegate {\n contextEntered?(context: string, route: MatchDSL): void;\n willAddRoute?(context: string | undefined, route: string): string;\n}\n\nexport type Opaque = {} | void | null | undefined;\n\nexport interface Route {\n path: string;\n handler: Opaque;\n queryParams?: string[];\n}\n\nexport interface RouteRecognizer {\n delegate: Delegate | undefined;\n add(routes: Route[]): void;\n}\n\nexport interface MatchCallback {\n (match: MatchDSL): void;\n}\n\nexport interface MatchDSL {\n (path: string): ToDSL;\n (path: string, callback: MatchCallback): void;\n}\n\nexport interface ToDSL {\n to(name: string, callback?: MatchCallback): void;\n}\n\nclass Target implements ToDSL {\n path: string;\n matcher: Matcher;\n delegate: Delegate | undefined;\n\n constructor(path: string, matcher: Matcher, delegate: Delegate | undefined) {\n this.path = path;\n this.matcher = matcher;\n this.delegate = delegate;\n }\n\n to(target: string, callback: MatchCallback) {\n let delegate = this.delegate;\n\n if (delegate && delegate.willAddRoute) {\n target = delegate.willAddRoute(this.matcher.target, target);\n }\n\n this.matcher.add(this.path, target);\n\n if (callback) {\n if (callback.length === 0) { throw new Error(\"You must have an argument in the function passed to `to`\"); }\n this.matcher.addChild(this.path, target, callback, this.delegate);\n }\n }\n}\n\nexport class Matcher {\n routes: {\n [path: string]: string | undefined;\n };\n children: {\n [path: string]: Matcher | undefined;\n };\n target: string | undefined;\n\n constructor(target?: string) {\n this.routes = createMap<string>();\n this.children = createMap<Matcher>();\n this.target = target;\n }\n\n add(path: string, target: string) {\n this.routes[path] = target;\n }\n\n addChild(path: string, target: string, callback: MatchCallback, delegate: Delegate | undefined) {\n let matcher = new Matcher(target);\n this.children[path] = matcher;\n\n let match = generateMatch(path, matcher, delegate);\n\n if (delegate && delegate.contextEntered) {\n delegate.contextEntered(target, match);\n }\n\n callback(match);\n }\n}\n\nfunction generateMatch(startingPath: string, matcher: Matcher, delegate: Delegate | undefined): MatchDSL {\n function match(path: string): ToDSL;\n function match(path: string, callback: MatchCallback): void;\n function match(path: string, callback?: MatchCallback): ToDSL | void {\n let fullPath = startingPath + path;\n if (callback) {\n callback(generateMatch(fullPath, matcher, delegate));\n } else {\n return new Target(fullPath, matcher, delegate);\n }\n };\n return match;\n}\n\nfunction addRoute(routeArray: Route[], path: string, handler: any) {\n let len = 0;\n for (let i = 0; i < routeArray.length; i++) {\n len += routeArray[i].path.length;\n }\n\n path = path.substr(len);\n let route = { path: path, handler: handler };\n routeArray.push(route);\n}\n\nfunction eachRoute<T>(baseRoute: Route[], matcher: Matcher, callback: (this: T, routes: Route[]) => void, binding: T) {\n let routes = matcher.routes;\n let paths = Object.keys(routes);\n for (let i = 0; i < paths.length; i++) {\n let path = paths[i];\n let routeArray = baseRoute.slice();\n addRoute(routeArray, path, routes[path]);\n let nested = matcher.children[path];\n if (nested) {\n eachRoute(routeArray, nested, callback, binding);\n } else {\n callback.call(binding, routeArray);\n }\n }\n}\n\nexport default function <T extends RouteRecognizer>(this: T, callback: MatchCallback, addRout
|