messages now a bit different, less contructions

not constructing one for each possible message but only constructing when message happens
This commit is contained in:
Ozbolt Menegatti 2020-02-16 23:17:46 +01:00
parent 23c1ec33a1
commit e3792005ba
7 changed files with 52 additions and 51 deletions

View File

@ -38,12 +38,13 @@ class Message:
self._args = [] self._args = []
def msg(message, params): def msg(message_class, params):
if not issubclass(type(message), Message):
window.console.log("Not scheduling a Message type, this will not work!")
return lambda: None
def callback(event): def callback(event):
message.on_event(event) #message_class(event, params) message = message_class(params)
if not issubclass(type(message), Message):
window.console.log("Not scheduling a Message type, this will not work!")
return
message.on_event(event)
update.schedule(message) update.schedule(message)
return callback return callback

View File

@ -55,7 +55,7 @@ class ShowEditTranslation(ClickMessage):
translation.make_copy() translation.make_copy()
num_clusters = len(parent.translations) num_clusters = len(parent.translations)
model.modal_set(lambda: modals.edit_translation( model.modal_set(lambda: modals.edit_translation(
translation, cidx, num_clusters, EditTranslation(translation, cidx))) translation, cidx, num_clusters, (EditTranslation, translation, cidx)))
class ShowAddTranslation(ClickMessage): class ShowAddTranslation(ClickMessage):
@ -68,7 +68,7 @@ class ShowAddTranslation(ClickMessage):
translation, translation,
-1, -1,
len(chosen_sense_or_example.translations), len(chosen_sense_or_example.translations),
AddTranslation(translation, -1, chosen_sense_or_example))) (AddTranslation, translation, -1, chosen_sense_or_example)))
class ShowEntryLabelsEdit(ClickMessage): class ShowEntryLabelsEdit(ClickMessage):

View File

@ -62,7 +62,7 @@ class _ModalResetDelayed(Message):
class ModalNotOkClose(Reset): class ModalNotOkClose(Reset):
def update_model(self, model): def update_model(self, model):
# msg just creates a callback, need to actually run it! # msg just creates a callback, need to actually run it!
window.setTimeout(lambda: msg(_ModalResetDelayed())(None), 100) window.setTimeout(lambda: msg(_ModalResetDelayed)(None), 100)
def data_change(self): def data_change(self):
return False return False

View File

