Debian Codegraph @

public/js/settings.js

raw symbols SCIP
1 /**
2 * dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports.
3 * Copyright (C) 2021-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 Settings {
22 constructor() {
23 this._page = null;
24 this._table = null;
25 this._scroll = null;
26 this._sort = "ascent";
27 this._element = document.getElementById("main-block");
28 }
29
30 display() {
31 this._make_page_container();
32 this._make_scroll_container();
33 this._make_table();
34 this._scroll.append(this._table.element());
35 this._page.append(this._scroll);
36 this._element.appendChild(this._page);
37 this._table.focus();
38 }
39
40 update() {
41 this._fetch_settings();
42 }
43
44 title() {
45 return "Advanced Settings";
46 }
47
48 _fetch_settings() {
49 this._table.display_status("wait");
50 let that = this;
51
52 let uparams = new URLSearchParams();
53 uparams.set("direction", this._sort);
54
55 return window.fetch("settings.php?" + uparams.toString(), {
56 method: "GET",
57 cache: "no-store",
58 headers: HTTP_HEADERS,
59 credentials: "same-origin"
60 }).then(function(resp) {
61 if (!resp.ok)
62 throw new Error("Failed to fetch the settings");
63 return resp.json();
64 }).then(function(data) {
65 that._table.display_status(null);
66 Common.checkResult(data);
67 let d = { more: data.more };
68 d.rows = data.settings.map(function(it) {
69 return that._make_row_data(it);
70 });
71 that._table.clear();
72 let fr = new ITableFrame(d, 0);
73 that._table.add_frame(fr);
74 that._table.focus();
75 }).catch(function(err) {
76 Common.displayError(err);
77 that._table.display_status("error", err.message);
78 });
79 }
80
81 _make_page_container() {
82 this._page = document.createElement("div");
83 this._page.classList.add("page-container");
84 }
85
86 _make_scroll_container() {
87 this._scroll = document.createElement("div");
88 this._scroll.classList.add("table-wrapper");
89 }
90
91 _make_table() {
92 this._table = new ITable({
93 class: "main-table small-cards",
94 onclick: function(row) {
95 let data = row.userdata();
96 if (data) {
97 this._display_edit_dialog(data);
98 }
99 }.bind(this),
100 onsort: function(col) {
101 let dir = col.sorted() && "toggle" || "ascent";
102 this._table.set_sorted(col.name(), dir);
103 this._sort = col.sorted();
104 this.update();
105 }.bind(this),
106 onfocus: function(el) {
107 scroll_to_element(el, this._scroll);
108 }.bind(this)
109 });
110 [
111 { content: "Name", name: "name", sortable: true },
112 { content: "Value", name: "value" },
113 { content: "Description", name: "descr" }
114 ].forEach(function(col) {
115 let c = this._table.add_column(col);
116 if (c.name() === "name") {
117 c.sort(this._sort);
118 }
119 }, this);
120 }
121
122 _make_row_data(d) {
123 let rd = { cells: [], userdata: d.name };
124 rd.cells.push({ content: d.name, class: "setting-name", label: "Name " });
125 rd.cells.push({ content: d.value, class: "setting-value", label: "Value " });
126 rd.cells.push({
127 content: Settings._descriptions_short[d.name] || Settings._descriptions[d.name] || "No description",
128 label: "Description "
129 });
130 if (d.value !== d.default) {
131 rd.class = "custom-value";
132 }
133 return rd;
134 }
135
136 _display_edit_dialog(name) {
137 let dlg = new SettingEditDialog({
138 name: name,
139 description: Settings._descriptions[name]
140 });
141 this._element.appendChild(dlg.element());
142 let that = this;
143 dlg.show().then(function(d) {
144 if (d) {
145 that.update();
146 }
147 }).finally(function() {
148 dlg.element().remove();
149 that._table.focus();
150 });
151 }
152
153 static _descriptions = {
154 "status.emails-for-last-n-days": "The period in days for which statistics are displayed in the status block.",
155 "status.emails-filter-when-list-filtered": "Applying the filter to the status block when filtering the report list.",
156 "report-view.sort-records-by": "How records are sorted in the report view dialog.",
157 "report-view.filter.initial-value": "The initial value of the record filter in the report view dialog. from-list - use current filter value of the report list. last-value - use the previous filter value. last-value-tab - use the previous filter value of the active tab.",
158 "log-view.sort-list-by": "How report log items are sorted by default in the log view dialog.",
159 "ui.datetime.offset": "Time zone offset of displayed dates in UI. Auto means that the report range is in UTC and all other dates are in local.",
160 "ui.ipv4.url": "The URL that will be used as a link when clicking on the IPv4 address. For example: https://somewhoisservice.net/ip/{$ip}, where {$ip} is IP address from the UI. Use {$eip} if you want to insert url encoded IP address. Use an empty string to disable. Prefix the link template with ! or !! to open such links in one separate tab or each link in a separate tab, respectively. If there is no prefix, the link opens in the current tab.",
161 "ui.ipv6.url": "The URL that will be used as a link when clicking on the IPv6 address. For example: https://somewhoisservice.net/ip/{$ip}, where {$ip} is IP address from the UI. Use {$eip} if you want to insert url encoded IP address. Use an empty string to disable. Prefix the link template with ! or !! to open such links in one separate tab or each link in a separate tab, respectively. If there is no prefix, the link opens in the current tab."
162 };
163
164 static _descriptions_short = {
165 "report-view.filter.initial-value": "The initial value of the record filter in the report view dialog.",
166 "ui.datetime.offset": "Time zone offset of displayed dates in UI.",
167 "ui.ipv4.url": "The URL that will be used as a link when clicking on the IPv4 address.",
168 "ui.ipv6.url": "The URL that will be used as a link when clicking on the IPv6 address."
169 };
170 }
171
172 class SettingEditDialog extends VerticalDialog {
173 constructor(param) {
174 super({ title: "Setting dialog", buttons: [ "ok", "close" ] });
175 this._data = param || {};
176 this._content = null;
177 this._val_el = null;
178 this._val_tp = null;
179 this._desc_el = null;
180 this._save_bt = null;
181 this._fetched = false;
182 }
183
184 _gen_content() {
185 let nm = document.createElement("input");
186 nm.setAttribute("type", "text");
187 nm.setAttribute("disabled", "disabled");
188 nm.setAttribute("value", this._data.name);
189 this._insert_input_row("Name", nm);
190
191 let val = document.createElement("input");
192 val.setAttribute("type", "text");
193 val.disabled = true;
194 this._insert_input_row("Value", val);
195 this._val_el = val;
196 this._val_tp = "string";
197
198 let desc = document.createElement("textarea");
199 desc.setAttribute("disabled", "disabled");
200 if (this._data.description) {
201 desc.appendChild(document.createTextNode(this._data.description));
202 }
203 desc.classList.add("description");
204 this._insert_input_row("Description", desc);
205 this._desc_el = desc;
206
207 this._save_bt = this._buttons[1];
208 this._save_bt.disabled = true;
209
210 this._inputs.addEventListener("input", function(event) {
211 if (this._fetched && event.target == this._val_el) {
212 let e_val = null;
213 switch (this._val_tp) {
214 case "select":
215 e_val = this._val_el.value;
216 break;
217 case "integer":
218 e_val = this._val_el.valueAsNumber;
219 break;
220 }
221 this._save_bt.disabled = (e_val === this._data.value);
222 }
223 }.bind(this));
224
225 this._fetch_data();
226 }
227
228 _add_button(container, text, type) {
229 if (type == "submit") {
230 text = "Save";
231 }
232 super._add_button(container, text, type);
233 }
234
235 _fetch_data() {
236 this._enable_ui(false);
237 this.display_status("wait", "Getting data...");
238 let uparams = new URLSearchParams();
239 uparams.set("name", this._data.name);
240
241 window.fetch("settings.php?" + uparams.toString(), {
242 method: "GET",
243 cache: "no-store",
244 headers: HTTP_HEADERS,
245 credentials: "same-origin"
246 }).then(resp => {
247 if (!resp.ok) throw new Error("Failed to fetch setting data for " + this._data.name);
248 return resp.json();
249 }).then(data => {
250 Common.checkResult(data);
251 this._data.value = data.value;
252 this._update_ui(data);
253 this._enable_ui(true);
254 this._fetched = true;
255 }).catch(err => {
256 Common.displayError(err);
257 this.display_status("error", err.message);
258 }).finally(() => {
259 this.display_status("wait", null);
260 });
261 }
262
263 _enable_ui(en) {
264 this._val_el.disabled = !en;
265 this.focus();
266 }
267
268 _update_ui(data) {
269 if (data.type !== this._val_tp) {
270 let new_el = null;
271 if (data.type == "integer") {
272 new_el = document.createElement("input");
273 new_el.setAttribute("type", "number");
274 if (typeof(data.minimum) == "number") {
275 new_el.setAttribute("min", data.minimum);
276 }
277 if (typeof(data.maximum) == "number") {
278 new_el.setAttribute("max", data.maximum);
279 }
280 } else if (data.type == "select") {
281 new_el = document.createElement("select");
282 data.options.forEach(function(op) {
283 let opt_el = document.createElement("option");
284 opt_el.setAttribute("value", op);
285 opt_el.appendChild(document.createTextNode(op));
286 if (op === data.value) {
287 opt_el.setAttribute("selected", "selected");
288 }
289 new_el.appendChild(opt_el);
290 });
291 }
292 if (new_el) {
293 new_el.setAttribute("required", "required");
294 this._val_el.replaceWith(new_el);
295 this._val_el = new_el;
296 }
297 this._val_tp = data.type;
298 }
299 this._val_el.value = data.value;
300 }
301
302 _submit() {
303 this._save_bt.disabled = true;
304 this._enable_ui(false);
305 this.display_status("wait", "Getting data...");
306
307 const body = {};
308 body.name = this._data.name;
309 if (this._val_tp == "integer") {
310 body.value = this._val_el.valueAsNumber;
311 } else {
312 body.value = this._val_el.value;
313 }
314 body.action = "update";
315
316 window.fetch("settings.php", {
317 method: "POST",
318 cache: "no-store",
319 headers: Object.assign(HTTP_HEADERS, HTTP_HEADERS_POST),
320 credentials: "same-origin",
321 body: JSON.stringify(body)
322 }).then(resp => {
323 if (!resp.ok) throw new Error("Failed to update the setting");
324 return resp.json();
325 }).then(data => {
326 Common.checkResult(data);
327 this._data.value = this._val_el.value;
328 this._result = body;
329 this.hide();
330 Notification.add({ type: "info", text: (data.message || "Updated successfully!") });
331 }).catch(err => {
332 Common.displayError(err);
333 this.display_status("error", err.message);
334 }).finally(() => {
335 this.display_status("wait", null);
336 this._save_bt.disabled = false;
337 this._enable_ui(true);
338 });
339 }
340 }