lexonomy_custom_editor/src/message/example_edit.py

116 lines
3.5 KiB
Python

from browser import document
from message.simple_messages import DataChgClickMessage, ClickMessage, NoReset
from message.message import Message
from model.example import Example, ComponentLexeme
from model.sense import Sense
def _get_example_idx(example, model):
for sense in model.entry.senses:
for eidx, e in enumerate(sense.examples):
if e == example:
return (sense, eidx)
class ExampleMoveUp(DataChgClickMessage):
def update_model(self, model):
example = self.get_arg(0, Example)
sense, idx = _get_example_idx(example, model)
assert(idx >= 0)
if idx == 0:
return
sense.examples[idx], sense.examples[idx - 1] = sense.examples[idx - 1], sense.examples[idx]
class ExampleMoveDown(DataChgClickMessage):
def update_model(self, model):
example = self.get_arg(0, Example)
sense, idx = _get_example_idx(example, model)
assert(idx >= 0)
if idx == len(sense.examples) - 1:
return
sense.examples[idx], sense.examples[idx + 1] = sense.examples[idx + 1], sense.examples[idx]
class ExampleBin(DataChgClickMessage):
def update_model(self, model):
example = self.get_arg(0, Example)
sense, idx = _get_example_idx(example, model)
assert(idx >= 0)
sense.examples.splice(idx, 1)
class EditExampleText(Message):
def update_model(self, model):
example = self.get_arg(0, Example)
sense = self.get_arg(1, Sense)
example.overwrite_with_copy()
example.edited = True
# if this was a newly created example, now we can add it to the sense
# easier to add here than to add before editing and checking if the user
# canceled the operation
if example.newly_created:
example.newly_created = False
sense.examples.append(example)
idx = 0
for txt in document.getElementsByClassName("example-component-text"):
example.components[idx].text = txt.value
idx += 1
class ExampleRoleChange(NoReset):
def update_model(self, model):
example = self.get_arg(0, Example)
component_num = self.get_arg(1, int)
component_new_role = self.get_arg(2, str)
if component_new_role == "none":
component_new_role = None
example.copy().components[component_num].role = component_new_role
class ExampleComponentSpace(NoReset):
def update_model(self, model):
example = self.get_arg(0, Example)
component_num = self.get_arg(1, int)
component = example.copy().components[component_num]
component.no_space = not component.no_space
class ExampleComponentAdd(NoReset):
def update_model(self, model):
example = self.get_arg(0, Example)
component_num = self.get_arg(1, int)
new_component = ComponentLexeme()
example.copy().components.insert(component_num + 1, new_component)
class ExampleComponentRemove(NoReset):
def update_model(self, model):
example = self.get_arg(0, Example)
component_num = self.get_arg(1, int)
example.copy().components.splice(component_num, 1)
class ToggleExamples(Message):
def update_model(self, model):
model.examples_shown = not model.examples_shown
class ToggleClusters(Message):
def update_model(self, model):
model.clusters_shown = not model.clusters_shown