diff --git a/res/main.less b/res/main.less index 42d005a..6ae765d 100644 --- a/res/main.less +++ b/res/main.less @@ -157,13 +157,16 @@ .example-dot, .example-rest { float: left; - ._hoverable(); } .example-arrow, .example-dot { margin-right: 1em; } + .example-dot, .example-text { + ._hoverable(); + } + .example-rest { border: 1px transparent solid; } diff --git a/src/export.py b/src/export.py index 6238e7d..d996263 100644 --- a/src/export.py +++ b/src/export.py @@ -105,14 +105,9 @@ def export_sense(doc, sense): definition_list.appendChild(definition) translation_container_list = doc.createElement("translationContainerList") + export_translation_list(doc, sense, translation_container_list) sense_xml.appendChild(translation_container_list) - for cidx, cluster in enumerate(sense.translations): - for translation in cluster: - translation_container = export_translation(doc, translation) - translation_container.setAttribute("cluster", str(cidx + 1)) - translation_container_list.appendChild(translation_container) - example_container_list = doc.createElement("exampleContainerList") sense_xml.appendChild(example_container_list) @@ -121,17 +116,18 @@ def export_sense(doc, sense): example_container_list.appendChild(example_container) example_container.appendChild(example.original_xml) - - translation_container = doc.createElement("translationContainer") - example_container.appendChild(translation_container) - - for t in example.translations: - translation = doc.createElement("translation") - translation.textContent = t - translation_container.appendChild(translation) + export_translation_list(doc, example, example_container) return sense_xml +def export_translation_list(doc, py_parent, xml_parent): + for cidx, cluster in enumerate(py_parent.translations): + for translation in cluster: + translation_container = export_translation(doc, translation) + translation_container.setAttribute("cluster", str(cidx + 1)) + xml_parent.appendChild(translation_container) + + def export_translation(doc, translation): translation_xml = doc.createElement("translationContainer") diff --git a/src/message/show_messages.py b/src/message/show_messages.py index a2fad15..d7eab08 100644 --- a/src/message/show_messages.py +++ b/src/message/show_messages.py @@ -94,25 +94,31 @@ class ShowEditTranslation(ClickMessage): # fount the one! translation.make_copy() model.modal_set(lambda: modals.edit_translation( - translation, cidx, num_clusters, EditTranslation(translation, cidx))) + translation, (cidx, num_clusters), EditTranslation(translation, cidx))) return + - console.log("Should not be here!") + # if here, that his must be example translation + translation.make_copy() + model.modal_set(lambda: modals.edit_translation( + translation, None, EditTranslation(translation, -1))) class ShowAddTranslation(ClickMessage): def update_model(self, model): - chosen_sense = self.get_arg(0, Sense) + chosen_sense_or_example = self.get_arg(0) - for sense in model.entry.senses: - if sense == chosen_sense: - translation = Translation.new_empty() - translation.make_copy() - model.modal_set(lambda: modals.edit_translation( - translation, -1, len(sense.translations), AddTranslation(translation, -1, sense))) - return - - console.log("Should not be here!") + if isinstance(chosen_sense_or_example, Sense): + cluster = (-1, len(chosen_sense_or_example.translations)) + else: + cluster = None + + translation = Translation.new_empty() + translation.make_copy() + model.modal_set(lambda: modals.edit_translation( + translation, + cluster, + AddTranslation(translation, -1, chosen_sense_or_example))) class ShowEntryLabelsEdit(ClickMessage): diff --git a/src/message/translation_edit.py b/src/message/translation_edit.py index ba29aec..1c50bb3 100644 --- a/src/message/translation_edit.py +++ b/src/message/translation_edit.py @@ -7,11 +7,23 @@ from model.sense import Sense def get_translation_location(entry, translation): - for si, sense in enumerate(entry.senses): - for ci, cluster in enumerate(sense.translations): + def find_in_clusters(si, clusters): + for ci, cluster in enumerate(clusters): for ti, search_translation in enumerate(cluster): if search_translation == translation: return (si, ci, ti), (sense, cluster) + return None + + for si, sense in enumerate(entry.senses): + res = find_in_clusters(si, sense.translations) + if res is not None: + return res + for example in sense.examples: + res = find_in_clusters(si, example.translations) + if res is not None: + return res + + window.console.log("should not be here...") class EditTranslation(DataChgClickMessage): @@ -42,23 +54,23 @@ class EditTranslation(DataChgClickMessage): # we are done, lets return return - def do_cluster_change(self, sense, cluster, cidx, tidx, new_cluster_idx): + def do_cluster_change(self, sense_or_example, cluster, cidx, tidx, new_cluster_idx): # remove the translation from the old cluster cluster.splice(tidx, 1) # we maybe are creating a new cluster, handle that - if len(sense.translations) == new_cluster_idx: - sense.translations.append([self.translation]) - elif len(sense.translations) > new_cluster_idx: + if len(sense_or_example.translations) == new_cluster_idx: + sense_or_example.translations.append([self.translation]) + elif len(sense_or_example.translations) > new_cluster_idx: # lets append the translation to new cluster - sense.translations[new_cluster_idx].append(self.translation) + sense_or_example.translations[new_cluster_idx].append(self.translation) else: raise ValueError("Bad new cluster idx :(") - # we still hols cluster reference, check if empty and remove if necessary + # we still hold cluster reference, check if empty and remove if necessary # we cant do this earlier since indexes change and yeah, fun stuff if len(cluster) == 0: - sense.translations.splice(cidx, 1) + sense_or_example.translations.splice(cidx, 1) class MoveRight(DataChgClickMessage): @@ -97,5 +109,5 @@ class AddTranslation(EditTranslation): # sense was actually given in constructor in third place # we make a dummy cluster, cluster_idx and translation_idx # we give a correct new_cluster_idx - self.do_cluster_change(self.get_arg(2, Sense), [None, None], None, None, new_cluster_idx) + self.do_cluster_change(self.get_arg(2), [None, None], None, None, new_cluster_idx) diff --git a/src/model/example.py b/src/model/example.py index 4e17269..8a3c0af 100644 --- a/src/model/example.py +++ b/src/model/example.py @@ -1,4 +1,5 @@ from model.editable import Editable +from model.translation import from_container_list class Example(Editable): def __init__(self, example_xml): @@ -15,7 +16,4 @@ class Example(Editable): self.original_xml = inner_xml self.text = inner_xml.textContent - - self.translations = [] - for translation in example_xml.querySelectorAll("translationContainer translation"): - self.translations.append(translation.textContent) + self.translations = from_container_list(example_xml.querySelectorAll("translationContainer")) diff --git a/src/model/sense.py b/src/model/sense.py index a762b2f..8646ae6 100644 --- a/src/model/sense.py +++ b/src/model/sense.py @@ -1,5 +1,5 @@ from model.example import Example -from model.translation import Translation +from model.translation import from_container_list from model.editable import Editable from model.tags import import_label_list @@ -15,18 +15,8 @@ class Sense(Editable): self.examples = [Example(example_xml) for example_xml in sense_xml.querySelectorAll("exampleContainerList exampleContainer")] - translations = [] - max_num_cluster = 0 - - for translation_xml in sense_xml.querySelectorAll("translationContainerList translationContainer"): - num_cluster = int(translation_xml.getAttribute("cluster")) - max_num_cluster = max(max_num_cluster, num_cluster) - translations.append((num_cluster, Translation(translation_xml))) - - self.translations = [[] for _ in range(max_num_cluster)] - for clusterNum, translation in translations: - self.translations[clusterNum - 1].append(translation) - + self.translations = from_container_list( + sense_xml.querySelectorAll("translationContainerList translationContainer")) def merge_labels(self): return ", ".join(val for _, val in self.labels) diff --git a/src/model/translation.py b/src/model/translation.py index 0046685..86ea168 100644 --- a/src/model/translation.py +++ b/src/model/translation.py @@ -2,6 +2,25 @@ from model.tags import import_label_list from model.editable import Editable +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) + translations.append((num_cluster, Translation(translation_xml))) + + result = [[] for _ in range(max_num_cluster)] + for clusterNum, translation in translations: + result[clusterNum - 1].append(translation) + + return result + + class Translation(Editable): def __init__(self, translation_xml): translation = translation_xml.querySelector("translation") diff --git a/src/view/modals.py b/src/view/modals.py index 1f75b9f..aa73c41 100644 --- a/src/view/modals.py +++ b/src/view/modals.py @@ -3,20 +3,27 @@ import message from view.modal_templates import * -def edit_translation(translation, cluster_idx, num_clusters, cls): - def split_line2(left, right): - return h("div.flex.two", {}, [ +def edit_translation(translation, cluster_info, cls): + def split_line2(left, right, display): + style = {"style": {"display": "initial" if display else "none"}} + return h("div.flex.two", style, [ h("span.third.span-left-of-input", {}, left), h("span.two-third", {}, right)]) # first line: transalation itself content = [split_line2("Prevedek:", - h("input#etv", {"props": {"type": "text", "value": translation.translation}}, "")), + h("input#etv", {"props": {"type": "text", "value": translation.translation}}, ""), True), split_line2("Razlaga:", - h("input#ete", {"props": {"type": "text", "value": translation.explanation}}, ""))] + h("input#ete", {"props": {"type": "text", "value": translation.explanation}}, ""), True)] + + if cluster_info is None: + cluster_idx, num_clusters, show_cluster_options = 0, 1, False + else: + cluster_idx, num_clusters = cluster_info + show_cluster_options = True # cluster number options = [h("option", {"props": {"selected": idx == cluster_idx}}, str(idx + 1)) for idx in range(num_clusters + 1)] - content.append(split_line2("Stevilka gruce:", h("select#cluster-num", {}, options))) + content.append(split_line2("Stevilka gruce:", h("select#cluster-num", {}, options), show_cluster_options)) content.append(h("h4", {}, "Tags")) content.extend(label_list_editor(translation.copy().tags, message.AddToLabelList(translation.copy().tags))) diff --git a/src/view/view.py b/src/view/view.py index a2f81a0..0df1f61 100644 --- a/src/view/view.py +++ b/src/view/view.py @@ -3,6 +3,7 @@ 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 @@ -88,7 +89,7 @@ class View: def view_sense(sense, senseNum, model): examples = [View.view_example(example, model) for example in sense.examples] - return h("div.elm-div", {}, [ + 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)) }}, [ @@ -96,6 +97,7 @@ class View: 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, model): @@ -105,21 +107,21 @@ class View: return h("div.example", {}, [ h("div.example-dot", {"on": {"click": msg(ShowExampleMenu(example))} }, "▣"), - h(example_tag, {"on": {"click": msg(ShowExampleEdit(example))} }, [ - h("span.example-text", {}, example.text), + h(example_tag, {}, [ + h("span.example-text", {"on": {"click": msg(ShowExampleEdit(example))}}, example.text), h("div.example-translation-list", {}, [ h("div.example-translation", {}, [ h("span.example-arrow", {}, "↪"), - h("span", {}, t)]) - for t in example.translations])])]) + vt + ]) for vt in View.view_translations(example.translations, example, model)])])]) @staticmethod - def view_translations(translations, sense, model): + 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(sense))}}, "+")) + result.append(h("button.add-button", {"on": {"click": msg(ShowAddTranslation(parent))}}, "+")) return result @staticmethod