lexonomy_custom_editor/src/message/simple_edits.py

136 lines
4.0 KiB
Python
Raw Normal View History

2019-11-11 22:04:45 +00:00
from message.message import Message
from message.simple_messages import NoReset, DataChgClickMessage
import message.common_accessors as common_accessors
2019-11-11 22:04:45 +00:00
from browser import document
from model import Sense, Example
from model.example_clusters import ExampleClusters
2019-11-11 22:04:45 +00:00
class QuestionMessage(Message):
def on_event(self, event):
input_element = document.getElementById("modal-question")
2019-11-11 22:04:45 +00:00
self.new_text = input_element.value
class EditSenseLabel(Message):
2019-11-11 22:04:45 +00:00
def update_model(self, model):
sense = self.get_arg(0, Sense)
2019-11-17 21:09:09 +00:00
sense.labels = common_accessors.label_list_getter()
class AddToGenericList(NoReset):
def update_model(self, model):
list_getter = self.get_arg(0)
list_getter().append("")
2020-07-15 11:17:58 +00:00
def data_change(self):
return False
2020-07-15 11:17:58 +00:00
class AddToLabelList(NoReset):
2019-11-16 13:18:37 +00:00
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()
2020-07-15 11:17:58 +00:00
2019-11-16 13:18:37 +00:00
# just adding to the copy to show in the modal
list_to_add_to.append(thing_to_add)
2020-07-15 11:17:58 +00:00
def data_change(self):
return False
2019-11-16 13:18:37 +00:00
class AddSense(Message):
def update_model(self, model):
sense = Sense()
2020-01-14 19:59:15 +00:00
sense.definition = {"indicator": "New Sense"}
model.entry.senses.append(sense)
2020-07-15 11:17:58 +00:00
class EditSenseDefinition(QuestionMessage):
2019-11-11 22:04:45 +00:00
def update_model(self, model):
sense = self.get_arg(0, Sense)
2020-01-14 19:59:15 +00:00
sense.definition["indicator"] = self.new_text
2020-07-15 11:17:58 +00:00
class EditComment(QuestionMessage):
2019-11-11 22:04:45 +00:00
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)
2020-07-15 11:17:58 +00:00
inputs = document.getElementsByClassName("checkable-input")
selected = None
for idx, el in enumerate(inputs):
if el.checked:
selected = idx
break
2020-07-15 11:17:58 +00:00
# none was selected
if selected is None:
return
2020-07-15 11:17:58 +00:00
# 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)
2020-07-15 11:17:58 +00:00
# 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()
2020-08-25 09:12:23 +00:00
_remove_deleted_quries(variants, "head variantList variant", model)
console.log(variants)
model.entry.variants = variants
2020-07-15 11:17:58 +00:00
class EditHomonymy(Message):
def update_model(self, model):
homonymy = common_accessors.double_list_getter("value", "name")
2020-08-25 09:12:23 +00:00
_remove_deleted_quries(homonymy, "head headword homonymy homonymyFeature", model)
2020-07-15 11:17:58 +00:00
model.entry.homonymy = homonymy
class EditRelatedEntries(Message):
def update_model(self, model):
related_entries = common_accessors.generic_list_getter()
model.entry.related_entries = related_entries
2019-11-18 19:57:33 +00:00
class EditEntryLabels(Message):
def update_model(self, model):
labels = common_accessors.label_list_getter()
model.entry.labels = labels
2020-07-15 11:17:58 +00:00
class ExampleClusterEdit(DataChgClickMessage):
2020-01-28 22:22:52 +00:00
def update_model(self, model):
example = self.get_arg(0, Example)
cluster = self.get_arg(1, int)
example.set_cluster(cluster)
2020-07-15 11:17:58 +00:00
class ExampleClusterAdd(DataChgClickMessage):
2020-02-01 23:07:42 +00:00
def update_model(self, model):
example = self.get_arg(0, Example)
example.set_cluster(ExampleClusters.first_empty_cluster())
2020-07-15 11:17:58 +00:00
2020-08-25 09:12:23 +00:00
def _remove_deleted_quries(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