forked from vectordotdev/vector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
110 lines (94 loc) · 2.63 KB
/
app.js
File metadata and controls
110 lines (94 loc) · 2.63 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
{{ $latest := index site.Data.docs.versions 0 }}
{{ $defaultPlatformTab := index site.Home.Params.platform.tabs 0 }}
{{ $siteGeneration := site.Params.site_generation }}
import '@ryangjchandler/spruce';
import 'alpinejs';
const sayHello = () => {
console.log('Welcome to the Vector website and documentation!');
}
const clearLocalStorageOnNewGeneration = () => {
const currentGeneration = {{ $siteGeneration }};
const storedGeneration = localStorage.getItem('generation');
if ((storedGeneration != null) && (storedGeneration < currentGeneration)) {
['__spruce:global'].forEach((item) => localStorage.removeItem(item));
}
localStorage.setItem('generation', currentGeneration);
}
/* Global state management */
const manageState = () => {
// Persist global state in localStorage
const useLocalStorage = true;
// Detect the user's dark mode preference and set that to the default
const darkModeDefault = window.matchMedia('(prefers-color-scheme: dark)').matches;
window.Spruce.store('global', {
// Dark mode state
dark: darkModeDefault,
// Whether the top banner is showing (user can dismiss)
banner: true,
// The Vector version selected (for the download and releases pages)
version: '{{ $latest }}',
// A "backup" version for use in release toggling
versionBackup: '{{ $latest }}',
// Release version
release: 'stable',
// Home page platform tab
platformTab: '{{ $defaultPlatformTab }}',
// Config format
format: 'yaml',
// Helper functions
setFormat(f) {
this.format = f;
},
isFormat(f) {
return this.format === f;
},
isDark() {
return this.dark;
},
isLight() {
return !this.dark;
},
// Set release directly
setRelease(release) {
this.release = release;
},
// Set a new version
setVersion(v) {
this.version = v;
if (v === 'nightly') {
this.setRelease('nightly');
}
if (v != 'nightly') {
this.setRelease('stable');
this.versionBackup = v;
}
},
isVersion(v) {
return this.version === v;
},
notLatest() {
return this.version != '{{ $latest }}';
},
setToLatest() {
this.setVersion('{{ $latest }}');
},
// Switch dark mode on and off
toggleDarkMode() {
this.dark = !this.dark;
},
// Switch the banner on and off
toggleBanner() {
this.banner = !this.banner;
},
// Boolean helpers
isNightly() {
return this.release === 'nightly';
},
}, useLocalStorage);
}
const main = () => {
sayHello();
clearLocalStorageOnNewGeneration();
manageState();
}
main();