diff options
author | Wojtek Kosior <koszko@koszko.org> | 2022-01-08 19:23:17 +0100 |
---|---|---|
committer | Wojtek Kosior <koszko@koszko.org> | 2022-01-08 19:23:17 +0100 |
commit | 38650a8102fe0841617cd80f3a6e45b1f5f62fd5 (patch) | |
tree | de1916787c83ec317b51f0dcfc9c95f42ae23e5c /html | |
parent | 448820a11634de6ec356c77b8c7c0cf4937b344c (diff) | |
download | browser-extension-38650a8102fe0841617cd80f3a6e45b1f5f62fd5.tar.gz browser-extension-38650a8102fe0841617cd80f3a6e45b1f5f62fd5.zip |
further item list work
There is now a mechanism for removing an item and there are more tests for item list.
The entire thing is still work in progress.
Diffstat (limited to 'html')
-rw-r--r-- | html/item_list.html | 2 | ||||
-rw-r--r-- | html/item_list.js | 62 | ||||
-rw-r--r-- | html/item_preview.js | 2 |
3 files changed, 53 insertions, 13 deletions
diff --git a/html/item_list.html b/html/item_list.html index 41c7734..d082c5d 100644 --- a/html/item_list.html +++ b/html/item_list.html @@ -48,7 +48,7 @@ <template> <div id="item_list" data-template="main_div" class="grid_2"> <ul data-template="ul"></ul> - <div data-template="preview_container"> + <div data-template="preview_container" class="hide"> <!-- preview div will be dynamically inserted here --> <button data-template="remove_but">Remove</button> diff --git a/html/item_list.js b/html/item_list.js index f6b9bd3..55e54fb 100644 --- a/html/item_list.js +++ b/html/item_list.js @@ -52,10 +52,12 @@ function preview_item(list_ctx, item, ignore_dialog=false) if (list_ctx.dialog_ctx.shown && !ignore_dialog) return; - list_ctx.preview_ctx = - list_ctx.preview_cb(item.definition, list_ctx.preview_ctx); - list_ctx.preview_container - .prepend(list_ctx.preview_ctx.main_div); + list_ctx.preview_ctx = list_ctx.preview_cb( + item.definition, + list_ctx.preview_ctx, + list_ctx.dialog_ctx + ); + list_ctx.preview_container.prepend(list_ctx.preview_ctx.main_div); if (list_ctx.previewed_item !== null) list_ctx.previewed_item.li.classList.remove("item_li_highlight"); @@ -110,7 +112,6 @@ function find_item_idx(definition) function item_changed(list_ctx, change) { - /* Remove item. */ const old_item = list_ctx.by_identifier.get(change.key); if (old_item !== undefined) { @@ -133,7 +134,26 @@ function item_changed(list_ctx, change) preview_item(list_ctx, new_item, true); } -async function item_list(preview_cb, track_cb) +async function remove_clicked(list_ctx) +{ + if (list_ctx.dialog_ctx.shown || list_ctx.previewed_item === null) + return; + + const identifier = list_ctx.previewed_item.definition.identifier; + + if (!(await dialog.ask(list_ctx.dialog_ctx, + `Are you sure you want to delete '${identifier}'?`))) + return; + + try { + await list_ctx.remove_cb(identifier); + } catch(e) { + console.error(e); + dialog.error(list_ctx.dialog_ctx, `Couldn't remove '${identifier}' :(`) + } +} + +async function item_list(preview_cb, track_cb, remove_cb) { const list_ctx = clone_template("item_list"); @@ -148,6 +168,7 @@ async function item_list(preview_cb, track_cb) tracking, previewed_item: null, preview_cb, + remove_cb, dialog_ctx: dialog.make(() => on_dialog_show(list_ctx), () => on_dialog_hide(list_ctx)) }); @@ -156,6 +177,9 @@ async function item_list(preview_cb, track_cb) for (const def of definitions) insert_item(list_ctx, def, list_ctx.items.length); + list_ctx.remove_but + .addEventListener("click", () => remove_clicked(list_ctx)); + return list_ctx; } @@ -169,16 +193,32 @@ function on_dialog_show(list_ctx) function on_dialog_hide(list_ctx) { list_ctx.ul; - list_ctx.preview_container.classList.remove("hide"); + if (list_ctx.previewed_item !== null) + list_ctx.preview_container.classList.remove("hide"); list_ctx.dialog_container.classList.add("hide"); } -const resource_list = - () => item_list(resource_preview, haketilodb.track.resources); +async function remove_single_item(item_type, identifier) +{ + const store = ({resource: "resources", mapping: "mappings"})[item_type]; + const transaction_ctx = + await haketilodb.start_items_transaction([store], {}); + await haketilodb[`remove_${item_type}`](identifier, transaction_ctx); + await haketilodb.finalize_transaction(transaction_ctx); +} + +function resource_list() +{ + return item_list(resource_preview, haketilodb.track.resources, + id => remove_single_item("resource", id)); +} #EXPORT resource_list -const mapping_list = - () => item_list(mapping_preview, haketilodb.track.mappings); +function mapping_list() +{ + return item_list(mapping_preview, haketilodb.track.mappings, + id => remove_single_item("mapping", id)); +} #EXPORT mapping_list function destroy_list(list_ctx) diff --git a/html/item_preview.js b/html/item_preview.js index f59e30e..447b16a 100644 --- a/html/item_preview.js +++ b/html/item_preview.js @@ -64,7 +64,7 @@ async function file_link_clicked(preview_object, file_ref, event) "files", file_ref.hash_key); if (file === undefined) { dialog.error(preview_object.dialog_context, - "File missing from Haketilo's inernal database :("); + "File missing from Haketilo's internal database :("); } else { const encoded_file = encodeURIComponent(file.contents); open(`data:text/plain;charset=utf8,${encoded_file}`, '_blank'); |