changeset 1422:027f5059e1e2

pages (lists/view_item): browser part was not in the repository
author Goffi <goffi@goffi.org>
date Sat, 01 May 2021 19:05:05 +0200
parents e065c8886b81
children 870b198e98ea
files libervia/pages/lists/view_item/_browser/__init__.py
diffstat 1 files changed, 57 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libervia/pages/lists/view_item/_browser/__init__.py	Sat May 01 19:05:05 2021 +0200
@@ -0,0 +1,57 @@
+from browser import document, window, aio
+from aio_bridge import Bridge
+import dialog
+
+try:
+    pubsub_service = window.pubsub_service
+    pubsub_node = window.pubsub_node
+    pubsub_item = window.pubsub_item
+except AttributeError:
+    can_delete = False
+else:
+    bridge = Bridge()
+    can_delete = True
+
+
+async def on_delete(evt):
+    evt.stopPropagation()
+    confirmed = await dialog.Confirm(
+        "This item will be deleted, are you sure?",
+        ok_label="delete",
+        ok_color="danger",
+    ).ashow()
+    if confirmed:
+        try:
+            comments_service = window.comments_service
+            comments_node = window.comments_node
+        except AttributeError:
+            pass
+        else:
+            print(f"deleting comment node at [{comments_service}] {comments_node!r}")
+            try:
+                await bridge.psNodeDelete(comments_service, comments_node)
+            except Exception as e:
+                dialog.notification.show(
+                    f"Can't delete comment node: {e}",
+                    level="error"
+                )
+
+        print(f"deleting list item {pubsub_item!r} at [{pubsub_service}] {pubsub_node!r}")
+        try:
+            await bridge.psItemRetract(pubsub_service, pubsub_node, pubsub_item, True)
+        except Exception as e:
+            dialog.notification.show(
+                f"Can't delete list item: {e}",
+                level="error"
+            )
+        else:
+            # FIXME: Q&D way to get list view URL, need to have a proper method (would
+            #   be nice to have a way to reference pages by name from browser)
+            list_url = '/'.join(window.location.pathname.split('/')[:-1]).replace(
+                'view_item', 'view')
+            window.location.replace(list_url)
+
+
+if can_delete:
+    for elt in document.select('.action_delete'):
+        elt.bind("click", lambda evt: aio.run(on_delete(evt)))