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

231 lines
9.8 KiB

from lib.snabbdom import h, patch
from message import *
import random
from view.utils import *
from model import Translation, Sense, Example
from browser import document
from export import export_to_xml
class View:
def __init__(self, container):
self.vdom = h('div', {}, "Loading...")
self.model = None
patch(container, self.vdom)
# this does not work on parent div, so attaching to document here
document.addEventListener("keyup", msg(KeyboardPress))
def view(self, model):
self.model = model
self.model.pre_view()
new_vdom = self._view()
patch(self.vdom, new_vdom)
self.vdom = new_vdom
def _view(self):
return h("div", {"on": { "click": msg(Reset) }}, [
self.model.entry.view(self.model),
h("button.blk", {"on": { "click": lambda _: check_export(self.model) } }, "CHK"),
View.view_menu(self.model.menu_location, self.model.menu_target, self.model.entry),
View.view_modal(self.model.modal_shown, self.model.modal)])
@staticmethod
def view_entry(entry, model):
view_sense_list = [View.view_sense(sense, idx, model) for idx, sense in enumerate(entry.senses)]
buttons_left = View.view_entry_button_section(entry, model)
buttons_right = View.view_toggle_buttons(model)
return h("div#entry", {}, [
h("div#entry-status", {}, entry.status),
h("div#entry-header", {}, [
h("span#headword", {}, entry.headword),
h("span#grammar", {}, entry.grammar),
h("span#measure", {}, entry.get_measure_text())]),
h("div.flex.five", {}, [
h("div.four-fifth", {}, buttons_left),
h("div.one-fifth", {}, buttons_right)]),
h("div#sense-container", {}, view_sense_list),
h("button.add-button", {"on": {"click": msg(AddSense())}}, "+")])
@staticmethod
def view_toggle_buttons(model):
txt_examples = "Hide examples" if model.examples_shown else "Show examples"
txt_clusters = "Hide clusters" if model.clusters_shown else "Show clusters"
return [h("span.button.toggle", {"on": {"click": msg(ToggleExamples)}}, txt_examples),
h("span.button.toggle", {"on": {"click": msg(ToggleClusters)}}, txt_clusters)]
@staticmethod
def view_entry_button_section(entry, model):
clk = lambda cls: {"on": {"click": msg(cls)}}
buttons = [
h("button.normal", clk(ShowVariantsEdit()), "Variante"),
h("button.success", clk(ShowRelatedEntriesEdit()), "Povezano"),
h("button.success", clk(ShowEntryLabelsEdit()), "Oznake"),
h("button.normal", clk(ShowCommentEdit()), "Opombe")]
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.related_entries) == 0:
view_buttons.append(buttons[1])
else:
view_table.append((buttons[1], ", ".join(entry.related_entries)))
if len(entry.labels) == 0:
view_buttons.append(buttons[2])
else:
labels = ", ".join([clean_label(val) for _, val in entry.labels])
view_table.append((buttons[2], labels))
if entry.comment == "":
view_buttons.append(buttons[3])
else:
view_table.append((buttons[3], 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, model):
examples = [View.view_example(example, sense, model) for example in sense.examples]
result = h("div.elm-div", {}, [
h("div.sense-num", {"on": {"click": msg(ShowSenseMenu(sense))}}, str(senseNum + 1)),
h("div.sense", {}, [
h("span.sense-label-list", { "on": { "click": msg(ShowSenseLabelEdit(sense)) }}, [
h("span.sense-label", {}, clean_label(slabel)) for _, slabel in sense.labels ]),
h("span.sense-definition", { "on": { "click": msg(ShowSenseDefinitionEdit(sense)) }}, sense.definition["indicator"]),
h("div", {}, View.view_translations(sense.translations, sense, model)),
h("div", {}, examples)])])
return result
@staticmethod
def view_example(example, sense, model):
example_tag = "div.example-rest"
if example in model.chosen_examples:
example_tag += ".example-chosen"
cluster = example.get_cluster()
dot_attr = {"style": { "visibility": "visible" if cluster is None else "hidden"}}
example_content = []
if cluster is not None:
example_content.append(h("span.example-cluster", {}, str(cluster + 1)))
example_text_inner_tag = "span.example-text-{}".format(example.get_view_type())
example_content.append(h(example_text_inner_tag, {}, example.view()))
other_attributes = example.get_other_attributes()
if "frequency" in other_attributes:
example_content.append(h("span.example-frequency", {}, other_attributes["frequency"]))
if "logDice" in other_attributes:
example_content.append(h("span.example-logdice", {}, other_attributes["logDice"]))
parent_display = "inherit"
if not model.examples_shown and not example.is_multiword():
parent_display = "none"
clusters_display = "inherit"
if not model.clusters_shown:
clusters_display = "none"
return h("div.example", {"style": {"display": parent_display}}, [
h("div.example-dot", dot_attr, ""),
h(example_tag, {}, [
h("span.example-text", {"on": {"click": msg(ShowExampleMenu, example)} }, example_content),
h("div.example-translation-list", {}, [
h("div.example-translation", {}, View.view_translations(example.translations, example, model))]),
h("div.example-clusters",
{"style": {"display": clusters_display }}, show_toggle_cluster_buttons(sense, example))])])
@staticmethod
def view_translations(translations, parent, model):
result = []
for cluster in translations:
result.append(h("div.translation-div-cluster", {}, [View.view_one_translation(t, model) for t in cluster]))
result.append(h("button.add-button", {"on": {"click": msg(ShowAddTranslation, parent)}}, "+"))
return result
@staticmethod
def view_one_translation(translation, model):
elements = []
if translation.tags:
tags = h("div.translation-tags", {}, [
h("span", {"attr": {"title": key}}, clean_label(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(ShowTranslationMenu, translation) }}, elements)
@staticmethod
def view_menu(location, menu_target, entry):
style = {
"left": "{}px".format(location[0]),
"top": "{}px".format(location[1])
}
if menu_target is None:
style["opacity"] = "0"
style["visibility"] = "hidden"
elif type(menu_target) is Translation:
translation = menu_target
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)}}, "🗑")])
elif type(menu_target) is Sense:
sense = menu_target
return h("span.popup-menu", { "style": style }, [
h("button.shyButton", { "on": {"click": msg(SenseMoveUp, sense)}}, ""),
h("button.shyButton", { "on": {"click": msg(SenseMoveDown, sense)}}, ""),
h("button.shyButton", { "on": {"click": msg(SenseBin, sense)}}, "🗑")])
elif type(menu_target) is Example:
example = menu_target
sense = example_sense(example, entry)
return h("span.popup-menu", { "style": style }, [
h("button.shyButton", { "on": {"click": msg(ShowExampleEdit, example, sense)}}, ""),
h("button.shyButton", { "on": {"click": msg(ExampleMoveUp, example)}}, ""),
h("button.shyButton", { "on": {"click": msg(ExampleMoveDown, example)}}, ""),
h("button.shyButton", { "on": {"click": msg(ExampleBin, example)}}, "🗑")])
else:
console.log("Should not be heree!!")
@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())])