WIP: on_event instead of constructor and constructor acceppting args

This commit is contained in:
Ozbolt Menegatti 2019-11-17 21:30:30 +01:00
parent 3cfcef6253
commit 0fd32205cc
4 changed files with 77 additions and 79 deletions

View File

@ -1,16 +1,39 @@
from update import update from update import update
from browser import window
class Message: class Message:
def __init__(self, *args):
self._args = args
self.error = None
def update_model(self, model): 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 on_event(self, event):
pass
def get_arg(self, n, argtype=None):
if n > len(self._args):
window.console.log("not enough arguments: {} <= {}".format(n, len(self._args)))
result = self._args[n]
if argtype is not None:
if not type(result) is argtype:
window.console.trace("Error type check!")
return None
return result
def add_arg(self, arg):
self._args.append(arg)
def reset(self): def reset(self):
return True return True
class ClickMessage(Message): class ClickMessage(Message):
def __init__(self, event): def on_event(self, event):
event.stopPropagation() event.stopPropagation()

View File

@ -5,79 +5,68 @@ from model.sense import NewSense
from view import modals from view 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): class ShowMenu(ClickMessage):
def __init__(self, event, translation): def on_event(self, event):
super().__init__(event)
location_x = event.currentTarget.offsetLeft location_x = event.currentTarget.offsetLeft
location_y = event.currentTarget.offsetTop + event.currentTarget.offsetHeight location_y = event.currentTarget.offsetTop + event.currentTarget.offsetHeight
self.menu_location = (location_x, location_y) self.menu_location = (location_x, location_y)
console.log(self.menu_location, event.currentTarget) super().on_event(event)
self.translation = translation
def update_model(self, model): def update_model(self, model):
model.menu_location = self.menu_location model.menu_location = self.menu_location
model.menu_shown = True model.menu_shown = True
model.translation = self.translation model.translation = self.get_arg(0, Translation)
class ShowSenseLabelEdit(GenericShowModal): class ShowSenseLabelEdit(ClickMessage):
def update_model(self, model): def update_model(self, model):
super().update_model(model) model.modal_shown = True
model.sense = self.arg model.sense = self.get_arg(0, Sense)
model.sense.make_copy() model.sense.make_copy()
model.modal = lambda: modals.edit_sense_label(model.sense) model.modal = lambda: modals.edit_sense_label(model.sense)
class ShowSenseDefinitionEdit(GenericShowModal): class ShowSenseDefinitionEdit(ClickMessage):
def update_model(self, model): def update_model(self, model):
super().update_model(model) model.modal_shown = True
model.sense = self.arg model.sense = self.get_arg(0, Sense)
model.modal = lambda: modals.edit_sense_definition(self.arg) model.modal = lambda: modals.edit_sense_definition(model.sense)
class ShowSenseAdd(GenericShowModal): class ShowSenseAdd(ClickMessage):
def update_model(self, model): def update_model(self, model):
super().update_model(model) model.modal_shown = True
model.modal = lambda: modals.add_sense(NewSense()) model.modal = lambda: modals.add_sense()
# I don't need GenericShowModal since there is only one comment
class ShowCommentEdit(ClickMessage): class ShowCommentEdit(ClickMessage):
def update_model(self, model): def update_model(self, model):
model.modal_shown = True model.modal_shown = True
model.modal = lambda: modals.edit_comment(model.entry.comment) model.modal = lambda: modals.edit_comment(model.entry.comment)
class ShowExampleEdit(GenericShowModal): class ShowExampleEdit(ClickMessage):
def update_model(self, model): def update_model(self, model):
super().update_model(model) model.modal_shown = True
model.modal = lambda: modals.edit_example(self.arg) example = self.get_arg(0, Example)
model.modal = lambda: modals.edit_example(example)
class ShowExampleTranslationEdit(GenericShowModal): class ShowExampleTranslationEdit(ClickMessage):
def update_model(self, model): def update_model(self, model):
super().update_model(model) model.modal_shown = True
example = self.arg example = self.get_arg(0, Example)
example.make_copy() example.make_copy()
model.modal = lambda: modals.edit_example_translation(example) model.modal = lambda: modals.edit_example_translation(example)
class ShowEditTranslation(GenericShowModal): class ShowEditTranslation(ClickMessage):
def update_model(self, model): def update_model(self, model):
model.modal_shown = True model.modal_shown = True
# I need to get number of all clusters and cluster of self.arg # I need to get number of all clusters and cluster of self.arg
translation = self.arg translation = self.get_arg(0, Translation)
for sense in model.entry.senses: for sense in model.entry.senses:
num_clusters = len(sense.translations) num_clusters = len(sense.translations)
for cidx, cluster in enumerate(sense.translations): for cidx, cluster in enumerate(sense.translations):
@ -91,10 +80,10 @@ class ShowEditTranslation(GenericShowModal):
console.log("Should not be here!") console.log("Should not be here!")
class ShowAddTranslation(GenericShowModal): class ShowAddTranslation(ClickMessage):
def update_model(self, model): def update_model(self, model):
model.modal_shown = True model.modal_shown = True
chosen_sense = self.arg chosen_sense = self.get_arg(0, Sense)
for sense in model.entry.senses: for sense in model.entry.senses:
if sense == chosen_sense: if sense == chosen_sense:

View File

