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/js/menu/translate.js | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/website/static/src/js/menu/translate.js')
| -rw-r--r-- | addons/website/static/src/js/menu/translate.js | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/addons/website/static/src/js/menu/translate.js b/addons/website/static/src/js/menu/translate.js new file mode 100644 index 00000000..afb2aff2 --- /dev/null +++ b/addons/website/static/src/js/menu/translate.js @@ -0,0 +1,88 @@ +odoo.define('website.translateMenu', function (require) { +'use strict'; + +var utils = require('web.utils'); +var TranslatorMenu = require('website.editor.menu.translate'); +var websiteNavbarData = require('website.navbar'); + +var TranslatePageMenu = websiteNavbarData.WebsiteNavbarActionWidget.extend({ + assetLibs: ['web_editor.compiled_assets_wysiwyg', 'website.compiled_assets_wysiwyg'], + + actions: _.extend({}, websiteNavbarData.WebsiteNavbar.prototype.actions || {}, { + edit_master: '_goToMasterPage', + translate: '_startTranslateMode', + }), + + /** + * @override + */ + start: function () { + var context; + this.trigger_up('context_get', { + extra: true, + callback: function (ctx) { + context = ctx; + }, + }); + this._mustEditTranslations = context.edit_translations; + if (this._mustEditTranslations) { + var url = window.location.href.replace(/([?&])&*edit_translations[^&#]*&?/, '\$1'); + window.history.replaceState({}, null, url); + + this._startTranslateMode(); + } + return this._super.apply(this, arguments); + }, + + //-------------------------------------------------------------------------- + // Actions + //-------------------------------------------------------------------------- + + /** + * Redirects the user to the same page but in the original language and in + * edit mode. + * + * @private + * @returns {Promise} + */ + _goToMasterPage: function () { + var current = document.createElement('a'); + current.href = window.location.toString(); + current.search += (current.search ? '&' : '?') + 'enable_editor=1'; + // we are in translate mode, the pathname starts with '/<url_code/' + current.pathname = current.pathname.substr(Math.max(0, current.pathname.indexOf('/', 1))); + + var link = document.createElement('a'); + link.href = '/website/lang/default'; + link.search += (link.search ? '&' : '?') + 'r=' + encodeURIComponent(current.pathname + current.search + current.hash); + + window.location = link.href; + return new Promise(function () {}); + }, + /** + * Redirects the user to the same page in translation mode (or start the + * translator is translation mode is already enabled). + * + * @private + * @returns {Promise} + */ + _startTranslateMode: function () { + if (!this._mustEditTranslations) { + window.location.search += '&edit_translations'; + return new Promise(function () {}); + } + + var translator = new TranslatorMenu(this); + + // We don't want the BS dropdown to close + // when clicking in a element to translate + $('.dropdown-menu').on('click', '.o_editable', function (ev) { + ev.stopPropagation(); + }); + + return translator.prependTo(document.body); + }, +}); + +websiteNavbarData.websiteNavbarRegistry.add(TranslatePageMenu, '.o_menu_systray:has([data-action="translate"])'); +}); |
