forked from bitfoundation/bitplatform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitAppShell.ts
63 lines (51 loc) · 2.22 KB
/
BitAppShell.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
namespace BitBlazorUI {
export class AppShell {
private static STORE_KEY = 'bit-appshell-scrolls';
public static PreScroll: number = 0;
private static _currentUrl: string;
private static _container: HTMLElement | undefined;
private static _scrolls: { [key: string]: number | undefined } = {};
public static initScroll(container: HTMLElement, url: string) {
AppShell._container = container;
AppShell._currentUrl = url;
AppShell._scrolls = JSON.parse(sessionStorage.getItem(this.STORE_KEY) || '{}');
AppShell.storeScroll(url, AppShell.PreScroll > 0 ? AppShell.PreScroll : AppShell._scrolls[url]);
if (AppShell._scrolls[url]! > 0) {
AppShell._container.scrollTo({ top: AppShell._scrolls[url], behavior: 'instant' });
}
AppShell.addScroll();
}
public static locationChangedScroll(url: string) {
AppShell.removeScroll();
}
public static afterRenderScroll(url: string) {
AppShell._currentUrl = url;
AppShell.storeScroll(url, AppShell._scrolls[url]);
AppShell._container?.scrollTo({ top: AppShell._scrolls[url], behavior: 'instant' });
AppShell.addScroll();
}
public static disposeScroll() {
AppShell.removeScroll();
}
private static addScroll() {
AppShell._container?.addEventListener('scroll', AppShell.onScroll);
}
private static removeScroll() {
AppShell._container?.removeEventListener('scroll', AppShell.onScroll);
}
private static onScroll() {
AppShell.storeScroll(AppShell._currentUrl, AppShell._container?.scrollTop);
}
private static storeScroll(url: string, value: number | undefined) {
AppShell._scrolls[url] = value || 0;
window.sessionStorage.setItem(this.STORE_KEY, JSON.stringify(AppShell._scrolls));
}
}
}
(function () {
const container = document.getElementById('BitAppShell-container');
if (!container) return;
container.addEventListener('scroll', e => {
BitBlazorUI.AppShell.PreScroll = container.scrollTop;
});
}());