Project

General

Profile

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

haketilo / html / import_frame.js @ 826b4fd8

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 get_template
13
 * IMPORT nice_name
14
 * IMPORT make_once
15
 * IMPORTS_END
16
 */
17

    
18
let storage;
19

    
20
const import_li_template = get_template("import_li");
21
import_li_template.removeAttribute("id");
22

    
23
function import_li_id(prefix, item)
24
{
25
    return `ili_${prefix}_${item}`;
26
}
27

    
28
let import_ul = by_id("import_ul");
29
let import_chbxs_colliding = undefined;
30
let settings_import_map = undefined;
31

    
32
function add_import_li(prefix, name)
33
{
34
    let li = import_li_template.cloneNode(true);
35
    let name_span = li.firstElementChild;
36
    let chbx = name_span.nextElementSibling;
37
    let warning_span = chbx.nextElementSibling;
38

    
39
    li.setAttribute("data-prefix", prefix);
40
    li.setAttribute("data-name", name);
41
    li.id = import_li_id(prefix, name);
42
    name_span.textContent = nice_name(prefix, name);
43

    
44
    if (storage.get(prefix, name) !== undefined) {
45
	import_chbxs_colliding.push(chbx);
46
	warning_span.textContent = "(will overwrite existing setting!)";
47
    }
48

    
49
    import_ul.appendChild(li);
50
}
51

    
52
function check_all_imports()
53
{
54
    for (let li of import_ul.children)
55
	li.firstElementChild.nextElementSibling.checked = true;
56
}
57

    
58
function uncheck_all_imports()
59
{
60
    for (let li of import_ul.children)
61
	li.firstElementChild.nextElementSibling.checked = false;
62
}
63

    
64
function uncheck_colliding_imports()
65
{
66
    for (let chbx of import_chbxs_colliding)
67
	chbx.checked = false;
68
}
69

    
70
function commit_import()
71
{
72
    for (let li of import_ul.children) {
73
	let chbx = li.firstElementChild.nextElementSibling;
74

    
75
	if (!chbx.checked)
76
	    continue;
77

    
78
	let prefix = li.getAttribute("data-prefix");
79
	let name = li.getAttribute("data-name");
80
	let key = prefix + name;
81
	let value = settings_import_map.get(key);
82
	storage.set(prefix, name, value);
83
    }
84

    
85
    deactivate();
86
}
87

    
88
const import_loading_radio = by_id("import_loading_radio");
89

    
90
function show_loading()
91
{
92
    import_loading_radio.checked = true;
93
}
94

    
95
const import_failed_radio = by_id("import_failed_radio");
96
const import_errormsg = by_id("import_errormsg");
97
const import_errordetail_chbx = by_id("import_errordetail_chbx");
98
const import_errordetail = by_id("import_errordetail");
99

    
100
function show_error(errormsg, errordetail)
101
{
102
    import_failed_radio.checked = true;
103
    import_errormsg.textContent = errormsg;
104
    import_errordetail_chbx.checked = errordetail;
105
    import_errordetail.textContent = errordetail;
106
}
107

    
108
const import_selection_radio = by_id("import_selection_radio");
109

    
110
function show_selection(settings)
111
{
112
    import_selection_radio.checked = true;
113

    
114
    let old_children = import_ul.children;
115
    while (old_children[0] !== undefined)
116
	import_ul.removeChild(old_children[0]);
117

    
118
    import_chbxs_colliding = [];
119
    settings_import_map = new Map();
120

    
121
    for (let setting of settings) {
122
	let [key, value] = Object.entries(setting)[0];
123
	let prefix = key[0];
124
	let name = key.substring(1);
125
	add_import_li(prefix, name);
126
	settings_import_map.set(key, value);
127
    }
128
}
129

    
130
function deactivate()
131
{
132
    /* Let GC free some memory */
133
    import_chbxs_colliding = undefined;
134
    settings_import_map = undefined;
135

    
136
    if (exports.onclose)
137
	exports.onclose();
138
}
139

    
140
const exports = {show_loading, show_error, show_selection, deactivate};
141

    
142
async function init()
143
{
144
    storage = await get_remote_storage();
145

    
146
    by_id("commit_import_but").addEventListener("click", commit_import);
147
    by_id("check_all_import_but").addEventListener("click", check_all_imports);
148
    by_id("uncheck_all_import_but")
149
	.addEventListener("click", uncheck_all_imports);
150
    by_id("uncheck_colliding_import_but")
151
	.addEventListener("click", uncheck_colliding_imports);
152
    by_id("cancel_import_but").addEventListener("click", deactivate);
153
    by_id("import_failok_but").addEventListener("click", deactivate);
154

    
155
    return exports;
156
}
157

    
158
const get_import_frame = make_once(init);
159

    
160
/*
161
 * EXPORTS_START
162
 * EXPORT get_import_frame
163
 * EXPORTS_END
164
 */
(6-6/8)