-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathFragmentNavigation.ts
64 lines (46 loc) · 1.59 KB
/
FragmentNavigation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
namespace Uno.UI {
export class FragmentNavigationHandler {
private static currentFragment: string;
public static getCurrentFragment(): string {
return window.location.hash;
}
public static setCurrentFragment(fragment: string): string {
window.location.hash = fragment;
this.currentFragment = window.location.hash;
return "ok";
}
private static subscribed: boolean = false;
public static subscribeToFragmentChanged(): string {
if (this.subscribed) {
return "already subscribed";
}
this.subscribed = true;
this.currentFragment = this.getCurrentFragment();
window.addEventListener(
"hashchange",
_ => this.notifyFragmentChanged(),
false);
return "ok";
}
private static notifyFragmentChangedMethod: any;
private static notifyFragmentChanged() {
const newFragment: string = this.getCurrentFragment();
if (newFragment === this.currentFragment) {
return; // nothing to do
}
this.currentFragment = newFragment;
this.initializeMethods();
const newFragmentStr = MonoRuntime.mono_string(newFragment);
MonoRuntime.call_method(this.notifyFragmentChangedMethod, null, [newFragmentStr]);
}
private static initializeMethods(): void {
if (this.notifyFragmentChangedMethod) {
return; // already initialized.
}
const asm = MonoRuntime.assembly_load("Uno.Playground.WASM");
const handlerClass = MonoRuntime.find_class(asm, "Uno.UI.Wasm", "FragmentHavigationHandler");
this.notifyFragmentChangedMethod = MonoRuntime.find_method(handlerClass, "NotifyFragmentChanged", -1);
}
}
declare var MonoRuntime: any;
}