DEV: Convert d-modal and d-modal-body to native class syntax

This commit is contained in:
David Taylor
2023-05-12 14:18:27 +01:00
parent 660a40ca06
commit 11e7e949b7
2 changed files with 59 additions and 53 deletions

View File

@ -1,15 +1,17 @@
import { attributeBindings, classNames } from "@ember-decorators/component";
import Component from "@ember/component"; import Component from "@ember/component";
import { scheduleOnce } from "@ember/runloop"; import { scheduleOnce } from "@ember/runloop";
export default Component.extend({
classNames: ["modal-body"], @classNames("modal-body")
fixed: false, @attributeBindings("tabindex")
submitOnEnter: true, export default class DModalBody extends Component {
dismissable: true, fixed = false;
attributeBindings: ["tabindex"], submitOnEnter = true;
tabindex: -1, dismissable = true;
tabindex = -1;
didInsertElement() { didInsertElement() {
this._super(...arguments); super.didInsertElement(...arguments);
this._modalAlertElement = document.getElementById("modal-alert"); this._modalAlertElement = document.getElementById("modal-alert");
if (this._modalAlertElement) { if (this._modalAlertElement) {
this._clearFlash(); this._clearFlash();
@ -24,14 +26,14 @@ export default Component.extend({
scheduleOnce("afterRender", this, this._afterFirstRender); scheduleOnce("afterRender", this, this._afterFirstRender);
this.appEvents.on("modal-body:flash", this, "_flash"); this.appEvents.on("modal-body:flash", this, "_flash");
this.appEvents.on("modal-body:clearFlash", this, "_clearFlash"); this.appEvents.on("modal-body:clearFlash", this, "_clearFlash");
}, }
willDestroyElement() { willDestroyElement() {
this._super(...arguments); super.willDestroyElement(...arguments);
this.appEvents.off("modal-body:flash", this, "_flash"); this.appEvents.off("modal-body:flash", this, "_flash");
this.appEvents.off("modal-body:clearFlash", this, "_clearFlash"); this.appEvents.off("modal-body:clearFlash", this, "_clearFlash");
this.appEvents.trigger("modal:body-dismissed"); this.appEvents.trigger("modal:body-dismissed");
}, }
_afterFirstRender() { _afterFirstRender() {
const maxHeight = this.maxHeight; const maxHeight = this.maxHeight;
@ -57,7 +59,7 @@ export default Component.extend({
"headerClass" "headerClass"
) )
); );
}, }
_clearFlash() { _clearFlash() {
if (this._modalAlertElement) { if (this._modalAlertElement) {
@ -70,7 +72,7 @@ export default Component.extend({
"alert-warning" "alert-warning"
); );
} }
}, }
_flash(msg) { _flash(msg) {
this._clearFlash(); this._clearFlash();
@ -83,5 +85,5 @@ export default Component.extend({
`alert-${msg.messageClass || "success"}` `alert-${msg.messageClass || "success"}`
); );
this._modalAlertElement.innerHTML = msg.text || ""; this._modalAlertElement.innerHTML = msg.text || "";
}, }
}); }

View File

@ -1,37 +1,41 @@
import {
attributeBindings,
classNameBindings,
} from "@ember-decorators/component";
import Component from "@ember/component"; import Component from "@ember/component";
import I18n from "I18n"; import I18n from "I18n";
import { next, schedule } from "@ember/runloop"; import { next, schedule } from "@ember/runloop";
import discourseComputed, { bind } from "discourse-common/utils/decorators"; import discourseComputed, { bind } from "discourse-common/utils/decorators";
export default Component.extend({ @classNameBindings(
classNameBindings: [ ":modal",
":modal", ":d-modal",
":d-modal", "modalClass",
"modalClass", "modalStyle",
"modalStyle", "hasPanels"
"hasPanels", )
], @attributeBindings(
attributeBindings: [ "dataKeyboard:data-keyboard",
"data-keyboard", "ariaModal:aria-modal",
"aria-modal", "role",
"role", "ariaLabelledby:aria-labelledby"
"ariaLabelledby:aria-labelledby", )
], export default class DModal extends Component {
submitOnEnter: true, submitOnEnter = true;
dismissable: true, dismissable = true;
title: null, title = null;
titleAriaElementId: null, titleAriaElementId = null;
subtitle: null, subtitle = null;
role: "dialog", role = "dialog";
headerClass: null, headerClass = null;
// We handle ESC ourselves // // We handle ESC ourselves
"data-keyboard": "false", dataKeyboard = "false";
// Inform screen readers of the modal // // Inform screen readers of the modal
"aria-modal": "true", ariaModal = "true";
init() { init() {
this._super(...arguments); super.init(...arguments);
// If we need to render a second modal for any reason, we can't // If we need to render a second modal for any reason, we can't
// use `elementId` // use `elementId`
@ -39,27 +43,27 @@ export default Component.extend({
this.set("elementId", "discourse-modal"); this.set("elementId", "discourse-modal");
this.set("modalStyle", "fixed-modal"); this.set("modalStyle", "fixed-modal");
} }
}, }
didInsertElement() { didInsertElement() {
this._super(...arguments); super.didInsertElement(...arguments);
this.appEvents.on("modal:body-shown", this, "_modalBodyShown"); this.appEvents.on("modal:body-shown", this, "_modalBodyShown");
document.documentElement.addEventListener( document.documentElement.addEventListener(
"keydown", "keydown",
this._handleModalEvents this._handleModalEvents
); );
}, }
willDestroyElement() { willDestroyElement() {
this._super(...arguments); super.willDestroyElement(...arguments);
this.appEvents.off("modal:body-shown", this, "_modalBodyShown"); this.appEvents.off("modal:body-shown", this, "_modalBodyShown");
document.documentElement.removeEventListener( document.documentElement.removeEventListener(
"keydown", "keydown",
this._handleModalEvents this._handleModalEvents
); );
}, }
@discourseComputed("title", "titleAriaElementId") @discourseComputed("title", "titleAriaElementId")
ariaLabelledby(title, titleAriaElementId) { ariaLabelledby(title, titleAriaElementId) {
@ -71,7 +75,7 @@ export default Component.extend({
} }
return; return;
}, }
triggerClickOnEnter(e) { triggerClickOnEnter(e) {
if (!this.submitOnEnter) { if (!this.submitOnEnter) {
@ -87,7 +91,7 @@ export default Component.extend({
} }
return true; return true;
}, }
mouseDown(e) { mouseDown(e) {
if (!this.dismissable) { if (!this.dismissable) {
@ -103,7 +107,7 @@ export default Component.extend({
// it unclickable. // it unclickable.
return this.attrs.closeModal?.("initiatedByClickOut"); return this.attrs.closeModal?.("initiatedByClickOut");
} }
}, }
_modalBodyShown(data) { _modalBodyShown(data) {
if (this.isDestroying || this.isDestroyed) { if (this.isDestroying || this.isDestroyed) {
@ -145,7 +149,7 @@ export default Component.extend({
schedule("afterRender", () => { schedule("afterRender", () => {
this._trapTab(); this._trapTab();
}); });
}, }
@bind @bind
_handleModalEvents(event) { _handleModalEvents(event) {
@ -165,7 +169,7 @@ export default Component.extend({
if (event.key === "Tab") { if (event.key === "Tab") {
this._trapTab(event); this._trapTab(event);
} }
}, }
_trapTab(event) { _trapTab(event) {
if (this.element.classList.contains("hidden")) { if (this.element.classList.contains("hidden")) {
@ -221,5 +225,5 @@ export default Component.extend({
event.preventDefault(); event.preventDefault();
} }
} }
}, }
}); }