aboutsummaryrefslogtreecommitdiff
path: root/src/app/js/settings.js
blob: 9eb6c760fbdb3d191a83d4a1f7161a87635499f6 (plain)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
var settings_fuse;

var Settings = {
	default: {settings},
	toggle: (state) => {
		Settings.load();
		options.scrollTo(0, 0);

		popups.set("#options", state);
	},
	apply: () => {
		settings = {...settings, ...Settings.get()};
		ipcRenderer.send("save-settings", Settings.get());
	},
	get: () => {
		let opts = {};
		let options = document.querySelectorAll(".option");

		for (let i = 0; i < options.length; i++) {
			let optName = options[i].getAttribute("name");
			if (options[i].querySelector(".actions input")) {
				let input = options[i].querySelector(".actions input").value;
				if (options[i].getAttribute("type")) {
					opts[optName] = input.split(" ");
				} else {
					opts[optName] = input;
				}
			} else if (options[i].querySelector(".actions select")) {
				opts[optName] = options[i].querySelector(".actions select").value;
			} else if (options[i].querySelector(".actions .switch")) {
				if (options[i].querySelector(".actions .switch.on")) {
					opts[optName] = true;
				} else {
					opts[optName] = false;
				}
			}
		}

		return opts;
	},
	load: () => {
		// re-opens any closed categories
		let categories = document.querySelectorAll("#options details");
		for (let i = 0; i < categories.length; i++) {
			categories[i].setAttribute("open", true);

			// hide categories that aren't for the current platform
			let for_platform = categories[i].getAttribute("platform");
			if (for_platform && process.platform != for_platform) {
				categories[i].style.display = "none";
				categories[i].setAttribute("perma-hidden", true);
			}
		}

		let options = document.querySelectorAll(".option");

		for (let i = 0; i < options.length; i++) {
			// hide options that aren't for the current platform
			let for_platform = options[i].getAttribute("platform");
			if (for_platform && process.platform != for_platform) {
				options[i].style.display = "none";
				options[i].setAttribute("perma-hidden", true);
			}

			let optName = options[i].getAttribute("name");
			if (optName == "forcedlang") {
				let div = options[i].querySelector("select");

				div.innerHTML = "";
				let lang_dir = __dirname + "/../lang";
				let langs = fs.readdirSync(lang_dir);
				for (let i in langs) {
					let lang_file = require(lang_dir + "/" + langs[i]);
					let lang_no_extension = langs[i].replace(/\..*$/, "");

					if (! lang_file.lang || ! lang_file.lang.title) {
						continue;
					}

					let title = lang_file.lang.title;

					if (title) {
						div.innerHTML += `<option value="${lang_no_extension}">${title}</option>`
					}
					
				}

				div.value = settings.forcedlang;
				continue;
			}

			if (settings[optName] != undefined) {
				// check if setting has a `<select>`
				let select_el = options[i].querySelector(".actions select");
				if (select_el) {
					// get `<option>` for settings value, if it exists
					let option = select_el.querySelector(
						`option[value="${settings[optName]}"]`
					)

					// check if it exists
					if (option) {
						// set the `<select>` to the settings value
						select_el.value = settings[optName];
					} else { // use the default value
						select_el.value = Settings.default[optName];
					}

					continue;
				}

				switch(typeof settings[optName]) {
					case "string":
						options[i].querySelector(".actions input").value = settings[optName];
						break
					case "object":
						options[i].querySelector(".actions input").value = settings[optName].join(" ");
						break
					case "boolean":
						let switchDiv = options[i].querySelector(".actions .switch");
						if (settings[optName]) {
							switchDiv.classList.add("on");
							switchDiv.classList.remove("off");
						} else {
							switchDiv.classList.add("off");
							switchDiv.classList.remove("on");
						}
						break
				}
			}
		}

		// create Fuse based on options from `get_search_arr()`
		settings_fuse = new Fuse(get_search_arr(), {
			keys: ["text"],
			threshold: 0.4,
			ignoreLocation: true
		})

		// reset search
		Settings.search();
		search_el.value = "";

		ipcRenderer.send("can-autoupdate");
		ipcRenderer.on("cant-autoupdate", () => {
			document.querySelector(".option[name=autoupdate]")
				.style.display = "none";

			document.querySelector(".option[name=autoupdate]")
				.setAttribute("perma-hidden", true);
		})
	},
	switch: (el, state) => {
		if (state) {
			return el.classList.add("on");
		} else if (state === false) {
			return el.classList.remove("on");
		}

		if (el.classList.contains("switch") && el.tagName == "BUTTON") {
			el.classList.toggle("on");
		}
	},

	// searches for `query` in the list of options, hides the options
	// that dont match, and shows the one that do, if `query` is falsy,
	// it'll simply reset everything back to being visible
	search: (query = "") => {
		// get list of elements that can be hidden
		let search_els = [
			...document.querySelectorAll(".options .title"),
			...document.querySelectorAll(".options .option"),
			...document.querySelectorAll(".options .buttons"),
			...document.querySelectorAll(".options .details"),
		]

		// this sets the visibility of all elements found in
		// `search_els` unless the element has the `perma-hidden`
		// attribute set
		let set_all = (state) => {
			// set `state` to the CSS equivalent
			if (state) {
				state = null;
			} else {
				state = "none";
			}

			// run through elements
			for (let i = 0; i < search_els.length; i++) {
				// check that the element shouldn't be perma hidden, and
				// if so, hide it correctly.
				if (search_els[i].hasAttribute("perma-hidden")) {
					search_els[i].style.display = "none";
					continue;
				}

				// set the visibility
				search_els[i].style.display = state;
			}
		}

		// check if `query` is empty, and reset search if so
		if (! query || ! query.trim()) {
			set_all(true);
		} else {
			// hide everything
			set_all(false);
		}

		// unhides `el` and relevant elements related to it
		let unhide = (el) => {
			// list of elements that could be relevant through `el`'s
			// `.closest()` function
			let closest = [
				".option",
				"details",
				".buttons",
			]

			// run through `closest`
			for (let i = 0; i < closest.length; i++) {
				// shorthand
				let closest_el = el.closest(closest[i]);

				// was a relevant element found?
				if (closest_el) {
					// is it supposed to always be hidden? do nothing
					if (el.hasAttribute("perma-hidden")
						|| closest_el.hasAttribute("perma-hidden")) {

						break;
					}

					// reset visibility
					closest_el.style.display = null;

					// are we at a `<details>`?
					if (closest[i] == "details") {
						// attempt to get a `.title` inside that
						// `<details>` element
						let title = closest_el.querySelector(".title");

						// did we find a title?
						if (title) {
							// reset visibility of title and also reset
							// open state of `<details>`
							title.style.display = null;
							closest_el.setAttribute("open", false);
						}
					}
				}
			}
		}

		// do a Fuse.js search with `query`
		let res = settings_fuse.search(query);

		// if nothing was found, reset all
		if (res.length == 0) {
			return set_all(true);
		}

		// run through results and unhide all of them
		for (let i = 0; i < res.length; i++) {
			unhide(res[i].item.el);
		}
	}
}

