summaryrefslogtreecommitdiff
path: root/kvmd/web/js/ui.js
blob: e6b3de330a00ade3271ca7f6c2ce480e163f68f2 (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
var ui = new function() {
	this.init = function() {
		window.onclick = __windowClickHandler;

		Array.prototype.forEach.call(document.getElementsByClassName("window"), function(el_window) {
			var el_header = el_window.querySelector(".window-header");
			__makeWindowMovable(el_header, el_window);
		});
	};

	this.toggleMenu = function(el_a) {
		Array.prototype.forEach.call(document.getElementsByClassName("ctl-item"), function(el_item) {
			var el_menu = el_item.parentElement.querySelector(".ctl-dropdown-content");
			if (el_item === el_a && el_menu.style.display === "none") {
				el_menu.style.display = "block";
				el_item.setAttribute("style", "background-color: var(--bg-color-selected)");
			} else {
				el_menu.style.display = "none";
				el_item.setAttribute("style", "background-color: default");
			}
		});
	};

	var __windowClickHandler = function(event) {
		if (!event.target.matches(".ctl-item")) {
			for (el_item = event.target; el_item && el_item !== document; el_item = el_item.parentNode) {
				if (el_item.hasAttribute("data-force-hide-menu")) {
					break;
				}
				else if (el_item.hasAttribute("data-dont-hide-menu")) {
					return;
				}
			}
			ui.toggleMenu(null);
		}
	};

	var __makeWindowMovable = function(el_header, el_body) {
		var prev_x = 0;
		var prev_y = 0;

		function startMoving(event) {
			event = (event || window.event);
			event.preventDefault();
			prev_x = event.clientX;
			prev_y = event.clientY;
			document.onmousemove = doMoving;
			document.onmouseup = stopMoving;
		}

		function doMoving(event) {
			event = (event || window.event);
			event.preventDefault();
			x = prev_x - event.clientX;
			y = prev_y - event.clientY;
			prev_x = event.clientX;
			prev_y = event.clientY;
			el_body.style.top = (el_body.offsetTop - y) + "px";
			el_body.style.left = (el_body.offsetLeft - x) + "px";
		}

		function stopMoving() {
			document.onmousemove = null;
			document.onmouseup = null;
		}

		el_header.onmousedown = startMoving;
	};
};