Unified searching for translation location and cluster nums back in example translations

This commit is contained in:
Ozbolt Menegatti 2020-01-26 13:35:00 +01:00
parent 747cd694a7
commit b860fa4807
3 changed files with 40 additions and 65 deletions

View File

@ -46,40 +46,27 @@ class ShowExampleEdit(ClickMessage):
class ShowEditTranslation(ClickMessage): class ShowEditTranslation(ClickMessage):
def update_model(self, model): def update_model(self, model):
# I need to get number of all clusters and cluster of self.arg
translation = self.get_arg(0, Translation) 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
# Get translation location
(cidx, idx), (parent, cluster) = EditTranslation.get_translation_location(model.entry, translation)
# if here, that his must be example translation
translation.make_copy() translation.make_copy()
num_clusters = len(parent.translations)
model.modal_set(lambda: modals.edit_translation( model.modal_set(lambda: modals.edit_translation(
translation, None, EditTranslation(translation, -1))) translation, cidx, num_clusters, EditTranslation(translation, cidx)))
class ShowAddTranslation(ClickMessage): class ShowAddTranslation(ClickMessage):
def update_model(self, model): def update_model(self, model):
chosen_sense_or_example = self.get_arg(0) 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 = Translation.new_empty()
translation.make_copy() translation.make_copy()
model.modal_set(lambda: modals.edit_translation( model.modal_set(lambda: modals.edit_translation(
translation, translation,
cluster, -1,
len(chosen_sense_or_example.translations),
AddTranslation(translation, -1, chosen_sense_or_example))) AddTranslation(translation, -1, chosen_sense_or_example)))

View File

@ -6,25 +6,6 @@ from model.translation import Translation
from model.sense import Sense 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): class EditTranslation(DataChgClickMessage):
def update_model(self, model): def update_model(self, model):
@ -40,19 +21,33 @@ class EditTranslation(DataChgClickMessage):
new_cluster_idx = int(document.getElementById("cluster-num").value) - 1 new_cluster_idx = int(document.getElementById("cluster-num").value) - 1
self.handle_cluster_change(new_cluster_idx, model) 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): def handle_cluster_change(self, new_cluster_idx, model):
if self.old_cluster_idx == new_cluster_idx: if self.old_cluster_idx == new_cluster_idx:
return return
# first, find out the correct sense (cidx, tidx), (parent, cluster) = EditTranslation.get_translation_location(model.entry, self.translation)
for sense in model.entry.senses: self.do_cluster_change(parent, cluster, cidx, tidx, new_cluster_idx)
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
def do_cluster_change(self, sense_or_example, 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 # remove the translation from the old cluster
@ -76,7 +71,7 @@ class EditTranslation(DataChgClickMessage):
class MoveRight(DataChgClickMessage): class MoveRight(DataChgClickMessage):
def update_model(self, model): def update_model(self, model):
translation = self.get_arg(0, Translation) 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: if idx != len(cluster) - 1:
cluster[idx], cluster[idx + 1] = cluster[idx + 1], cluster[idx] cluster[idx], cluster[idx + 1] = cluster[idx + 1], cluster[idx]
model.translation = None model.translation = None
@ -85,7 +80,7 @@ class MoveRight(DataChgClickMessage):
class MoveLeft(DataChgClickMessage): class MoveLeft(DataChgClickMessage):
def update_model(self, model): def update_model(self, model):
translation = self.get_arg(0, Translation) 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: if idx != 0 and len(cluster) > 1:
cluster[idx], cluster[idx - 1] = cluster[idx - 1], cluster[idx] cluster[idx], cluster[idx - 1] = cluster[idx - 1], cluster[idx]
model.translation = None model.translation = None
@ -94,7 +89,7 @@ class MoveLeft(DataChgClickMessage):
class BinTranslation(DataChgClickMessage): class BinTranslation(DataChgClickMessage):
def update_model(self, model): def update_model(self, model):
translation = self.get_arg(0, Translation) 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: if len(cluster) == 1:
# remove empty cluster # remove empty cluster
sense.translations.splice(cidx, 1) sense.translations.splice(cidx, 1)

View File

@ -3,27 +3,20 @@ import message
from view.modal_templates import * from view.modal_templates import *
def edit_translation(translation, cluster_info, cls): def edit_translation(translation, cluster_idx, num_clusters, cls):
def split_line2(left, right, display): def split_line2(left, right):
style = {"style": {"display": "initial" if display else "none"}} return h("div.flex.two", {}, [
return h("div.flex.two", style, [
h("span.third.span-left-of-input", {}, left), h("span.two-third", {}, right)]) h("span.third.span-left-of-input", {}, left), h("span.two-third", {}, right)])
# first line: transalation itself # first line: transalation itself
content = [split_line2("Prevedek:", 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:", 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 # cluster number
options = [h("option", {"props": {"selected": idx == cluster_idx}}, str(idx + 1)) for idx in range(num_clusters + 1)] 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.append(h("h4", {}, "Tags"))
content.extend(label_list_editor(translation.copy().tags, message.AddToLabelList(translation.copy().tags))) content.extend(label_list_editor(translation.copy().tags, message.AddToLabelList(translation.copy().tags)))