diff --git a/app/javascript/controllers/evaluation_form_controller.js b/app/javascript/controllers/evaluation_form_controller.js index 1d9fc0e0..104c6dfd 100644 --- a/app/javascript/controllers/evaluation_form_controller.js +++ b/app/javascript/controllers/evaluation_form_controller.js @@ -32,7 +32,9 @@ export default class extends Controller { // Opens all accordions, remove existing points/weights, update max points/weights values updateMaxPoints(e) { - const form = e.target.closest('form[data-controller="evaluation-form"]'); + const form = e.target.closest( + 'form[data-controller="evaluation-form modal"]' + ); const pointsWeights = form.querySelectorAll(".points-or-weight"); const weightedScale = e.target.value === "true"; diff --git a/app/javascript/controllers/index.js b/app/javascript/controllers/index.js index 82491138..dd866ed6 100644 --- a/app/javascript/controllers/index.js +++ b/app/javascript/controllers/index.js @@ -8,7 +8,10 @@ import DeleteEvaluatorModalController from "./delete_evaluator_modal_controller" application.register("delete-evaluator-modal", DeleteEvaluatorModalController); import UnassignEvaluatorSubmissionModalController from "./unassign_evaluator_submission_modal_controller"; -application.register("unassign-evaluator-submission-modal", UnassignEvaluatorSubmissionModalController); +application.register( + "unassign-evaluator-submission-modal", + UnassignEvaluatorSubmissionModalController +); import EvaluationCriteriaController from "./evaluation_criteria_controller"; application.register("evaluation-criteria", EvaluationCriteriaController); @@ -17,4 +20,7 @@ import EvaluationFormController from "./evaluation_form_controller"; application.register("evaluation-form", EvaluationFormController); import HotdogController from "./hotdog_controller"; -application.register("hotdog", HotdogController); \ No newline at end of file +application.register("hotdog", HotdogController); + +import ModalController from "./modal_controller"; +application.register("modal", ModalController); diff --git a/app/javascript/controllers/modal_controller.js b/app/javascript/controllers/modal_controller.js new file mode 100644 index 00000000..c2eb5b9d --- /dev/null +++ b/app/javascript/controllers/modal_controller.js @@ -0,0 +1,87 @@ +import { Controller } from "@hotwired/stimulus"; + +export default class extends Controller { + static targets = ["modal"]; + static values = { + modalId: String, + }; + + open(event) { + const modalId = event.currentTarget.dataset.modalTargetId; + const modal = this.modalTargets.find((modal) => modal.id === modalId); + + this.openEvent = event; + + event.preventDefault(); + + if (modal) { + modal.showModal(); + } else { + console.warn(`Modal with ID '${modalId}' not found.`); + } + } + + confirm(event) { + const modal = this._getModal(event); + + if (modal) { + const confirmRedirect = modal.dataset.modalConfirmRedirect; + const confirmAction = modal.dataset.modalConfirmAction; + + if (confirmRedirect) { + window.location.href = confirmRedirect; + } else if (confirmAction) { + this.invokeAction(confirmAction); + modal.close(); + } else { + modal.close(); + return true; + } + } + } + + cancel(event) { + const modal = this._getModal(event); + + if (modal) { + const cancelRedirect = modal.dataset.modalCancelRedirect; + const cancelAction = modal.dataset.modalCancelAction; + + if (cancelRedirect) { + window.location.href = cancelRedirect; + } else if (cancelAction) { + this.invokeAction(cancelAction); + modal.close(); + } else { + modal.close(); + return false; + } + } + } + + _getModal(event) { + return event.currentTarget.closest("dialog"); + } + + invokeAction(actionName) { + const [controllerName, action] = actionName.split("#"); + const controllerElement = document.querySelector( + `[data-controller~="${controllerName}"]` + ); + + if (!controllerElement) { + console.warn(`Controller element for ${controllerName} not found.`); + } + + const controller = this.application.getControllerForElementAndIdentifier( + controllerElement, + controllerName + ); + + if (controller && typeof controller[action] === "function") { + controller[action](this.openEvent); + } else { + console.warn(`Action ${actionName} not found on ${controllerName}`); + } + } +} diff --git a/app/views/evaluation_forms/_evaluation_criterion_fields.html.erb b/app/views/evaluation_forms/_evaluation_criterion_fields.html.erb index 9250bafc..5a375040 100644 --- a/app/views/evaluation_forms/_evaluation_criterion_fields.html.erb +++ b/app/views/evaluation_forms/_evaluation_criterion_fields.html.erb @@ -114,7 +114,7 @@ -