Project

General

Profile

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

haketilo / html / import_frame.js @ 4b59dced

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

    
18
let storage;
19

    
20
let import_list = by_id("import_list");
21
let import_chbxs_colliding = undefined;
22
let entry_objects = undefined;
23
let settings_import_map = undefined;
24

    
25
function add_import_entry(prefix, name)
26
{
27
    const cloned_template = clone_template("import_entry");
28
    Object.assign(cloned_template, {prefix, name});
29

    
30
    cloned_template.name_span.textContent = nice_name(prefix, name);
31

    
32
    if (storage.get(prefix, name) !== undefined) {
33
	import_chbxs_colliding.push(cloned_template.chbx);
34
	cloned_template.warning.textContent = "!";
35
    }
36

    
37
    import_list.appendChild(cloned_template.entry);
38

    
39
    return cloned_template;
40
}
41

    
42
function check_all_imports()
43
{
44
    for (const entry_object of entry_objects)
45
	entry_object.chbx.checked = true;
46
}
47

    
48
function uncheck_all_imports()
49
{
50
    for (const entry_object of entry_objects)
51
	entry_object.chbx.checked = false;
52
}
53

    
54
function uncheck_colliding_imports()
55
{
56
    for (let chbx of import_chbxs_colliding)
57
	chbx.checked = false;
58
}
59

    
60
function commit_import()
61
{
62
    for (const entry_object of entry_objects) {
63
	if (!entry_object.chbx.checked)
64
	    continue;
65

    
66
	const key = entry_object.prefix + entry_object.name;
67
	const value = settings_import_map.get(key);
68
	storage.set(entry_object.prefix, entry_object.name, value);
69
    }
70

    
71
    deactivate();
72
}
73

    
74
const import_loading_radio = by_id("import_loading_radio");
75

    
76
function show_loading()
77
{
78
    import_loading_radio.checked = true;
79
}
80

    
81
const import_failed_radio = by_id("import_failed_radio");
82
const import_errormsg = by_id("import_errormsg");
83
const import_errordetail_chbx = by_id("import_errordetail_chbx");
84
const import_errordetail = by_id("import_errordetail");
85

    
86
function show_error(errormsg, errordetail)
87
{
88
    import_failed_radio.checked = true;
89
    import_errormsg.textContent = errormsg;
90
    import_errordetail_chbx.checked = errordetail;
91
    import_errordetail.textContent = errordetail;
92
}
93

    
94
const import_selection_radio = by_id("import_selection_radio");
95
const existing_settings_note = by_id("existing_settings_note");
96

    
97
function show_selection(settings)
98
{
99
    import_selection_radio.checked = true;
100

    
101
    let old_children = import_list.children;
102
    while (old_children[0] !== undefined)
103
	import_list.removeChild(old_children[0]);
104

    
105
    import_chbxs_colliding = [];
106
    entry_objects = [];
107
    settings_import_map = new Map();
108

    
109
    for (let setting of settings) {
110
	let [key, value] = Object.entries(setting)[0];
111
	let prefix = key[0];
112
	let name = key.substring(1);
113
	entry_objects.push(add_import_entry(prefix, name));
114
	settings_import_map.set(key, value);
115
    }
116

    
117
    const op = import_chbxs_colliding.length > 0 ? "remove" : "add";
118
    existing_settings_note.classList[op]("hide");
119
}
120

    
121
function deactivate()
122
{
123
    /* Let GC free some memory */
124
    import_chbxs_colliding = undefined;
125
    entry_objects = undefined;
126
    settings_import_map = undefined;
127

    
128
    if (exports.onclose)
129
	exports.onclose();
130
}
131

    
132
const wrapper = by_id("import_table_wrapper");
133
const style_table = (...cls) => cls.forEach(c => wrapper.classList.add(c));
134

    
135
const exports =
136
      {show_loading, show_error, show_selection, deactivate, style_table};
137

    
138
async function init()
139
{
140
    storage = await get_remote_storage();
141

    
142
    by_id("commit_import_but").addEventListener("click", commit_import);
143
    by_id("check_all_import_but").addEventListener("click", check_all_imports);
144
    by_id("uncheck_all_import_but")
145
	.addEventListener("click", uncheck_all_imports);
146
    by_id("uncheck_colliding_import_but")
147
	.addEventListener("click", uncheck_colliding_imports);
148
    by_id("cancel_import_but").addEventListener("click", deactivate);
149
    by_id("import_failok_but").addEventListener("click", deactivate);
150

    
151
    return exports;
152
}
153

    
154
const get_import_frame = make_once(init);
155

    
156
/*
157
 * EXPORTS_START
158
 * EXPORT get_import_frame
159
 * EXPORTS_END
160
 */
(7-7/11)