1
|
/**
|
2
|
* Hachette HTML import frame script
|
3
|
*
|
4
|
* Copyright (C) 2021 Wojtek Kosior
|
5
|
* Redistribution terms are gathered in the `copyright' file.
|
6
|
*/
|
7
|
|
8
|
/*
|
9
|
* IMPORTS_START
|
10
|
* IMPORT get_remote_storage
|
11
|
* IMPORT by_id
|
12
|
* IMPORT nice_name
|
13
|
* IMPORT make_once
|
14
|
* IMPORTS_END
|
15
|
*/
|
16
|
|
17
|
let storage;
|
18
|
|
19
|
const import_li_template = by_id("import_li_template");
|
20
|
import_li_template.removeAttribute("id");
|
21
|
|
22
|
function import_li_id(prefix, item)
|
23
|
{
|
24
|
return `ili_${prefix}_${item}`;
|
25
|
}
|
26
|
|
27
|
let import_ul = by_id("import_ul");
|
28
|
let import_chbxs_colliding = undefined;
|
29
|
let settings_import_map = undefined;
|
30
|
|
31
|
function add_import_li(prefix, name)
|
32
|
{
|
33
|
let li = import_li_template.cloneNode(true);
|
34
|
let name_span = li.firstElementChild;
|
35
|
let chbx = name_span.nextElementSibling;
|
36
|
let warning_span = chbx.nextElementSibling;
|
37
|
|
38
|
li.setAttribute("data-prefix", prefix);
|
39
|
li.setAttribute("data-name", name);
|
40
|
li.id = import_li_id(prefix, name);
|
41
|
name_span.textContent = nice_name(prefix, name);
|
42
|
|
43
|
if (storage.get(prefix, name) !== undefined) {
|
44
|
import_chbxs_colliding.push(chbx);
|
45
|
warning_span.textContent = "(will overwrite existing setting!)";
|
46
|
}
|
47
|
|
48
|
import_ul.appendChild(li);
|
49
|
}
|
50
|
|
51
|
function check_all_imports()
|
52
|
{
|
53
|
for (let li of import_ul.children)
|
54
|
li.firstElementChild.nextElementSibling.checked = true;
|
55
|
}
|
56
|
|
57
|
function uncheck_all_imports()
|
58
|
{
|
59
|
for (let li of import_ul.children)
|
60
|
li.firstElementChild.nextElementSibling.checked = false;
|
61
|
}
|
62
|
|
63
|
function uncheck_colliding_imports()
|
64
|
{
|
65
|
for (let chbx of import_chbxs_colliding)
|
66
|
chbx.checked = false;
|
67
|
}
|
68
|
|
69
|
function commit_import()
|
70
|
{
|
71
|
for (let li of import_ul.children) {
|
72
|
let chbx = li.firstElementChild.nextElementSibling;
|
73
|
|
74
|
if (!chbx.checked)
|
75
|
continue;
|
76
|
|
77
|
let prefix = li.getAttribute("data-prefix");
|
78
|
let name = li.getAttribute("data-name");
|
79
|
let key = prefix + name;
|
80
|
let value = settings_import_map.get(key);
|
81
|
storage.set(prefix, name, value);
|
82
|
}
|
83
|
|
84
|
deactivate();
|
85
|
}
|
86
|
|
87
|
const import_loading_radio = by_id("import_loading_radio");
|
88
|
|
89
|
function show_loading()
|
90
|
{
|
91
|
import_loading_radio.checked = true;
|
92
|
}
|
93
|
|
94
|
const import_failed_radio = by_id("import_failed_radio");
|
95
|
const import_errormsg = by_id("import_errormsg");
|
96
|
const import_errordetail_chbx = by_id("import_errordetail_chbx");
|
97
|
const import_errordetail = by_id("import_errordetail");
|
98
|
|
99
|
function show_error(errormsg, errordetail)
|
100
|
{
|
101
|
import_failed_radio.checked = true;
|
102
|
import_errormsg.textContent = errormsg;
|
103
|
import_errordetail_chbx.checked = errordetail;
|
104
|
import_errordetail.textContent = errordetail;
|
105
|
}
|
106
|
|
107
|
const import_selection_radio = by_id("import_selection_radio");
|
108
|
|
109
|
function show_selection(settings)
|
110
|
{
|
111
|
import_selection_radio.checked = true;
|
112
|
|
113
|
let old_children = import_ul.children;
|
114
|
while (old_children[0] !== undefined)
|
115
|
import_ul.removeChild(old_children[0]);
|
116
|
|
117
|
import_chbxs_colliding = [];
|
118
|
settings_import_map = new Map();
|
119
|
|
120
|
for (let setting of settings) {
|
121
|
let [key, value] = Object.entries(setting)[0];
|
122
|
let prefix = key[0];
|
123
|
let name = key.substring(1);
|
124
|
add_import_li(prefix, name);
|
125
|
settings_import_map.set(key, value);
|
126
|
}
|
127
|
}
|
128
|
|
129
|
function deactivate()
|
130
|
{
|
131
|
/* Let GC free some memory */
|
132
|
import_chbxs_colliding = undefined;
|
133
|
settings_import_map = undefined;
|
134
|
|
135
|
if (exports.onclose)
|
136
|
exports.onclose();
|
137
|
}
|
138
|
|
139
|
const exports = {show_loading, show_error, show_selection, deactivate};
|
140
|
|
141
|
async function init()
|
142
|
{
|
143
|
storage = await get_remote_storage();
|
144
|
|
145
|
by_id("commit_import_but").addEventListener("click", commit_import);
|
146
|
by_id("check_all_import_but").addEventListener("click", check_all_imports);
|
147
|
by_id("uncheck_all_import_but")
|
148
|
.addEventListener("click", uncheck_all_imports);
|
149
|
by_id("uncheck_colliding_import_but")
|
150
|
.addEventListener("click", uncheck_colliding_imports);
|
151
|
by_id("cancel_import_but").addEventListener("click", deactivate);
|
152
|
by_id("import_failok_but").addEventListener("click", deactivate);
|
153
|
|
154
|
return exports;
|
155
|
}
|
156
|
|
157
|
const get_import_frame = make_once(init);
|
158
|
|
159
|
/*
|
160
|
* EXPORTS_START
|
161
|
* EXPORT get_import_frame
|
162
|
* EXPORTS_END
|
163
|
*/
|