@ -4,16 +4,16 @@ from model.tags import TAGS
from browser import document from browser import document
def modal_template(content, title, msg, delete_msg=None): def modal_template(content, title, msg, delete_msg=None):
reset = message.msg(message.ModalNotOkClose()) reset = message.msg(message.ModalNotOkClose)
footer = [] footer = []
if msg is not None: if msg is not None:
footer.append(h("a#modal-ok.button", {"on": {"click": message.msg(msg)}}, "OK")) footer.append(h("a#modal-ok.button", {"on": {"click": message.msg(*msg)}}, "OK"))
footer.append(h("label.button.dangerous", {"on": {"click": reset}}, "Cancel")) footer.append(h("label.button.dangerous", {"on": {"click": reset}}, "Cancel"))
if delete_msg is not None: if delete_msg is not None:
footer.append(h("label.button.warning.modal-delete", {"on": {"click": message.msg(delete_msg)}}, "🗑")) footer.append(h("label.button.warning.modal-delete", {"on": {"click": message.msg(*delete_msg)}}, "🗑"))
return [ return [
h("header", {}, [ h("header", {}, [
@ -35,7 +35,7 @@ def generic_list_editor(title, element_list_getter):
for slabel in element_list_getter(): for slabel in element_list_getter():
content.append(h("label", {}, [ content.append(h("label", {}, [
h("input.list-adder-input", {"props": {"type": "text", "value": slabel}}, "")])) h("input.list-adder-input", {"props": {"type": "text", "value": slabel}}, "")]))
content.append(h("button", {"on": {"click": message.msg(message.AddToGenericList(element_list_getter))}}, "+")) content.append(h("button", {"on": {"click": message.msg(message.AddToGenericList, element_list_getter)}}, "+"))
return content return content
@ -66,11 +66,11 @@ def label_list_editor(current_labels, add_label_message_class):
def get_new_label_type(): def get_new_label_type():
select = document.getElementById("new-tag-select") select = document.getElementById("new-tag-select")
return (select.options[select.selectedIndex].text, "") return (select.options[select.selectedIndex].text, "")
add_label_message_class.add_arg(get_new_label_type) add_label_message_class.append(get_new_label_type)
left = h("span", {}, "Add more!") left = h("span", {}, "Add more!")
center = h("select#new-tag-select", {}, [h("option", {}, ltype) for ltype in TAGS.keys()]) center = h("select#new-tag-select", {}, [h("option", {}, ltype) for ltype in TAGS.keys()])
right = h("button", {"style": {"float": "right"}, "on": {"click": message.msg(add_label_message_class)}}, "+") right = h("button", {"style": {"float": "right"}, "on": {"click": message.msg(*add_label_message_class)}}, "+")
content.append(split_line3(left, center, right, False)) content.append(split_line3(left, center, right, False))
content.append(h("hr", {}, [])) content.append(h("hr", {}, []))

View File

@ -20,14 +20,14 @@ def edit_translation(translation, cluster_idx, num_clusters, cls):
content.append(split_line2("Stevilka gruce:", h("select#cluster-num", {}, options))) content.append(split_line2("Stevilka gruce:", h("select#cluster-num", {}, options)))
content.append(h("h4", {}, "Tags")) content.append(h("h4", {}, "Tags"))
content.extend(label_list_editor(translation.copy().tags, message.AddToLabelList(translation.copy().tags))) content.extend(label_list_editor(translation.copy().tags, [message.AddToLabelList, translation.copy().tags]))
return modal_template(content, "Translation", cls) return modal_template(content, "Translation", cls)
def edit_sense_label(sense): def edit_sense_label(sense):
content = label_list_editor(sense.copy().labels, message.AddToLabelList(sense.copy().labels)) content = label_list_editor(sense.copy().labels, [message.AddToLabelList, sense.copy().labels])
return modal_template(content, "Translation", message.EditSenseLabel(sense)) return modal_template(content, "Translation", (message.EditSenseLabel, sense))
def edit_example(example, sense): def edit_example(example, sense):
@ -35,7 +35,7 @@ def edit_example(example, sense):
example = example_original.copy() example = example_original.copy()
def role_msg(idx, role): def role_msg(idx, role):
return message.msg(message.ExampleRoleChange(example_original, idx, role)) return message.msg(message.ExampleRoleChange, example_original, idx, role)
divs = [] divs = []
buttons_right = lambda idx: [ buttons_right = lambda idx: [
@ -48,9 +48,9 @@ def edit_example(example, sense):
h("span.example-component-button.example-component-none", h("span.example-component-button.example-component-none",
{"on": {"click": role_msg(idx, "none")}}, "N"), {"on": {"click": role_msg(idx, "none")}}, "N"),
h("span.example-component-button", h("span.example-component-button",
{"on": {"click": message.msg(message.ExampleComponentAdd(example_original, idx))}}, "+"), {"on": {"click": message.msg(message.ExampleComponentAdd, example_original, idx)}}, "+"),
h("span.example-component-button", h("span.example-component-button",
{"on": {"click": message.msg(message.ExampleComponentRemove(example_original, idx))}}, "-")] {"on": {"click": message.msg(message.ExampleComponentRemove, example_original, idx)}}, "-")]
for idx, component in enumerate(example.components): for idx, component in enumerate(example.components):
role_txt = component.role if component.role is not None else "none" role_txt = component.role if component.role is not None else "none"
@ -71,32 +71,32 @@ def edit_example(example, sense):
h("div.one-fifth", {}, "Cluster"), h("div.one-fifth", {}, "Cluster"),
h("div.four-fifth", {}, show_toggle_cluster_buttons(sense, example))])) h("div.four-fifth", {}, show_toggle_cluster_buttons(sense, example))]))
return modal_template(divs, "Edit Example", message.EditExampleText(example_original)) return modal_template(divs, "Edit Example", (message.EditExampleText, example_original))
def edit_variants(entry): def edit_variants(entry):
vget = lambda: entry.copy().variants vget = lambda: entry.copy().variants
content = generic_list_editor("Variants", vget) content = generic_list_editor("Variants", vget)
return modal_template(content, "Add or remove variants", message.EditVariants(), message.DeleteVariants()) return modal_template(content, "Add or remove variants", (message.EditVariants,), (message.DeleteVariants,))
def edit_related_entries(entry): def edit_related_entries(entry):
reget = lambda: entry.copy().related_entries reget = lambda: entry.copy().related_entries
content = generic_list_editor("Related entries", reget) content = generic_list_editor("Related entries", reget)
return modal_template(content, "Add or remove related entries", message.EditRelatedEntries(), message.DeleteRelatedEntries()) return modal_template(content, "Add or remove related entries", (message.EditRelatedEntries,), (message.DeleteRelatedEntries,))
def edit_entry_labels(entry): def edit_entry_labels(entry):
content = label_list_editor(entry.copy().labels, message.AddToLabelList(entry.copy().labels)) content = label_list_editor(entry.copy().labels, [message.AddToLabelList, entry.copy().labels])
return modal_template(content, "Translation", message.EditEntryLabels(), message.DeleteEntryLabels()) return modal_template(content, "Translation", (message.EditEntryLabels,), (message.DeleteEntryLabels,))
def edit_sense_definition(sense): def edit_sense_definition(sense):
return modal_template(question("Edit sense definition", sense.definition["indicator"]), "Sense definition", message.EditSenseDefinition(sense)) return modal_template(question("Edit sense definition", sense.definition["indicator"]), "Sense definition", (message.EditSenseDefinition, sense))
def edit_comment(comment): def edit_comment(comment):
return modal_template(question("Edit comment", comment), "Comment", message.EditComment(), message.DeleteComment()) return modal_template(question("Edit comment", comment), "Comment", (message.EditComment,), (message.DeleteComment,))
def do_chosen_examples(example_list, entry): def do_chosen_examples(example_list, entry):
@ -122,4 +122,4 @@ def do_chosen_examples(example_list, entry):
options.append(h("label.checkable", {"attrs": {"for": id_}}, text)) options.append(h("label.checkable", {"attrs": {"for": id_}}, text))
options.append(h("br", {}, [])) options.append(h("br", {}, []))
return modal_template(options, "Examples picker", message.DoChosenExamples(example_list)) return modal_template(options, "Examples picker", (message.DoChosenExamples, example_list))