// search on key events in search input
let search_el = document.body.querySelector("#options .search");
search_el.addEventListener("keyup", () => {
	Settings.search(search_el.value);
})

// returns a Fuse.js compatible Array for searching through the settings
function get_search_arr() {
	// list of elements that should be taken into consideration when
	// trying to do a search
	let searchables_queries = [
		".option .text",
		".buttons .text",
		".option .actions input",
		".option .actions select",
		".buttons .actions button:not(.switch)",
	]

	let searchables_els = [];

	// run through queries
	for (let i = 0; i < searchables_queries.length; i++) {
		// attempt to get element(s) with that query
		let query = document.querySelectorAll(
			".options " + searchables_queries[i]
		)

		// if something was found add it
		searchables_els = [
			...query,
			...searchables_els
		]
	}

	let searchables = [];

	// run through the found elements
	for (let i = 0; i < searchables_els.length; i++) {
		let el = searchables_els[i];
		switch(el.tagName.toLowerCase()) {
			// is this an `<input>`?
			case "input":
				// if no value is in the input, do nothing
				if (! el.value) {
					continue;
				}

				// add to list of searchable elements, using the value
				// of the input as the text
				searchables.push({
					el: el,
					text: el.value
				})
				break;

			// is this a `<select>`?
			case "select":
				// get options in `<select>`
				let options = el.children;

				// run through options
				for (let ii = 0; ii < options.length; ii++) {
					// add to list of searchable elements, using the
					// insides of the option as the text, and the
					// element being the `<select>` and not the
					// `<option>`!
					searchables.push({
						el: el,
						text: options[ii].innerText
					})
				}
				break;

			default:
				// add to list of searchable elements using the insides
				// of the element as the text
				searchables.push({
					el: el,
					text: el.innerText
				})
		}
	}

	// return the actual searchable elements and text
	return searchables;
}

events.on("popup-changed", () => {
	let settings_is_shown =
		document.getElementById("options")
		.classList.contains("shown");

	let settings_btn = document.getElementById("settings");

	if (settings_is_shown) {
		settings_btn.classList.add("shown");
	} else {
		settings_btn.classList.remove("shown");
	}
})

document.body.addEventListener("click", (e) => {
	let el = document.elementFromPoint(e.clientX, e.clientY);
	Settings.switch(el);
})

Settings.load();