From 4344a845616667bb380597c211b9af406f40223d Mon Sep 17 00:00:00 2001 From: Gregoo Date: Mon, 27 Jan 2025 23:13:42 +0100 Subject: [PATCH] Properly use the _listener variables as expected, and allow Enter key to execute the action --- static/scripts/socket/instructions.js | 21 ++++++++------ static/scripts/socket/set.js | 42 +++++++++++++++++---------- 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/static/scripts/socket/instructions.js b/static/scripts/socket/instructions.js index 271c0c3..fb19224 100644 --- a/static/scripts/socket/instructions.js +++ b/static/scripts/socket/instructions.js @@ -11,15 +11,9 @@ class BrickInstructionsSocket extends BrickSocket { this.html_files = document.getElementById(`${id}-files`); if (this.html_button) { - this.download_listener = ((bricksocket) => (e) => { - if (!bricksocket.disabled && bricksocket.socket !== undefined && bricksocket.socket.connected) { - bricksocket.toggle(false); - - bricksocket.download_instructions(); - } - })(this); - - this.html_button.addEventListener("click", this.download_listener); + this.download_listener = this.html_button.addEventListener("click", ((bricksocket) => (e) => { + bricksocket.execute(); + })(this)); } if (this.html_card_dismiss && this.html_card) { @@ -43,6 +37,15 @@ class BrickInstructionsSocket extends BrickSocket { this.download_instructions(true); } + // Execute the action + execute() { + if (!this.disabled && this.socket !== undefined && this.socket.connected) { + this.toggle(false); + + this.download_instructions(); + } + } + // Get the list of checkboxes describing files get_files(checked=false) { let files = []; diff --git a/static/scripts/socket/set.js b/static/scripts/socket/set.js index 60a2244..41056b8 100644 --- a/static/scripts/socket/set.js +++ b/static/scripts/socket/set.js @@ -5,6 +5,7 @@ class BrickSetSocket extends BrickSocket { // Listeners this.add_listener = undefined; + this.input_listener = undefined; this.confirm_listener = undefined; // Form elements (built based on the initial id) @@ -23,24 +24,15 @@ class BrickSetSocket extends BrickSocket { this.html_card_dismiss = document.getElementById(`${id}-card-dismiss`); if (this.html_button) { - this.add_listener = ((bricksocket) => (e) => { - if (!bricksocket.disabled && bricksocket.socket !== undefined && bricksocket.socket.connected) { - bricksocket.toggle(false); + this.add_listener = this.html_button.addEventListener("click", ((bricksocket) => (e) => { + bricksocket.execute(); + })(this)); - // Split and save the list if bulk - if (bricksocket.bulk) { - bricksocket.read_set_list() - } - - if (bricksocket.bulk || (bricksocket.html_no_confim && bricksocket.html_no_confim.checked)) { - bricksocket.import_set(true); - } else { - bricksocket.load_set(); - } + this.input_listener = this.html_input.addEventListener("keyup", ((bricksocket) => (e) => { + if (e.key === 'Enter') { + bricksocket.execute(); } - })(this); - - this.html_button.addEventListener("click", this.add_listener); + })(this)) } if (this.html_card_dismiss && this.html_card) { @@ -80,6 +72,24 @@ class BrickSetSocket extends BrickSocket { } } + // Execute the action + execute() { + if (!this.disabled && this.socket !== undefined && this.socket.connected) { + this.toggle(false); + + // Split and save the list if bulk + if (this.bulk) { + this.read_set_list(); + } + + if (this.bulk || (this.html_no_confim && this.html_no_confim.checked)) { + this.import_set(true); + } else { + this.load_set(); + } + } + } + // Upon receiving a fail message fail(data) { super.fail(data);