diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
| commit | 3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch) | |
| tree | a44932296ef4a9b71d5f010906253d8c53727726 /addons/website/static/src/snippets/s_facebook_page | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/website/static/src/snippets/s_facebook_page')
| -rw-r--r-- | addons/website/static/src/snippets/s_facebook_page/000.js | 56 | ||||
| -rw-r--r-- | addons/website/static/src/snippets/s_facebook_page/options.js | 157 |
2 files changed, 213 insertions, 0 deletions
diff --git a/addons/website/static/src/snippets/s_facebook_page/000.js b/addons/website/static/src/snippets/s_facebook_page/000.js new file mode 100644 index 00000000..0a88f1b3 --- /dev/null +++ b/addons/website/static/src/snippets/s_facebook_page/000.js @@ -0,0 +1,56 @@ +odoo.define('website.s_facebook_page', function (require) { +'use strict'; + +var publicWidget = require('web.public.widget'); +var utils = require('web.utils'); + +const FacebookPageWidget = publicWidget.Widget.extend({ + selector: '.o_facebook_page', + disabledInEditableMode: false, + + /** + * @override + */ + start: function () { + var def = this._super.apply(this, arguments); + + var params = _.pick(this.$el.data(), 'href', 'height', 'tabs', 'small_header', 'hide_cover', 'show_facepile'); + if (!params.href) { + return def; + } + params.width = utils.confine(Math.floor(this.$el.width()), 180, 500); + + var src = $.param.querystring('https://www.facebook.com/plugins/page.php', params); + this.$iframe = $('<iframe/>', { + src: src, + class: 'o_temp_auto_element', + width: params.width, + height: params.height, + css: { + border: 'none', + overflow: 'hidden', + }, + scrolling: 'no', + frameborder: '0', + allowTransparency: 'true', + }); + this.$el.append(this.$iframe); + + return def; + }, + /** + * @override + */ + destroy: function () { + this._super.apply(this, arguments); + + if (this.$iframe) { + this.$iframe.remove(); + } + }, +}); + +publicWidget.registry.facebookPage = FacebookPageWidget; + +return FacebookPageWidget; +}); diff --git a/addons/website/static/src/snippets/s_facebook_page/options.js b/addons/website/static/src/snippets/s_facebook_page/options.js new file mode 100644 index 00000000..da6c94dc --- /dev/null +++ b/addons/website/static/src/snippets/s_facebook_page/options.js @@ -0,0 +1,157 @@ +odoo.define('website.s_facebook_page_options', function (require) { +'use strict'; + +const options = require('web_editor.snippets.options'); + +options.registry.facebookPage = options.Class.extend({ + /** + * Initializes the required facebook page data to create the iframe. + * + * @override + */ + willStart: function () { + var defs = [this._super.apply(this, arguments)]; + + var defaults = { + href: '', + height: 215, + width: 350, + tabs: '', + small_header: true, + hide_cover: true, + show_facepile: false, + }; + this.fbData = _.defaults(_.pick(this.$target.data(), _.keys(defaults)), defaults); + + if (!this.fbData.href) { + // Fetches the default url for facebook page from website config + var self = this; + defs.push(this._rpc({ + model: 'website', + method: 'search_read', + args: [[], ['social_facebook']], + limit: 1, + }).then(function (res) { + if (res) { + self.fbData.href = res[0].social_facebook || ''; + } + })); + } + + return Promise.all(defs).then(() => this._markFbElement()).then(() => this._refreshPublicWidgets()); + }, + + //-------------------------------------------------------------------------- + // Options + //-------------------------------------------------------------------------- + + /** + * Toggles a checkbox option. + * + * @see this.selectClass for parameters + * @param {String} optionName the name of the option to toggle + */ + toggleOption: function (previewMode, widgetValue, params) { + let optionName = params.optionName; + if (optionName.startsWith('tab.')) { + optionName = optionName.replace('tab.', ''); + if (widgetValue) { + this.fbData.tabs = this.fbData.tabs + .split(',') + .filter(t => t !== '') + .concat([optionName]) + .join(','); + } else { + this.fbData.tabs = this.fbData.tabs + .split(',') + .filter(t => t !== optionName) + .join(','); + } + } else { + if (optionName === 'show_cover') { + this.fbData.hide_cover = !widgetValue; + } else { + this.fbData[optionName] = widgetValue; + } + } + return this._markFbElement(); + }, + /** + * Sets the facebook page's URL. + * + * @see this.selectClass for parameters + */ + pageUrl: function (previewMode, widgetValue, params) { + this.fbData.href = widgetValue; + return this._markFbElement(); + }, + + //-------------------------------------------------------------------------- + // Private + //-------------------------------------------------------------------------- + + /** + * Sets the correct dataAttributes on the facebook iframe and refreshes it. + * + * @see this.selectClass for parameters + */ + _markFbElement: function () { + return this._checkURL().then(() => { + // Managing height based on options + if (this.fbData.tabs) { + this.fbData.height = this.fbData.tabs === 'events' ? 300 : 500; + } else if (this.fbData.small_header) { + this.fbData.height = this.fbData.show_facepile ? 165 : 70; + } else { + this.fbData.height = this.fbData.show_facepile ? 225 : 150; + } + _.each(this.fbData, (value, key) => { + this.$target.attr('data-' + key, value); + this.$target.data(key, value); + }); + }); + }, + /** + * @override + */ + _computeWidgetState: function (methodName, params) { + const optionName = params.optionName; + switch (methodName) { + case 'toggleOption': { + if (optionName.startsWith('tab.')) { + return this.fbData.tabs.split(',').includes(optionName.replace(/^tab./, '')); + } else { + if (optionName === 'show_cover') { + return !this.fbData.hide_cover; + } + return this.fbData[optionName]; + } + } + case 'pageUrl': { + return this._checkURL().then(() => this.fbData.href); + } + } + return this._super(...arguments); + }, + /** + * @private + */ + _checkURL: function () { + const defaultURL = 'https://www.facebook.com/Odoo'; + const match = this.fbData.href.match(/^(?:https?:\/\/)?(?:www\.)?(?:fb|facebook)\.com\/(?:([\w.]+)|[^/?#]+-([0-9]{15,16}))(?:$|[/?# ])/); + if (match) { + // Check if the page exists on Facebook or not + return new Promise((resolve, reject) => $.ajax({ + url: 'https://graph.facebook.com/' + (match[2] || match[1]) + '/picture', + success: () => resolve(), + error: () => { + this.fbData.href = defaultURL; + resolve(); + }, + })); + } + this.fbData.href = defaultURL; + return Promise.resolve(); + }, +}); +}); |
