Project

General

Profile

Download (5.2 KB) Statistics
| Branch: | Tag: | Revision:

haketilo / html / settings.js @ f8dedf60

1
/**
2
 * This file is part of Haketilo.
3
 *
4
 * Function: Driving Haketilo's settings page.
5
 *
6
 * Copyright (C) 2022 Wojtek Kosior
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * As additional permission under GNU GPL version 3 section 7, you
19
 * may distribute forms of that code without the copy of the GNU
20
 * GPL normally required by section 4, provided you include this
21
 * license notice and, in case of non-source distribution, a URL
22
 * through which recipients can access the Corresponding Source.
23
 * If you modify file(s) with this exception, you may extend this
24
 * exception to your version of the file(s), but you are not
25
 * obligated to do so. If you do not wish to do so, delete this
26
 * exception statement from your version.
27
 *
28
 * As a special exception to the GPL, any HTML file which merely
29
 * makes function calls to this code, and for that purpose
30
 * includes it by reference shall be deemed a separate work for
31
 * copyright law purposes. If you modify this code, you may extend
32
 * this exception to your version of the code, but you are not
33
 * obligated to do so. If you do not wish to do so, delete this
34
 * exception statement from your version.
35
 *
36
 * You should have received a copy of the GNU General Public License
37
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
38
 *
39
 * I, Wojtek Kosior, thereby promise not to sue for violation of this file's
40
 * license. Although I request that you do not make use of this code in a
41
 * proprietary program, I am not going to enforce this in court.
42
 */
43

    
44
#IMPORT common/indexeddb.js AS haketilodb
45
#IMPORT html/dialog.js
46

    
47
#IF MOZILLA
48
#FROM common/browser.js IMPORT browser
49
#ENDIF
50

    
51
#FROM html/DOM_helpers.js     IMPORT by_id
52
#FROM html/text_entry_list.js IMPORT blocking_allowing_lists, repo_list
53
#FROM html/item_list.js       IMPORT mapping_list, resource_list
54
#FROM html/payload_create.js  IMPORT payload_create_form
55

    
56
let tab_names = ["blocking", "mappings", "resources", "new_payload", "repos"];
57
let current_tab_name = "new_payload";
58

    
59
const [tabs, heads] = [{}, {}];
60

    
61
for (const tab_name of tab_names) {
62
    tabs[tab_name] = by_id(`${tab_name}_tab`);
63
    heads[tab_name] = by_id(`${tab_name}_head`);
64
}
65

    
66
function switch_to_tab(target_tab_name) {
67
    if (current_tab_name == target_tab_name)
68
	return;
69

    
70
    tabs[current_tab_name].classList.remove("active_tab");
71
    heads[current_tab_name].classList.remove("active_head");
72

    
73
    current_tab_name = target_tab_name;
74
    tabs[current_tab_name].classList.add("active_tab");
75
    heads[current_tab_name].classList.add("active_head");
76
}
77

    
78
for (const [tab_name, head] of Object.entries(heads))
79
    head.addEventListener("click", () => switch_to_tab(tab_name));
80

    
81
async function set_up_blocking_tab() {
82
    const containers = ["editable", "dialog"]
83
	  .map(n => by_id(`blocking_${n}_container`));
84

    
85
    function show_container(idx) {
86
	containers[idx].classList.remove("hide");
87
	containers[1 - idx].classList.add("hide");
88
    }
89

    
90
    const dialog_ctx = dialog.make(...[1, 0].map(n => () => show_container(n)));
91
    containers[1].append(dialog_ctx.main_div);
92

    
93
    const [blocking_list, allowing_list] =
94
	  await blocking_allowing_lists(dialog_ctx);
95

    
96
    by_id("blocking_list_container").append(blocking_list.main_div);
97
    by_id("allowing_list_container").append(allowing_list.main_div);
98
}
99

    
100
async function set_up_mappings_tab() {
101
    tabs["mappings"].append((await mapping_list()).main_div);
102
}
103

    
104
async function set_up_resources_tab() {
105
    tabs["resources"].append((await resource_list()).main_div);
106
}
107

    
108
function set_up_new_payload_tab() {
109
    tabs["new_payload"].append(payload_create_form().main_div);
110
}
111

    
112
async function set_up_repos_tab() {
113
    const containers = ["list", "dialog"]
114
	  .map(n => by_id(`repos_${n}_container`));
115

    
116
    function show_container(idx) {
117
	containers[idx].classList.remove("hide");
118
	containers[1 - idx].classList.add("hide");
119
    }
120

    
121
    const dialog_ctx = dialog.make(...[1, 0].map(n => () => show_container(n)));
122
    containers[1].append(dialog_ctx.main_div);
123
    containers[0].append((await repo_list(dialog_ctx)).main_div);
124
}
125

    
126
function set_up_settings_view() {
127
    by_id("loader").remove();
128
    by_id("main_view").classList.remove("hide");
129

    
130
    set_up_blocking_tab();
131
    set_up_mappings_tab();
132
    set_up_resources_tab();
133
    set_up_new_payload_tab();
134
    set_up_repos_tab();
135
}
136

    
137
async function init_settings_page() {
138
#IF MOZILLA
139
    const this_tab = await browser.tabs.getCurrent();
140

    
141
    if (this_tab.incognito) {
142
	by_id("private_mode_error").classList.remove("hide");
143
	by_id("loader").remove();
144
	return;
145
    }
146
#ENDIF
147

    
148
    try {
149
	await haketilodb.get();
150
    } catch(e) {
151
	console.error("Haketilo:", e);
152
	by_id("indexeddb_error").classList.remove("hide");
153
	by_id("loader").remove();
154
	return;
155
    }
156

    
157
    set_up_settings_view();
158
}
159

    
160
#IF UNIT_TEST
161
window.init_settings_page = init_settings_page;
162
#ELSE
163
init_settings_page();
164
#ENDIF
(26-26/28)