changeset 1320:0cbf86b1dcca

browser (photos/album): invitation manager first draft.
author Goffi <goffi@goffi.org>
date Sat, 01 Aug 2020 16:47:24 +0200
parents 6e2821e78489
children eb85ef26cb4a
files libervia/pages/_browser/slideshow.py libervia/pages/photos/album/_browser/__init__.py
diffstat 2 files changed, 141 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/libervia/pages/_browser/slideshow.py	Sat Aug 01 16:47:24 2020 +0200
+++ b/libervia/pages/_browser/slideshow.py	Sat Aug 01 16:47:24 2020 +0200
@@ -205,5 +205,5 @@
         side_panel = self.comments_panel_elt.select_one('.comments_side_panel')
         timer.set_timeout(lambda: side_panel.classList.add("open"), 0)
         for close_elt in self.comments_panel_elt.select('.click_to_close'):
-             close_elt.bind("click", self.on_comment_close)
+            close_elt.bind("click", self.on_comment_close)
         side_panel.bind("click", self.on_comments_panel_click)
--- a/libervia/pages/photos/album/_browser/__init__.py	Sat Aug 01 16:47:24 2020 +0200
+++ b/libervia/pages/photos/album/_browser/__init__.py	Sat Aug 01 16:47:24 2020 +0200
@@ -1,4 +1,4 @@
-from browser import document, window, bind, html, DOMNode
+from browser import document, window, bind, html, DOMNode, timer
 from javascript import JSON
 from bridge import Bridge
 from template import Template
@@ -9,6 +9,7 @@
 files_path = window.files_path
 bridge = Bridge()
 
+# file upload
 
 def on_progress(ev, photo_elt):
     if ev.lengthComputable:
@@ -104,6 +105,7 @@
     files = evt.currentTarget.files
     upload_files(files)
 
+# delete
 
 def file_delete_cb(item_elt, item):
     item_elt.classList.add("state_deleted")
@@ -146,6 +148,7 @@
         cancel_cb=lambda evt, notif_elt: delete_cancel(evt, notif_elt, item_elt, item),
     )
 
+# slideshow
 
 @bind(".photo_thumb_click", "click")
 def photo_click(evt):
@@ -165,3 +168,139 @@
 
 for elt in document.select('.action_delete'):
     elt.bind("click", on_delete)
