mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2025-12-12 09:10:30 +08:00
109 lines
3.0 KiB
JavaScript
109 lines
3.0 KiB
JavaScript
var ui = new function() {
|
|
var __top_z_index = 1;
|
|
|
|
this.init = function() {
|
|
Array.prototype.forEach.call(document.getElementsByClassName("ctl-item"), function(el_item) {
|
|
el_item.onclick = function() { __toggleMenu(el_item); };
|
|
});
|
|
|
|
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);
|
|
});
|
|
|
|
if (typeof document.hidden !== "undefined") {
|
|
__hidden_attr = "hidden";
|
|
__visibility_change_attr = "visibilitychange";
|
|
} else if (typeof document.webkitHidden !== "undefined") {
|
|
__hidden_attr = "webkitHidden";
|
|
__visibility_change_attr = "webkitvisibilitychange";
|
|
} else if (typeof document.mozHidden !== "undefined") {
|
|
__hidden_attr = "mozHidden";
|
|
__visibility_change_attr = "mozvisibilitychange";
|
|
}
|
|
|
|
if (__visibility_change_attr) {
|
|
document.addEventListener(
|
|
__visibility_change_attr,
|
|
function() {
|
|
if (document[__hidden_attr]) {
|
|
hid.releaseAll();
|
|
}
|
|
},
|
|
false,
|
|
);
|
|
}
|
|
|
|
window.onpagehide = hid.releaseAll;
|
|
window.onblur = hid.releaseAll;
|
|
};
|
|
|
|
var __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;
|
|
}
|
|
}
|
|
__toggleMenu(null);
|
|
}
|
|
};
|
|
|
|
var __makeWindowMovable = function(el_header, el_body) {
|
|
var prev_x = 0;
|
|
var prev_y = 0;
|
|
|
|
function startMoving(event) {
|
|
__raiseWindow(el_body);
|
|
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;
|
|
el_body.onclick = function () { __raiseWindow(el_body) };
|
|
};
|
|
|
|
var __raiseWindow = function(el_body) {
|
|
__top_z_index += 1;
|
|
el_body.style.zIndex = __top_z_index;
|
|
};
|
|
};
|