diff --git a/browser/components/payments/res/components/rich-select.js b/browser/components/payments/res/components/rich-select.js index 5c92139c9c57..4860ef5447d5 100644 --- a/browser/components/payments/res/components/rich-select.js +++ b/browser/components/payments/res/components/rich-select.js @@ -2,6 +2,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +import HandleEventMixin from "../mixins/HandleEventMixin.js"; import ObservedPropertiesMixin from "../mixins/ObservedPropertiesMixin.js"; import RichOption from "./rich-option.js"; @@ -13,7 +14,7 @@ import RichOption from "./rich-option.js"; * Note: The only supported way to change the selected option is via the * `value` setter. */ -export default class RichSelect extends ObservedPropertiesMixin(HTMLElement) { +export default class RichSelect extends HandleEventMixin(ObservedPropertiesMixin(HTMLElement)) { static get observedAttributes() { return [ "disabled", @@ -57,15 +58,10 @@ export default class RichSelect extends ObservedPropertiesMixin(HTMLElement) { return this.popupBox.querySelector(`:scope > [value="${CSS.escape(value)}"]`); } - handleEvent(event) { - switch (event.type) { - case "change": { - // Since the render function depends on the popupBox's value, we need to - // re-render if the value changes. - this.render(); - break; - } - } + onChange(event) { + // Since the render function depends on the popupBox's value, we need to + // re-render if the value changes. + this.render(); } render() { diff --git a/browser/components/payments/res/containers/address-form.js b/browser/components/payments/res/containers/address-form.js index 11d1a4265349..3ddea319c9d4 100644 --- a/browser/components/payments/res/containers/address-form.js +++ b/browser/components/payments/res/containers/address-form.js @@ -7,6 +7,7 @@ import LabelledCheckbox from "../components/labelled-checkbox.js"; import PaymentRequestPage from "../components/payment-request-page.js"; import PaymentStateSubscriberMixin from "../mixins/PaymentStateSubscriberMixin.js"; import paymentRequest from "../paymentRequest.js"; +import HandleEventMixin from "../mixins/HandleEventMixin.js"; /* import-globals-from ../unprivileged-fallbacks.js */ /** @@ -20,7 +21,8 @@ import paymentRequest from "../paymentRequest.js"; * as it will be much easier to share the logic once we switch to Fluent. */ -export default class AddressForm extends PaymentStateSubscriberMixin(PaymentRequestPage) { +export default class AddressForm extends + HandleEventMixin(PaymentStateSubscriberMixin(PaymentRequestPage)) { constructor() { super(); @@ -216,32 +218,18 @@ export default class AddressForm extends PaymentStateSubscriberMixin(PaymentRequ this.updateSaveButtonState(); } - handleEvent(event) { - switch (event.type) { - case "change": { - if (event.target.id == "country") { - this.updateRequiredState(); - } - this.updateSaveButtonState(); - break; - } - case "click": { - this.onClick(event); - break; - } - case "input": { - this.onInput(event); - break; - } - case "invalid": { - if (event.target instanceof HTMLFormElement) { - this.onInvalidForm(event); - break; - } + onChange(event) { + if (event.target.id == "country") { + this.updateRequiredState(); + } + this.updateSaveButtonState(); + } - this.onInvalidField(event); - break; - } + onInvalid(event) { + if (event.target instanceof HTMLFormElement) { + this.onInvalidForm(event); + } else { + this.onInvalidField(event); } } diff --git a/browser/components/payments/res/containers/address-picker.js b/browser/components/payments/res/containers/address-picker.js index 4db41a1b6df8..fef2343a74dd 100644 --- a/browser/components/payments/res/containers/address-picker.js +++ b/browser/components/payments/res/containers/address-picker.js @@ -6,6 +6,7 @@ import AddressForm from "./address-form.js"; import AddressOption from "../components/address-option.js"; import RichPicker from "./rich-picker.js"; import paymentRequest from "../paymentRequest.js"; +import HandleEventMixin from "../mixins/HandleEventMixin.js"; /** * @@ -13,7 +14,7 @@ import paymentRequest from "../paymentRequest.js"; * listening to savedAddresses & tempAddresses. */ -export default class AddressPicker extends RichPicker { +export default class AddressPicker extends HandleEventMixin(RichPicker) { static get pickerAttributes() { return [ "address-fields", @@ -197,18 +198,6 @@ export default class AddressPicker extends RichPicker { }) || ""; } - handleEvent(event) { - switch (event.type) { - case "change": { - this.onChange(event); - break; - } - case "click": { - this.onClick(event); - } - } - } - onChange(event) { let [selectedKey, selectedLeaf] = this.selectedStateKey.split("|"); if (!selectedKey) { diff --git a/browser/components/payments/res/containers/basic-card-form.js b/browser/components/payments/res/containers/basic-card-form.js index 11025317b2d8..a5bb6c56ecc1 100644 --- a/browser/components/payments/res/containers/basic-card-form.js +++ b/browser/components/payments/res/containers/basic-card-form.js @@ -10,6 +10,7 @@ import LabelledCheckbox from "../components/labelled-checkbox.js"; import PaymentRequestPage from "../components/payment-request-page.js"; import PaymentStateSubscriberMixin from "../mixins/PaymentStateSubscriberMixin.js"; import paymentRequest from "../paymentRequest.js"; +import HandleEventMixin from "../mixins/HandleEventMixin.js"; /* import-globals-from ../unprivileged-fallbacks.js */ @@ -20,7 +21,8 @@ import paymentRequest from "../paymentRequest.js"; * as it will be much easier to share the logic once we switch to Fluent. */ -export default class BasicCardForm extends PaymentStateSubscriberMixin(PaymentRequestPage) { +export default class BasicCardForm extends + HandleEventMixin(PaymentStateSubscriberMixin(PaymentRequestPage)) { constructor() { super(); @@ -268,32 +270,6 @@ export default class BasicCardForm extends PaymentStateSubscriberMixin(PaymentRe this.updateSaveButtonState(); } - handleEvent(event) { - switch (event.type) { - case "change": { - this.onChange(event); - break; - } - case "click": { - this.onClick(event); - break; - } - case "input": { - this.onInput(event); - break; - } - case "invalid": { - if (event.target instanceof HTMLFormElement) { - this.onInvalidForm(event); - break; - } - - this.onInvalidField(event); - break; - } - } - } - onChange(evt) { let ccType = this.form.querySelector("#cc-type"); this.cscInput.setAttribute("card-type", ccType.value); @@ -390,6 +366,14 @@ export default class BasicCardForm extends PaymentStateSubscriberMixin(PaymentRe this.updateSaveButtonState(); } + onInvalid(event) { + if (event.target instanceof HTMLFormElement) { + this.onInvalidForm(event); + } else { + this.onInvalidField(event); + } + } + /** * @param {Event} event - "invalid" event * Note: Keep this in-sync with the equivalent version in address-form.js diff --git a/browser/components/payments/res/containers/completion-error-page.js b/browser/components/payments/res/containers/completion-error-page.js index 167967899845..83b40bf2331c 100644 --- a/browser/components/payments/res/containers/completion-error-page.js +++ b/browser/components/payments/res/containers/completion-error-page.js @@ -2,6 +2,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +import HandleEventMixin from "../mixins/HandleEventMixin.js"; import PaymentRequestPage from "../components/payment-request-page.js"; import PaymentStateSubscriberMixin from "../mixins/PaymentStateSubscriberMixin.js"; import paymentRequest from "../paymentRequest.js"; @@ -15,7 +16,8 @@ import paymentRequest from "../paymentRequest.js"; * as it will be much easier to implement and share the logic once we switch to Fluent. */ -export default class CompletionErrorPage extends PaymentStateSubscriberMixin(PaymentRequestPage) { +export default class CompletionErrorPage extends + HandleEventMixin(PaymentStateSubscriberMixin(PaymentRequestPage)) { constructor() { super(); @@ -80,16 +82,14 @@ export default class CompletionErrorPage extends PaymentStateSubscriberMixin(Pay this.suggestionsList.appendChild(suggestionsFragment); } - handleEvent(event) { - if (event.type == "click") { - switch (event.target) { - case this.doneButton: { - this.onDoneButtonClick(event); - break; - } - default: { - throw new Error("Unexpected click target"); - } + onClick(event) { + switch (event.target) { + case this.doneButton: { + this.onDoneButtonClick(event); + break; + } + default: { + throw new Error("Unexpected click target"); } } } diff --git a/browser/components/payments/res/containers/payment-dialog.js b/browser/components/payments/res/containers/payment-dialog.js index 9af57c827fc3..64ce56171bff 100644 --- a/browser/components/payments/res/containers/payment-dialog.js +++ b/browser/components/payments/res/containers/payment-dialog.js @@ -2,6 +2,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +import HandleEventMixin from "../mixins/HandleEventMixin.js"; import PaymentStateSubscriberMixin from "../mixins/PaymentStateSubscriberMixin.js"; import paymentRequest from "../paymentRequest.js"; @@ -26,7 +27,8 @@ import "./shipping-option-picker.js"; * being exported once tests stop depending on it. */ -export default class PaymentDialog extends PaymentStateSubscriberMixin(HTMLElement) { +export default class PaymentDialog extends + HandleEventMixin(PaymentStateSubscriberMixin(HTMLElement)) { constructor() { super(); this._template = document.getElementById("payment-dialog-template"); @@ -77,22 +79,20 @@ export default class PaymentDialog extends PaymentStateSubscriberMixin(HTMLEleme super.disconnectedCallback(); } - handleEvent(event) { - if (event.type == "click") { - switch (event.currentTarget) { - case this._viewAllButton: - let orderDetailsShowing = !this.requestStore.getState().orderDetailsShowing; - this.requestStore.setState({ orderDetailsShowing }); - break; - case this._payButton: - this.pay(); - break; - case this._manageText: - if (event.target instanceof HTMLAnchorElement) { - this.openPreferences(event); - } - break; - } + onClick(event) { + switch (event.currentTarget) { + case this._viewAllButton: + let orderDetailsShowing = !this.requestStore.getState().orderDetailsShowing; + this.requestStore.setState({ orderDetailsShowing }); + break; + case this._payButton: + this.pay(); + break; + case this._manageText: + if (event.target instanceof HTMLAnchorElement) { + this.openPreferences(event); + } + break; } } diff --git a/browser/components/payments/res/containers/payment-method-picker.js b/browser/components/payments/res/containers/payment-method-picker.js index a930b7eaec98..10e85ab079a6 100644 --- a/browser/components/payments/res/containers/payment-method-picker.js +++ b/browser/components/payments/res/containers/payment-method-picker.js @@ -4,8 +4,10 @@ import BasicCardOption from "../components/basic-card-option.js"; import CscInput from "../components/csc-input.js"; +import HandleEventMixin from "../mixins/HandleEventMixin.js"; import RichPicker from "./rich-picker.js"; import paymentRequest from "../paymentRequest.js"; + /* import-globals-from ../unprivileged-fallbacks.js */ /** @@ -14,7 +16,7 @@ import paymentRequest from "../paymentRequest.js"; * listening to savedBasicCards. */ -export default class PaymentMethodPicker extends RichPicker { +export default class PaymentMethodPicker extends HandleEventMixin(RichPicker) { constructor() { super(); this.dropdown.setAttribute("option-type", "basic-card-option"); @@ -115,18 +117,12 @@ export default class PaymentMethodPicker extends RichPicker { return this.getAttribute("selected-state-key"); } - handleEvent(event) { - switch (event.type) { - case "input": - case "change": { - this.onInputOrChange(event); - break; - } - case "click": { - this.onClick(event); - break; - } - } + onInput(event) { + this.onInputOrChange(event); + } + + onChange(event) { + this.onInputOrChange(event); } onInputOrChange({currentTarget}) { diff --git a/browser/components/payments/res/containers/shipping-option-picker.js b/browser/components/payments/res/containers/shipping-option-picker.js index 47af49bf78b6..2c5d8d654800 100644 --- a/browser/components/payments/res/containers/shipping-option-picker.js +++ b/browser/components/payments/res/containers/shipping-option-picker.js @@ -4,6 +4,7 @@ import RichPicker from "./rich-picker.js"; import ShippingOption from "../components/shipping-option.js"; +import HandleEventMixin from "../mixins/HandleEventMixin.js"; /** * @@ -11,7 +12,7 @@ import ShippingOption from "../components/shipping-option.js"; *