summaryrefslogtreecommitdiff
path: root/addons/website/static/src/js/website.translator.js
diff options
context:
space:
mode:
authorChristophe Matthieu <chm@odoo.com>2015-07-07 16:02:53 +0200
committerChristophe Matthieu <chm@odoo.com>2015-07-10 17:00:11 +0200
commit8c559044a5e1be111dcb577ad7e5ab462c1002a0 (patch)
treeba1602e15b2bca38babd6cca63a41da1d05b4321 /addons/website/static/src/js/website.translator.js
parenta616c57c609da0e5e56835dcb45e1c05590d25b8 (diff)
[IMP] web_editor: split website into web_editor (to use editor in backend for html field) and website
Diffstat (limited to 'addons/website/static/src/js/website.translator.js')
-rw-r--r--addons/website/static/src/js/website.translator.js254
1 files changed, 53 insertions, 201 deletions
diff --git a/addons/website/static/src/js/website.translator.js b/addons/website/static/src/js/website.translator.js
index 775012e1aa8..cb02f7346e9 100644
--- a/addons/website/static/src/js/website.translator.js
+++ b/addons/website/static/src/js/website.translator.js
@@ -2,229 +2,81 @@ odoo.define('website.translator', function (require) {
'use strict';
var core = require('web.core');
-var editor = require('website.editor');
+var ajax = require('web.ajax');
+var Widget = require('web.Widget');
+var base = require('web_editor.base');
+var translate = require('web_editor.translate');
var website = require('website.website');
-var ajax = require('ajax');
-var QWeb = core.qweb;
+var qweb = core.qweb;
-if (!website.translatable) {
- // Temporary hack until the editor bar is moved to the web client
+if (!translate.translatable) {
return;
}
-website.add_template_file('/website/static/src/xml/website.translator.xml');
-var nodialog = 'website_translator_nodialog';
-
-editor.EditorBar.include({
- do_not_translate : ['-','*','!'],
- start: function () {
- var self = this;
- this.initial_content = {};
- return this._super.apply(this, arguments).then(function () {
- var $edit_button = $("button[data-action=edit]");
- $edit_button.removeClass("hidden");
-
- if(website.no_editor) {
- $edit_button.removeProp('disabled');
- } else {
- $edit_button.parent().after(QWeb.render('website.TranslatorAdditionalButtons'));
- $('a[data-action=edit_master]').on('click', self, function(ev) {
- self.edit_master(ev);
- });
- }
- $('.js_hide_on_translate').hide();
- });
- },
- edit: function () {
- var self = this;
- var mysuper = this._super;
- if (!localStorage[nodialog]) {
- var dialog = new website.TranslatorDialog();
- dialog.appendTo($(document.body));
- dialog.on('activate', this, function () {
- localStorage[nodialog] = dialog.$('input[name=do_not_show]').prop('checked') || '';
- dialog.$el.modal('hide');
- self.translate().then(function () {
- mysuper.call(self, true);
- if(self.gengo_translate){
- self.translation_gengo_display()
- }
- });
- });
+website.TopBar.include({
+ events: _.extend({}, website.TopBar.prototype.events, {
+ 'click [data-action="edit_master"]': 'edit_master',
+ 'click [data-action="translate"]': 'translate',
+ }),
+ translate: function (ev) {
+ ev.preventDefault();
+ if (translate.edit_translations) {
+ translate.instance.edit();
} else {
- this.translate().then(function () {
- mysuper.call(self, true);
- if(self.gengo_translate){
- self.translation_gengo_display()
- }
- });
+ location.search += '&edit_translations';
}
},
edit_master: function (ev) {
ev.preventDefault();
- var link = $('.js_language_selector a[data-default-lang]')[0];
- if (link) {
- link.search += (link.search ? '&' : '?') + 'enable_editor=1';
- var url = link.pathname + link.search + window.location.hash;
- link.pathname = '/website/lang/default';
- link.search = '?' + $.param({r: url});
- window.location = link.href;
- }
+ var $link = $('.js_language_selector a[data-default-lang]');
+ $link[0].search += ($link[0].search ? '&' : '?') + 'enable_editor=1';
+ $link.click();
},
- translate: function () {
- var self = this;
- this.translations = null;
- return ajax.jsonRpc('/website/get_view_translations', 'call', {
- 'xml_id': $(document.documentElement).data('view-xmlid'),
- 'lang': website.get_context().lang,
- }).then(function (translations) {
- self.translations = translations;
- self.processTranslatableNodes();
- // Disable non translatable t-fields
- $('[data-oe-type][data-oe-translate="0"]').removeAttr('data-oe-type');
- });
- },
- processTranslatableNodes: function () {
- var self = this;
- var source_attr = 'data-oe-source-id';
- var $editables = $('[data-oe-model="ir.ui.view"]')
- .not('link, script')
- .not('.oe_snippets,.oe_snippet, .oe_snippet *, .navbar-toggle')
- .not('[data-oe-type]')
- .add($('[data-oe-translate="1"]:not([data-oe-model="ir.ui.view"])').addClass('o_editable'));
+});
- $editables.each(function () {
- var $node = $(this);
- var source_id = $node.parents('[' + source_attr + ']:first').attr(source_attr)|0;
- var view_id = $node.attr('data-oe-source-id') || source_id || $node.attr('data-oe-id');
- self.transNode(this, view_id|0);
- });
- $('.oe_translatable_text').on('paste', function () {
- var node = $(this);
- setTimeout(function () {
- self.sanitizeNode(node);
- }, 0);
- });
- $(document).on('blur keyup paste', '.oe_translatable_text[contenteditable]', function(ev) {
- var $node = $(this);
- setTimeout(function () {
- // Doing stuff next tick because paste and keyup events are
- // fired before the content is changed
- if (ev.type == 'paste') {
- self.sanitizeNode($node[0]);
- }
- if (self.getInitialContent($node[0]) !== $node.text()) {
- $node.addClass('o_dirty').removeClass('oe_translatable_todo oe_translatable_inprogress');
- }
- }, 0);
- });
- },
- getInitialContent: function (node) {
- return this.initial_content[node.attributes['data-oe-nodeid'].value];
- },
- sanitizeNode: function (node) {
- node.text(node.text());
- },
- isTextNode: function (node) {
- return node.nodeType === 3 || node.nodeType === 4;
- },
- isTranslatable: function (text) {
- return text && _.str.trim(text) !== '';
- },
- markTranslatableNode: function (node, view_id) {
- // TODO: link nodes with same content
- node.className += ' oe_translatable_text';
- node.setAttribute('data-oe-translation-view-id', view_id);
- var content = $(node).text().trim();
- var trans = this.translations.filter(function (t) {
- return t.res_id === view_id && t.value.trim() === content;
- });
- if (trans.length) {
- node.setAttribute('data-oe-translation-id', trans[0].id);
- if(trans[0].gengo_translation && (trans[0].state == 'inprogress' || trans[0].state == 'to_translate')){
- node.className += ' oe_translatable_inprogress';
- }
- } else {
- node.className += this.do_not_translate.indexOf(node.textContent.trim()) ? ' oe_translatable_todo' : '';
+
+if (!translate.edit_translations) {
+ return;
+}
+
+ajax.loadXML('/website/static/src/xml/website.translator.xml', qweb);
+
+var nodialog = 'website_translator_nodialog';
+
+var Translate = translate.Class.include({
+ onTranslateReady: function () {
+ if(this.gengo_translate){
+ this.translation_gengo_display();
}
- node.contentEditable = true;
- var nid = _.uniqueId();
- $(node).attr('data-oe-nodeid', nid);
- this.initial_content[nid] = content;
+ this._super();
},
- save: function () {
- var self = this;
- var trans = {};
- // this._super.apply(this, arguments);
- $('.oe_translatable_text.o_dirty').each(function () {
- var $node = $(this);
- var data = $node.data();
- if (!trans[data.oeTranslationViewId]) {
- trans[data.oeTranslationViewId] = [];
+ edit: function () {
+ $("#oe_main_menu_navbar").hide();
+ if (localStorage[nodialog]) {
+ return;
+ }
+ var dialog = new TranslatorDialog();
+ dialog.appendTo($(document.body));
+ dialog.on('activate', this, function () {
+ if (dialog.$('input[name=do_not_show]').prop('checked')) {
+ localStorage.removeItem(nodialog);
+ } else {
+ localStorage.setItem(nodialog, true);
}
- trans[data.oeTranslationViewId].push({
- initial_content: self.getInitialContent(this),
- new_content: $node.text(),
- translation_id: data.oeTranslationId || null
- });
+ dialog.$el.modal('hide');
});
- ajax.jsonRpc('/website/set_translations', 'call', {
- 'data': trans,
- 'lang': website.get_context()['lang'],
- }).then(function () {
- self._save();
- }).fail(function () {
- // TODO: bootstrap alert with error message
- alert("Could not save translation");
- });
- },
- transNode: function (node, view_id) {
- // Mostly handling text and cdata nodes here
- // so avoid jquery usage in this function
- if (node.attributes['data-oe-type']) {
- if (node.attributes['data-oe-translate'].value == '1') {
- node.className += ' oe_translatable_field';
- }
- this.markTranslatableNode(node, view_id);
- } else if (node.childNodes.length === 1
- && this.isTextNode(node.childNodes[0])
- && !node.getAttribute('data-oe-model')) {
- this.markTranslatableNode(node, view_id);
- } else {
- for (var i = 0, l = node.childNodes.length; i < l; i ++) {
- var n = node.childNodes[i];
- if (this.isTextNode(n)) {
- if (this.isTranslatable(n.data)) {
- var container = document.createElement('span');
- node.insertBefore(container, n);
- container.appendChild(n);
- this.markTranslatableNode(container, view_id);
- }
- } else {
- this.transNode(n, view_id);
- }
- }
- }
+ return this._super();
},
-});
-
-website.RTE.include({
- start: function () {
- this._super.apply(this, arguments);
- this.$el.hide();
- },
- fetch_editables: function (root) {
- $(root).click(function (ev) {
- ev.preventDefault();
- });
- return $('[data-oe-translate="1"]');
+ cancel: function () {
+ $("#oe_main_menu_navbar").show();
+ return this._super();
}
});
-website.TranslatorDialog = core.Widget.extend({
- events: _.extend({}, website.EditorBar.prototype.events, {
+var TranslatorDialog = Widget.extend({
+ events: _.extend({}, website.TopBar.prototype.events, {
'hidden.bs.modal': 'destroy',
'click button[data-action=activate]': function (ev) {
this.trigger('activate');