|
|
|
from message.message import Message, ClickMessage
|
|
|
|
import message.common_accessors as common_accessors
|
|
|
|
from browser import document, window
|
|
|
|
from model.translation import TAGS, Translation
|
|
|
|
from model.sense import Sense
|
|
|
|
|
|
|
|
|
|
|
|
def get_translation_location(entry, translation):
|
|
|
|
for si, sense in enumerate(entry.senses):
|
|
|
|
for ci, cluster in enumerate(sense.translations):
|
|
|
|
for ti, search_translation in enumerate(cluster):
|
|
|
|
if search_translation == translation:
|
|
|
|
return (si, ci, ti), (sense, cluster)
|
|
|
|
|
|
|
|
|
|
|
|
class EditTranslation(ClickMessage):
|
|
|
|
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()
|
|
|
|
|
|
|
|
new_cluster_idx = int(document.getElementById("cluster-num").value) - 1
|
|
|
|
self.handle_cluster_change(new_cluster_idx, model)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def do_cluster_change(self, sense, 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.translations) == new_cluster_idx:
|
|
|
|
sense.translations.append([self.translation])
|
|
|
|
elif len(sense.translations) > new_cluster_idx:
|
|
|
|
# lets append the translation to new cluster
|
|
|
|
sense.translations[new_cluster_idx].append(self.translation)
|
|
|
|
else:
|
|
|
|
raise ValueError("Bad new cluster idx :(")
|
|
|
|
|
|
|
|
# we still hols 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.translations.splice(cidx, 1)
|
|
|
|
|
|
|
|
|
|
|
|
class MoveRight(ClickMessage):
|
|
|
|
def update_model(self, model):
|
|
|
|
translation = self.get_arg(0, Translation)
|
|
|
|
(_, _, idx), (_, cluster) = 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(ClickMessage):
|
|
|
|
def update_model(self, model):
|
|
|
|
translation = self.get_arg(0, Translation)
|
|
|
|
(_, _, idx), (_, cluster) = 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(ClickMessage):
|
|
|
|
def update_model(self, model):
|
|
|
|
translation = self.get_arg(0, Translation)
|
|
|
|
(_, cidx, tidx), (sense, cluster) = 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, Sense), [None, None], None, None, new_cluster_idx)
|
|
|
|
|