aboutsummaryrefslogtreecommitdiff
path: root/src/app/js/popups.js
blob: 10c69958b9898bd9f78494b21ebfa23489628393 (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
let popups = {};

popups.set = (popup, state, auto_close_all = true) => {
	let popup_el = popup;

	if (typeof popup == "string") {
		popup_el = document.querySelector(popup);
	}

	if (! popup_el) {return false}

	if (auto_close_all && overlay.classList.contains("shown")) {
		popups.set_all(false, popup_el);
	}

	if (! state && state !== false) {
		state = ! popup_el.classList.contains("shown");
	}

	if (state) {
		overlay.classList.add("shown");
		popup_el.classList.add("shown");
	} else if (! state) {
		overlay.classList.remove("shown");
		popup_el.classList.remove("shown");
	}

	events.emit("popup-changed", {
		popup: popup_el,
		new_state: state
	})
}

popups.show = (popup, auto_close_all = true) => {
	return popups.set(popup, true, auto_close_all);
}

popups.hide = (popup, auto_close_all = true) => {
	return popups.set(popup, false, auto_close_all);
}

popups.list = () => {
	return document.querySelectorAll(".popup");
}

popups.set_all = (state = false, exclude_popup) => {
	let popups_list = document.querySelectorAll(".popup.shown");

	for (let i = 0; i < popups_list.length; i++) {
		if (popups_list[i] == exclude_popup) {
			continue;
		}

		popups.set(popups_list[i], state, false);
	}
}

module.exports = popups;