Moving view into classes

This commit is contained in:
Ozbolt Menegatti 2020-02-16 23:18:07 +01:00
parent e3792005ba
commit 8c2197e78b
7 changed files with 126 additions and 91 deletions

View File

@ -37,7 +37,7 @@ def copy(parent):
return result return result
class Editable: class Data:
def make_copy(self): def make_copy(self):
# makes an internal copy of self # makes an internal copy of self
# delete old copy # delete old copy
@ -66,6 +66,10 @@ class Editable:
self._copy = None self._copy = None
def view(self, model, *args):
raise NotImplementedError()
@classmethod @classmethod
def new_empty(cls): def import_xml(cls, xml, *args):
return cls(empty_doc) raise NotImplementedError()

View File

@ -1,9 +1,28 @@
from model.sense import Sense from model.sense import Sense
from model.editable import Editable from model.data import Data
from model.tags import import_label_list from model.tags import import_label_list
class Entry(Editable): from lib.snabbdom import h
def __init__(self, entry_xml): import message as M
from view import View
from view.utils import clean_label
class Entry(Data):
def __init__(self):
self.status = ""
self.headword = ""
self.headword_type = None
self.grammar = ""
self.comment = ""
self.variants = []
self.related_entries = []
self.lexical_unit = {}
self.measure = {}
self.labels = []
self.senses = []
def import_xml(self, entry_xml):
status = entry_xml.querySelector("head status") status = entry_xml.querySelector("head status")
headword = entry_xml.querySelector("head headword lemma") headword = entry_xml.querySelector("head headword lemma")
grammar = entry_xml.querySelector("head grammar category") grammar = entry_xml.querySelector("head grammar category")
@ -17,13 +36,11 @@ class Entry(Editable):
self.variants = [v.textContent for v in entry_xml.querySelectorAll("head variantList variant")] self.variants = [v.textContent for v in entry_xml.querySelectorAll("head variantList variant")]
self.related_entries = [re.textContent for re in entry_xml.querySelectorAll("head relatedEntryList relatedEntry")] self.related_entries = [re.textContent for re in entry_xml.querySelectorAll("head relatedEntryList relatedEntry")]
self.lexical_unit = {}
lex_unit = entry_xml.querySelector("lexical_unit lexeme,lexicalUnit lexeme") lex_unit = entry_xml.querySelector("lexical_unit lexeme,lexicalUnit lexeme")
if lex_unit: if lex_unit:
self.lexical_unit['id'] = lex_unit.getAttribute("lexical_unit_lexeme_id") self.lexical_unit['id'] = lex_unit.getAttribute("lexical_unit_lexeme_id")
self.lexical_unit['text'] = lex_unit.textContent self.lexical_unit['text'] = lex_unit.textContent
self.measure = {}
measure = entry_xml.querySelector("measureList measure") measure = entry_xml.querySelector("measureList measure")
if measure: if measure:
self.measure["source"] = measure.getAttribute("source") self.measure["source"] = measure.getAttribute("source")
@ -32,8 +49,70 @@ class Entry(Editable):
self.labels = import_label_list("head labelList label", entry_xml) self.labels = import_label_list("head labelList label", entry_xml)
self.senses = [Sense(sense_xml, i) for i, sense_xml in for i, sense_xml in enumerate(entry_xml.querySelectorAll("body senseList sense")):
enumerate(entry_xml.querySelectorAll("body senseList sense"))] sense = Sense()
sense.import_xml(sense_xml, i)
self.senses.append(sense)
def view(self, model):
view_sense_list = [sense.view(model, idx) for idx, sense in enumerate(self.senses)]
buttons_left = self._view_button_section(model)
buttons_right = View.view_toggle_buttons(model)
return h("div#entry", {}, [
h("div#entry-status", {}, self.status),
h("div#entry-header", {}, [
h("span#headword", {}, self.headword),
h("span#grammar", {}, self.grammar),
h("span#measure", {}, self.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": M.msg(M.AddSense)}}, "+")])
def _view_button_section(self, model):
clk = lambda cls: {"on": {"click": M.msg(cls)}}
buttons = [
h("button.normal", clk(M.ShowVariantsEdit), "Variante"),
h("button.success", clk(M.ShowRelatedEntriesEdit), "Povezano"),
h("button.success", clk(M.ShowEntryLabelsEdit), "Oznake"),
h("button.normal", clk(M.ShowCommentEdit), "Opombe")]
view_buttons = []
view_table = []
if len(self.variants) == 0:
view_buttons.append(buttons[0])
else:
view_table.append((buttons[0], ", ".join(self.variants)))
if len(self.related_entries) == 0:
view_buttons.append(buttons[1])
else:
view_table.append((buttons[1], ", ".join(self.related_entries)))
if len(self.labels) == 0:
view_buttons.append(buttons[2])
else:
labels = ", ".join([clean_label(val) for _, val in self.labels])
view_table.append((buttons[2], labels))
if self.comment == "":
view_buttons.append(buttons[3])
else:
view_table.append((buttons[3], self.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)
def get_measure_text(self): def get_measure_text(self):
return self.measure["text"] if "text" in self.measure else "" return self.measure["text"] if "text" in self.measure else ""

View File

@ -1,4 +1,4 @@
from model.editable import Editable from model.data import Data as Editable
from model.translation import from_container_list from model.translation import from_container_list
from model.example_clusters import ExampleClusters from model.example_clusters import ExampleClusters
from lib.snabbdom import h from lib.snabbdom import h

View File

@ -7,7 +7,7 @@ from model.example_clusters import ExampleClusters
class Model: class Model:
def __init__(self): def __init__(self):
# main data stuff # main data stuff
self.entry = None self.entry = Entry()
# report everything that happens! # report everything that happens!
self.log = [] self.log = []
@ -60,6 +60,6 @@ class Model:
def import_xml(self, xml_text): def import_xml(self, xml_text):
parser = __new__(DOMParser()) parser = __new__(DOMParser())
xmlDoc = parser.parseFromString(xml_text, "text/xml") xmlDoc = parser.parseFromString(xml_text, "text/xml")
self.entry = Entry(xmlDoc.querySelector("entry")) self.entry.import_xml(xmlDoc.querySelector("entry"))
self.reset() self.reset()

View File

@ -1,14 +1,23 @@
from model.example import Example from model.example import Example
from model.translation import from_container_list from model.translation import from_container_list
from model.editable import Editable from model.data import Data
from model.tags import import_label_list from model.tags import import_label_list
from view import View
import message as M
from lib.snabbdom import h
from view.utils import clean_label
class Sense(Editable): class Sense(Data):
def __init__(self, sense_xml, idx): def __init__(self):
self.original_idx = idx self.original_idx = -1
self.definition = {} self.definition = {}
self.labels = []
self.translations = []
self.examples = []
def import_xml(self, sense_xml, idx):
self.original_idx = idx
for definition in sense_xml.querySelectorAll("definitionList definition"): for definition in sense_xml.querySelectorAll("definitionList definition"):
key = definition.getAttribute("type") key = definition.getAttribute("type")
self.definition[key] = definition.textContent self.definition[key] = definition.textContent
@ -19,7 +28,21 @@ class Sense(Editable):
self.examples = [Example(example_xml) for example_xml in self.examples = [Example(example_xml) for example_xml in
sense_xml.querySelectorAll("exampleContainerList exampleContainer")] sense_xml.querySelectorAll("exampleContainerList exampleContainer")]
def merge_labels(self): def merge_labels(self):
return ", ".join(val for _, val in self.labels) return ", ".join(val for _, val in self.labels)
def view(self, model, sense_num):
examples = [View.view_example(example, self, model) for example in self.examples]
result = h("div.elm-div", {}, [
h("div.sense-num", {"on": {"click": M.msg(M.ShowSenseMenu, self)}}, str(sense_num + 1)),
h("div.sense", {}, [
h("span.sense-label-list", { "on": { "click": M.msg(M.ShowSenseLabelEdit, self) }}, [
h("span.sense-label", {}, clean_label(slabel)) for _, slabel in self.labels ]),
h("span.sense-definition", { "on": { "click": M.msg(M.ShowSenseDefinitionEdit, self) }}, self.definition["indicator"]),
h("div", {}, View.view_translations(self.translations, self, model)),
h("div", {}, examples)])])
return result

View File

@ -1,5 +1,5 @@
from model.tags import import_label_list from model.tags import import_label_list
from model.editable import Editable from model.data import Data as Editable
def from_container_list(translation_list_container_xml): def from_container_list(translation_list_container_xml):

View File

@ -31,25 +31,6 @@ class View:
h("button.blk", {"on": { "click": lambda _: check_export(self.model) } }, "CHK"), 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_menu(self.model.menu_location, self.model.menu_target, self.model.entry),
View.view_modal(self.model.modal_shown, self.model.modal)]) 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 @staticmethod
@ -61,59 +42,7 @@ class View:
h("span.button.toggle", {"on": {"click": msg(ToggleClusters)}}, txt_clusters)] 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 @staticmethod
def view_example(example, sense, model): def view_example(example, sense, model):