From b75a5717a084c9e5a727c2e960f2b910abcb5ace Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Tue, 25 Jan 2022 09:37:34 +0100 Subject: add a repo querying HTML interface --- html/DOM_helpers.js | 47 +++++++++++ html/base.css | 4 +- html/dialog.js | 41 ++-------- html/install.html | 3 +- html/install.js | 73 +++++++---------- html/repo_query.html | 148 ++++++++++++++++++++++++++++++++++ html/repo_query.js | 210 ++++++++++++++++++++++++++++++++++++++++++++++++ html/settings.html | 4 +- html/text_entry_list.js | 7 +- 9 files changed, 448 insertions(+), 89 deletions(-) create mode 100644 html/repo_query.html create mode 100644 html/repo_query.js (limited to 'html') diff --git a/html/DOM_helpers.js b/html/DOM_helpers.js index 88092e5..9e64956 100644 --- a/html/DOM_helpers.js +++ b/html/DOM_helpers.js @@ -85,3 +85,50 @@ function clone_template(template_id) return result_object; } #EXPORT clone_template + +function Showable(on_show_cb, on_hide_cb) { + this.shown = false; + + /* + * Wrap the requested callback into one that only executes it if showable is + * not shown. + */ + this.when_hidden = cb => { + const wrapped_cb = (...args) => { + if (!this.shown) + return cb(...args); + } + return wrapped_cb; + } + + this.show = () => { + if (this.shown) + return false; + + this.shown = true; + + try { + on_show_cb(); + } catch(e) { + console.error(e); + } + + return true; + } + + this.hide = () => { + if (!this.shown) + return false; + + this.shown = false; + + try { + on_hide_cb(); + } catch(e) { + console.error(e); + } + + return true; + } +} +#EXPORT Showable diff --git a/html/base.css b/html/base.css index df6213a..dd08387 100644 --- a/html/base.css +++ b/html/base.css @@ -88,11 +88,11 @@ body { --line-height: 0.4em; } -div.bottom_line { +div.top_line { height: var(--line-height); background: linear-gradient(#555, transparent); } -div.top_line { +div.bottom_line { height: var(--line-height); background: linear-gradient(transparent, #555); } diff --git a/html/dialog.js b/html/dialog.js index a4e275f..fbea657 100644 --- a/html/dialog.js +++ b/html/dialog.js @@ -41,17 +41,14 @@ * proprietary program, I am not going to enforce this in court. */ -#FROM html/DOM_helpers.js IMPORT clone_template +#FROM html/DOM_helpers.js IMPORT clone_template, Showable function make(on_dialog_show, on_dialog_hide) { const dialog_context = clone_template("dialog"); - Object.assign(dialog_context, { - on_dialog_show, - on_dialog_hide, - shown: false, - queue: [], - }); + dialog_context.queue = []; + + Showable.call(dialog_context, on_dialog_show, on_dialog_hide); for (const [id, val] of [["yes", true], ["no", false], ["ok", undefined]]) { const but = dialog_context[`${id}_but`]; @@ -74,12 +71,7 @@ function close_dialog(dialog_context, event) if (dialog_context.queue.length > 0) { process_queue_item(dialog_context); } else { - dialog_context.shown = false; - try { - dialog_context.on_dialog_hide(); - } catch(e) { - console.error(e); - } + dialog_context.hide(); } resolve(event ? event.target.haketilo_dialog_result : undefined); @@ -104,15 +96,8 @@ async function show_dialog(dialog_context, shown_buts_id, msg) const result_prom = new Promise(cb => resolve = cb); dialog_context.queue.push([shown_buts_id, msg, resolve]); - if (!dialog_context.shown) { + if (dialog_context.show()) process_queue_item(dialog_context); - dialog_context.shown = true; - try { - dialog_context.on_dialog_show(); - } catch(e) { - console.error(e); - } - } return await result_prom; } @@ -129,17 +114,3 @@ const ask = (ctx, ...msg) => show_dialog(ctx, "ask_buts", msg); const loader = (ctx, ...msg) => show_dialog(ctx, null, msg); #EXPORT loader - -/* - * Wrapper the requested callback into one that only executes it if dialog is - * not shown. - */ -function when_hidden(ctx, cb) -{ - function wrapped_cb(...args) { - if (!ctx.shown) - return cb(...args); - } - return wrapped_cb; -} -#EXPORT when_hidden diff --git a/html/install.html b/html/install.html index b8d0927..0146a8d 100644 --- a/html/install.html +++ b/html/install.html @@ -67,7 +67,7 @@ } .install_bottom_buttons { - margin: 1em auto; + margin: 1em; text-align: center; } @@ -95,6 +95,7 @@ +
  • diff --git a/html/install.js b/html/install.js index dbc490d..e972924 100644 --- a/html/install.js +++ b/html/install.js @@ -46,8 +46,9 @@ #IMPORT html/item_preview.js AS ip #FROM common/browser.js IMPORT browser -#FROM html/DOM_helpers.js IMPORT clone_template -#FROM common/entities.js IMPORT item_id_string, version_string, get_files +#FROM html/DOM_helpers.js IMPORT clone_template, Showable +#FROM common/entities.js IMPORT item_id_string, version_string, get_files, \ + is_valid_version #FROM common/misc.js IMPORT sha256_async AS sha256 const coll = new Intl.Collator(); @@ -78,7 +79,7 @@ function ItemEntry(install_view, item) { } let preview_cb = () => install_view.preview_item(item.def); - preview_cb = dialog.when_hidden(install_view.dialog_ctx, preview_cb); + preview_cb = install_view.dialog_ctx.when_hidden(preview_cb); this.details_but.addEventListener("click", preview_cb); } @@ -114,8 +115,9 @@ async function init_work() { } function InstallView(tab_id, on_view_show, on_view_hide) { + Showable.call(this, on_view_show, on_view_hide); + Object.assign(this, clone_template("install_view")); - this.shown = false; const show_container = name => { for (const cid of container_ids) { @@ -154,8 +156,8 @@ function InstallView(tab_id, on_view_show, on_view_hide) { this[container_name].prepend(preview_ctx.main_div); } - const back_cb = dialog.when_hidden(this.dialog_ctx, - () => show_container("install_preview")); + let back_cb = () => show_container("install_preview"); + back_cb = this.dialog_ctx.when_hidden(back_cb); for (const type of ["resource", "mapping"]) this[`${type}_back_but`].addEventListener("click", back_cb); @@ -189,17 +191,18 @@ function InstallView(tab_id, on_view_show, on_view_hide) { "Repository's response is not valid JSON :("); } - if (response.json.api_schema_version > [1]) { - let api_ver = ""; - try { - api_ver = - ` (${version_string(response.json.api_schema_version)})`; - } catch(e) { - console.warn(e); - } + if (!is_valid_version(response.json.api_schema_version)) { + var bad_api_ver = ""; + } else if (response.json.api_schema_version > [1]) { + var bad_api_ver = + ` (${version_string(response.json.api_schema_version)})`; + } else { + var bad_api_ver = false; + } + if (bad_api_ver !== false) { const captype = item_type[0].toUpperCase() + item_type.substring(1); - const msg = `${captype} ${item_id_string(id, ver)} was served using unsupported Hydrilla API version${api_ver}. You might need to update Haketilo.`; + const msg = `${captype} ${item_id_string(id, ver)} was served using unsupported Hydrilla API version${bad_api_ver}. You might need to update Haketilo.`; return work.err(null, msg); } @@ -256,22 +259,15 @@ function InstallView(tab_id, on_view_show, on_view_hide) { return items; } + const show_super = this.show; this.show = async (repo_url, item_type, item_id, item_ver) => { - if (this.shown) + if (!show_super()) return; - this.shown = true; - this.repo_url = repo_url; dialog.loader(this.dialog_ctx, "Fetching data from repository..."); - try { - on_view_show(); - } catch(e) { - console.error(e); - } - try { var items = await compute_deps(item_type, item_id, item_ver); } catch(e) { @@ -288,7 +284,7 @@ function InstallView(tab_id, on_view_show, on_view_hide) { await dialog_prom; - hide(); + this.hide(); return; } @@ -419,35 +415,22 @@ function InstallView(tab_id, on_view_show, on_view_hide) { await dialog_prom; - hide(); + this.hide(); } - const hide = () => { - if (!this.shown) + const hide_super = this.hide; + this.hide = () => { + if (!hide_super()) return; - this.shown = false; delete this.item_entries; [...this.to_install_list.children].forEach(n => n.remove()); - - try { - on_view_hide(); - } catch(e) { - console.error(e); - } - } - - this.when_hidden = cb => { - const wrapped_cb = (...args) => { - if (!this.shown) - return cb(...args); - } - return wrapped_cb; } - const hide_cb = dialog.when_hidden(this.dialog_ctx, hide); + const hide_cb = this.dialog_ctx.when_hidden(this.hide); this.cancel_but.addEventListener("click", hide_cb); - const install_cb = dialog.when_hidden(this.dialog_ctx, perform_install); + const install_cb = this.dialog_ctx.when_hidden(perform_install); this.install_but.addEventListener("click", install_cb); } +#EXPORT InstallView diff --git a/html/repo_query.html b/html/repo_query.html new file mode 100644 index 0000000..73b0f00 --- /dev/null +++ b/html/repo_query.html @@ -0,0 +1,148 @@ +#IF !REPO_QUERY_LOADED +#DEFINE REPO_QUERY_LOADED + + + + +#INCLUDE html/install.html + +#LOADCSS html/reset.css +#LOADCSS html/base.css +#LOADCSS html/grid.css + + +#ENDIF diff --git a/html/repo_query.js b/html/repo_query.js new file mode 100644 index 0000000..8f33356 --- /dev/null +++ b/html/repo_query.js @@ -0,0 +1,210 @@ +/** + * This file is part of Haketilo. + * + * Function: Show available repositories and allow querying them for resources. + * + * Copyright (C) 2022 Wojtek Kosior + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * As additional permission under GNU GPL version 3 section 7, you + * may distribute forms of that code without the copy of the GNU + * GPL normally required by section 4, provided you include this + * license notice and, in case of non-source distribution, a URL + * through which recipients can access the Corresponding Source. + * If you modify file(s) with this exception, you may extend this + * exception to your version of the file(s), but you are not + * obligated to do so. If you do not wish to do so, delete this + * exception statement from your version. + * + * As a special exception to the GPL, any HTML file which merely + * makes function calls to this code, and for that purpose + * includes it by reference shall be deemed a separate work for + * copyright law purposes. If you modify this code, you may extend + * this exception to your version of the code, but you are not + * obligated to do so. If you do not wish to do so, delete this + * exception statement from your version. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * I, Wojtek Kosior, thereby promise not to sue for violation of this file's + * license. Although I request that you do not make use of this code in a + * proprietary program, I am not going to enforce this in court. + */ + +#IMPORT common/indexeddb.js AS haketilodb + +#FROM common/browser.js IMPORT browser +#FROM html/DOM_helpers.js IMPORT clone_template, Showable +#FROM common/entities.js IMPORT item_id_string, version_string, \ + is_valid_version +#FROM html/install.js IMPORT InstallView + +const coll = new Intl.Collator(); + +function ResultEntry(repo_entry, mapping_ref) { + Object.assign(this, clone_template("repo_query_single_result")); + Object.assign(this, {repo_entry, mapping_ref}); + + this.mapping_name.innerText = mapping_ref.long_name; + this.mapping_id.innerText = item_id_string(mapping_ref); + + const iv = repo_entry.query_view.install_view; + + function start_install() { + iv.show(repo_entry.repo_url, "mapping", + mapping_ref.identifier, mapping_ref.version); + } + + const cb = repo_entry.query_view.install_view.when_hidden(start_install); + this.install_but.addEventListener("click", cb); +} + +function RepoEntry(query_view, repo_url) { + Object.assign(this, clone_template("repo_query_single_repo")); + Object.assign(this, {query_view, repo_url}); + this.results_shown_before = false; + + this.repo_url_label.innerText = repo_url; + + const query_results = async () => { + const msg = [ + "repo_query", + `${repo_url}query?url=${encodeURIComponent(query_view.url)}` + ]; + const response = await browser.tabs.sendMessage(query_view.tab_id, msg); + + if ("error" in response) + throw "Failure to communicate with repository :("; + + if (!response.ok) + throw `Repository sent HTTP code ${response.status} :(`; + if ("error_json" in response) + throw "Repository's response is not valid JSON :("; + + if (!is_valid_version(response.json.api_schema_version)) { + var bad_api_ver = ""; + } else if (response.json.api_schema_version > [1]) { + var bad_api_ver = + ` (${version_string(response.json.api_schema_version)})`; + } else { + var bad_api_ver = false; + } + + if (bad_api_ver !== false) + throw `Results were served using unsupported Hydrilla API version${bad_api_ver}. You might need to update Haketilo.`; + + /* TODO: here we should perform JSON schema validation! */ + + return response.json.mappings; + } + + const populate_results = async () => { + this.results_shown_before = true; + + try { + var results = await query_results(); + } catch(e) { + this.info_span.innerText = e; + return; + } + + this.info_span.remove(); + this.results_list.classList.remove("hide"); + + this.result_entries = results.map(ref => new ResultEntry(this, ref)); + + const to_append = this.result_entries.length > 0 ? + this.result_entries.map(re => re.main_li) : + ["No results :("]; + + this.results_list.append(...to_append); + } + + let show_results = () => { + if (!query_view.shown) + return; + + if (!this.results_shown_before) + populate_results(); + + this.list_container.classList.remove("hide"); + this.hide_results_but.classList.remove("hide"); + this.show_results_but.classList.add("hide"); + } + show_results = query_view.install_view.when_hidden(show_results); + + let hide_results = () => { + if (!query_view.shown) + return; + + this.list_container.classList.add("hide"); + this.hide_results_but.classList.add("hide"); + this.show_results_but.classList.remove("hide"); + } + hide_results = query_view.install_view.when_hidden(hide_results); + + this.show_results_but.addEventListener("click", show_results); + this.hide_results_but.addEventListener("click", hide_results); +} + +const container_ids = ["repos_list_container", "install_view_container"]; + +function RepoQueryView(tab_id, on_view_show, on_view_hide) { + Showable.call(this, on_view_show, on_view_hide); + + Object.assign(this, clone_template("repo_query")); + this.tab_id = tab_id; + + const show_container = name => { + for (const cid of container_ids) { + if (cid !== name) + this[cid].classList.add("hide"); + } + this[name].classList.remove("hide"); + } + + this.install_view = new InstallView( + tab_id, + () => show_container("install_view_container"), + () => show_container("repos_list_container") + ); + this.install_view_container.prepend(this.install_view.main_div); + + const show_super = this.show; + this.show = async url => { + if (!show_super()) + return; + + this.url = url; + this.url_span.innerText = url; + + [...this.repos_list.children].forEach(c => c.remove()); + + const repo_urls = await haketilodb.get_repos(); + repo_urls.sort((a, b) => coll.compare(a, b)); + this.repo_entries = repo_urls.map(ru => new RepoEntry(this, ru)); + + if (repo_urls.length === 0) { + const info_li = document.createElement("li"); + info_li.innerText = "You have no repositories configured :("; + this.repos_list.append(info_li); + return; + } + + this.repos_list.append(...this.repo_entries.map(re => re.main_li)); + } + + this.cancel_but.addEventListener("click", + this.install_view.when_hidden(this.hide)); +} +#EXPORT RepoQueryView diff --git a/html/settings.html b/html/settings.html index f318e2c..ce19e55 100644 --- a/html/settings.html +++ b/html/settings.html @@ -115,14 +115,14 @@ #INCLUDE html/item_preview.html #INCLUDE html/text_entry_list.html #INCLUDE html/payload_create.html -
      +
      • Blocking
      • Mappings
      • Resources
      • New payload
      • Repositories
      -
      +
      diff --git a/html/text_entry_list.js b/html/text_entry_list.js index 8cef08f..0ea2862 100644 --- a/html/text_entry_list.js +++ b/html/text_entry_list.js @@ -162,11 +162,11 @@ function Entry(text, list, entry_idx) { [this.cancel_but, this.make_noneditable], [this.noneditable_view, this.make_editable], ]) - node.addEventListener("click", dialog.when_hidden(list.dialog_ctx, cb)); + node.addEventListener("click", list.dialog_ctx.when_hidden(cb)); const enter_cb = e => (e.key === 'Enter') && enter_hit(); this.input.addEventListener("keypress", - dialog.when_hidden(list.dialog_ctx, enter_cb)); + list.dialog_ctx.when_hidden(enter_cb)); if (entry_idx > 0) { const prev_text = list.shown_texts[entry_idx - 1]; @@ -227,8 +227,7 @@ function TextEntryList(dialog_ctx, destroy_cb, const add_new = () => new Entry(null, this, 0); - this.new_but.addEventListener("click", - dialog.when_hidden(dialog_ctx, add_new)); + this.new_but.addEventListener("click", dialog_ctx.when_hidden(add_new)); } async function repo_list(dialog_ctx) { -- cgit v1.2.3