view libervia/pages/_browser/dialog.py @ 1306:c07112ef01cd

browser (template): adapted filters/global/extensions to manage SàT templates: `template` module has been update so most SàT template can be run from browser: - `profile` and `csrf_token` are set as globals - an implementation of `xmlattr` filter has been added - `date_fmt` filter has been implemented using `moment.js` - i18n method `_` has been added to globals, and `{% trans %}` statement has been implemented using an extension. For now they are not actually translating but just returning the unmodified string. - new `get_args` helper method to handle `nunjucks` convention for arguments. - fixed `get_elt` to only return the first child element (avoiding any text child) + added a defaut value for `context`
author Goffi <goffi@goffi.org>
date Thu, 16 Jul 2020 09:08:50 +0200
parents 053141849206
children 683e50799d6d
line wrap: on
line source

"""manage common dialogs"""

from browser import document, timer
from template import Template


class Confirm:

    def __init__(self, message, ok_label="", cancel_label=""):
        self._tpl = Template("dialogs/confirm.html")
        self.message = message
        self.ok_label = ok_label
        self.cancel_label = cancel_label

    def cancel_cb(self, evt, notif_elt):
        notif_elt.remove()

    def show(self, ok_cb, cancel_cb=None):
        if cancel_cb is None:
            cancel_cb = self.cancel_cb
        notif_elt = self._tpl.get_elt({
            "message": self.message,
            "ok_label": self.ok_label,
            "cancel_label": self.cancel_label,
        })

        document['notifs_area'] <= notif_elt
        timer.set_timeout(lambda: notif_elt.classList.add('state_appended'), 0)
        for cancel_elt in notif_elt.select(".click_to_cancel"):
            cancel_elt.bind("click", lambda evt: cancel_cb(evt, notif_elt))
        for cancel_elt in notif_elt.select(".click_to_ok"):
            cancel_elt.bind("click", lambda evt: ok_cb(evt, notif_elt))