-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.js
More file actions
168 lines (146 loc) · 3.6 KB
/
Copy pathconfig.js
File metadata and controls
168 lines (146 loc) · 3.6 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// API
SERVER_URL = "https://minesweeper-leaderboard.tuggi.dev";
// SERVER_URL = 'http://localhost:3001';
LEADERBOARD_ENABLED = true;
// Local Storage
LOCAL_STORAGE_USERNAME_KEY = "ms_lb_username";
LOCAL_STORAGE_COLLAPSE_KEY = "ms_lb_collapsed";
LOCAL_STORAGE_SHORTCUTS_REVEAL_KEY = "ms_shortcuts_reveal_enabled";
LOCAL_STORAGE_SHORTCUTS_FLAG_KEY = "ms_shortcuts_flag_enabled";
LOCAL_STORAGE_COLOR_KEY = "ms_color_enabled";
LOCAL_STORAGE_MARKS_KEY = "ms_marks_enabled";
LOCAL_STORAGE_GUESS_FREE_KEY = "ms_guess_free";
// Window Management
WINDOW_Z_INDEX_STACK_START = 10;
// name
// onChange ==> (state) => ...
// local storage key
// value
// State Management
const setPreferenceValue = (key, value) => {
const pref = preferences.find((p) => p.name === key);
if (!pref) {
return;
}
pref.value = value;
this.localStorage.setItem(pref.lsKey, value);
pref.onChange(value);
};
const getPreferenceValue = (key) => {
const pref = preferences.find((p) => p.name === key);
return pref?.value ?? undefined;
};
const initPrefValuesFromLs = () => {
preferences.forEach((p) => {
const lsValue = this.localStorage.getItem(p.lsKey);
if (lsValue) {
p.value = p.onLsLoad(lsValue);
} else {
this.localStorage.setItem(p.lsKey, p.value);
}
p.onChange(p.value);
});
};
const handleRadioUpdate = (id, state) => {
const radioEl = this.document.getElementById(id);
if (state) {
radioEl.classList.add("selected");
} else {
radioEl.classList.remove("selected");
}
};
const preferences = [
{
name: "collapsed",
onChange: toggleCollapsed,
onLsLoad: (value) => {
return value === "true";
},
lsKey: LOCAL_STORAGE_COLLAPSE_KEY,
value: false,
},
{
name: "shortcuts_reveal",
onChange: (state) => {
handleRadioUpdate("shortcuts_reveal", state);
},
onLsLoad: (value) => {
return value === "true";
},
lsKey: LOCAL_STORAGE_SHORTCUTS_REVEAL_KEY,
value: true,
},
{
name: "shortcuts_flag",
onChange: (state) => {
handleRadioUpdate("shortcuts_flag", state);
},
onLsLoad: (value) => {
return value === "true";
},
lsKey: LOCAL_STORAGE_SHORTCUTS_FLAG_KEY,
value: false,
},
{
name: "marks",
onChange: (state) => {
handleRadioUpdate("marks", state);
},
onLsLoad: (value) => {
return value === "true";
},
lsKey: LOCAL_STORAGE_MARKS_KEY,
value: false,
},
{
name: "color",
onChange: (state) => {
handleRadioUpdate("saturation", state);
document.body.style.filter = `saturate(${state ? 1 : 0})`;
document.body.style.backdropFilter = `saturate(${state ? 1 : 0})`;
},
onLsLoad: (value) => {
return value === "true";
},
lsKey: LOCAL_STORAGE_COLOR_KEY,
value: true,
},
{
name: "guessfree",
onChange: (state) => {
handleRadioUpdate("guessfree", state);
newGame();
},
onLsLoad: (value) => {
return value === "true";
},
lsKey: LOCAL_STORAGE_GUESS_FREE_KEY,
value: false,
},
];
document.addEventListener("DOMContentLoaded", () => {
initPrefValuesFromLs();
});
// Window Defaults
LEADERBOARD_ID = "leaderboard";
MINESWEEPER_ID = "minesweeper";
NOTEPAD_ID = "notepad";
const WINDOW_DEFAULTS = {
[MINESWEEPER_ID]: {
open: true,
zIndex: WINDOW_Z_INDEX_STACK_START,
},
};
// Desktop Actions
const DESKTOP_CONFIG = {
["desktop-open-minesweeper"]: {
clickHandler: (event) => {
setWindowVisibility(MINESWEEPER_ID, true);
},
},
["desktop-open-notepad"]: {
clickHandler: (event) => {
setWindowVisibility(NOTEPAD_ID, true);
},
},
};