2020-01-21 19:23:12 +00:00
|
|
|
from model import Translation, Sense, Example
|
2019-11-20 23:28:49 +00:00
|
|
|
from message.simple_messages import ClickMessage
|
2020-01-24 01:04:12 +00:00
|
|
|
from view import modals
|
|
|
|
from browser import document
|
2019-11-20 23:28:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
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)
|
2020-01-24 01:04:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2019-11-20 23:28:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2020-01-21 19:23:12 +00:00
|
|
|
|
|
|
|
|
2020-01-24 01:04:12 +00:00
|
|
|
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):
|
2020-01-21 19:23:12 +00:00
|
|
|
example = self.get_arg(0, Example)
|
2020-01-24 01:04:12 +00:00
|
|
|
|
|
|
|
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)
|
2020-02-19 22:15:42 +00:00
|
|
|
|
|
|
|
def entry_redraw(self):
|
|
|
|
# when choosing messages we need to redraw
|
|
|
|
return self.ctrl or self.shift
|
|
|
|
|