From f50d812efb77169f691056dd0b7e0a45a82adb40 Mon Sep 17 00:00:00 2001 From: Ozbolt Menegatti Date: Thu, 21 Nov 2019 00:28:49 +0100 Subject: [PATCH] Finished refactoring menu showing, now also sense editing works! --- res/main.less | 1 + src/message/__init__.py | 6 +++-- src/message/message.py | 8 ------- src/message/sense_edit.py | 40 +++++++++++++++++++++++++++++++++ src/message/show_menu.py | 24 ++++++++++++++++++++ src/message/show_messages.py | 16 ++----------- src/message/simple_messages.py | 20 +++++++++++++++-- src/message/translation_edit.py | 18 ++++++--------- src/view/view.py | 13 ++++++----- 9 files changed, 104 insertions(+), 42 deletions(-) create mode 100644 src/message/sense_edit.py create mode 100644 src/message/show_menu.py diff --git a/res/main.less b/res/main.less index e85310d..2b53c2e 100644 --- a/res/main.less +++ b/res/main.less @@ -58,6 +58,7 @@ .sense-num { float: left; + ._hoverable(); } .sense { diff --git a/src/message/__init__.py b/src/message/__init__.py index eb286b8..bdc49cb 100644 --- a/src/message/__init__.py +++ b/src/message/__init__.py @@ -1,7 +1,9 @@ -from message.simple_messages import NoReset, Reset, ModalNotOkClose +from message.simple_messages import NoReset, Reset, ModalNotOkClose, ClickMessage, DataChgClickMessage from message.translation_edit import EditTranslation, MoveRight, MoveLeft, BinTranslation -from message.show_messages import ShowMenu, ShowEntryLabelsEdit, ShowEditTranslation, ShowSenseLabelEdit, ShowSenseDefinitionEdit, ShowCommentEdit, ShowAddTranslation, ShowExampleEdit, ShowExampleTranslationEdit, ShowVariantsEdit +from message.show_messages import ShowEntryLabelsEdit, ShowEditTranslation, ShowSenseLabelEdit, ShowSenseDefinitionEdit, ShowCommentEdit, ShowAddTranslation, ShowExampleEdit, ShowExampleTranslationEdit, ShowVariantsEdit from message.simple_edits import EditSenseLabel, EditSenseDefinition, EditComment, AddSenseLabel, AddSense, EditExample, AddExampleTranslation, EditExampleTranslation, AddToLabelList, AddToGenericList, EditVariants, EditEntryLabels +from message.show_menu import ShowTranslationMenu, ShowSenseMenu +from message.sense_edit import SenseMoveUp, SenseMoveDown, SenseBin from message.message import msg diff --git a/src/message/message.py b/src/message/message.py index 3095763..0106608 100644 --- a/src/message/message.py +++ b/src/message/message.py @@ -34,14 +34,6 @@ class Message: def reset(self): return True - -class ClickMessage(Message): - def on_event(self, event): - event.stopPropagation() - - def data_change(self): - return False - def msg(message, params): if not issubclass(type(message), Message): diff --git a/src/message/sense_edit.py b/src/message/sense_edit.py new file mode 100644 index 0000000..3e16eb0 --- /dev/null +++ b/src/message/sense_edit.py @@ -0,0 +1,40 @@ +from message.simple_messages import DataChgClickMessage +from model import Sense + + +def _get_sense_idx(sense, model): + for idx, s in enumerate(model.entry.senses): + if s == sense: + return idx + +class SenseMoveUp(DataChgClickMessage): + def update_model(self, model): + sense = self.get_arg(0, Sense) + sidx = _get_sense_idx(sense, model) + + assert(sidx >= 0) + if sidx == 0: + return + + model.entry.senses[sidx], model.entry.senses[sidx - 1] = model.entry.senses[sidx - 1], model.entry.senses[sidx] + + +class SenseMoveDown(DataChgClickMessage): + def update_model(self, model): + sense = self.get_arg(0, Sense) + sidx = _get_sense_idx(sense, model) + + assert(sidx >= 0) + if sidx == len(model.senses) - 1: + return + + model.entry.senses[sidx], model.entry.senses[sidx + 1] = model.entry.senses[sidx + 1], model.entry.senses[sidx] + + +class SenseBin(DataChgClickMessage): + def update_model(self, model): + sense = self.get_arg(0, Sense) + sidx = _get_sense_idx(sense, model) + + assert(sidx >= 0) + model.entry.senses.splice(sidx, 1) diff --git a/src/message/show_menu.py b/src/message/show_menu.py new file mode 100644 index 0000000..700b9be --- /dev/null +++ b/src/message/show_menu.py @@ -0,0 +1,24 @@ +from model import Translation, Sense +from message.simple_messages import ClickMessage + + +class ShowMenu(ClickMessage): + def on_event(self, event): + location_x = event.currentTarget.offsetLeft + location_y = event.currentTarget.offsetTop + event.currentTarget.offsetHeight + self.menu_location = (location_x, location_y) + super().on_event(event) + + +class ShowTranslationMenu(ShowMenu): + def update_model(self, model): + model.menu_location = self.menu_location + model.translation = self.get_arg(0, Translation) + model.menu_target = model.translation + + +class ShowSenseMenu(ShowMenu): + def update_model(self, model): + model.menu_location = self.menu_location + sense = self.get_arg(0, Sense) + model.menu_target = sense diff --git a/src/message/show_messages.py b/src/message/show_messages.py index f8d5bd4..0675656 100644 --- a/src/message/show_messages.py +++ b/src/message/show_messages.py @@ -1,21 +1,9 @@ -from message.message import Message, ClickMessage +from message.message import Message from message.translation_edit import AddTranslation, EditTranslation +from message.simple_messages import ClickMessage from model import Example, Sense, Translation from view import modals - - -class ShowMenu(ClickMessage): - def on_event(self, event): - location_x = event.currentTarget.offsetLeft - location_y = event.currentTarget.offsetTop + event.currentTarget.offsetHeight - self.menu_location = (location_x, location_y) - super().on_event(event) - - def update_model(self, model): - model.menu_location = self.menu_location - model.translation = self.get_arg(0, Translation) - model.menu_target = model.translation class ShowSenseLabelEdit(ClickMessage): diff --git a/src/message/simple_messages.py b/src/message/simple_messages.py index 62711bc..c709a42 100644 --- a/src/message/simple_messages.py +++ b/src/message/simple_messages.py @@ -1,7 +1,23 @@ -from message.message import Message, ClickMessage, msg from browser import window +from message.message import Message, msg +# click messages stuff + + +class DataChgClickMessage(Message): + def on_event(self, event): + event.stopPropagation() + + +class ClickMessage(DataChgClickMessage): + def data_change(self): + return True + + +# reset messages stuff + + class Reset(ClickMessage): def update_model(self, model): pass @@ -11,7 +27,7 @@ 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 diff --git a/src/message/translation_edit.py b/src/message/translation_edit.py index cd0264c..533aa78 100644 --- a/src/message/translation_edit.py +++ b/src/message/translation_edit.py @@ -1,4 +1,5 @@ -from message.message import Message, ClickMessage +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 @@ -13,12 +14,7 @@ def get_translation_location(entry, translation): return (si, ci, ti), (sense, cluster) -class TranslationEditMessage(ClickMessage): - def data_change(self): - return True - - -class EditTranslation(TranslationEditMessage): +class EditTranslation(DataChgClickMessage): def update_model(self, model): self.translation = self.get_arg(0, Translation) self.old_cluster_idx = self.get_arg(1, int) @@ -65,7 +61,7 @@ class EditTranslation(TranslationEditMessage): sense.translations.splice(cidx, 1) -class MoveRight(TranslationEditMessage): +class MoveRight(DataChgClickMessage): def update_model(self, model): translation = self.get_arg(0, Translation) (_, _, idx), (_, cluster) = get_translation_location(model.entry, translation) @@ -74,7 +70,7 @@ class MoveRight(TranslationEditMessage): model.translation = None -class MoveLeft(TranslationEditMessage): +class MoveLeft(DataChgClickMessage): def update_model(self, model): translation = self.get_arg(0, Translation) (_, _, idx), (_, cluster) = get_translation_location(model.entry, translation) @@ -83,7 +79,7 @@ class MoveLeft(TranslationEditMessage): model.translation = None -class BinTranslation(TranslationEditMessage): +class BinTranslation(DataChgClickMessage): def update_model(self, model): translation = self.get_arg(0, Translation) (_, cidx, tidx), (sense, cluster) = get_translation_location(model.entry, translation) @@ -95,7 +91,7 @@ class BinTranslation(TranslationEditMessage): model.translation = None -class AddTranslation(TranslationEditMessage): +class AddTranslation(DataChgClickMessage): def handle_cluster_change(self, new_cluster_idx, _): # we need to cheat here # sense was actually given in constructor in third place diff --git a/src/view/view.py b/src/view/view.py index 1070072..966e6ec 100644 --- a/src/view/view.py +++ b/src/view/view.py @@ -79,7 +79,7 @@ class View: examples = [View.view_example(example) for example in sense.examples] return h("div.elm-div", {}, [ - h("div.sense-num", {}, str(senseNum + 1)), + h("div.sense-num", {"on": {"click": msg(ShowSenseMenu(sense))}}, str(senseNum + 1)), h("div.sense", {}, [ h("span.sense-label-list", { "on": { "click": msg(ShowSenseLabelEdit(sense)) }}, [ h("span.sense-label", {}, clean_label(slabel)) for _, slabel in sense.labels ]), @@ -125,7 +125,7 @@ class View: explanation_class = ".translation-explanation" if translation.translation else "" elements.append(h("span{}".format(explanation_class), {}, translation.explanation)) - return h("div.translation-div", {"on": {"click": msg(ShowMenu(translation)) }}, elements) + return h("div.translation-div", {"on": {"click": msg(ShowTranslationMenu(translation)) }}, elements) @staticmethod @@ -135,9 +135,6 @@ class View: "top": "{}px".format(location[1]) } - console.log(menu_target) - console.log(type(menu_target)) - if menu_target is None: style["opacity"] = "0" style["visibility"] = "hidden" @@ -150,6 +147,12 @@ class View: h("button.shyButton", { "on": {"click": msg(MoveLeft(translation))}}, "←"), h("button.shyButton", { "on": {"click": msg(BinTranslation(translation))}}, "🗑")]) + elif type(menu_target) is Sense: + sense = menu_target + return h("span.popup-menu", { "style": style }, [ + h("button.shyButton", { "on": {"click": msg(SenseMoveUp(sense))}}, "↑"), + h("button.shyButton", { "on": {"click": msg(SenseMoveDown(sense))}}, "↓"), + h("button.shyButton", { "on": {"click": msg(SenseBin(sense))}}, "🗑")]) else: console.log("Should not be heree!!")