from model.tags import import_label_list from model.explanation import Explanation from model.data import Data from lib.snabbdom import h from view.utils import clean_label import message as M def from_container_list(translation_list_container_xml): translations = [] max_num_cluster = 0 for translation_xml in translation_list_container_xml: num_cluster = 1 # default cluster if translation_xml.hasAttribute("cluster"): num_cluster = int(translation_xml.getAttribute("cluster")) max_num_cluster = max(max_num_cluster, num_cluster) t = Translation() t.import_xml(translation_xml) translations.append((num_cluster, t)) result = [[] for _ in range(max_num_cluster)] for clusterNum, translation in translations: result[clusterNum - 1].append(translation) return result class Translation(Data): def __init__(self, translation_xml): self.translation = "" self.source = "" self.targetLang = "" self.audio = "" self.explanationList = set() self.tags = [] def import_xml(self, translation_xml): translation = translation_xml.querySelector("translation") if translation: self.translation = translation.textContent self.source = translation.getAttribute("source") if translation.hasAttribute("source") else "" self.targetLang = translation.getAttribute("targetLang") if translation.hasAttribute("targetLang") else "" self.audio = translation.getAttribute("audio") if translation.hasAttribute("audio") else "" explanationList = translation_xml.querySelectorAll("explanationList explanation") for explanation_dom in explanationList: explanation = Explanation() explanation.import_dom(explanation_dom) self.explanationList.append(explanation) self.tags = import_label_list("labelList label", translation_xml) def view(self, model): elements = [] if self.tags: tags = h("div.translation-tags", {}, [ h("span", {"attr": {"title": key}}, clean_label(value)) for key, value in self.tags]) elements.append(tags) elements.append(h("span.translation-text", {}, self.translation)) if self.source: elements.append(h("span.translation-source", {}, self.source)) if (self.explanationList): explanation_class = ".explanations" if self.translation else ".explanations.solo" explanations = [explanation.value for explanation in self.explanationList] elements.append(h("span{}".format(explanation_class), {}, ", ".join(explanations))) return h("div.translation-div", {"on": {"click": M.msg(M.ShowTranslationMenu, self) }}, elements) def is_empty(self): result = True result = result and self.translation == "" # next two are not checked as the also can not be deleted via gui # result = result and self.source == "" # result = result and self.targetLang == "" result = result and len(self.explanationList) == 0 result = result and len(self.tags) == 0 return result