View File

@ -23,12 +23,12 @@ def show_toggle_cluster_buttons(sense, example):
result.append(h(tag, result.append(h(tag,
{"attrs": attrs, "style": style, {"attrs": attrs, "style": style,
"on": {"click": message.msg(message.ExampleClusterEdit(example, opt))}}, [])) "on": {"click": message.msg(message.ExampleClusterEdit, example, opt)}}, []))
result.append(h(base_tag, result.append(h(base_tag,
{"attrs": {"value": "+", "type": "button"}, {"attrs": {"value": "+", "type": "button"},
"style": {"width": "1em"}, "style": {"width": "1em"},
"on": {"click": message.msg(message.ExampleClusterAdd(example))}}, "on": {"click": message.msg(message.ExampleClusterAdd, example)}},
[])) []))
return result return result

View File

@ -15,7 +15,7 @@ class View:
patch(container, self.vdom) patch(container, self.vdom)
# this does not work on parent div, so attaching to document here # this does not work on parent div, so attaching to document here
document.addEventListener("keyup", msg(KeyboardPress())) document.addEventListener("keyup", msg(KeyboardPress))
def view(self, model): def view(self, model):
self.model = model self.model = model
@ -26,8 +26,8 @@ class View:
self.vdom = new_vdom self.vdom = new_vdom
def _view(self): def _view(self):
return h("div", {"on": { "click": msg(Reset()) }}, [ return h("div", {"on": { "click": msg(Reset) }}, [
View.view_entry(self.model.entry, self.model), self.model.entry.view(self.model),
h("button.blk", {"on": { "click": lambda _: check_export(self.model) } }, "CHK"), h("button.blk", {"on": { "click": lambda _: check_export(self.model) } }, "CHK"),
View.view_menu(self.model.menu_location, self.model.menu_target, self.model.entry), View.view_menu(self.model.menu_location, self.model.menu_target, self.model.entry),
View.view_modal(self.model.modal_shown, self.model.modal)]) View.view_modal(self.model.modal_shown, self.model.modal)])
@ -57,8 +57,8 @@ class View:
txt_examples = "Hide examples" if model.examples_shown else "Show examples" txt_examples = "Hide examples" if model.examples_shown else "Show examples"
txt_clusters = "Hide clusters" if model.clusters_shown else "Show clusters" txt_clusters = "Hide clusters" if model.clusters_shown else "Show clusters"
return [h("span.button.toggle", {"on": {"click": msg(ToggleExamples())}}, txt_examples), return [h("span.button.toggle", {"on": {"click": msg(ToggleExamples)}}, txt_examples),
h("span.button.toggle", {"on": {"click": msg(ToggleClusters())}}, txt_clusters)] h("span.button.toggle", {"on": {"click": msg(ToggleClusters)}}, txt_clusters)]
@staticmethod @staticmethod
@ -148,7 +148,7 @@ class View:
return h("div.example", {"style": {"display": parent_display}}, [ return h("div.example", {"style": {"display": parent_display}}, [
h("div.example-dot", dot_attr, ""), h("div.example-dot", dot_attr, ""),
h(example_tag, {}, [ h(example_tag, {}, [
h("span.example-text", {"on": {"click": msg(ShowExampleMenu(example))} }, example_content), h("span.example-text", {"on": {"click": msg(ShowExampleMenu, example)} }, example_content),
h("div.example-translation-list", {}, [ h("div.example-translation-list", {}, [
h("div.example-translation", {}, View.view_translations(example.translations, example, model))]), h("div.example-translation", {}, View.view_translations(example.translations, example, model))]),
h("div.example-clusters", h("div.example-clusters",
@ -160,7 +160,7 @@ class View:
for cluster in translations: for cluster in translations:
result.append(h("div.translation-div-cluster", {}, [View.view_one_translation(t, model) for t in cluster])) result.append(h("div.translation-div-cluster", {}, [View.view_one_translation(t, model) for t in cluster]))
result.append(h("button.add-button", {"on": {"click": msg(ShowAddTranslation(parent))}}, "+")) result.append(h("button.add-button", {"on": {"click": msg(ShowAddTranslation, parent)}}, "+"))
return result return result
@staticmethod @staticmethod
@ -180,7 +180,7 @@ class View:
explanation_class = ".translation-explanation" if translation.translation else "" explanation_class = ".translation-explanation" if translation.translation else ""
elements.append(h("span{}".format(explanation_class), {}, translation.explanation)) elements.append(h("span{}".format(explanation_class), {}, translation.explanation))
return h("div.translation-div", {"on": {"click": msg(ShowTranslationMenu(translation)) }}, elements) return h("div.translation-div", {"on": {"click": msg(ShowTranslationMenu, translation) }}, elements)
@staticmethod @staticmethod
@ -197,26 +197,26 @@ class View:
elif type(menu_target) is Translation: elif type(menu_target) is Translation:
translation = menu_target translation = menu_target
return h("span.popup-menu", { "style": style }, [ return h("span.popup-menu", { "style": style }, [
h("button.shyButton", { "on": {"click": msg(ShowEditTranslation(translation))}}, ""), h("button.shyButton", { "on": {"click": msg(ShowEditTranslation, translation)}}, ""),
h("button.shyButton", { "on": {"click": msg(MoveRight(translation))}}, ""), h("button.shyButton", { "on": {"click": msg(MoveRight, translation)}}, ""),
h("button.shyButton", { "on": {"click": msg(MoveLeft(translation))}}, ""), h("button.shyButton", { "on": {"click": msg(MoveLeft, translation)}}, ""),
h("button.shyButton", { "on": {"click": msg(BinTranslation(translation))}}, "🗑")]) h("button.shyButton", { "on": {"click": msg(BinTranslation, translation)}}, "🗑")])
elif type(menu_target) is Sense: elif type(menu_target) is Sense:
sense = menu_target sense = menu_target
return h("span.popup-menu", { "style": style }, [ return h("span.popup-menu", { "style": style }, [
h("button.shyButton", { "on": {"click": msg(SenseMoveUp(sense))}}, ""), h("button.shyButton", { "on": {"click": msg(SenseMoveUp, sense)}}, ""),
h("button.shyButton", { "on": {"click": msg(SenseMoveDown(sense))}}, ""), h("button.shyButton", { "on": {"click": msg(SenseMoveDown, sense)}}, ""),
h("button.shyButton", { "on": {"click": msg(SenseBin(sense))}}, "🗑")]) h("button.shyButton", { "on": {"click": msg(SenseBin, sense)}}, "🗑")])
elif type(menu_target) is Example: elif type(menu_target) is Example:
example = menu_target example = menu_target
sense = example_sense(example, entry) sense = example_sense(example, entry)
return h("span.popup-menu", { "style": style }, [ return h("span.popup-menu", { "style": style }, [
h("button.shyButton", { "on": {"click": msg(ShowExampleEdit(example, sense))}}, ""), h("button.shyButton", { "on": {"click": msg(ShowExampleEdit, example, sense)}}, ""),
h("button.shyButton", { "on": {"click": msg(ExampleMoveUp(example))}}, ""), h("button.shyButton", { "on": {"click": msg(ExampleMoveUp, example)}}, ""),
h("button.shyButton", { "on": {"click": msg(ExampleMoveDown(example))}}, ""), h("button.shyButton", { "on": {"click": msg(ExampleMoveDown, example)}}, ""),
h("button.shyButton", { "on": {"click": msg(ExampleBin(example))}}, "🗑")]) h("button.shyButton", { "on": {"click": msg(ExampleBin, example)}}, "🗑")])
else: else:
console.log("Should not be heree!!") console.log("Should not be heree!!")
@ -226,5 +226,5 @@ class View:
return h("div.modal", {}, [ return h("div.modal", {}, [
h("input", { "props": {"type": "checkbox", "checked": modal_shown} }, ""), h("input", { "props": {"type": "checkbox", "checked": modal_shown} }, ""),
h("label.overlay", {}, ""), h("label.overlay", {}, ""),
h("article", {"on": { "click": msg(NoReset()) }}, modal())]) h("article", {"on": { "click": msg(NoReset) }}, modal())])