lexonomy_custom_editor/src/message/translation_edit.py

114 lines
4.6 KiB
Python
Raw Normal View History

from message.message import Message
from message.simple_messages import DataChgClickMessage
import message.common_accessors as common_accessors
2019-11-11 22:04:45 +00:00
from browser import document, window
2019-11-17 21:08:53 +00:00
from model.translation import Translation
from model.sense import Sense
2019-11-11 22:04:45 +00:00
class EditTranslation(DataChgClickMessage):
2019-11-11 22:04:45 +00:00
def update_model(self, model):
self.translation = self.get_arg(0, Translation)
self.old_cluster_idx = self.get_arg(1, int)
self.translation.translation = document.getElementById("etv").value
self.translation.explanation = document.getElementById("ete").value
# common_accessors.label_list_getter()
self.translation.tags = common_accessors.label_list_getter()
2019-11-11 22:04:45 +00:00
2020-01-26 16:10:27 +00:00
# check if empty, remove!
if self.translation.is_empty():
model.entry.remove_translation(self.translation)
return
new_cluster_idx = int(document.getElementById("cluster-num").value) - 1
2019-11-11 22:04:45 +00:00
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...")
2019-11-11 22:04:45 +00:00
def handle_cluster_change(self, new_cluster_idx, model):
if self.old_cluster_idx == new_cluster_idx:
return
(cidx, tidx), (parent, cluster) = EditTranslation.get_translation_location(model.entry, self.translation)
self.do_cluster_change(parent, cluster, cidx, tidx, new_cluster_idx)
2019-11-11 22:04:45 +00:00
def do_cluster_change(self, sense_or_example, cluster, cidx, tidx, new_cluster_idx):
2019-11-11 22:04:45 +00:00
# remove the translation from the old cluster
cluster.splice(tidx, 1)
# we maybe are creating a new cluster, handle that
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:
2019-11-11 22:04:45 +00:00
# lets append the translation to new cluster
sense_or_example.translations[new_cluster_idx].append(self.translation)
2019-11-11 22:04:45 +00:00
else:
raise ValueError("Bad new cluster idx :(")
# we still hold cluster reference, check if empty and remove if necessary
2019-11-11 22:04:45 +00:00
# we cant do this earlier since indexes change and yeah, fun stuff
if len(cluster) == 0:
sense_or_example.translations.splice(cidx, 1)
2019-11-11 22:04:45 +00:00
class MoveRight(DataChgClickMessage):
2019-11-11 22:04:45 +00:00
def update_model(self, model):
translation = self.get_arg(0, Translation)
(_, idx), (_, cluster) = EditTranslation.get_translation_location(model.entry, translation)
2019-11-11 22:04:45 +00:00
if idx != len(cluster) - 1:
cluster[idx], cluster[idx + 1] = cluster[idx + 1], cluster[idx]
model.translation = None
class MoveLeft(DataChgClickMessage):
2019-11-11 22:04:45 +00:00
def update_model(self, model):
translation = self.get_arg(0, Translation)
(_, idx), (_, cluster) = EditTranslation.get_translation_location(model.entry, translation)
2019-11-11 22:04:45 +00:00
if idx != 0 and len(cluster) > 1:
cluster[idx], cluster[idx - 1] = cluster[idx - 1], cluster[idx]
model.translation = None
class BinTranslation(DataChgClickMessage):
2019-11-11 22:04:45 +00:00
def update_model(self, model):
translation = self.get_arg(0, Translation)
(cidx, tidx), (sense, cluster) = EditTranslation.get_translation_location(model.entry, translation)
2019-11-11 22:04:45 +00:00
if len(cluster) == 1:
# remove empty cluster
sense.translations.splice(cidx, 1)
else:
cluster.splice(tidx, 1)
model.translation = None
class AddTranslation(EditTranslation):
2019-11-11 22:04:45 +00:00
def handle_cluster_change(self, new_cluster_idx, _):
# we need to cheat here
# sense was actually given in constructor in third place
2019-11-11 22:04:45 +00:00
# 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), [None, None], None, None, new_cluster_idx)
2019-11-11 22:04:45 +00:00