From b860fa4807ead9ca0362eb81176f64002cc3e58e Mon Sep 17 00:00:00 2001 From: Ozbolt Menegatti Date: Sun, 26 Jan 2020 13:35:00 +0100 Subject: [PATCH] Unified searching for translation location and cluster nums back in example translations --- src/message/show_messages.py | 27 ++++----------- src/message/translation_edit.py | 59 +++++++++++++++------------------ src/view/modals.py | 19 ++++------- 3 files changed, 40 insertions(+), 65 deletions(-) diff --git a/src/message/show_messages.py b/src/message/show_messages.py index 27270ef..6687631 100644 --- a/src/message/show_messages.py +++ b/src/message/show_messages.py @@ -46,40 +46,27 @@ class ShowExampleEdit(ClickMessage): class ShowEditTranslation(ClickMessage): def update_model(self, model): - # I need to get number of all clusters and cluster of self.arg translation = self.get_arg(0, Translation) - for sense in model.entry.senses: - num_clusters = len(sense.translations) - for cidx, cluster in enumerate(sense.translations): - for t in cluster: - if t == translation: - # fount the one! - translation.make_copy() - model.modal_set(lambda: modals.edit_translation( - translation, (cidx, num_clusters), EditTranslation(translation, cidx))) - return - - # if here, that his must be example translation + # Get translation location + (cidx, idx), (parent, cluster) = EditTranslation.get_translation_location(model.entry, translation) + translation.make_copy() + num_clusters = len(parent.translations) model.modal_set(lambda: modals.edit_translation( - translation, None, EditTranslation(translation, -1))) + translation, cidx, num_clusters, EditTranslation(translation, cidx))) class ShowAddTranslation(ClickMessage): def update_model(self, model): chosen_sense_or_example = self.get_arg(0) - 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, + -1, + len(chosen_sense_or_example.translations), AddTranslation(translation, -1, chosen_sense_or_example))) diff --git a/src/message/translation_edit.py b/src/message/translation_edit.py index 1c50bb3..a9c1d57 100644 --- a/src/message/translation_edit.py +++ b/src/message/translation_edit.py @@ -6,25 +6,6 @@ from model.translation import Translation from model.sense import Sense -def get_translation_location(entry, translation): - 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): def update_model(self, model): @@ -39,20 +20,34 @@ class EditTranslation(DataChgClickMessage): new_cluster_idx = int(document.getElementById("cluster-num").value) - 1 self.handle_cluster_change(new_cluster_idx, model) + + @staticmethod + def get_translation_location(entry, translation): + def find_in_clusters(parent): + for ci, cluster in enumerate(parent.translations): + for ti, search_translation in enumerate(cluster): + if search_translation == translation: + return (ci, ti), (parent, cluster) + return None + + for sense in entry.senses: + res = find_in_clusters(sense) + if res is not None: + return res + for example in sense.examples: + res = find_in_clusters(example) + if res is not None: + return res + + window.console.log("should not be here...") + def handle_cluster_change(self, new_cluster_idx, model): if self.old_cluster_idx == new_cluster_idx: return - - # first, find out the correct sense - for sense in model.entry.senses: - for cidx, cluster in enumerate(sense.translations): - for tidx, t in enumerate(cluster): - if t == self.translation: - #found, lets do whatever needs to be done - self.do_cluster_change(sense, cluster, cidx, tidx, new_cluster_idx) - # we are done, lets return - return + + (cidx, tidx), (parent, cluster) = EditTranslation.get_translation_location(model.entry, self.translation) + self.do_cluster_change(parent, 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 @@ -76,7 +71,7 @@ class EditTranslation(DataChgClickMessage): class MoveRight(DataChgClickMessage): def update_model(self, model): translation = self.get_arg(0, Translation) - (_, _, idx), (_, cluster) = get_translation_location(model.entry, translation) + (_, idx), (_, cluster) = EditTranslation.get_translation_location(model.entry, translation) if idx != len(cluster) - 1: cluster[idx], cluster[idx + 1] = cluster[idx + 1], cluster[idx] model.translation = None @@ -85,7 +80,7 @@ class MoveRight(DataChgClickMessage): class MoveLeft(DataChgClickMessage): def update_model(self, model): translation = self.get_arg(0, Translation) - (_, _, idx), (_, cluster) = get_translation_location(model.entry, translation) + (_, idx), (_, cluster) = EditTranslation.get_translation_location(model.entry, translation) if idx != 0 and len(cluster) > 1: cluster[idx], cluster[idx - 1] = cluster[idx - 1], cluster[idx] model.translation = None @@ -94,7 +89,7 @@ class MoveLeft(DataChgClickMessage): class BinTranslation(DataChgClickMessage): def update_model(self, model): translation = self.get_arg(0, Translation) - (_, cidx, tidx), (sense, cluster) = get_translation_location(model.entry, translation) + (cidx, tidx), (sense, cluster) = EditTranslation.get_translation_location(model.entry, translation) if len(cluster) == 1: # remove empty cluster sense.translations.splice(cidx, 1) diff --git a/src/view/modals.py b/src/view/modals.py index a325237..71df35c 100644 --- a/src/view/modals.py +++ b/src/view/modals.py @@ -3,27 +3,20 @@ import message from view.modal_templates import * -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, [ +def edit_translation(translation, cluster_idx, num_clusters, cls): + def split_line2(left, right): + return h("div.flex.two", {}, [ 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}}, ""), True), + h("input#etv", {"props": {"type": "text", "value": translation.translation}}, "")), split_line2("Razlaga:", - h("input#ete", {"props": {"type": "text", "value": translation.explanation}}, ""), True)] + h("input#ete", {"props": {"type": "text", "value": translation.explanation}}, ""))] - 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), show_cluster_options)) + content.append(split_line2("Stevilka gruce:", h("select#cluster-num", {}, options))) content.append(h("h4", {}, "Tags")) content.extend(label_list_editor(translation.copy().tags, message.AddToLabelList(translation.copy().tags)))