2024-10-14 23:09:27 +02:00
|
|
|
import { Controller } from "@hotwired/stimulus";
|
2024-02-08 10:46:05 -06:00
|
|
|
|
2025-04-30 18:14:22 -04:00
|
|
|
// Connects to data-controller="dialog"
|
2024-02-08 10:46:05 -06:00
|
|
|
export default class extends Controller {
|
2025-04-30 18:14:22 -04:00
|
|
|
static targets = ["content"]
|
|
|
|
|
2024-11-27 16:01:50 -05:00
|
|
|
static values = {
|
2025-04-30 18:14:22 -04:00
|
|
|
autoOpen: { type: Boolean, default: false },
|
2024-11-27 16:01:50 -05:00
|
|
|
reloadOnClose: { type: Boolean, default: false },
|
|
|
|
};
|
|
|
|
|
2024-02-08 10:46:05 -06:00
|
|
|
connect() {
|
2024-10-14 23:09:27 +02:00
|
|
|
if (this.element.open) return;
|
2025-04-30 18:14:22 -04:00
|
|
|
if (this.autoOpenValue) {
|
|
|
|
this.element.showModal();
|
|
|
|
}
|
2024-02-08 10:46:05 -06:00
|
|
|
}
|
2025-04-30 18:14:22 -04:00
|
|
|
|
|
|
|
// If the user clicks anywhere outside of the visible content, close the dialog
|
2024-03-14 15:30:36 +01:00
|
|
|
clickOutside(e) {
|
2025-04-30 18:14:22 -04:00
|
|
|
if (!this.contentTarget.contains(e.target)) {
|
2024-11-27 16:01:50 -05:00
|
|
|
this.close();
|
2024-02-08 10:46:05 -06:00
|
|
|
}
|
|
|
|
}
|
2024-02-27 22:55:36 +05:30
|
|
|
|
|
|
|
close() {
|
|
|
|
this.element.close();
|
2024-11-27 16:01:50 -05:00
|
|
|
|
|
|
|
if (this.reloadOnCloseValue) {
|
2025-04-18 11:39:58 -04:00
|
|
|
Turbo.visit(window.location.href);
|
2024-11-27 16:01:50 -05:00
|
|
|
}
|
2024-02-27 22:55:36 +05:30
|
|
|
}
|
2024-02-08 10:46:05 -06:00
|
|
|
}
|