from message.message import Message from message.simple_messages import NoReset, DataChgClickMessage import message.common_accessors as common_accessors from browser import document from model import Sense, Example from model.example_clusters import ExampleClusters class QuestionMessage(Message): def on_event(self, event): input_element = document.getElementById("modal-question") self.new_text = input_element.value class EditSenseLabel(Message): def update_model(self, model): sense = self.get_arg(0, Sense) sense.labels = common_accessors.label_list_getter() keptIds = [entry[2] for entry in sense.labels] for index, entry in enumerate(sense.original_xml.querySelectorAll("labelList label")): if (index not in keptIds): entry.remove() for index, entry in enumerate(sense.labels): if (entry[2] >= 0): entry[2] = index class AddToGenericList(NoReset): def update_model(self, model): list_getter = self.get_arg(0) list_getter().append("") def data_change(self): return False class AddToLabelList(NoReset): def update_model(self, model): list_to_add_to = self.get_arg(0, list) thing_to_add_getter = self.get_arg(1) thing_to_add = thing_to_add_getter() # just adding to the copy to show in the modal list_to_add_to.append(thing_to_add) def data_change(self): return False class AddSense(Message): def update_model(self, model): sense = Sense() sense.definitions.append({"type": "indicator", "value": "New Sense"}) model.entry.senses.append(sense) class EditSenseDefinition(QuestionMessage): def update_model(self, model): sense = self.get_arg(0, Sense) sense.definitions[0].value = self.new_text class EditComment(QuestionMessage): def update_model(self, model): model.entry.comment = self.new_text class DoChosenExamples(Message): def update_model(self, model): chosen_examples = self.get_arg(0, list) inputs = document.getElementsByClassName("checkable-input") selected = None for idx, el in enumerate(inputs): if el.checked: selected = idx break # none was selected if selected is None: return # first, remove the selected examples from wherever they were for sense in model.entry.senses: for example in chosen_examples: if example in sense.examples: sense.examples.remove(example) # now, append selected examples to chosen sense model.entry.senses[selected].examples.extend(chosen_examples) class EditVariants(Message): def update_model(self, model): variants = common_accessors.generic_list_getter() update_entries(variants, "head variantList variant", model) model.entry.variants = variants class EditHomonymy(Message): def update_model(self, model): homonymy = common_accessors.double_list_getter("value", "name") update_entries(homonymy, "head headword homonymy homonymyFeature", model) model.entry.homonymy = homonymy class EditRelatedEntries(Message): def update_model(self, model): related_entries = common_accessors.generic_list_getter() update_entries(related_entries, "head relatedEntryList relatedEntry", model) model.entry.related_entries = related_entries class EditEntryLabels(Message): def update_model(self, model): labels = common_accessors.label_list_getter() keptIds = [entry[2] for entry in labels] for index, entry in enumerate(model.entry.original_xml.querySelectorAll("labelList label")): if (index not in keptIds): entry.remove() for index, entry in enumerate(labels): if (entry[2] >= 0): entry[2] = index model.entry.labels = labels class ExampleClusterEdit(DataChgClickMessage): def update_model(self, model): example = self.get_arg(0, Example) cluster = self.get_arg(1, int) example.set_cluster(cluster) class ExampleClusterAdd(DataChgClickMessage): def update_model(self, model): example = self.get_arg(0, Example) example.set_cluster(ExampleClusters.first_empty_cluster()) def update_entries(updated_list, query, model): keptIds = [entry.id for entry in updated_list] for index, entry in enumerate(model.entry.original_xml.querySelectorAll(query)): if (index not in keptIds): entry.remove() for index, entry in enumerate(updated_list): if (entry.id >= 0): entry.id = index