Skip to content

Commit 12e117d

Browse files
blse-odooFrancoisGe
authored andcommitted
InstagramPage (.s_instagram_page)
1 parent b355875 commit 12e117d

File tree

7 files changed

+170
-24
lines changed

7 files changed

+170
-24
lines changed

addons/html_builder/static/src/plugins/facebook_option_plugin.js

+20-18
Original file line numberDiff line numberDiff line change
@@ -90,31 +90,33 @@ class FacebookOptionPlugin extends Plugin {
9090
}
9191
}
9292

93-
loadAndSetEmptyLink(nodes) {
93+
async loadAndSetEmptyLink(nodes) {
94+
// TODO: look in shared cache with social info: was SocialMediaOption.getDbSocialValuesCache()
9495
if (this.facebookUrl) {
9596
this.setEmptyLink(nodes);
9697
return;
9798
}
9899
// Fetches the default url for facebook page from website config
99-
this.services.orm
100-
.searchRead("website", [], ["social_facebook"], { limit: 1 })
101-
.then((res) => {
102-
if (res && res[0].social_facebook) {
103-
this.facebookUrl = res[0].social_facebook;
100+
const res = this.services.orm.read(
101+
"website",
102+
[this.services.website.currentWebsite.id],
103+
["social_facebook"]
104+
);
105+
if (res && res[0].social_facebook) {
106+
this.facebookUrl = res[0].social_facebook;
104107

105-
// WARNING: the call to disableObserver and enableObserver are very dangerous,
106-
// and should be avoided in most cases (if you think you need those, ask html_editor team)
107-
this.dependencies.history.disableObserver();
108-
const hasChanged = this.setEmptyLink(nodes);
109-
this.dependencies.history.enableObserver();
108+
// WARNING: the call to ignoreDOMMutations is very dangerous,
109+
// and should be avoided in most cases (if you think you need those, ask html_editor team)
110+
const hasChanged = this.dependencies.history.ignoreDOMMutations(() =>
111+
this.setEmptyLink(nodes)
112+
);
110113

111-
if (hasChanged) {
112-
const commonAncestor = getCommonAncestor(nodes, this.editable);
113-
this.dispatchTo("content_manually_updated_handlers", commonAncestor);
114-
this.config.onChange({ isPreviewing: false });
115-
}
116-
}
117-
});
114+
if (hasChanged) {
115+
const commonAncestor = getCommonAncestor(nodes, this.editable);
116+
this.dispatchTo("content_manually_updated_handlers", commonAncestor);
117+
this.config.onChange({ isPreviewing: false });
118+
}
119+
}
118120
}
119121

120122
setEmptyLink(nodes) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<templates xml:space="preserve">
3+
4+
<t t-name="html_builder.InstagramOption">
5+
<div class="alert alert-primary p-2 m-2">
6+
Your instagram page must be public to be integrated into an Odoo website.
7+
</div>
8+
<BuilderRow label.translate="Instagram Page">
9+
<BuilderTextInput action="'instagramPageAction'" placeholder="'odoo.official'" preview="false"/>
10+
</BuilderRow>
11+
</t>
12+
13+
</templates>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import { Plugin } from "@html_editor/plugin";
2+
import { registry } from "@web/core/registry";
3+
import { _t } from "@web/core/l10n/translation";
4+
import { getCommonAncestor } from "@html_editor/utils/dom_traversal";
5+
import { withSequence } from "@html_editor/utils/resource";
6+
7+
class InstagramOptionPlugin extends Plugin {
8+
static id = "instagramOption";
9+
static dependencies = ["history"];
10+
11+
resources = {
12+
builder_options: [
13+
withSequence(40, {
14+
template: "html_builder.InstagramOption",
15+
selector: ".s_instagram_page",
16+
}),
17+
],
18+
builder_actions: {
19+
instagramPageAction: {
20+
getValue: ({ editingElement }) => editingElement.dataset["instagramPage"],
21+
apply: ({ editingElement, value }) => {
22+
delete editingElement.dataset.instagramPageIsDefault;
23+
if (value.includes(this.instagramUrlStr)) {
24+
value = this.instagramPageNameFromUrl(value) || "";
25+
}
26+
editingElement.dataset["instagramPage"] = value;
27+
if (value === "") {
28+
this.services.notification.add(_t("The Instagram page name is not valid"), {
29+
type: "warning",
30+
});
31+
}
32+
},
33+
},
34+
},
35+
normalize_handlers: this.normalize.bind(this),
36+
};
37+
38+
setup() {
39+
this.instagramUrlStr = "instagram.com/";
40+
}
41+
42+
normalize(root) {
43+
const SELECTOR = ".s_instagram_page[data-instagram-page-is-default]";
44+
const nodes = [
45+
...root.querySelectorAll(SELECTOR),
46+
...(root.matches(SELECTOR) ? [root] : []),
47+
];
48+
if (nodes.length) {
49+
this.loadAndSetPage(nodes);
50+
}
51+
}
52+
53+
async loadAndSetPage(nodes) {
54+
// TODO: look in shared cache with social info: was SocialMediaOption.getDbSocialValuesCache()
55+
if (this.instagramUrl) {
56+
this.setPage(nodes);
57+
return;
58+
}
59+
// Fetches the default url for instagram page from website config
60+
const res = await this.services.orm.read(
61+
"website",
62+
[this.services.website.currentWebsite.id],
63+
["social_instagram"]
64+
);
65+
if (res && res[0].social_instagram) {
66+
this.instagramUrl = this.instagramPageNameFromUrl(res[0].social_instagram);
67+
68+
// WARNING: the call to ignoreDOMMutations is very dangerous,
69+
// and should be avoided in most cases (if you think you need those, ask html_editor team)
70+
const hasChanged = this.dependencies.history.ignoreDOMMutations(() =>
71+
this.setPage(nodes)
72+
);
73+
74+
if (hasChanged) {
75+
const commonAncestor = getCommonAncestor(nodes, this.editable);
76+
this.dispatchTo("content_manually_updated_handlers", commonAncestor);
77+
this.config.onChange({ isPreviewing: false });
78+
}
79+
}
80+
}
81+
82+
setPage(nodes) {
83+
let hasChanged = false;
84+
for (const element of nodes) {
85+
if (element.dataset.instagramPageIsDefault) {
86+
delete element.dataset.instagramPageIsDefault;
87+
if (this.instagramUrl) {
88+
element.dataset.instagramPage = this.instagramUrl;
89+
}
90+
hasChanged = true;
91+
}
92+
}
93+
return hasChanged;
94+
}
95+
96+
/**
97+
* Returns the instagram page name from the given url.
98+
*
99+
* @private
100+
* @param {string} url
101+
* @returns {string|undefined}
102+
*/
103+
instagramPageNameFromUrl(url) {
104+
const pageName = url.split(this.instagramUrlStr)[1];
105+
if (
106+
!pageName ||
107+
pageName.includes("?") ||
108+
pageName.includes("#") ||
109+
(pageName.includes("/") && pageName.split("/")[1].length > 0)
110+
) {
111+
return;
112+
}
113+
return pageName.split("/")[0];
114+
}
115+
}
116+
117+
registry.category("website-plugins").add(InstagramOptionPlugin.id, InstagramOptionPlugin);

addons/html_editor/static/src/core/history_plugin.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ export class HistoryPlugin extends Plugin {
316316
ignoreDOMMutations(callback) {
317317
const enableObserver = this.disableObserver();
318318
try {
319-
callback();
319+
return callback();
320320
} finally {
321321
enableObserver();
322322
}

addons/website/static/src/snippets/s_instagram_page/instagram_page.js

-4
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,3 @@ export class InstagramPage extends Interaction {
6464
registry
6565
.category("public.interactions")
6666
.add("website.instagram_page", InstagramPage);
67-
68-
registry
69-
.category("public.interactions.edit")
70-
.add("website.instagram_page", { Interaction: InstagramPage });
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { registry } from "@web/core/registry";
2+
import { InstagramPage } from "./instagram_page";
3+
4+
const InstagramPageEdit = I => class extends I {
5+
setup () {
6+
super.setup();
7+
this.dynamicContent["iframe"] = {
8+
"t-att-style": () => ({ "pointer-events": "none" }),
9+
};
10+
}
11+
}
12+
13+
registry
14+
.category("public.interactions.edit")
15+
.add("website.instagram_page", {
16+
Interaction: InstagramPage,
17+
mixin: InstagramPageEdit,
18+
});

addons/website/views/snippets/s_instagram_page.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<odoo>
33

44
<template id="s_instagram_page" name="Instagram Page">
5-
<section class="s_instagram_page" data-instagram-page="odoo.official">
5+
<section class="s_instagram_page" data-instagram-page="odoo.official" data-instagram-page-is-default="true">
66
<div class="o_container_small o_instagram_container o_not_editable">
77
<!-- The iframe will be added here by the public widget. -->
88
</div>

0 commit comments

Comments
 (0)