1
|
/**
|
2
|
* This file is part of Haketilo.
|
3
|
*
|
4
|
* Function: Logic for the settings import frame.
|
5
|
*
|
6
|
* Copyright (C) 2021 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 this code in a
|
41
|
* proprietary program, I am not going to enforce this in court.
|
42
|
*/
|
43
|
|
44
|
/*
|
45
|
* IMPORTS_START
|
46
|
* IMPORT get_remote_storage
|
47
|
* IMPORT by_id
|
48
|
* IMPORT clone_template
|
49
|
* IMPORT nice_name
|
50
|
* IMPORT make_once
|
51
|
* IMPORTS_END
|
52
|
*/
|
53
|
|
54
|
let storage;
|
55
|
|
56
|
let import_list = by_id("import_list");
|
57
|
let import_chbxs_colliding = undefined;
|
58
|
let entry_objects = undefined;
|
59
|
let settings_import_map = undefined;
|
60
|
|
61
|
function add_import_entry(prefix, name)
|
62
|
{
|
63
|
const cloned_template = clone_template("import_entry");
|
64
|
Object.assign(cloned_template, {prefix, name});
|
65
|
|
66
|
cloned_template.name_span.textContent = nice_name(prefix, name);
|
67
|
|
68
|
if (storage.get(prefix, name) !== undefined) {
|
69
|
import_chbxs_colliding.push(cloned_template.chbx);
|
70
|
cloned_template.warning.textContent = "!";
|
71
|
}
|
72
|
|
73
|
import_list.appendChild(cloned_template.entry);
|
74
|
|
75
|
return cloned_template;
|
76
|
}
|
77
|
|
78
|
function check_all_imports()
|
79
|
{
|
80
|
for (const entry_object of entry_objects)
|
81
|
entry_object.chbx.checked = true;
|
82
|
}
|
83
|
|
84
|
function uncheck_all_imports()
|
85
|
{
|
86
|
for (const entry_object of entry_objects)
|
87
|
entry_object.chbx.checked = false;
|
88
|
}
|
89
|
|
90
|
function uncheck_colliding_imports()
|
91
|
{
|
92
|
for (let chbx of import_chbxs_colliding)
|
93
|
chbx.checked = false;
|
94
|
}
|
95
|
|
96
|
function commit_import()
|
97
|
{
|
98
|
for (const entry_object of entry_objects) {
|
99
|
if (!entry_object.chbx.checked)
|
100
|
continue;
|
101
|
|
102
|
const key = entry_object.prefix + entry_object.name;
|
103
|
const value = settings_import_map.get(key);
|
104
|
storage.set(entry_object.prefix, entry_object.name, value);
|
105
|
}
|
106
|
|
107
|
deactivate();
|
108
|
}
|
109
|
|
110
|
const import_loading_radio = by_id("import_loading_radio");
|
111
|
|
112
|
function show_loading()
|
113
|
{
|
114
|
import_loading_radio.checked = true;
|
115
|
}
|
116
|
|
117
|
const import_failed_radio = by_id("import_failed_radio");
|
118
|
const import_errormsg = by_id("import_errormsg");
|
119
|
const import_errordetail_chbx = by_id("import_errordetail_chbx");
|
120
|
const import_errordetail = by_id("import_errordetail");
|
121
|
|
122
|
function show_error(errormsg, errordetail)
|
123
|
{
|
124
|
import_failed_radio.checked = true;
|
125
|
import_errormsg.textContent = errormsg;
|
126
|
import_errordetail_chbx.checked = errordetail;
|
127
|
import_errordetail.textContent = errordetail;
|
128
|
}
|
129
|
|
130
|
const import_selection_radio = by_id("import_selection_radio");
|
131
|
const existing_settings_note = by_id("existing_settings_note");
|
132
|
|
133
|
function show_selection(settings)
|
134
|
{
|
135
|
import_selection_radio.checked = true;
|
136
|
|
137
|
let old_children = import_list.children;
|
138
|
while (old_children[0] !== undefined)
|
139
|
import_list.removeChild(old_children[0]);
|
140
|
|
141
|
import_chbxs_colliding = [];
|
142
|
entry_objects = [];
|
143
|
settings_import_map = new Map();
|
144
|
|
145
|
for (let setting of settings) {
|
146
|
let [key, value] = Object.entries(setting)[0];
|
147
|
let prefix = key[0];
|
148
|
let name = key.substring(1);
|
149
|
entry_objects.push(add_import_entry(prefix, name));
|
150
|
settings_import_map.set(key, value);
|
151
|
}
|
152
|
|
153
|
const op = import_chbxs_colliding.length > 0 ? "remove" : "add";
|
154
|
existing_settings_note.classList[op]("hide");
|
155
|
}
|
156
|
|
157
|
function deactivate()
|
158
|
{
|
159
|
/* Let GC free some memory */
|
160
|
import_chbxs_colliding = undefined;
|
161
|
entry_objects = undefined;
|
162
|
settings_import_map = undefined;
|
163
|
|
164
|
if (exports.onclose)
|
165
|
exports.onclose();
|
166
|
}
|
167
|
|
168
|
const wrapper = by_id("import_table_wrapper");
|
169
|
const style_table = (...cls) => cls.forEach(c => wrapper.classList.add(c));
|
170
|
|
171
|
const exports =
|
172
|
{show_loading, show_error, show_selection, deactivate, style_table};
|
173
|
|
174
|
async function init()
|
175
|
{
|
176
|
storage = await get_remote_storage();
|
177
|
|
178
|
by_id("commit_import_but").addEventListener("click", commit_import);
|
179
|
by_id("check_all_import_but").addEventListener("click", check_all_imports);
|
180
|
by_id("uncheck_all_import_but")
|
181
|
.addEventListener("click", uncheck_all_imports);
|
182
|
by_id("uncheck_colliding_import_but")
|
183
|
.addEventListener("click", uncheck_colliding_imports);
|
184
|
by_id("cancel_import_but").addEventListener("click", deactivate);
|
185
|
by_id("import_failok_but").addEventListener("click", deactivate);
|
186
|
|
187
|
return exports;
|
188
|
}
|
189
|
|
190
|
const get_import_frame = make_once(init);
|
191
|
|
192
|
/*
|
193
|
* EXPORTS_START
|
194
|
* EXPORT get_import_frame
|
195
|
* EXPORTS_END
|
196
|
*/
|