Debian Codegraph @

addons/website/static/src/snippets/s_facebook_page/facebook_page.js

raw symbols SCIP
1 import { Interaction } from "@web/public/interaction";
2 import { registry } from "@web/core/registry";
3
4 import { _t } from "@web/core/l10n/translation";
5 import { clamp } from "@web/core/utils/numbers";
6 import { pick } from "@web/core/utils/objects";
7
8 export class FacebookPage extends Interaction {
9 static selector = ".o_facebook_page";
10
11 setup() {
12 this.previousWidth = 0;
13 const params = pick(
14 this.el.dataset,
15 "href",
16 "id",
17 "height",
18 "tabs",
19 "small_header",
20 "hide_cover"
21 );
22 if (!params.href) {
23 return;
24 }
25 if (params.id) {
26 params.href = `https://www.facebook.com/${params.id}`;
27 delete params.id;
28 }
29
30 this.renderIframe(params);
31
32 this.resizeObserver = new ResizeObserver(
33 this.debounced(this.renderIframe.bind(this, params), 100)
34 );
35 this.resizeObserver.observe(this.el.parentElement);
36 this.registerCleanup(() => {
37 this.resizeObserver.disconnect();
38 });
39 }
40
41 /**
42 * Prepare iframe element & replace it with existing iframe.
43 *
44 * @param {Object} params
45 */
46 renderIframe(params) {
47 params.width = clamp(Math.floor(this.el.getBoundingClientRect().width), 180, 500);
48 if (this.previousWidth !== params.width) {
49 this.previousWidth = params.width;
50 const searchParams = new URLSearchParams(params);
51
52 const iframeEl = document.createElement("iframe");
53 iframeEl.setAttribute("style", "border: none; overflow: hidden;");
54 iframeEl.setAttribute("aria-label", _t("Facebook"));
55 iframeEl.height = params.height;
56 iframeEl.width = params.width;
57
58 this.el.replaceChildren(iframeEl);
59 this.registerCleanup(() => {
60 iframeEl.remove();
61 });
62
63 const src = "https://www.facebook.com/plugins/page.php?" + searchParams;
64 this.services.website_cookies.manageIframeSrc(iframeEl, src);
65 }
66 }
67 }
68
69 registry.category("public.interactions").add("website.facebook_page", FacebookPage);