summaryrefslogtreecommitdiff
path: root/web/share/js/index/main.js
blob: 497a4d73baa028b4c814edb961c81a9d535f4ff5 (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
/*****************************************************************************
#                                                                            #
#    KVMD - The main Pi-KVM daemon.                                          #
#                                                                            #
#    Copyright (C) 2018  Maxim Devaev <[email protected]>                    #
#                                                                            #
#    This program is free software: you can redistribute it and/or modify    #
#    it under the terms of the GNU General Public License as published by    #
#    the Free Software Foundation, either version 3 of the License, or       #
#    (at your option) any later version.                                     #
#                                                                            #
#    This program is distributed in the hope that it will be useful,         #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of          #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           #
#    GNU General Public License for more details.                            #
#                                                                            #
#    You should have received a copy of the GNU General Public License       #
#    along with this program.  If not, see <https://www.gnu.org/licenses/>.  #
#                                                                            #
*****************************************************************************/


"use strict";


import {tools, $} from "../tools.js";
import {checkBrowser} from "../bb.js";
import {wm, initWindowManager} from "../wm.js";


export function main() {
	initWindowManager();

	if (checkBrowser()) {
		__setAppText();
		__loadKvmdInfo();
	}
}

function __setAppText() {
	$("app-text").innerHTML = `
		<span class="code-comment"># On Linux using Chromium/Chrome via any terminal:<br>
		$</span> \`which chromium 2>/dev/null || which chrome 2>/dev/null\` --app="${window.location.href}"<br>
		<br>
		<span class="code-comment"># On MacOS using Terminal application:<br>
		$</span> /Applications/Google&bsol; Chrome.app/Contents/MacOS/Google&bsol; Chrome --app="${window.location.href}"<br>
		<br>
		<span class="code-comment"># On Windows via cmd.exe:<br>
		C:&bsol;&gt;</span> start chrome --app="${window.location.href}"
	`;
}

function __loadKvmdInfo() {
	let http = tools.makeRequest("GET", "/api/info", function() {
		if (http.readyState === 4) {
			if (http.status === 200) {
				let info = JSON.parse(http.responseText).result;

				let apps = [];
				if (info.extras === null) {
					wm.error("Not all applications in the menu can be displayed<br>due an error. See KVMD logs for details.");
				} else {
					apps = Object.values(info.extras).sort(function(a, b) {
						if (a.place < b.place) {
							return -1;
						} else if (a.place > b.place) {
							return 1;
						} else {
							return 0;
						}
					});
				}

				$("apps-box").innerHTML = "<ul id=\"apps\"></ul>";

				$("apps").innerHTML += __makeApp(null, "kvm", "share/svg/kvm.svg", "KVM");

				for (let app of apps) {
					if (app.enabled) {
						$("apps").innerHTML += __makeApp(null, app.path, app.icon, app.name);
					}
				}

				$("apps").innerHTML += __makeApp("logout-button", "#", "share/svg/logout.svg", "Logout");
				tools.setOnClick($("logout-button"), __logout);

				if (info.meta !== null && info.meta.server && info.meta.server.host) {
					$("kvmd-meta-server-host").innerHTML = info.meta.server.host;
					document.title = `Pi-KVM Index: ${info.meta.server.host}`;
				} else {
					$("kvmd-meta-server-host").innerHTML = "";
					document.title = "Pi-KVM Index";
				}
			} else if (http.status === 401 || http.status === 403) {
				document.location.href = "/login";
			} else {
				setTimeout(__loadKvmdInfo, 1000);
			}
		}
	});
}

function __makeApp(id, path, icon, name) {
	return `<li>
		<div ${id ? "id=\"" + id + "\"" : ""} class="app">
			<a href="${path}">
				<div>
					<img class="svg-gray" src="${icon}">
					${name}
				</div>
			</a>
		</div>
	</li>`;
}

function __logout() {
	let http = tools.makeRequest("POST", "/api/auth/logout", function() {
		if (http.readyState === 4) {
			if (http.status === 200 || http.status === 401 || http.status === 403) {
				document.location.href = "/login";
			} else {
				wm.error("Logout error:<br>", http.responseText);
			}
		}
	});
}