|
| 1 | +import { ConfirmationDialog } from "@web/core/confirmation_dialog/confirmation_dialog"; |
| 2 | +import { Plugin } from "@html_editor/plugin"; |
| 3 | +import { registry } from "@web/core/registry"; |
| 4 | +import { user } from "@web/core/user"; |
| 5 | +import { _t } from "@web/core/l10n/translation"; |
| 6 | +import { NewsletterSubscribeCommonOption } from "./newsletter_subscribe_common_option"; |
| 7 | +import { getSelectorParams } from "@html_builder/utils/utils"; |
| 8 | +import { applyFunDependOnSelectorAndExclude } from "@html_builder/plugins/utils"; |
| 9 | + |
| 10 | +class MailingListSubscribeOptionPlugin extends Plugin { |
| 11 | + static id = "mailingListSubscribeOption"; |
| 12 | + static dependencies = ["remove", "savePlugin"]; |
| 13 | + static shared = ["fetchMailingLists"]; |
| 14 | + resources = { |
| 15 | + builder_actions: [ |
| 16 | + { |
| 17 | + toggleThanksMessage: { |
| 18 | + apply: ({ editingElement }) => { |
| 19 | + this.setThanksMessageVisibility(editingElement, true); |
| 20 | + }, |
| 21 | + clean: ({ editingElement }) => { |
| 22 | + this.setThanksMessageVisibility(editingElement, false); |
| 23 | + }, |
| 24 | + isApplied: ({ editingElement }) => |
| 25 | + editingElement |
| 26 | + .querySelector(".js_subscribed_wrap") |
| 27 | + .classList.contains("o_enable_preview"), |
| 28 | + }, |
| 29 | + }, |
| 30 | + ], |
| 31 | + on_add_element_handlers: this.onAddElement.bind(this), |
| 32 | + clean_for_save_handlers: this.cleanForSave.bind(this), |
| 33 | + }; |
| 34 | + |
| 35 | + setup() { |
| 36 | + this.mailingListSubscribeOptionSelectorParams = getSelectorParams( |
| 37 | + this.getResource("builder_options"), |
| 38 | + NewsletterSubscribeCommonOption |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + setThanksMessageVisibility(editingElement, isVisible) { |
| 43 | + const toSubscribeEl = editingElement.querySelector(".js_subscribe_wrap"); |
| 44 | + const thanksMessageEl = editingElement.querySelector(".js_subscribed_wrap"); |
| 45 | + thanksMessageEl.classList.toggle("o_enable_preview", isVisible); |
| 46 | + thanksMessageEl.classList.toggle("o_disable_preview", !isVisible); |
| 47 | + toSubscribeEl.classList.toggle("o_enable_preview", !isVisible); |
| 48 | + toSubscribeEl.classList.toggle("o_disable_preview", isVisible); |
| 49 | + } |
| 50 | + |
| 51 | + async onAddElement({ elementToAdd }) { |
| 52 | + for (const mailingListSubscribeOptionSelector of this |
| 53 | + .mailingListSubscribeOptionSelectorParams) { |
| 54 | + applyFunDependOnSelectorAndExclude( |
| 55 | + this.addNewsletterListElement.bind(this), |
| 56 | + elementToAdd, |
| 57 | + mailingListSubscribeOptionSelector |
| 58 | + ); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + async addNewsletterListElement(elementToAdd) { |
| 63 | + await this.fetchMailingLists(); |
| 64 | + if (this.mailingLists.length) { |
| 65 | + elementToAdd.dataset.listId = this.mailingLists[0].id; |
| 66 | + } else { |
| 67 | + this.services.dialog.add(ConfirmationDialog, { |
| 68 | + body: _t( |
| 69 | + "No mailing list found, do you want to create a new one? This will save all your changes, are you sure you want to proceed?" |
| 70 | + ), |
| 71 | + confirm: async () => { |
| 72 | + await this.dependencies.savePlugin.save(); |
| 73 | + window.location.href = |
| 74 | + "/odoo/action-mass_mailing.action_view_mass_mailing_lists"; |
| 75 | + }, |
| 76 | + cancel: () => { |
| 77 | + this.dependencies.remove.removeElement(elementToAdd); |
| 78 | + }, |
| 79 | + }); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + async fetchMailingLists() { |
| 84 | + if (!this.mailingLists) { |
| 85 | + const context = Object.assign({}, user.context, { |
| 86 | + website_id: this.services.website.currentWebsite.id, |
| 87 | + lang: this.services.website.currentWebsite.metadata.lang, |
| 88 | + user_lang: user.context.lang, |
| 89 | + }); |
| 90 | + const response = await this.services.orm.call( |
| 91 | + "mailing.list", |
| 92 | + "name_search", |
| 93 | + ["", [["is_public", "=", true]]], |
| 94 | + { context } |
| 95 | + ); |
| 96 | + this.mailingLists = []; |
| 97 | + for (const entry of response) { |
| 98 | + this.mailingLists.push({ id: entry[0], name: entry[1] }); |
| 99 | + } |
| 100 | + } |
| 101 | + return this.mailingLists; |
| 102 | + } |
| 103 | + |
| 104 | + cleanForSave({ root }) { |
| 105 | + for (const mailingListSubscribeOptionSelector of this |
| 106 | + .mailingListSubscribeOptionSelectorParams) { |
| 107 | + applyFunDependOnSelectorAndExclude( |
| 108 | + this.removePreview.bind(this), |
| 109 | + root, |
| 110 | + mailingListSubscribeOptionSelector |
| 111 | + ); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + removePreview(editingElement) { |
| 116 | + const previewClasses = ["o_disable_preview", "o_enable_preview"]; |
| 117 | + const toCleanElsSelector = ".js_subscribe_wrap, .js_subscribed_wrap"; |
| 118 | + const toCleanEls = editingElement.querySelectorAll(toCleanElsSelector); |
| 119 | + for (const toCleanEl of toCleanEls) { |
| 120 | + toCleanEl.classList.remove(...previewClasses); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +registry |
| 126 | + .category("website-plugins") |
| 127 | + .add(MailingListSubscribeOptionPlugin.id, MailingListSubscribeOptionPlugin); |
0 commit comments