Permalink
Browse files

[IMP]web: translation issue fixed

Before this commit:
After transating any field -> clicking on transalation warning -> come back through breadcrumb do not preserve translation warning.

After this commit:
After transating any field -> clicking on transalation warning -> come back through breadcrumb preserves warning, warning alert remains there until view or record is changed.

alertFields is now object which stores tranlsation alerts per record, so changing record and come back to previous record for which alert fields are available then form renderer display those alert fields until form renderer is destroyed.

Issue-ID: 1877480

Co-authored-by: Mohammed Shekha <msh@openerp.com>
  • Loading branch information...
tsh-odoo and msh-odoo committed Sep 12, 2018
1 parent e6c2721 commit 7979a5264327b98c0d2eae4e0e1bf5216a776228
@@ -219,17 +219,16 @@ var FormController = BasicController.extend({
// are displayed with an alert
var fields = self.renderer.state.fields;
var data = self.renderer.state.data;
var alertFields = [];
var alertFields = {};
for (var k = 0; k < changedFields.length; k++) {
var field = fields[changedFields[k]];
var fieldData = data[changedFields[k]];
if (field.translate && fieldData) {
alertFields.push(field);
alertFields[changedFields[k]] = field;
}
}
if (alertFields.length) {
self.renderer.alertFields = alertFields;
self.renderer.displayTranslationAlert();
if (!_.isEmpty(alertFields)) {
self.renderer.updateAlertFields(alertFields);
}
}
return changedFields;
@@ -13,6 +13,7 @@ var FormRenderer = BasicRenderer.extend({
className: "o_form_view",
events: _.extend({}, BasicRenderer.prototype.events, {
'click .o_notification_box .oe_field_translate': '_onTranslate',
'click .o_notification_box .close': '_onTranslateNotificationClose',
'click .oe_title, .o_inner_group': '_onClick',
}),
custom_events: _.extend({}, BasicRenderer.prototype.custom_events, {
@@ -29,6 +30,7 @@ var FormRenderer = BasicRenderer.extend({
init: function () {
this._super.apply(this, arguments);
this.idsForLabels = {};
this.alertFields = {};
this.lastActivatedFieldIndex = -1;
},
/**
@@ -103,23 +105,32 @@ var FormRenderer = BasicRenderer.extend({
});
return fieldNames;
},
/*
* Updates translation alert fields for the current state and display updated fields
*
* @param {Object} alertFields
*/
updateAlertFields: function (alertFields) {
this.alertFields[this.state.res_id] = _.extend(this.alertFields[this.state.res_id] || {}, alertFields);
this.displayTranslationAlert();
},
/**
* Show a warning message if the user modified a translated field. For each
* field, the notification provides a link to edit the field's translations.
*
* @param {Object[]} alertFields field list
*/
displayTranslationAlert: function () {
this.$('.o_notification_box').remove();
var $notification = $(qweb.render('notification-box', {type: 'info'}))
.append(qweb.render('translation-alert', {
fields: this.alertFields,
lang: _t.database.parameters.name
}));
if (this.$('.o_form_statusbar').length) {
this.$('.o_form_statusbar').after($notification);
} else {
this.$el.prepend($notification);
if (this.alertFields[this.state.res_id]) {
var $notification = $(qweb.render('notification-box', {type: 'info'}))
.append(qweb.render('translation-alert', {
fields: this.alertFields[this.state.res_id],
lang: _t.database.parameters.name
}));
if (this.$('.o_form_statusbar').length) {
this.$('.o_form_statusbar').after($notification);
} else {
this.$el.prepend($notification);
}
}
},
/**
@@ -905,6 +916,9 @@ var FormRenderer = BasicRenderer.extend({

return $.when.apply($, defs).then(function () {
self._updateView($form.contents());
if (!_.isEmpty(self.alertFields) && self.state.res_id in self.alertFields) {
self.displayTranslationAlert();
}
}, function () {
$form.remove();
}).then(function(){
@@ -1018,6 +1032,15 @@ var FormRenderer = BasicRenderer.extend({
event.preventDefault();
this.trigger_up('translate', {fieldName: event.target.name, id: this.state.id});
},
/**
* remove alert fields of record from alertFields object
*
* @private
* @param {MouseEvent} ev
*/
_onTranslateNotificationClose: function(ev) {
delete this.alertFields[this.state.res_id];
},
});

return FormRenderer;
@@ -1525,7 +1525,7 @@
<div role="alertdialog">
Please update translations of :
<t t-foreach="fields" t-as="field">
<strong><a class="oe_field_translate" t-att-name="field.name" href="#"><t t-esc="field.string"/><t t-if="!field_last">, </t></a></strong>
<strong><a class="oe_field_translate" t-att-name="field" href="#"><t t-esc="field_value.string"/><t t-if="field_index &lt; _.size(fields)-1">, </t></a></strong>
</t>
</div>
</t>
@@ -5620,9 +5620,10 @@ QUnit.module('Views', {
});

QUnit.test('display translation alert', function (assert) {
assert.expect(1);
assert.expect(2);

this.data.partner.fields.foo.translate = true;
this.data.partner.fields.display_name.translate = true;

var multi_lang = _t.database.multi_lang;
_t.database.multi_lang = true;
@@ -5635,6 +5636,7 @@ QUnit.module('Views', {
'<sheet>' +
'<group>' +
'<field name="foo"/>' +
'<field name="display_name"/>' +
'</group>' +
'</sheet>' +
'</form>',
@@ -5645,13 +5647,65 @@ QUnit.module('Views', {
form.$('input[name="foo"]').val("test").trigger("input");
form.$buttons.find('.o_form_button_save').click();

assert.strictEqual(form.$('.o_form_view > .alert > div').length, 1,"should have a translation alert");
assert.strictEqual(form.$('.o_form_view > .alert > div .oe_field_translate').length, 1,"should have single translation alert");
form.$buttons.find('.o_form_button_edit').click();
form.$('input[name="display_name"]').val("test2").trigger("input");
form.$buttons.find('.o_form_button_save').click();
assert.strictEqual(form.$('.o_form_view > .alert > div .oe_field_translate').length, 2,"should have two translate fields in translation alert");

form.destroy();

_t.database.multi_lang = multi_lang;
});

QUnit.test('translation alert are preserved on pager change', function (assert) {
assert.expect(5);

this.data.partner.fields.foo.translate = true;

var multi_lang = _t.database.multi_lang;
_t.database.multi_lang = true;

var form = createView({
View: FormView,
model: 'partner',
data: this.data,
arch: '<form string="Partners">' +
'<sheet>' +
'<field name="foo"/>' +
'</sheet>' +
'</form>',
viewOptions: {
ids: [1, 2],
index: 0,
},
res_id: 1,
});

form.$buttons.find('.o_form_button_edit').click();
form.$('input[name="foo"]').val("test").trigger("input");
form.$buttons.find('.o_form_button_save').click();

assert.strictEqual(form.$('.o_form_view > .alert > div').length, 1, "should have a translation alert");

// click on the pager to switch to the next record
form.pager.$('.o_pager_next').click();
assert.strictEqual(form.$('.o_form_view > .alert > div').length, 0, "should not have a translation alert");

// click on the pager to switch back to the previous record
form.pager.$('.o_pager_previous').click();
assert.strictEqual(form.$('.o_form_view > .alert > div').length, 1, "should have a translation alert");

// remove translation alert by click X and check alert even after form reload
form.$('.o_form_view > .alert > .close').click();
assert.strictEqual(form.$('.o_form_view > .alert > div').length, 0, "should not have a translation alert");
form.reload();
assert.strictEqual(form.$('.o_form_view > .alert > div').length, 0, "should not have a translation alert after reload");

form.destroy();
_t.database.multi_lang = multi_lang;
});

QUnit.test('translate event correctly handled with multiple controllers', function (assert) {
assert.expect(3);

0 comments on commit 7979a52

Please sign in to comment.