You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lexonomy_custom_editor/src/message/translation_edit.py

121 lines
4.8 KiB

from message.message import Message
from message.simple_messages import DataChgClickMessage
import message.common_accessors as common_accessors
from browser import document, window
from model.translation import Translation
from model.sense import Sense
from model.explanation import Explanation
class EditTranslation(DataChgClickMessage):
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
# This could be dangerous if double_list_getter is getting data from any other list as well.
explanations = common_accessors.double_list_getter('value', 'language', True)
self.translation.explanationList = []
for entry in explanations:
explanation = Explanation()
explanation.value = entry.value
explanation.language = entry.language
self.translation.explanationList.append(explanation)
# common_accessors.label_list_getter()
self.translation.tags = common_accessors.label_list_getter()
# 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
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
(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
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:
# lets append the translation to new cluster
sense_or_example.translations[new_cluster_idx].append(self.translation)
else:
raise ValueError("Bad new cluster idx :(")
# 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_or_example.translations.splice(cidx, 1)
class MoveRight(DataChgClickMessage):
def update_model(self, model):
translation = self.get_arg(0, 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
class MoveLeft(DataChgClickMessage):
def update_model(self, model):
translation = self.get_arg(0, 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
class BinTranslation(DataChgClickMessage):
def update_model(self, model):
translation = self.get_arg(0, Translation)
(cidx, tidx), (sense, cluster) = EditTranslation.get_translation_location(model.entry, translation)
if len(cluster) == 1:
# remove empty cluster
sense.translations.splice(cidx, 1)
else:
cluster.splice(tidx, 1)
model.translation = None
class AddTranslation(EditTranslation):
def handle_cluster_change(self, new_cluster_idx, _):
# we need to cheat here
# 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), [None, None], None, None, new_cluster_idx)