lexonomy_custom_editor/src/message/show_menu.py

105 lines
3.4 KiB
Python
Raw Normal View History

from model import Translation, Sense, Example
from message.simple_messages import ClickMessage
from view import modals
from browser import document
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)
# Special message to handle ctrl+click, shift+click and click
# overwrite update_model_default/ctrl/shift
class KeyPlusClickMessage(ShowMenu):
def on_event(self, event):
super().on_event(event)
self.ctrl = event.ctrlKey
self.shift = event.shiftKey
def update_model(self, model):
if self.ctrl:
self.update_model_ctrl(model)
elif self.shift:
document.getSelection().removeAllRanges()
self.update_model_shift(model)
else:
self.update_model_default(model)
def update_model_ctrl(self, model):
pass
def update_model_shift(self, model):
pass
def update_model_default(self, model):
pass
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
class ShowExampleMenu(KeyPlusClickMessage):
def update_model_default(self, model):
example = self.get_arg(0, Example)
# if some are chosen, then show modal for choosing senses
if len(model.chosen_examples) > 0 and example in model.chosen_examples:
chosen_examples = model.chosen_examples
model.modal_set(lambda: modals.do_chosen_examples(chosen_examples, model.entry))
else:
model.menu_location = self.menu_location
model.menu_target = example
def update_model_ctrl(self, model):
example = self.get_arg(0, Example)
if example in model.chosen_examples:
model.chosen_examples.remove(example)
else:
model.chosen_examples.append(example)
def update_model_shift(self, model):
first_example = self.get_arg(0, Example)
last_example = model.chosen_examples[model.chosen_examples.length - 1]
adding = False
# logic to add all senses between these two
for sense in model.entry.senses:
for example in sense.examples:
match = example in (first_example, last_example)
if adding and match:
adding = False
elif match:
adding = True
elif not adding:
continue
if example not in model.chosen_examples:
model.chosen_examples.append(example)
def reset(self):
# special case, when choosing messages dont reset
return not (self.ctrl or self.shift)
def entry_redraw(self):
# when choosing messages we need to redraw
return self.ctrl or self.shift