from browser import document from message.simple_messages import DataChgClickMessage, ClickMessage, NoReset from message.message import Message from model.example import Example, ComponentLexeme 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) example.overwrite_with_copy() 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 ExampleComponentAdd(NoReset): def update_model(self, model): example = self.get_arg(0, Example) component_num = self.get_arg(1, int) new_component = ComponentLexeme.new_empty() 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