You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lexonomy_custom_editor/src/view/view.py

153 lines
6.0 KiB

from lib.snabbdom import h, patch
from message import *
import random
from export import export_to_xml
class View:
def __init__(self, container):
self.vdom = h('div', {}, "Loading...")
self.model = None
patch(container, self.vdom)
def view(self, model):
self.model = model
new_vdom = self._view()
patch(self.vdom, new_vdom)
self.vdom = new_vdom
def _view(self):
return h("div", {"on": { "click": msg(Reset()) }}, [
View.view_entry(self.model.entry),
h("button.blk", {"on": { "click": lambda _: console.log(export_to_xml(self.model)) } }, "XML2Console"),
View.view_menu(self.model.menu_location, self.model.menu_shown, self.model.translation),
View.view_modal(self.model.modal_shown, self.model.modal)])
@staticmethod
def view_entry(entry):
view_sense_list = [View.view_sense(sense, idx) for idx, sense in enumerate(entry.senses)]
return h("div#entry", {}, [
h("div#entry-status", {}, entry.status),
h("div#entry-header", {}, [
h("span#headword", {}, entry.headword),
h("span#grammar", {}, entry.grammar)]),
View.view_entry_button_section(entry),
h("div#sense-container", {}, view_sense_list),
h("button.add-button", {"on": {"click": msg(AddSense())}}, "+")])
@staticmethod
def view_entry_button_section(entry):
clk = lambda cls: {"on": {"click": msg(cls)}}
buttons = [
h("button.normal", clk(ShowVariantsEdit()), "Variants"),
h("button.success", clk(ShowEntryLabelsEdit()), "Labels"),
h("button.warning", clk(ShowCommentEdit()), "Comment")]
view_buttons = []
view_table = []
if len(entry.variants) == 0:
view_buttons.append(buttons[0])
else:
view_table.append((buttons[0], ", ".join(entry.variants)))
if len(entry.labels) == 0:
view_buttons.append(buttons[1])
else:
labels = ", ".join([val for _, val in entry.labels])
view_table.append((buttons[1], labels))
if entry.comment == "":
view_buttons.append(buttons[2])
else:
view_table.append((buttons[2], entry.comment))
table_rows = [
h("tr", {}, [ h("td", {}, btn), h("td", {}, content)])
for btn, content in view_table]
view_buttons.append(h("table", {}, table_rows))
return h("div", {}, view_buttons)
@staticmethod
def view_sense(sense, senseNum):
examples = [View.view_example(example) for example in sense.examples]
return h("div.elm-div", {}, [
h("div.sense-num", {}, str(senseNum + 1)),
h("div.sense", {}, [
h("span.sense-label-list", { "on": { "click": msg(ShowSenseLabelEdit(sense)) }}, [
h("span.sense-label", {}, slabel) for _, slabel in sense.labels ]),
h("span.sense-definition", { "on": { "click": msg(ShowSenseDefinitionEdit(sense)) }}, sense.definition),
h("div", {}, View.view_translations(sense.translations, sense)),
h("div", {}, examples)])])
@staticmethod
def view_example(example):
return h("div.example", {}, [
h("div.example-dot", {}, ""),
h("div.example-rest", {}, [
h("span.example-text", {"on": {"click": msg(ShowExampleEdit(example))} }, example.example),
h("div.example-translation-list", { "on": {"click": msg(ShowExampleTranslationEdit(example))} }, [
h("div.example-translation", {}, [
h("span.example-arrow", {}, ""),
h("span", {}, t)])
for t in example.translations])])])
@staticmethod
def view_translations(translations, sense):
result = []
for cluster in translations:
result.append(h("div.translation-div-cluster", {}, [View.view_one_translation(t) for t in cluster]))
result.append(h("button.add-button", {"on": {"click": msg(ShowAddTranslation(sense))}}, "+"))
return result
@staticmethod
def view_one_translation(translation):
elements = []
if translation.tags:
tags = h("div.translation-tags", {}, [
h("span", {"attr": {"title": key}}, value)
for key, value in translation.tags])
elements.append(tags)
elements.append(h("span.translation-text", {}, translation.translation))
if translation.source:
elements.append(h("span.translation-source", {}, translation.source))
explanation_class = ".translation-explanation" if translation.translation else ""
elements.append(h("span{}".format(explanation_class), {}, translation.explanation))
return h("div.translation-div", {"on": {"click": msg(ShowMenu(translation)) }}, elements)
@staticmethod
def view_menu(location, menu_shown, translation):
style = {
"left": "{}px".format(location[0]),
"top": "{}px".format(location[1])
}
if menu_shown:
style["opacity"] = "1"
style["visibility"] = "visible"
return h("span.popup-menu", { "style": style }, [
h("button.shyButton", { "on": {"click": msg(ShowEditTranslation(translation))}}, ""),
h("button.shyButton", { "on": {"click": msg(MoveRight(translation))}}, ""),
h("button.shyButton", { "on": {"click": msg(MoveLeft(translation))}}, ""),
h("button.shyButton", { "on": {"click": msg(BinTranslation(translation))}}, "🗑")])
@staticmethod
def view_modal(modal_shown, modal):
return h("div.modal", {}, [
h("input", { "props": {"type": "checkbox", "checked": modal_shown} }, ""),
h("label.overlay", {}, ""),
h("article", {"on": { "click": msg(NoReset()) }}, modal())])