2019-11-11 22:04:45 +00:00
|
|
|
from lib.snabbdom import h, patch
|
|
|
|
from message import *
|
|
|
|
import random
|
|
|
|
|
|
|
|
from export import export_to_xml
|
2019-11-05 21:18:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
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):
|
2019-11-17 20:15:35 +00:00
|
|
|
return h("div", {"on": { "click": msg(Reset()) }}, [
|
2019-11-11 22:04:45 +00:00
|
|
|
View.view_entry(self.model.entry),
|
2019-11-13 22:21:29 +00:00
|
|
|
# h("button.blk", {"on": { "click": lambda _: console.log(export_to_xml(self.model)) } }, "XML2Console"),
|
2019-11-11 22:04:45 +00:00
|
|
|
View.view_menu(self.model.menu_location, self.model.menu_shown, self.model.translation),
|
|
|
|
View.view_modal(self.model.modal_shown, self.model.modal)])
|
2019-11-05 21:18:20 +00:00
|
|
|
|
|
|
|
@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", {}, [
|
2019-11-11 22:04:45 +00:00
|
|
|
h("span#headword", {}, entry.headword),
|
2019-11-05 21:18:20 +00:00
|
|
|
h("span#grammar", {}, entry.grammar),
|
2019-11-17 20:15:35 +00:00
|
|
|
h("button#comment.warning", {"on": {"click": msg(ShowCommentEdit())}}, entry.comment)]),
|
2019-11-15 21:24:32 +00:00
|
|
|
h("div#sense-container", {}, view_sense_list),
|
2019-11-17 20:15:35 +00:00
|
|
|
h("button.add-button", {"on": {"click": msg(ShowSenseAdd())}}, "+")])
|
2019-11-05 21:18:20 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def view_sense(sense, senseNum):
|
|
|
|
examples = [View.view_example(example) for example in sense.examples]
|
2019-11-11 22:04:45 +00:00
|
|
|
|
2019-11-05 21:18:20 +00:00
|
|
|
return h("div.elm-div", {}, [
|
|
|
|
h("div.sense-num", {}, str(senseNum + 1)),
|
|
|
|
h("div.sense", {}, [
|
2019-11-17 20:15:35 +00:00
|
|
|
h("span.sense-label-list", { "on": { "click": msg(ShowSenseLabelEdit(sense)) }}, [
|
2019-11-11 23:34:52 +00:00
|
|
|
h("span.sense-label", {}, slabel) for slabel in sense.labels ]),
|
2019-11-17 20:15:35 +00:00
|
|
|
h("span.sense-definition", { "on": { "click": msg(ShowSenseDefinitionEdit(sense)) }}, sense.definition),
|
2019-11-11 22:04:45 +00:00
|
|
|
h("div", {}, View.view_translations(sense.translations, sense)),
|
|
|
|
h("div", {}, examples)])])
|
2019-11-05 21:18:20 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def view_example(example):
|
|
|
|
return h("div.example", {}, [
|
|
|
|
h("div.example-dot", {}, "▣"),
|
|
|
|
h("div.example-rest", {}, [
|
2019-11-17 20:15:35 +00:00
|
|
|
h("span.example-text", {"on": {"click": msg(ShowExampleEdit(example))} }, example.example),
|
|
|
|
h("div.example-translation-list", { "on": {"click": msg(ShowExampleTranslationEdit(example))} }, [
|
2019-11-16 13:18:37 +00:00
|
|
|
h("div.example-translation", {}, [
|
|
|
|
h("span.example-arrow", {}, "↪"),
|
|
|
|
h("span", {}, t)])
|
|
|
|
for t in example.translations])])])
|
2019-11-05 21:18:20 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2019-11-11 22:04:45 +00:00
|
|
|
def view_translations(translations, sense):
|
2019-11-05 21:18:20 +00:00
|
|
|
result = []
|
|
|
|
for cluster in translations:
|
2019-11-13 22:20:34 +00:00
|
|
|
result.append(h("div.translation-div-cluster", {}, [View.view_one_translation(t) for t in cluster]))
|
2019-11-05 21:18:20 +00:00
|
|
|
|
2019-11-17 20:15:35 +00:00
|
|
|
result.append(h("button.add-button", {"on": {"click": msg(ShowAddTranslation(sense))}}, "+"))
|
2019-11-05 21:18:20 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def view_one_translation(translation):
|
|
|
|
elements = []
|
|
|
|
|
|
|
|
if translation.tags:
|
|
|
|
tags = h("div.translation-tags", {}, [
|
|
|
|
h("span", {"attr": {"title": key}}, value)
|
2019-11-17 20:15:35 +00:00
|
|
|
for key, value in translation.tags])
|
2019-11-05 21:18:20 +00:00
|
|
|
elements.append(tags)
|
|
|
|
|
|
|
|
elements.append(h("span.translation-text", {}, translation.translation))
|
2019-11-15 21:24:32 +00:00
|
|
|
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))
|
|
|
|
|
2019-11-17 20:15:35 +00:00
|
|
|
return h("div.translation-div", {"on": {"click": msg(ShowMenu(translation)) }}, elements)
|
2019-11-05 21:18:20 +00:00
|
|
|
|
2019-11-11 22:04:45 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def view_menu(location, menu_shown, translation):
|
|
|
|
style = {
|
2019-11-13 22:21:02 +00:00
|
|
|
"left": "{}px".format(location[0]),
|
|
|
|
"top": "{}px".format(location[1])
|
2019-11-11 22:04:45 +00:00
|
|
|
}
|
|
|
|
if menu_shown:
|
|
|
|
style["opacity"] = "1"
|
|
|
|
style["visibility"] = "visible"
|
|
|
|
|
|
|
|
return h("span.popup-menu", { "style": style }, [
|
2019-11-17 20:15:35 +00:00
|
|
|
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))}}, "🗑")])
|
2019-11-11 22:04:45 +00:00
|
|
|
|
2019-11-05 21:18:20 +00:00
|
|
|
|
2019-11-11 22:04:45 +00:00
|
|
|
@staticmethod
|
|
|
|
def view_modal(modal_shown, modal):
|
|
|
|
return h("div.modal", {}, [
|
|
|
|
h("input", { "props": {"type": "checkbox", "checked": modal_shown} }, ""),
|
|
|
|
h("label.overlay", {}, ""),
|
2019-11-17 20:15:35 +00:00
|
|
|
h("article", {"on": { "click": msg(NoReset()) }}, modal())])
|
2019-11-11 22:04:45 +00:00
|
|
|
|