aboutsummaryrefslogtreecommitdiff
path: root/html/options_main.js
diff options
context:
space:
mode:
Diffstat (limited to 'html/options_main.js')
-rw-r--r--html/options_main.js209
1 files changed, 69 insertions, 140 deletions
diff --git a/html/options_main.js b/html/options_main.js
index e1e6cbe..830c860 100644
--- a/html/options_main.js
+++ b/html/options_main.js
@@ -12,33 +12,31 @@
* IMPORT TYPE_NAME
* IMPORT list_prefixes
* IMPORT nice_name
+ * IMPORT parse_json_with_schema
+ * IMPORT by_id
+ * IMPORT matchers
+ * IMPORT get_import_frame
* IMPORTS_END
*/
var storage;
-function by_id(id)
-{
- return document.getElementById(id);
-}
const item_li_template = by_id("item_li_template");
const bag_component_li_template = by_id("bag_component_li_template");
const chbx_component_li_template = by_id("chbx_component_li_template");
const radio_component_li_template = by_id("radio_component_li_template");
-const import_li_template = by_id("import_li_template");
/* Make sure they are later cloned without id. */
item_li_template.removeAttribute("id");
bag_component_li_template.removeAttribute("id");
chbx_component_li_template.removeAttribute("id");
radio_component_li_template.removeAttribute("id");
-import_li_template.removeAttribute("id");
function item_li_id(prefix, item)
{
return `li_${prefix}_${item}`;
}
-/* Insert into list of bags/pages/scripts */
+/* Insert into list of bags/pages/scripts/repos */
function add_li(prefix, item, at_the_end=false)
{
let ul = ul_by_prefix[prefix];
@@ -58,6 +56,8 @@ function add_li(prefix, item, at_the_end=false)
let export_button = remove_button.nextElementSibling;
export_button.addEventListener("click",
() => export_item(prefix, item));
+ if (prefix === TYPE_PREFIX.REPO)
+ export_button.remove();
if (!at_the_end) {
for (let element of ul.ul.children) {
@@ -89,7 +89,7 @@ function radio_li_id(prefix, item)
function add_chbx_li(prefix, name)
{
- if (prefix === TYPE_PREFIX.PAGE)
+ if (![TYPE_PREFIX.BAG, TYPE_PREFIX.SCRIPT].includes(prefix))
return;
let li = chbx_component_li_template.cloneNode(true);
@@ -109,7 +109,7 @@ var radio_component_none_li = by_id("radio_component_none_li");
function add_radio_li(prefix, name)
{
- if (prefix === TYPE_PREFIX.PAGE)
+ if (![TYPE_PREFIX.BAG, TYPE_PREFIX.SCRIPT].includes(prefix))
return;
let li = radio_component_li_template.cloneNode(true);
@@ -125,6 +125,18 @@ function add_radio_li(prefix, name)
radio_components_ul.insertBefore(li, radio_component_none_li);
}
+/* Used to reset edited repo. */
+function reset_work_repo_li(ul, item, _)
+{
+ ul.work_name_input.value = maybe_string(item);
+}
+
+/* Used to get repo data for saving */
+function work_repo_li_data(ul)
+{
+ return [ul.work_name_input.value, {}];
+}
+
const page_payload_span = by_id("page_payload");
function set_page_components(components)
@@ -461,6 +473,15 @@ const UL_STATE = {
};
const ul_by_prefix = {
+ [TYPE_PREFIX.REPO] : {
+ ul : by_id("repos_ul"),
+ work_li : by_id("work_repo_li"),
+ work_name_input : by_id("repo_url_field"),
+ reset_work_li : reset_work_repo_li,
+ get_work_li_data : work_repo_li_data,
+ state : UL_STATE.IDLE,
+ edited_item : undefined,
+ },
[TYPE_PREFIX.PAGE] : {
ul : by_id("pages_ul"),
work_li : by_id("work_page_li"),
@@ -494,12 +515,6 @@ const ul_by_prefix = {
}
}
-const import_window = by_id("import_window");
-const import_loading_radio = by_id("import_loading_radio");
-const import_failed_radio = by_id("import_failed_radio");
-const import_selection_radio = by_id("import_selection_radio");
-const bad_file_errormsg = by_id("bad_file_errormsg");
-
/*
* Newer browsers could utilise `text' method of File objects.
* Older ones require FileReader.
@@ -520,6 +535,31 @@ function read_file(file)
_read_file(file, resolve, reject));
}
+const url_regex = /^[a-z0-9]+:\/\/[^/]+\.[^/]{2,}(\/[^?#]*)?$/;
+const empty_regex = /^$/;
+
+const settings_schema = [
+ [{}, "matchentry", "minentries", 1,
+ new RegExp(`^${TYPE_PREFIX.SCRIPT}`), {
+ /* script data */
+ "url": ["optional", url_regex, "or", empty_regex],
+ "sha256": ["optional", matchers.sha256, "or", empty_regex],
+ "text": ["optional", "string"]
+ },
+ new RegExp(`^${TYPE_PREFIX.BAG}`), [
+ "optional",
+ [matchers.component, "repeat"],
+ "default", undefined
+ ],
+ new RegExp(`^${TYPE_PREFIX.PAGE}`), {
+ /* page data */
+ "components": ["optional", matchers.component]
+ }], "repeat"
+];
+
+const import_window = by_id("import_window");
+let import_frame;
+
async function import_from_file(event)
{
let files = event.target.files;
@@ -527,98 +567,17 @@ async function import_from_file(event)
return;
import_window.classList.remove("hide");
- import_loading_radio.checked = true;
-
- let result = undefined;
+ import_frame.show_loading();
try {
- result = JSON.parse(await read_file(files[0]));
+ const file = await read_file(files[0]);
+ var result = parse_json_with_schema(settings_schema, file);
} catch(e) {
- bad_file_errormsg.textContent = "" + e;
- import_failed_radio.checked = true;
- return;
- }
-
- let errormsg = validate_settings(result);
- if (errormsg !== false) {
- bad_file_errormsg.textContent = errormsg;
- import_failed_radio.checked = true;
+ import_frame.show_error("Bad file :(", "" + e);
return;
}
- populate_import_list(result);
- import_selection_radio.checked = true;
-}
-
-function validate_settings(settings)
-{
- // TODO
- return false;
-}
-
-function import_li_id(prefix, item)
-{
- return `ili_${prefix}_${item}`;
-}
-
-let import_ul = by_id("import_ul");
-let import_chbxs_colliding = undefined;
-let settings_import_map = undefined;
-
-function populate_import_list(settings)
-{
- let old_children = import_ul.children;
- while (old_children[0] !== undefined)
- import_ul.removeChild(old_children[0]);
-
- import_chbxs_colliding = [];
- settings_import_map = new Map();
-
- for (let setting of settings) {
- let [key, value] = Object.entries(setting)[0];
- let prefix = key[0];
- let name = key.substring(1);
- add_import_li(prefix, name);
- settings_import_map.set(key, value);
- }
-}
-
-function add_import_li(prefix, name)
-{
- let li = import_li_template.cloneNode(true);
- let name_span = li.firstElementChild;
- let chbx = name_span.nextElementSibling;
- let warning_span = chbx.nextElementSibling;
-
- li.setAttribute("data-prefix", prefix);
- li.setAttribute("data-name", name);
- li.id = import_li_id(prefix, name);
- name_span.textContent = nice_name(prefix, name);
-
- if (storage.get(prefix, name) !== undefined) {
- import_chbxs_colliding.push(chbx);
- warning_span.textContent = "(will overwrite existing setting!)";
- }
-
- import_ul.appendChild(li);
-}
-
-function check_all_imports()
-{
- for (let li of import_ul.children)
- li.firstElementChild.nextElementSibling.checked = true;
-}
-
-function uncheck_all_imports()
-{
- for (let li of import_ul.children)
- li.firstElementChild.nextElementSibling.checked = false;
-}
-
-function uncheck_colliding_imports()
-{
- for (let chbx of import_chbxs_colliding)
- chbx.checked = false;
+ import_frame.show_selection(result);
}
const file_opener_form = by_id("file_opener_form");
@@ -626,9 +585,6 @@ const file_opener_form = by_id("file_opener_form");
function hide_import_window()
{
import_window.classList.add("hide");
- /* Let GC free some memory */
- import_chbxs_colliding = undefined;
- settings_import_map = undefined;
/*
* Reset file <input>. Without this, a second attempt to import the same
@@ -637,43 +593,16 @@ function hide_import_window()
file_opener_form.reset();
}
-function commit_import()
-{
- for (let li of import_ul.children) {
- let chbx = li.firstElementChild.nextElementSibling;
-
- if (!chbx.checked)
- continue;
-
- let prefix = li.getAttribute("data-prefix");
- let name = li.getAttribute("data-name");
- let key = prefix + name;
- let value = settings_import_map.get(key);
- storage.set(prefix, name, value);
- }
-
- hide_import_window();
-}
-
-function initialize_import_facility()
+async function initialize_import_facility()
{
let import_but = by_id("import_but");
let file_opener = by_id("file_opener");
- let import_failok_but = by_id("import_failok_but");
- let check_all_import_but = by_id("check_all_import_but");
- let uncheck_all_import_but = by_id("uncheck_all_import_but");
- let uncheck_existing_import_but = by_id("uncheck_existing_import_but");
- let commit_import_but = by_id("commit_import_but");
- let cancel_import_but = by_id("cancel_import_but");
+
import_but.addEventListener("click", () => file_opener.click());
file_opener.addEventListener("change", import_from_file);
- import_failok_but.addEventListener("click", hide_import_window);
- check_all_import_but.addEventListener("click", check_all_imports);
- uncheck_all_import_but.addEventListener("click", uncheck_all_imports);
- uncheck_colliding_import_but
- .addEventListener("click", uncheck_colliding_imports);
- commit_import_but.addEventListener("click", commit_import);
- cancel_import_but.addEventListener("click", hide_import_window);
+
+ import_frame = await get_import_frame();
+ import_frame.onclose = hide_import_window;
}
/*
@@ -727,7 +656,7 @@ async function main()
discard_but.addEventListener("click", () => cancel_work(prefix));
save_but.addEventListener("click", () => save_work(prefix));
- if (prefix === TYPE_PREFIX.SCRIPT)
+ if ([TYPE_PREFIX.REPO, TYPE_PREFIX.SCRIPT].includes(prefix))
continue;
let ul = ul_by_prefix[prefix];
@@ -745,9 +674,9 @@ async function main()
jump_to_item(document.URL);
- initialize_import_facility();
-
storage.add_change_listener(handle_change);
+
+ await initialize_import_facility();
}
function handle_change(change)
@@ -772,7 +701,7 @@ function handle_change(change)
let uls_creators = [[ul.ul, item_li_id]];
- if (change.prefix !== TYPE_PREFIX.PAGE) {
+ if ([TYPE_PREFIX.BAG, TYPE_PREFIX.SCRIPT].includes(change.prefix)) {
uls_creators.push([chbx_components_ul, chbx_li_id]);
uls_creators.push([radio_components_ul, radio_li_id]);
}