aboutsummaryrefslogtreecommitdiff
path: root/src/app/js/popups.js
blob: 62176a6f8c19dfa642088dcd5370601f03036186 (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
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 = ! open_list.includes(popup_el);
	}

	if (state) {
		popups.open_list.add(popup_el);
		overlay.classList.add("shown");
		popup_el.classList.add("shown");
	} else if (! state) {
		popups.open_list.remove(popup_el);
		popup_el.classList.remove("shown");
		if (! open_list.length) {
			overlay.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);
	}
}

// attempts to hide just the last shown popup
popups.hide_last = () => {
	if (open_list.length) {
		popups.hide(open_list[open_list.length - 1], false);
	}
}

let open_list = [];
popups.open_list = () => {
	return open_list;
}

popups.open_list.remove = (el) => {
	// no need to do anything if `el` isn't even in `open_list`
	if (! open_list.includes(el)) {
		return;
	}

	// filtered list
	let list = [];

	// run through open popups
	for (let i = 0; i < open_list.length; i++) {
		// add popup to `list` if it isn't `el`
		if (open_list[i] != el && el.classList.contains("shown")) {
			list.push(open_list[i]);
		}
	}

	// set `open_list` to the now filtered `list`
	open_list = list;
}

popups.open_list.add = (el) => {
	// make sure the `el` isn't already in the list
	popups.open_list.remove(el);

	// add `el` to the end of the list
	open_list.push(el);
}

module.exports = popups;