2019-11-20 23:28:49 +00:00
|
|
|
from message.message import Message
|
|
|
|
from message.simple_messages import DataChgClickMessage
|
2019-11-17 20:33:58 +00:00
|
|
|
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
|
2019-11-17 20:33:58 +00:00
|
|
|
from model.sense import Sense
|
2019-11-11 22:04:45 +00:00
|
|
|
|
|
|
|
|
2019-11-20 21:25:31 +00:00
|
|
|
|
2019-11-20 23:28:49 +00:00
|
|
|
class EditTranslation(DataChgClickMessage):
|
2019-11-11 22:04:45 +00:00
|
|
|
def update_model(self, model):
|
2019-11-17 20:33:58 +00:00
|
|
|
self.translation = self.get_arg(0, Translation)
|
|
|
|
self.old_cluster_idx = self.get_arg(1, int)
|
|
|
|
|
2019-11-15 21:24:32 +00:00
|
|
|
self.translation.translation = document.getElementById("etv").value
|
|
|
|
self.translation.explanation = document.getElementById("ete").value
|
2019-11-17 20:33:58 +00:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2019-11-15 21:24:32 +00:00
|
|
|
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)
|
2020-01-26 12:35:00 +00:00
|
|
|
|
|
|
|
@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
|
2020-01-26 12:35:00 +00:00
|
|
|
|
|
|
|
(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
|
|
|
|
2020-01-23 21:51:15 +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
|
2020-01-23 21:51:15 +00:00
|
|
|
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
|
2020-01-23 21:51:15 +00:00
|
|
|
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 :(")
|
|
|
|
|
2020-01-23 21:51:15 +00:00
|
|
|
# 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:
|
2020-01-23 21:51:15 +00:00
|
|
|
sense_or_example.translations.splice(cidx, 1)
|
2019-11-11 22:04:45 +00:00
|
|
|
|
|
|
|
|
2019-11-20 23:28:49 +00:00
|
|
|
class MoveRight(DataChgClickMessage):
|
2019-11-11 22:04:45 +00:00
|
|
|
def update_model(self, model):
|
2019-11-17 20:30:30 +00:00
|
|
|
translation = self.get_arg(0, Translation)
|
2020-01-26 12:35:00 +00:00
|
|
|
(_, 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
|
|
|
|
|
|
|
|
|
2019-11-20 23:28:49 +00:00
|
|
|
class MoveLeft(DataChgClickMessage):
|
2019-11-11 22:04:45 +00:00
|
|
|
def update_model(self, model):
|
2019-11-17 20:30:30 +00:00
|
|
|
translation = self.get_arg(0, Translation)
|
2020-01-26 12:35:00 +00:00
|
|
|
(_, 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
|
|
|
|
|
|
|
|
|
2019-11-20 23:28:49 +00:00
|
|
|
class BinTranslation(DataChgClickMessage):
|
2019-11-11 22:04:45 +00:00
|
|
|
def update_model(self, model):
|
2019-11-17 20:30:30 +00:00
|
|
|
translation = self.get_arg(0, Translation)
|
2020-01-26 12:35:00 +00:00
|
|
|
(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
|
|
|
|
|
|
|
|
|
2020-01-02 09:39:12 +00:00
|
|
|
class AddTranslation(EditTranslation):
|
2019-11-11 22:04:45 +00:00
|
|
|
def handle_cluster_change(self, new_cluster_idx, _):
|
|
|
|
# we need to cheat here
|
2019-11-17 20:33:58 +00:00
|
|
|
# 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
|
2020-01-23 21:51:15 +00:00
|
|
|
self.do_cluster_change(self.get_arg(2), [None, None], None, None, new_cluster_idx)
|
2019-11-11 22:04:45 +00:00
|
|
|
|