• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import { InteropNativeModule } from "@koalaui/interop/InteropNativeModule"
2import { Router } from "arkui/handwritten"
3import { PeerNode } from "arkui/PeerNode"
4import { UserViewBuilder } from "arkui/UserView"
5import { ComputableState } from "@koalaui/runtime"
6
7namespace router {
8    export interface RouterOptions {
9        url: string
10        params?: Object
11    }
12
13    export enum RouterMode {
14        Standard,
15        Single
16    }
17
18    export interface RouterState {
19        index: number;
20        name: string;
21        path: string;
22        params: Object;
23    }
24
25    export function error(prefix: string, e: Object|null|undefined): string {
26        if (e instanceof Error) {
27            return `${prefix}: ${e} ${e.stack}`
28        } else {
29            return `${prefix}: ${e}`
30        }
31    }
32
33    let pageEntries: Map<string, string>
34    let globalRouterImp: Router | undefined
35
36    export function setRouter(routerImp: Router): void {
37        globalRouterImp = routerImp
38    }
39
40    export function getParams(): Object {
41        return globalRouterImp!.getParams()
42    }
43
44    export function clear(): void {
45        globalRouterImp!.clear()
46    }
47
48    export function getLength(): string {
49        return globalRouterImp!.getLength();
50    }
51
52    export function getState(): RouterState {
53        return globalRouterImp!.getState();
54    }
55
56    export function getStateByIndex(index: number): RouterState | undefined {
57        return globalRouterImp!.getStateByIndex(index);
58    }
59
60    export function getStateByUrl(url: string): Array<RouterState> {
61        return globalRouterImp!.getStateByUrl(url);
62    }
63
64    export function pushUrl(options: RouterOptions): void {
65        InteropNativeModule._NativeLog("AceRouter:enter ohos pushUrl " + options.url)
66        globalRouterImp!.push(options)
67    }
68
69    export function replaceUrl(options: RouterOptions): void {
70        InteropNativeModule._NativeLog("AceRouter:enter ohos replaceUrl " + options.url)
71        globalRouterImp!.replace(options)
72    }
73
74    export function push(options: RouterOptions): void {
75        InteropNativeModule._NativeLog("AceRouter:enter ohos push " + options.url)
76        globalRouterImp!.push(options)
77    }
78
79    export function back(options?: RouterOptions): void {
80        InteropNativeModule._NativeLog("AceRouter:enter ohos back")
81        globalRouterImp!.back(options)
82    }
83
84    export function UpdateVisiblePagePeerNode(node: PeerNode, index: number = -1): void {
85        InteropNativeModule._NativeLog("AceRouter:enter ohos UpdateVisiblePagePeerNode")
86        globalRouterImp!.UpdateVisiblePagePeerNode(node, index);
87    }
88
89    export function getStateRoot(): Array<ComputableState<PeerNode>> {
90        return globalRouterImp!.getEntryRootValue();
91    }
92
93    export function runPage(options: RouterOptions, builder: UserViewBuilder): void {
94        globalRouterImp!.runPage(options, builder)
95    }
96}
97
98export default router