~opencrea/+junk/aprobio

« back to all changes in this revision

Viewing changes to web_translate_dialog/static/src/js/web_translate_dialog.js

  • Committer: joannes
  • Date: 2017-05-17 09:40:42 UTC
  • Revision ID: joannes@debian-20170517094042-47q3j6on72w2h1il
community module

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2012 Guewen Baconnier (Camptocamp SA)
 
2
   Copyright 2016 Antonio Espinosa <antonio.espinosa@tecnativa.com>
 
3
 * License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). */
 
4
 
 
5
odoo.define('web_translate_dialog.translate_dialog', function(require){
 
6
"use strict";
 
7
 
 
8
var core = require('web.core');
 
9
var common = require('web.form_common');
 
10
var data = require('web.data');
 
11
 
 
12
var Dialog = require('web.Dialog');
 
13
var FormView = require('web.FormView');
 
14
var View = require('web.View');
 
15
 
 
16
var _t = core._t;
 
17
var QWeb = core.qweb;
 
18
 
 
19
var translateDialog = Dialog.extend({
 
20
    template: "TranslateDialog",
 
21
    init: function(parent, field, content) {
 
22
        this._super(parent,
 
23
                    {title: _t("Translations"),
 
24
                     width: '90%',
 
25
                     height: '80%'},
 
26
                    content);
 
27
        this.view_language = this.session.user_context.lang;
 
28
        this.view = parent;
 
29
        this.view_type = parent.fields_view.type || '';
 
30
        this.$view_form = null;
 
31
        this.$sidebar_form = null;
 
32
        if (field) {
 
33
            this.translatable_fields_keys = [field];
 
34
            this.translatable_fields = _.filter(
 
35
                this.view.translatable_fields || [],
 
36
                function(i) {
 
37
                    return i.name === field;
 
38
                }
 
39
            );
 
40
        } else {
 
41
            this.translatable_fields_keys = _.map(
 
42
                this.view.translatable_fields || [],
 
43
                function(i) {
 
44
                    return i.name;
 
45
                }
 
46
            );
 
47
            this.translatable_fields = this.view.translatable_fields.slice(0);
 
48
        }
 
49
        this.languages = null;
 
50
        this.languages_loaded = $.Deferred();
 
51
        (new data.DataSetSearch(this, 'res.lang', this.view.dataset.get_context(),
 
52
                                [['translatable', '=', '1']])).read_slice(['code', 'name'],
 
53
                                { sort: 'id' }).then(this.on_languages_loaded);
 
54
    },
 
55
    on_languages_loaded: function(langs) {
 
56
        this.languages = langs;
 
57
        this.languages_loaded.resolve();
 
58
    },
 
59
    open: function() {
 
60
        // the template needs the languages
 
61
        return $.when(this.languages_loaded).then($.proxy(this._super, this));
 
62
    },
 
63
    start: function() {
 
64
        var self = this;
 
65
        this.$el.find('.oe_translation_field').change(function() {
 
66
            $(this).toggleClass('touched', $(this).val() !== $(this).attr('data-value'));
 
67
        });
 
68
        this.$footer.html(QWeb.render("TranslateDialog.buttons"));
 
69
        this.$footer.find(".oe_form_translate_dialog_save_button").click(function(){
 
70
            self.on_button_save();
 
71
            self.on_button_close();
 
72
        });
 
73
        this.$footer.find(".oe_form_translate_dialog_cancel_button").click(function(){
 
74
            self.on_button_close();
 
75
        });
 
76
 
 
77
        this.do_load_fields_values();
 
78
    },
 
79
    initialize_html_fields: function(lang) {
 
80
        var self = this;
 
81
        _.each(this.translatable_fields_keys, function(f) {
 
82
            // Initialize summernote if HTML field
 
83
            self.$el.find('.oe_form_field_html .oe_translation_field[name="' + lang.code + '-' + f + '"]').each(function() {
 
84
                var $parent = $(this).summernote({
 
85
                    'focus': false,
 
86
                    'toolbar': [
 
87
                        ['style', ['style']],
 
88
                        ['font', ['bold', 'italic', 'underline', 'clear']],
 
89
                        ['fontsize', ['fontsize']],
 
90
                        ['color', ['color']],
 
91
                        ['para', ['ul', 'ol', 'paragraph']],
 
92
                        ['table', ['table']],
 
93
                        ['insert', ['link', 'picture']],
 
94
                        ['misc', ['codeview']],
 
95
                        ['history', ['undo', 'redo']]
 
96
                    ],
 
97
                    'prettifyHtml': false,
 
98
                    'styleWithSpan': false,
 
99
                    'inlinemedia': ['p'],
 
100
                    'lang': "odoo",
 
101
                    'onChange': function (value) {
 
102
                        $(this).toggleClass('touched', value !== $(this).attr('data-value'));
 
103
                    }
 
104
                }).parent();
 
105
                // Triggers a mouseup to refresh the editor toolbar
 
106
                $parent.find('.note-editable').trigger('mouseup');
 
107
                $parent.find('.note-editing-area').css({
 
108
                    minHeight:'100px',
 
109
                    minWidth:'260px',
 
110
                });
 
111
            });
 
112
        });
 
113
    },
 
114
    set_fields_values: function(lang, values) {
 
115
        var self = this;
 
116
        _.each(this.translatable_fields_keys, function(f) {
 
117
            self.$el.find('.oe_translation_field[name="' + lang.code +
 
118
                    '-' + f + '"]').val(values[f] || '').attr(
 
119
                    'data-value', values[f] || '');
 
120
        });
 
121
        this.$el.find('textarea.oe_translation_field').css({
 
122
            minHeight:'100px',
 
123
        });
 
124
        $(window).resize();
 
125
        this.initialize_html_fields(lang);
 
126
    },
 
127
    do_load_fields_values: function() {
 
128
        var self = this,
 
129
            deferred = [];
 
130
 
 
131
        this.$el.find('.oe_translation_field').val('').removeClass('touched');
 
132
        _.each(self.languages, function(lg) {
 
133
            var deff = $.Deferred();
 
134
            deferred.push(deff);
 
135
            if (lg.code === self.view_language) {
 
136
                var values = {};
 
137
                _.each(self.translatable_fields_keys, function(field) {
 
138
                    values[field] = self.view.fields[field].get_value();
 
139
                });
 
140
                self.set_fields_values(lg, values);
 
141
                deff.resolve();
 
142
            } else {
 
143
                self.view.dataset.call('read',[[self.view.datarecord.id],
 
144
                    self.translatable_fields_keys,
 
145
                    self.view.dataset.get_context({
 
146
                        'lang': lg.code })]).done(
 
147
                        function (rows) {
 
148
                            self.set_fields_values(lg, rows[0]);
 
149
                            deff.resolve();
 
150
                        });
 
151
            }
 
152
        });
 
153
        return deferred;
 
154
    },
 
155
    on_button_save: function() {
 
156
        var translations = {},
 
157
            self = this,
 
158
            translation_mutex = new $.Mutex();
 
159
        self.$el.find('.oe_translation_field.touched').each(function() {
 
160
            var field = $(this).attr('name').split('-');
 
161
            if (!translations[field[0]]) {
 
162
                translations[field[0]] = {};
 
163
            }
 
164
            translations[field[0]][field[1]] = $(this).val();
 
165
        });
 
166
        _.each(translations, function(text, code) {
 
167
            if (code === self.view_language) {
 
168
                self.view.set_values(text);
 
169
            }
 
170
            translation_mutex.exec(function() {
 
171
                return new data.DataSet(self, self.view.dataset.model,
 
172
                                        self.view.dataset.get_context()).write(
 
173
                                        self.view.datarecord.id, text,
 
174
                                        { context : { 'lang': code }});
 
175
            });
 
176
        });
 
177
        this.close();
 
178
    },
 
179
    on_button_close: function() {
 
180
        this.close();
 
181
    },
 
182
 
 
183
});
 
184
 
 
185
FormView.include({
 
186
    render_sidebar: function($node) {
 
187
        this._super($node);
 
188
        if (this.sidebar) {
 
189
            this.sidebar.add_items('other', _.compact([
 
190
                this.is_action_enabled('edit') &&
 
191
                this.translatable_fields.length > 0 && {
 
192
                    label: _t('Translate'),
 
193
                    callback: this.on_button_translate
 
194
                },
 
195
            ]));
 
196
        }
 
197
    },
 
198
    on_button_translate: function() {
 
199
        var self = this;
 
200
        $.when(this.has_been_loaded).then(function() {
 
201
            self.open_translate_dialog();
 
202
        });
 
203
    },
 
204
});
 
205
 
 
206
View.include({
 
207
    open_translate_dialog: function(field) {
 
208
        new translateDialog(this, field).open();
 
209
    }
 
210
});
 
211
 
 
212
common.AbstractField.include({
 
213
    on_translate: function() {
 
214
        // the image next to the fields opens the translate dialog
 
215
        this.view.open_translate_dialog(this.name);
 
216
    },
 
217
});
 
218
 
 
219
return {
 
220
    translateDialog: translateDialog,
 
221
};
 
222
 
 
223
});
 
224