from snabbdom import h, patch from message import ListItemClick, msg 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 View.view_entry(self.model.entry) def view_list_elements(self): def callback(num): return msg(lambda: ListItemClick(num)) list_elements = [ h('li', {"on": {"click": callback(idx)}}, name) for idx, name in enumerate(self.model.names) ] return h('ol', {}, list_elements) @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), h("span#comment", {}, entry.comment)]), h("div#sense-container", {}, view_sense_list)]) @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", {}, sense.label), h("span.sense-definition", {}, sense.definition), h("div", {}, View.view_translations(sense.translations)), h("div", {}, examples), h("input#translation-add", {"attr": {"type": "button", "value": "+", "title": "Dodaj prevedek / HUN"}}, [])])]) @staticmethod def view_example(example): return h("div.example", {}, [ h("div.example-dot", {}, "▣"), h("div.example-rest", {}, [ h("span.example-text", {}, example.example), h("div.example-translation", {}, [ h("span.example-arrow", {}, "↪"), h("span", {}, example.translation)])])]) @staticmethod def view_translations(translations): joiner = lambda: h("span.translation-semicolon", {}, ";") result = [] for cluster in translations: result.extend([View.view_one_translation(t) for t in cluster]) result.append(joiner()) result.pop() 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.items()]) elements.append(tags) elements.append(h("span.translation-text", {}, translation.translation)) #elements.append(h("select.translation-select", {}, [ # h("option", {"style": {"color": "black"}, {"attr": {"value": "edit", "title": "Spremeni"}}}, "✎"), # h("option", {"style": {"color": "black"}, {"attr": {"value": "right", "title": "Desno"}}}, "→"), # h("option", {"style": {"color": "black"}, {"attr": {"value": "left", "title": "Levo"}}}, "←"), # h("option", {"style": {"color": "black"}, {"attr": {"value": "bin", "title": "Odstrani"}}}, "🗑")])) return h("div.translation-div", {}, elements)