@ -5,40 +5,34 @@ from browser import document
from model import Sense, Example from model import Sense, Example
class SimpleEditMessage(Message): class QuestionMessage(Message):
def __init__(self, event, prop): def on_event(self, event):
input_element = document.getElementById("modal-input") input_element = document.getElementById("modal-question")
self.new_text = input_element.value self.new_text = input_element.value
self.prop = prop
class EditSenseLabel(Message): class EditSenseLabel(Message):
def __init__(self, _, prop):
assert(type(prop) is Sense)
self.sense = prop
def update_model(self, model): def update_model(self, model):
self.sense.labels = [] sense = self.get_arg(0, Sense)
sense.labels = []
for input_el in document.getElementsByClassName("sense-edit-input"): for input_el in document.getElementsByClassName("sense-edit-input"):
new_sense_label = input_el.value new_sense_label = input_el.value
if new_sense_label != "": if new_sense_label != "":
self.sense.labels.append(new_sense_label) sense.labels.append(new_sense_label)
class AddSenseLabel(NoReset): class AddSenseLabel(NoReset):
def __init__(self, _, prop):
assert(type(prop) is Sense)
self.sense = prop
def update_model(self, model): def update_model(self, model):
sense = self.get_arg(0, Sense)
# just adding to the copy to show in the modal # just adding to the copy to show in the modal
self.sense.copy().labels.append("") sense.copy().labels.append("")
class AddExampleTranslation(NoReset): class AddExampleTranslation(NoReset):
def __init__(self, _, prop): def update_model(self, model):
assert(type(prop) is Example) example = self.get_arg(0, Example)
self.example = prop # just adding to the copy to show in the modal
example.copy().translations.append("")
def update_model(self, model): def update_model(self, model):
# just adding to the copy to show in the modal # just adding to the copy to show in the modal
@ -46,36 +40,31 @@ class AddExampleTranslation(NoReset):
class EditExampleTranslation(Message): class EditExampleTranslation(Message):
def __init__(self, _, prop):
assert(type(prop) is Example)
self.example = prop
def update_model(self, model): def update_model(self, model):
self.example.translations = common_accessors.generic_list_getter() example = self.get_arg(0, Example)
example.translations = common_accessors.generic_list_getter()
class AddSense(SimpleEditMessage): class AddSense(QuestionMessage):
def update_model(self, model): def update_model(self, model):
sense = Sense.new_empty() sense = Sense.new_empty()
sense.labels = [self.new_text] sense.labels = [self.new_text]
model.entry.senses.append(sense) model.entry.senses.append(sense)
class EditSenseDefinition(SimpleEditMessage): class EditSenseDefinition(QuestionMessage):
def update_model(self, model): def update_model(self, model):
sense = self.prop sense = self.get_arg(0, Sense)
assert(type(sense) is Sense)
sense.definition = self.new_text sense.definition = self.new_text
class EditComment(SimpleEditMessage): class EditComment(QuestionMessage):
def update_model(self, model): def update_model(self, model):
model.entry.comment = self.new_text model.entry.comment = self.new_text
class EditExample(SimpleEditMessage): class EditExample(QuestionMessage):
def update_model(self, model): def update_model(self, model):
example = self.prop example = self.get_arg(0, Example)
assert(type(example) is Example)
example.example = self.new_text example.example = self.new_text

View File

@ -3,12 +3,6 @@ from browser import document, window
from model.translation import TAGS, NewTranslation 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): def get_translation_location(entry, translation):
for si, sense in enumerate(entry.senses): for si, sense in enumerate(entry.senses):
for ci, cluster in enumerate(sense.translations): for ci, cluster in enumerate(sense.translations):
@ -72,25 +66,28 @@ class EditTranslation(TranslationActionMessage):
sense.translations.splice(cidx, 1) sense.translations.splice(cidx, 1)
class MoveRight(TranslationActionMessage): class MoveRight(ClickMessage):
def update_model(self, model): def update_model(self, model):
(_, _, idx), (_, cluster) = get_translation_location(model.entry, self.translation) translation = self.get_arg(0, Translation)
(_, _, idx), (_, cluster) = get_translation_location(model.entry, translation)
if idx != len(cluster) - 1: if idx != len(cluster) - 1:
cluster[idx], cluster[idx + 1] = cluster[idx + 1], cluster[idx] cluster[idx], cluster[idx + 1] = cluster[idx + 1], cluster[idx]
model.translation = None model.translation = None
class MoveLeft(TranslationActionMessage): class MoveLeft(ClickMessage):
def update_model(self, model): def update_model(self, model):
(_, _, idx), (_, cluster) = get_translation_location(model.entry, self.translation) translation = self.get_arg(0, Translation)
(_, _, idx), (_, cluster) = get_translation_location(model.entry, translation)
if idx != 0 and len(cluster) > 1: if idx != 0 and len(cluster) > 1:
cluster[idx], cluster[idx - 1] = cluster[idx - 1], cluster[idx] cluster[idx], cluster[idx - 1] = cluster[idx - 1], cluster[idx]
model.translation = None model.translation = None
class BinTranslation(TranslationActionMessage): class BinTranslation(ClickMessage):
def update_model(self, model): def update_model(self, model):
(_, cidx, tidx), (sense, cluster) = get_translation_location(model.entry, self.translation) translation = self.get_arg(0, Translation)
(_, cidx, tidx), (sense, cluster) = get_translation_location(model.entry, translation)
if len(cluster) == 1: if len(cluster) == 1:
# remove empty cluster # remove empty cluster
sense.translations.splice(cidx, 1) sense.translations.splice(cidx, 1)