All the code in one batch
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
from message.simple_messages import ListItemClick
|
||||
from message.simple_messages import NoReset, Reset, ModalNotOkClose
|
||||
from message.translation_edit import EditTranslation, MoveRight, MoveLeft, BinTranslation
|
||||
from message.show_messages import ShowMenu, ShowEditTranslation, ShowSenseLabelEdit, ShowSenseDefinitionEdit, ShowCommentEdit, ShowAddTranslation
|
||||
from message.simple_edits import EditSenseLabel, EditSenseDefinition, EditComment
|
||||
from message.message import msg
|
||||
|
||||
|
||||
def msg(message_class):
|
||||
from update import update
|
||||
def callback(arg):
|
||||
message_instance = message_class(arg)
|
||||
update.schedule(message_instance)
|
||||
return callback
|
||||
|
||||
|
||||
@@ -1,3 +1,21 @@
|
||||
from update import update
|
||||
|
||||
|
||||
class Message:
|
||||
def update_model(self, model):
|
||||
raise NotImplementedError("This message does not implement update_model method")
|
||||
raise NotImplementedError("This message does not implement update_model method")
|
||||
|
||||
def reset(self):
|
||||
return True
|
||||
|
||||
|
||||
class ClickMessage(Message):
|
||||
def __init__(self, event):
|
||||
event.stopPropagation();
|
||||
|
||||
|
||||
def msg(message_class, params):
|
||||
def callback(event):
|
||||
message_instance = message_class(event, params)
|
||||
update.schedule(message_instance)
|
||||
return callback
|
||||
|
||||
76
src/message/show_messages.py
Normal file
76
src/message/show_messages.py
Normal file
@@ -0,0 +1,76 @@
|
||||
from message.message import Message, ClickMessage
|
||||
from message.translation_edit import AddTranslation ,EditTranslation
|
||||
|
||||
import modals
|
||||
|
||||
|
||||
class GenericShowModal(ClickMessage):
|
||||
def __init__(self, event, arg):
|
||||
super().__init__(event)
|
||||
self.arg = arg
|
||||
|
||||
def update_model(self, model):
|
||||
model.modal_shown = True
|
||||
|
||||
|
||||
class ShowMenu(ClickMessage):
|
||||
def __init__(self, event, translation):
|
||||
super().__init__(event)
|
||||
self.menu_location = (event.pageX, event.pageY)
|
||||
self.translation = translation
|
||||
|
||||
def update_model(self, model):
|
||||
model.menu_location = self.menu_location
|
||||
model.menu_shown = True
|
||||
model.translation = self.translation
|
||||
|
||||
|
||||
class ShowSenseLabelEdit(GenericShowModal):
|
||||
def update_model(self, model):
|
||||
super().update_model(model)
|
||||
model.sense = self.arg
|
||||
model.modal = modals.edit_sense_label(self.arg)
|
||||
|
||||
|
||||
class ShowSenseDefinitionEdit(GenericShowModal):
|
||||
def update_model(self, model):
|
||||
super().update_model(model)
|
||||
model.sense = self.arg
|
||||
model.modal = modals.edit_sense_definition(self.arg)
|
||||
|
||||
|
||||
class ShowCommentEdit(ClickMessage):
|
||||
def update_model(self, model):
|
||||
model.modal_shown = True
|
||||
model.modal = modals.edit_comment(model.entry.comment)
|
||||
|
||||
|
||||
class ShowEditTranslation(GenericShowModal):
|
||||
def update_model(self, model):
|
||||
model.modal_shown = True
|
||||
|
||||
# I need to get number of all clusters and cluster of self.arg
|
||||
translation = self.arg
|
||||
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!
|
||||
model.modal = modals.edit_translation(translation, cidx, num_clusters, EditTranslation, (translation, cidx))
|
||||
return
|
||||
|
||||
console.log("Should not be here!")
|
||||
|
||||
|
||||
class ShowAddTranslation(GenericShowModal):
|
||||
def update_model(self, model):
|
||||
model.modal_shown = True
|
||||
chosen_sense = self.arg
|
||||
|
||||
for sense in model.entry.senses:
|
||||
if sense == chosen_sense:
|
||||
model.modal = modals.edit_translation(sense, -1, len(sense.translations), AddTranslation, sense)
|
||||
return
|
||||
|
||||
console.log("Should not be here!")
|
||||
29
src/message/simple_edits.py
Normal file
29
src/message/simple_edits.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from message.message import Message
|
||||
from browser import document
|
||||
from model import Sense
|
||||
|
||||
|
||||
class SimpleEditMessage(Message):
|
||||
def __init__(self, event, prop):
|
||||
input_element = document.getElementById("modal-input")
|
||||
self.new_text = input_element.value
|
||||
self.prop = prop
|
||||
|
||||
|
||||
class EditSenseLabel(SimpleEditMessage):
|
||||
def update_model(self, model):
|
||||
sense = self.prop
|
||||
assert(type(sense) is Sense)
|
||||
sense.label = self.new_text
|
||||
|
||||
|
||||
class EditSenseDefinition(SimpleEditMessage):
|
||||
def update_model(self, model):
|
||||
sense = self.prop
|
||||
assert(type(sense) is Sense)
|
||||
sense.definition = self.new_text
|
||||
|
||||
|
||||
class EditComment(SimpleEditMessage):
|
||||
def update_model(self, model):
|
||||
model.entry.comment = self.new_text
|
||||
@@ -1,10 +1,29 @@
|
||||
from message.message import Message
|
||||
from message.message import Message, ClickMessage, msg
|
||||
from browser import window
|
||||
import modals
|
||||
|
||||
|
||||
class ListItemClick(Message):
|
||||
def __init__(self, num):
|
||||
self.num = num
|
||||
|
||||
class Reset(ClickMessage):
|
||||
def update_model(self, model):
|
||||
print(self.num)
|
||||
model.names.splice(self.num, 1)
|
||||
pass
|
||||
|
||||
|
||||
class NoReset(Reset):
|
||||
def reset(self):
|
||||
return False
|
||||
|
||||
|
||||
# a "hack" message for reseting modals #
|
||||
# everytime a modal closes, run 100ms later model.modal = []
|
||||
# this is done to achieve nice close animation
|
||||
# after setting model.modal, do view update to actually update the DOM
|
||||
class _ModalResetDelayed(Message):
|
||||
def update_model(self, model):
|
||||
model.modal = []
|
||||
|
||||
class ModalNotOkClose(Reset):
|
||||
def update_model(self, model):
|
||||
# msg just creates a callback, need to actually run it!
|
||||
window.setTimeout(lambda: msg(_ModalResetDelayed)(None), 100)
|
||||
|
||||
|
||||
|
||||
113
src/message/translation_edit.py
Normal file
113
src/message/translation_edit.py
Normal file
@@ -0,0 +1,113 @@
|
||||
from message.message import Message, ClickMessage
|
||||
from browser import document, window
|
||||
from model.translation import TAGS, NewTranslation
|
||||
|
||||
|
||||
class TranslationActionMessage(ClickMessage):
|
||||
def __init__(self, event, translation):
|
||||
super().__init__(event)
|
||||
self.translation = translation
|
||||
|
||||
|
||||
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(TranslationActionMessage):
|
||||
def __init__(self, _, prop):
|
||||
self.translation, self.old_cluster_idx = prop
|
||||
|
||||
def update_model(self, model):
|
||||
self.translation.translation = document.getElementById("etv").value;
|
||||
for tag in TAGS.keys():
|
||||
select = document.getElementById("{}-s".format(tag));
|
||||
other = document.getElementById("{}-o".format(tag));
|
||||
|
||||
if other.value:
|
||||
self.translation.tags[tag] = other.value
|
||||
elif select.selectedIndex > 0:
|
||||
self.translation.tags[tag] = select.options[select.selectedIndex].text
|
||||
else:
|
||||
if tag in self.translation.tags:
|
||||
del self.translation.tags[tag]
|
||||
|
||||
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(TranslationActionMessage):
|
||||
def update_model(self, model):
|
||||
(_, _, idx), (_, cluster) = get_translation_location(model.entry, self.translation)
|
||||
if idx != len(cluster) - 1:
|
||||
cluster[idx], cluster[idx + 1] = cluster[idx + 1], cluster[idx]
|
||||
model.translation = None
|
||||
|
||||
|
||||
class MoveLeft(TranslationActionMessage):
|
||||
def update_model(self, model):
|
||||
(_, _, idx), (_, cluster) = get_translation_location(model.entry, self.translation)
|
||||
if idx != 0 and len(cluster) > 1:
|
||||
cluster[idx], cluster[idx - 1] = cluster[idx - 1], cluster[idx]
|
||||
model.translation = None
|
||||
|
||||
|
||||
class BinTranslation(TranslationActionMessage):
|
||||
def update_model(self, model):
|
||||
(_, cidx, tidx), (sense, cluster) = get_translation_location(model.entry, self.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 __init__(self, _, prop):
|
||||
self.translation = NewTranslation()
|
||||
self.old_cluster_idx = -1
|
||||
self.sense = prop
|
||||
|
||||
def handle_cluster_change(self, new_cluster_idx, _):
|
||||
# we need to cheat here
|
||||
# sense was actually given in constructor
|
||||
# we make a dummy cluster, cluster_idx and translation_idx
|
||||
# we give a correct new_cluster_idx
|
||||
self.do_cluster_change(self.sense, [None, None], None, None, new_cluster_idx)
|
||||
|
||||
Reference in New Issue
Block a user