#!/usr/bin/env python3
# -*- coding:utf-8 -*-
from flexx import flx
from ipfsdocs.common import (
BootstrapButton,
Modal,
URL,
DatePicker,
HR,
BootstrapInput,
set_to_localstorage,
get_from_localstorage,
)
from ipfsdocs.log import info
class ConfirmModal(Modal):
def init(self):
self.set_title("\uf058 \uf128")
with flx.VBox():
with flx.HBox():
self.message = flx.Label(html="\uf058 ?", style="font-size: xxx-large;")
with flx.HBox():
self.confirm = BootstrapButton(text="\uf058 \uf12a \uf12a \uf12a",
flex=0.1,
style="font-size: x-small;")
flx.Widget(flex=1)
self.confirm.node.setAttribute("data-dismiss", "modal")
flx.Widget(flex=1)
def display(self, message, callback):
self.callback = callback
# self.message.set_text(f"You are going to {message}. Are you sure?")
self.message.set_html(f"{message}")
super().display()
@flx.reaction("confirm.pointer_click")
def confirmed(self):
self.callback()
class ShowSharedModal(Modal):
cid = flx.StringProp("", settable=True)
webcid = flx.StringProp("", settable=True)
href = flx.StringProp("", settable=True)
webhref = flx.StringProp("", settable=True)
def init(self):
global navigator
global QRCode
self.set_title("Sharing")
with flx.HBox():
with flx.VBox():
self.link = flx.Label(style="font-size: large;")
self.copytoclipboard = BootstrapButton(text="\uf0ea", style="font-size: large;")
self.qrcode_widget = flx.Label()
with flx.VBox():
self.weblink = flx.Label()
self.copywebtoclipboard = BootstrapButton(text="\uf0ea")
self.qrcodeweb_widget = flx.Label()
self.share = BootstrapButton(text="Share")
if not navigator.share:
self.share.set_css_class("d-none")
flx.Widget(flex=1)
self.qrcode_generator = QRCode(self.qrcode_widget.node)
self.qrcodeweb_generator = QRCode(self.qrcodeweb_widget.node, {
"width": 210,
"height": 210,
})
@flx.reaction("share.pointer_click")
def do_share(self):
global navigator
if navigator.share:
navigator.share({
"title": "Sharing",
"text": "shared",
"url": self.href,
}).then(lambda: info("shared")).catch(lambda: info("error"))
else:
info("not supported")
@flx.reaction("copytoclipboard.pointer_click")
def do_copytoclipboard(self):
global document
el = document.createElement('textarea')
el.value = self.href
el.setAttribute('readonly', '')
el.style.position = 'absolute'
el.style.left = '-9999px'
document.body.appendChild(el)
el.select()
document.execCommand('copy')
document.body.removeChild(el)
@flx.reaction("copywebtoclipboard.pointer_click")
def do_copywebtoclipboard(self):
global document
el = document.createElement('textarea')
el.value = self.webhref
el.setAttribute('readonly', '')
el.style.position = 'absolute'
el.style.left = '-9999px'
document.body.appendChild(el)
el.select()
document.execCommand('copy')
document.body.removeChild(el)
@flx.reaction("cid")
def update_href(self):
self.set_href(f"{URL}{self.cid}")
@flx.reaction("webcid")
def update_webhref(self):
self.set_webhref(f"{URL}{self.webcid}")
@flx.reaction("href", "webhref")
def update_body(self):
self.link.set_html(f'\uf03e')
self.weblink.set_html(f'\uf03e')
self.qrcode_generator.clear()
self.qrcode_generator.makeCode(f"{self.href}")
self.qrcodeweb_generator.clear()
self.qrcodeweb_generator.makeCode(f"{self.webhref}")
class TagModal(Modal):
def init(self):
self.set_title("\uf0c4 \uf02b")
with flx.VBox():
self.key = flx.ComboBox(placeholder_text="key", editable=True)
self.value = flx.ComboBox(placeholder_text="value", editable=True)
with flx.HBox():
self.create = BootstrapButton(text="\uf067")
self.remove = BootstrapButton(text="\uf1f8")
@flx.reaction("root.state.tags")
def update_tags(self):
self.key.set_options(sorted(self.root.state.tags.keys(), key=lambda e: e.lower()))
@flx.reaction("key.selected_index", "root.state.tags")
def update_values(self):
if self.key.selected_index != -1:
self.value.set_options(sorted(self.root.state.tags[self.key.selected_key], key=lambda e: e.lower()))
@flx.reaction(
"key.text",
"value.text",
"key.selected_index",
"value.selected_index",
"pointer_click",
)
def update_buttons(self):
self.create.set_disabled(self.key.text == "" or self.value.text == ""
or (self.key.selected_index != -1 and self.value.selected_index != -1))
self.remove.set_disabled(self.key.selected_index == -1 or self.value.selected_index == -1)
@flx.reaction("create.pointer_click")
def add_new_tag(self):
self.root.jssays.add_new_tag(
self.key.text,
self.value.text,
)
@flx.reaction("remove.pointer_click")
def remove_tag(self):
self.root.jssays.count_tags(
self.key.selected_key,
self.value.selected_key,
)
@flx.reaction("root.pysays.counted_tags")
def ask_confirm_remove(self, *events):
if self.shown:
count = events[-1].count
self.root.frontend.confirm.display(
f"\uf02b {self.key.selected_key}={self.value.selected_key}
"
f" ({count} \uf15b)
"
" \uf061 \uf1f8",
self._remove_tag,
)
def _remove_tag(self):
self.root.jssays.remove_tag(
self.key.selected_key,
self.value.selected_key,
)
class SetOwnerModal(Modal):
def init(self):
self.set_title("\uf007")
with flx.VBox():
self.owner = flx.ComboBox(
placeholder_text="key",
options=("aylapomme", "konubinix", "ayla"),
)
self.confirm = BootstrapButton(text="\uf058")
@flx.reaction("confirm.pointer_click")
def do_move(self):
self.root.jssays.move_selection_to_owner(self.owner.selected_key)
class SetDateModal(Modal):
def init(self):
self.set_title("\uf073")
with flx.VBox():
self.date = DatePicker(flex=0.1)
self.confirm = BootstrapButton(text="\uf058")
self.update_disabled()
@flx.reaction("date.date")
def update_disabled(self):
self.confirm.set_disabled(self.date.date == "")
@flx.reaction("confirm.pointer_click")
def do_move(self):
self.root.jssays.move_selection_to_date(self.date.date)
class SetTagModal(Modal):
def init(self):
self.set_title("\uf02b")
with flx.VBox():
self.key = flx.ComboBox(placeholder_text="key")
self.value = flx.ComboBox(placeholder_text="value")
with flx.HBox():
self.plus = BootstrapButton(text="\uf067")
self.minus = BootstrapButton(text="\uf068")
@flx.reaction("root.state.tags")
def update_tags(self):
self.key.set_options(sorted(self.root.state.tags.keys(), key=lambda e: e.lower()))
@flx.reaction("key.selected_index", "root.state.tags")
def update_values(self):
if self.key.selected_index != -1:
self.value.set_options(sorted(self.root.state.tags[self.key.selected_key], key=lambda e: e.lower()))
@flx.reaction("key.selected_index", "value.selected_index")
def update_buttons(self):
self.plus.set_disabled(self.key.selected_index == -1 or self.value.selected_index == -1)
self.minus.set_disabled(self.key.selected_index == -1 or self.value.selected_index == -1)
@flx.reaction("plus.pointer_click")
def _plus(self):
self.root.jssays.add_tag_to_selection(
self.key.selected_key,
self.value.selected_key,
)
@flx.reaction("minus.pointer_click")
def _minus(self):
self.root.jssays.remove_tag_from_selection(
self.key.selected_key,
self.value.selected_key,
)
class ShowModal(Modal):
def init(self):
self.set_title("\uf05a")
self.message = flx.Label()
class OptionsModal(Modal):
def init(self):
self.set_title("\uf013")
with BootstrapInput(icon_text="\uf07e"):
self.thumbnail_width = flx.LineEdit(flex=1)
with BootstrapInput(icon_text="\uf0c0"):
self.query_limit = flx.LineEdit(flex=1)
with BootstrapInput(icon_text="\uf07c"):
self.number_per_page = flx.LineEdit(flex=1)
with BootstrapInput(icon_text="\uf15d"):
self.query_order = flx.LineEdit(flex=1)
with flx.HBox():
self.autoplay = flx.ToggleButton(text="Autoplay",
checked=get_from_localstorage("autoplay")["autoplay"] or False)
self.random = flx.ToggleButton(text="Random", checked=get_from_localstorage("random")["random"] or False)
self.update_width()
self.update_query_limit()
self.update_query_order()
@flx.reaction("autoplay.checked")
def onautoplay(self):
set_to_localstorage("autoplay", {"autoplay": self.autoplay.checked})
self.root.state.set_autoplay(self.autoplay.checked)
@flx.reaction("random.checked")
def onrandom(self):
set_to_localstorage("random", {"random": self.random.checked})
self.root.state.set_random(self.random.checked)
@flx.reaction("root.state.thumbnail_width")
def update_width(self):
self.thumbnail_width.set_text(str(self.root.state.thumbnail_width))
@flx.reaction("thumbnail_width.user_done")
def update_state_width(self):
if self.thumbnail_width.text != "":
self.root.state.set_thumbnail_width(int(self.thumbnail_width.text))
@flx.reaction("root.state.query_limit")
def update_query_limit(self):
self.query_limit.set_text(str(self.root.state.query_limit))
@flx.reaction("query_limit.user_done")
def update_state_query_limit(self):
if self.query_limit.text != "":
self.root.state.set_query_limit(int(self.query_limit.text))
@flx.reaction("root.state.query_order")
def update_query_order(self):
self.query_order.set_text(self.root.state.query_order)
@flx.reaction("query_order.user_done")
def update_state_query_order(self):
value = self.query_order.text or "asc"
self.root.state.set_query_order(value)
@flx.reaction("root.state.number_per_page")
def update_number_per_page(self):
self.number_per_page.set_text(str(self.root.state.number_per_page))
@flx.reaction("number_per_page.user_done")
def update_state_number_per_page(self):
if self.number_per_page.text != "":
self.root.state.set_number_per_page(int(self.number_per_page.text))