+
+# manage
+
+def on_manager_close(manager_panel_elt):
+    side_panel = manager_panel_elt.select_one('.invitation_manager_side_panel')
+    side_panel.classList.remove('open')
+    side_panel.bind("transitionend", lambda evt: manager_panel_elt.remove())
+
+def _on_invitation_cb(field_elt, entity):
+    print(f"invitation for {entity!r} sent successfully")
+    submit_elt = document['invitation_submit']
+    submit_elt.disabled = False
+    form_elt = document['invitation_form']
+    form_elt.disabled = False
+    jids_elt = form_elt.select_one('*[name="jids"]')
+    emails_elt = form_elt.select_one('*[name="emails"]')
+    new = [d.strip() for d in field_elt.value.split('\n') if d and d.strip() != entity]
+    field_elt.value = '\n'.join(new)
+    if not jids_elt.value.strip() and not emails_elt.value.strip():
+        # FIXME: Q&D notification, needs to do this properly in a separated module with
+        #   some animations
+        notifs_elt = document['invitation_notifications']
+        notification_elt = html.DIV(Class="notification is-success has-text-centered")
+        notification_elt <= "invitations sent successfully"
+        notifs_elt <= notification_elt
+        timer.set_timeout(lambda: notification_elt.remove(), 5000)
+
+
+def invitationSimpleCreateCb(invitation_data, email):
+    invitee_jid = invitation_data['jid']
+    album_name = files_path.rsplit('/')[-1]
+    form_elt = document['invitation_form']
+    emails_elt = form_elt.select_one('*[name="emails"]')
+    bridge.FISInvite(
+        invitee_jid,
+        files_service,
+        "photos",
+        "",
+        files_path,
+        album_name,
+        '',
+        callback=lambda: _on_invitation_cb(emails_elt, email),
+        errback=lambda e: window.alert(f"invitation failed for {email}: {e}")
+    )
+
+
+def invite_by_email(email):
+    guest_url_tpl = f'{window.URL.new("/g", document.baseURI).href}/{{uuid}}'
+    bridge.invitationSimpleCreate(
+        email,
+        guest_url_tpl,
+        '',
+        callback=lambda data: invitationSimpleCreateCb(data, email),
+        errback=lambda e: window.alert(f"can't send email invitation: {e}")
+    )
+
+
+def on_invitation_submit(evt, manager_panel_elt):
+    evt.stopPropagation()
+    evt.preventDefault()
+    submit_elt = document['invitation_submit']
+    submit_elt.disabled = True
+    form_elt = document['invitation_form']
+    form_elt.disabled = True
+    jids_elt = form_elt.select_one('*[name="jids"]')
+    emails_elt = form_elt.select_one('*[name="emails"]')
+    jids = [j.strip() for j in jids_elt.value.split('\n') if j.strip()]
+    emails = [e.strip() for e in emails_elt.value.split('\n') if e.strip()]
+    album_name = files_path.rsplit('/')[-1]
+    for entity_jid in jids:
+        print(f"inviting {entity_jid}")
+        bridge.FISInvite(
+            entity_jid,
+            files_service,
+            "photos",
+            "",
+            files_path,
+            album_name,
+            '',
+            callback=lambda entity=entity_jid: _on_invitation_cb(jids_elt, entity),
+            errback=lambda e: window.alert(f"invitation failed: {e}")
+        )
+
+    for email in emails:
+        invite_by_email(email)
+
+    print(f"{jids=}, {emails=}")
+
+
+def _FISAffiliationSetCb(affiliation_elt):
+    affiliation_elt.remove()
+
+
+def on_affiliation_remove(entity_jid, affiliation_elt):
+    bridge.FISAffiliationsSet(
+        files_service,
+        "",
+        files_path,
+        {entity_jid: "none"},
+        callback=lambda: _FISAffiliationSetCb(affiliation_elt),
+        errback=lambda e: window.alert(f"can't remove affiliation: {e}")
+    )
+
+
+@bind("#button_manage", "click")
+def manage_click(evt):
+    evt.stopPropagation()
+    evt.preventDefault()
+    manager_panel_tpl = Template('invitation/manager.html')
+    manager_panel_elt = manager_panel_tpl.get_elt()
+    document.body <= manager_panel_elt
+    document['invitation_submit'].bind(
+        "click", lambda evt: on_invitation_submit(evt, manager_panel_elt))
+    side_panel = manager_panel_elt.select_one('.invitation_manager_side_panel')
+    timer.set_timeout(lambda: side_panel.classList.add("open"), 0)
+    for close_elt in manager_panel_elt.select('.click_to_close'):
+        close_elt.bind("click", lambda evt: on_manager_close(manager_panel_elt))
+    side_panel.bind("click", lambda evt: evt.stopPropagation())
+    affiliations = window.affiliations.to_dict()
+    affiliation_tpl = Template('invitation/affiliation_item.html')
+    for entity_jid, affiliation in affiliations.items():
+        affiliation_elt = affiliation_tpl.get_elt({
+            "entity_jid": entity_jid,
+            "affiliation": affiliation,
+        })
+        document['affiliations'] <= affiliation_elt
+        for elt in affiliation_elt.select(".click_to_delete"):
+            elt.bind(
+                "click",
+                lambda evt,
+                       entity_jid=entity_jid,
+                       affiliation_elt=affiliation_elt: on_affiliation_remove(
+                    entity_jid,
+                    affiliation_elt
+                )
+            )