You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
8.9 KiB

  1. openerp.web_translate_dialog = function (instance) {
  2. "use strict";
  3. var QWeb = instance.web.qweb,
  4. _t = instance.web._t,
  5. _lt = instance.web._lt;
  6. instance.web.FormView.include({
  7. load_form: function(data) {
  8. var self = this;
  9. this._super(data);
  10. if (this.sidebar) {
  11. this.sidebar.add_items('other', _.compact([
  12. self.is_action_enabled('edit') && { label: _t('Translate'), callback: self.on_button_translate },
  13. ]));
  14. }
  15. },
  16. on_button_translate: function() {
  17. var self = this;
  18. $.when(this.has_been_loaded).then(function() {
  19. self.open_translate_dialog(this);
  20. });
  21. },
  22. });
  23. instance.web.View.include({
  24. open_translate_dialog: function() {
  25. new instance.web_translate_dialog.TranslateDialog(this).open();
  26. }
  27. });
  28. instance.web_translate_dialog.TranslateDialog = instance.web.Dialog.extend({
  29. template: "TranslateDialog",
  30. init: function(parent, options, content) {
  31. this._super(parent,
  32. {title: _t("Translations"),
  33. width: '90%',
  34. height: '80%'},
  35. content);
  36. this.view_language = this.session.user_context.lang;
  37. this.view = parent;
  38. this.view_type = parent.fields_view.type || '';
  39. this.$view_form = null;
  40. this.$sidebar_form = null;
  41. this.translatable_fields_keys = _.map(this.view.translatable_fields || [], function(i) { return i.name;});
  42. this.languages = null;
  43. this.languages_loaded = $.Deferred();
  44. (new instance.web.DataSetSearch(this,
  45. 'res.lang',
  46. this.view.dataset.get_context(),
  47. [['translatable', '=', '1']]))
  48. .read_slice(['code', 'name'], { sort: 'id' })
  49. .then(this.on_languages_loaded);
  50. },
  51. on_languages_loaded: function(langs) {
  52. this.languages = langs;
  53. this.languages_loaded.resolve();
  54. },
  55. open: function() {
  56. var self = this,
  57. sup = this._super;
  58. // the template needs the languages
  59. $.when(this.languages_loaded).then(function() {
  60. return sup.call(self);
  61. });
  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.$buttons.html(QWeb.render("TranslateDialog.buttons"));
  69. this.$buttons.find(".oe_form_translate_dialog_save_button").click(function(){
  70. self.on_button_save();
  71. self.on_button_close();
  72. });
  73. this.$buttons.find(".oe_form_translate_dialog_cancel_button").click(function(){
  74. self.on_button_close();
  75. });
  76. this.initialize_html_fields();
  77. this.do_load_fields_values();
  78. },
  79. initialize_html_fields: function() {
  80. this.$el.find('.oe_form_field_html textarea').each(function() {
  81. var $textarea = $(this);
  82. var width = 100; // forced to fixed size on initialization
  83. // will be changed to percentage right after
  84. // the creation
  85. var height = 250;
  86. $textarea.cleditor({
  87. width: width, // width not including margins, borders or padding
  88. height: height, // height not including margins, borders or padding
  89. controls: // controls to add to the toolbar
  90. "bold italic underline strikethrough " +
  91. "| removeformat | bullets numbering | outdent " +
  92. "indent | link unlink | source",
  93. bodyStyle: // style to assign to document body contained within the editor
  94. "margin:4px; color:#4c4c4c; font-size:13px; font-family:'Lucida Grande',Helvetica,Verdana,Arial,sans-serif; cursor:text"
  95. });
  96. var $cleditor = $textarea.cleditor()[0];
  97. // Down to -- end, this is a workaround for the bug
  98. // https://bugs.launchpad.net/openerp-web/+bug/1258463
  99. // The editor is initially created with a fixed size so
  100. // the buggy event is not bound to $(window), then we restore
  101. // a percentage width and bind the "normal" event without the
  102. // CHM's buggy change.
  103. $cleditor.$main.width('95%');
  104. $cleditor.options.width = '95%';
  105. $(window).resize(function() {
  106. //Forcefully blurred iframe contentWindow, chrome, IE, safari doesn't trigger blur on window resize and due to which text disappears
  107. var contentWindow = $cleditor.$frame[0].contentWindow;
  108. if(!$.browser.mozilla && contentWindow){
  109. $(contentWindow).trigger('blur');
  110. }
  111. });
  112. $cleditor.refresh();
  113. // -- end
  114. $cleditor.change(function() {
  115. this.updateTextArea();
  116. this.$area.toggleClass('touched',
  117. (this.$area.val() != this.$area.attr('data-value')));
  118. });
  119. });
  120. },
  121. set_fields_values: function(lang, values) {
  122. var self = this;
  123. _.each(this.translatable_fields_keys, function(f) {
  124. self.$el.find('.oe_translation_field[name="' + lang.code + '-' + f + '"]')
  125. .val(values[f] || '')
  126. .attr('data-value', values[f] || '');
  127. var $tarea = self.$el.find('.oe_form_field_html .oe_translation_field[name="' + lang.code + '-' + f + '"]');
  128. if ($tarea.length) {
  129. $tarea.cleditor()[0].updateFrame();
  130. }
  131. });
  132. var $textarea = this.$el.find('textarea.oe_translation_field');
  133. $textarea.css({minHeight:'100px'});
  134. $textarea.autosize();
  135. $(window).resize(); // triggers the autosize
  136. },
  137. do_load_fields_values: function() {
  138. var self = this,
  139. deferred = [];
  140. this.$el.find('.oe_translation_field').val('').removeClass('touched');
  141. _.each(self.languages, function(lg) {
  142. var deff = $.Deferred();
  143. deferred.push(deff);
  144. if (lg.code === self.view_language) {
  145. var values = {};
  146. _.each(self.translatable_fields_keys, function(field) {
  147. values[field] = self.view.fields[field].get_value();
  148. });
  149. self.set_fields_values(lg, values);
  150. deff.resolve();
  151. } else {
  152. self.view.dataset.call(
  153. 'read',
  154. [[self.view.datarecord.id],
  155. self.translatable_fields_keys,
  156. self.view.dataset.get_context({
  157. 'lang': lg.code
  158. })]).done(function (rows) {
  159. self.set_fields_values(lg, rows[0]);
  160. deff.resolve();
  161. });
  162. };
  163. });
  164. return deferred;
  165. },
  166. on_button_save: function() {
  167. var translations = {},
  168. self = this,
  169. translation_mutex = new $.Mutex();
  170. self.$el.find('.oe_translation_field.touched').each(function() {
  171. var field = $(this).attr('name').split('-');
  172. if (!translations[field[0]]) {
  173. translations[field[0]] = {};
  174. }
  175. translations[field[0]][field[1]] = $(this).val();
  176. });
  177. _.each(translations, function(data, code) {
  178. if (code === self.view_language) {
  179. self.view.set_values(data);
  180. }
  181. translation_mutex.exec(function() {
  182. return new instance.web.DataSet(self, self.view.dataset.model, self.view.dataset.get_context()).write(self.view.datarecord.id, data, { context : { 'lang': code }});
  183. });
  184. });
  185. this.close();
  186. },
  187. on_button_close: function() {
  188. this.close();
  189. },
  190. });
  191. instance.web.form.AbstractField.include({
  192. on_translate: function() {
  193. // the image next to the fields opens the translate dialog
  194. this.view.open_translate_dialog();
  195. },
  196. });
  197. };