Debian Codegraph @

public/js/main.js

raw symbols SCIP
1 /**
2 * dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports.
3 * Copyright (C) 2020-2025 Aleksey Andreev (liuch)
4 *
5 * Available at:
6 * https://github.com/liuch/dmarc-srg
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation, either version 3 of the License.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 class Router {
22 }
23
24 Router.start = function() {
25 Router._initial_header = document.querySelector("h1").textContent;
26
27 document.getElementsByTagName("body")[0].addEventListener("keydown", function(event) {
28 if (event.defaultPrevented) return;
29 if (event.code == "Escape" && !event.shiftKey && !event.ctrlKey && !event.altKey) {
30 let cbs = document.querySelectorAll(".close-btn.active");
31 for (let i = cbs.length - 1; i >= 0; --i) {
32 if (window.getComputedStyle(cbs[i]).display !== "none") {
33 cbs[i].click();
34 event.preventDefault();
35 break;
36 }
37 }
38 document.querySelectorAll("div.popup-menu:not(.hidden)").forEach(function(m) {
39 m.classList.add("hidden");
40 });
41 }
42 });
43
44 window.addEventListener("click", function(event) {
45 if (!event.target.closest("div.popup-menu")) {
46 document.querySelectorAll("div.popup-menu:not(.hidden)").forEach(function(m) {
47 m.classList.add("hidden");
48 });
49 }
50 });
51
52 document.getElementById("main-menu-block").addEventListener("click", function(event) {
53 let el = event.target;
54 if (el.tagName === "A") {
55 let href = el.getAttribute("href");
56 if (href !== "" && href !== "#") {
57 event.preventDefault();
58 window.history.pushState(null, "", href);
59 Router.go();
60 MenuBar.instance().updateCurrent();
61 }
62 }
63 });
64
65 window.addEventListener("popstate", function(event) {
66 let m = Router._url2module();
67 if (m) {
68 let p = m.pointer;
69 if (p && p.onpopstate) {
70 if (p.title) {
71 Router.update_title(p.title());
72 }
73 p.onpopstate(event.state);
74 } else {
75 Router.go();
76 MenuBar.instance().updateCurrent();
77 }
78 }
79 });
80
81 document.querySelector(".menu-box .about a").addEventListener("click", function(event) {
82 event.preventDefault();
83 setTimeout(function() {
84 let dlg = new AboutDialog({
85 authors: [
86 { name: "Aleksey Andreev", url: "https://github.com/liuch", years: "2020-2025" }
87 ],
88 documentation: [
89 { ancor: "README on GitHub", url: "https://github.com/liuch/dmarc-srg/blob/master/README.md" }
90 ],
91 source_code: [
92 { ancor: "DmarcSrg on GitHub", url: "https://github.com/liuch/dmarc-srg" }
93 ]
94 });
95 document.getElementById("main-block").appendChild(dlg.element());
96 dlg.show().finally(function() {
97 dlg.element().remove();
98 });
99 }, 0);
100 });
101
102 const menu = MenuBar.instance().init();
103 Router.go();
104 menu.updateCurrent();
105 };
106
107 Router.go = function(url) {
108 Status.instance().update({
109 page: this._page_name(url),
110 settings: [ "ui.datetime.offset", "ui.ipv4.url", "ui.ipv6.url", "report-view.filter.initial-value" ]
111 }).then(function(d) {
112 if (d) {
113 Router._update_menu(d.authenticated);
114 Router._update_user();
115 if (d.settings) {
116 if (d.settings["ui.datetime.offset"]) {
117 Common.tuneDateTimeOutput(d.settings["ui.datetime.offset"]);
118 }
119 Common.ipv4_url = d.settings["ui.ipv4.url"] || '';
120 Common.ipv6_url = d.settings["ui.ipv6.url"] || '';
121 Common.rv_filter = d.settings["report-view.filter.initial-value"] || null;
122 }
123 if (d.error_code !== -2) {
124 try {
125 Common.checkResult(d);
126 } catch (err) {
127 Common.displayError(err);
128 }
129 let module = Router._url2module(url);
130 if (module) {
131 if (!module.pointer)
132 module.start(module);
133 let p = module.pointer;
134 if (p.oncleardata)
135 p.oncleardata();
136 else
137 Router._clear_data();
138 if (p.title)
139 Router.update_title(p.title());
140 if (p.display)
141 p.display();
142 if (p.update)
143 p.update();
144 }
145 }
146 if (d.state && d.state !== "Ok" && !d.error_code && d.message) {
147 Notification.add({ type: "warn", text: d.message, delay: 20000, name: "st-err" });
148 }
149 if (d.version !== Router._app_ver) {
150 Router._app_ver = d.version;
151 Router.update_title();
152 }
153 if (d.php_version) {
154 Router.php_version = d.php_version;
155 }
156 }
157 });
158 };
159
160 Router.app_name = function(version) {
161 let name = "DmarcSrg";
162 if (version && Router._app_ver) {
163 name += " " + Router._app_ver;
164 }
165 return name;
166 }
167
168 Router.update_title = function(str) {
169 let title1 = Router.app_name(false);
170 let title2 = str || Router._title || null;
171 if (str) {
172 Router._title = str;
173 }
174 document.title = title1 + (title2 && (": " + title2) || "");
175 let h1 = document.querySelector("h1");
176 if (str === "") {
177 h1.textContent = Router._initial_header || "";
178 } else if (str) {
179 h1.textContent = title2 || "";
180 }
181 };
182
183 Router._update_menu = function(authenticated) {
184 let aa = document.getElementById("auth-action");
185 if (aa) aa.remove();
186
187 if (User && User.auth_type !== "base") {
188 MenuBar.instance().element(".users").classList.add("hidden");
189 }
190
191 if (authenticated === "yes") {
192 aa = MenuBar.instance().insertItem("Log out", "#", -1);
193 aa.id = "auth-action";
194 aa.addEventListener("click", event => {
195 event.preventDefault();
196 if (!aa.classList.contains("disabled")) {
197 aa.classList.add("disabled");
198 window.fetch("logout.php", {
199 method: "POST",
200 cache: "no-store",
201 headers: Object.assign(HTTP_HEADERS, HTTP_HEADERS_POST),
202 credentials: "same-origin",
203 body: JSON.stringify({})
204 }).then(resp => {
205 if (!resp.ok) throw new Error("Failed to log out");
206 return resp.json();
207 }).then(data => {
208 Common.checkResult(data);
209 User.name = null;
210 User.level = null;
211 Status.instance().reset();
212 Router._clear_data();
213 Router._update_menu("no");
214 Router._update_user();
215 Router.update_title("");
216 }).catch(err => {
217 Common.displayError(err);
218 aa.classList.remove("disabled");
219 Notification.add({ type: "error", text: err.message, name: "auth" });
220 });
221 }
222 });
223 } else if (authenticated === "no") {
224 aa = MenuBar.instance().insertItem("Log in", "#", -1);
225 aa.id = "auth-action";
226 aa.addEventListener("click", event => {
227 event.preventDefault();
228 LoginDialog.start();
229 });
230 }
231 };
232
233 Router._update_user = function() {
234 const levels = new Map([ [ "admin", false ], [ "manager", false ] ]);
235 switch (User.level) {
236 case null:
237 case "admin":
238 levels.set("admin", true);
239 //no break;
240 case "manager":
241 levels.set("manager", true);
242 break;
243 }
244 levels.forEach(function(value, key) {
245 document.body.classList[value ? "add" : "remove"]("level-" + key);
246 });
247 }
248
249 Router._clear_data = function() {
250 document.getElementById("main-block").replaceChildren();
251 document.getElementById("detail-block").replaceChildren();
252 };
253
254 Router._modules = {
255 list: {
256 start: function(m) {
257 m.pointer = new ReportList();
258 }
259 },
260 report: {
261 start: function(m) {
262 m.pointer = ReportWidget.instance();
263 }
264 },
265 admin: {
266 start: function(m) {
267 m.pointer = new Admin();
268 }
269 },
270 files: {
271 start: function(m) {
272 m.pointer = new Files();
273 }
274 },
275 users: {
276 start: function(m) {
277 m.pointer = new UserList();
278 }
279 },
280 domains: {
281 start: function(m) {
282 m.pointer = new DomainList();
283 }
284 },
285 logs: {
286 start: function(m) {
287 m.pointer = new Logs();
288 }
289 },
290 summary: {
291 start: function(m) {
292 m.pointer = new Summary();
293 }
294 },
295 settings: {
296 start: function(m) {
297 m.pointer = new Settings();
298 }
299 }
300 };
301
302 Router._page_name = function(url) {
303 const r = /([^\/]*)$/.exec(url || document.location.pathname);
304 return r && this._routes[r[1]] || null;
305 }
306
307 Router._url2module = function(url) {
308 const pname = this._page_name(url);
309 return pname && this._modules[pname] || null;
310 };
311
312 Router._routes = {
313 "": "list",
314 "list.php": "list",
315 "logs.php": "logs",
316 "admin.php": "admin",
317 "users.php": "users",
318 "files.php": "files",
319 "report.php": "report",
320 "domains.php": "domains",
321 "summary.php": "summary",
322 "settings.php": "settings"
323 };
324
325 window.onload = Router.start;