Project

General

Profile

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

haketilo / html / import_frame.js @ 2bd35bc4

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
 * Redistribution terms are gathered in the `copyright' file.
8
 */
9

    
10
/*
11
 * IMPORTS_START
12
 * IMPORT get_remote_storage
13
 * IMPORT by_id
14
 * IMPORT clone_template
15
 * IMPORT nice_name
16
 * IMPORT make_once
17
 * IMPORTS_END
18
 */
19

    
20
let storage;
21

    
22
let import_list = by_id("import_list");
23
let import_chbxs_colliding = undefined;
24
let entry_objects = undefined;
25
let settings_import_map = undefined;
26

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

    
32
    cloned_template.name_span.textContent = nice_name(prefix, name);
33

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

    
39
    import_list.appendChild(cloned_template.entry);
40

    
41
    return cloned_template;
42
}
43

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

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

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

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

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

    
73
    deactivate();
74
}
75

    
76
const import_loading_radio = by_id("import_loading_radio");
77

    
78
function show_loading()
79
{
80
    import_loading_radio.checked = true;
81
}
82

    
83
const import_failed_radio = by_id("import_failed_radio");
84
const import_errormsg = by_id("import_errormsg");
85
const import_errordetail_chbx = by_id("import_errordetail_chbx");
86
const import_errordetail = by_id("import_errordetail");
87

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

    
96
const import_selection_radio = by_id("import_selection_radio");
97
const existing_settings_note = by_id("existing_settings_note");
98

    
99
function show_selection(settings)
100
{
101
    import_selection_radio.checked = true;
102

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

    
107
    import_chbxs_colliding = [];
108
    entry_objects = [];
109
    settings_import_map = new Map();
110

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

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

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

    
130
    if (exports.onclose)
131
	exports.onclose();
132
}
133

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

    
137
const exports =
138
      {show_loading, show_error, show_selection, deactivate, style_table};
139

    
140
async function init()
141
{
142
    storage = await get_remote_storage();
143

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

    
153
    return exports;
154
}
155

    
156
const get_import_frame = make_once(init);
157

    
158
/*
159
 * EXPORTS_START
160
 * EXPORT get_import_frame
161
 * EXPORTS_END
162
 */
(10-10/14)