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.

162 lines
6.5 KiB

  1. /* Base phone module for Odoo
  2. Copyright (C) 2013-2015 Alexis de Lattre <alexis@via.ecp.fr>
  3. The licence is in the file __openerp__.py */
  4. odoo.define('base_phone.phone_widget', function (require) {
  5. "use strict";
  6. var core = require('web.core');
  7. var formwidgets = require('web.form_widgets');
  8. var web_client = require('web.web_client');
  9. var _t = core._t;
  10. var FieldPhone = formwidgets.FieldChar.extend({
  11. template: 'FieldPhone',
  12. initialize_content: function() {
  13. this._super();
  14. var $button = this.$el.find('button');
  15. $button.click(this.on_button_clicked);
  16. this.setupFocus($button);
  17. },
  18. render_value: function() {
  19. if (!this.get('effective_readonly')) {
  20. this._super();
  21. } else {
  22. var self = this;
  23. var phone_num = this.get('value');
  24. // console.log('BASE_PHONE phone_num = %s', phone_num);
  25. var href = '#';
  26. var href_text = '';
  27. if (phone_num) {
  28. href = 'tel:' + phone_num;
  29. href_text = formatInternational('', phone_num) || '';
  30. }
  31. if (href_text) {
  32. this.$el.find('a.oe_form_uri').attr('href', href).text(href_text);
  33. this.$el.find('span.oe_form_char_content').text('');
  34. } else {
  35. this.$el.find('a.oe_form_uri').attr('href', '').text('');
  36. this.$el.find('span.oe_form_char_content').text(phone_num || '');
  37. }
  38. var click2dial_text = '';
  39. if (href_text && !this.options.dial_button_invisible) {
  40. click2dial_text = _t('Dial');
  41. }
  42. this.$el.find('#click2dial').off('click');
  43. this.$el.find('#click2dial')
  44. .text(click2dial_text)
  45. .on('click', function(ev) {
  46. self.do_notify(
  47. _t('Click2dial started'),
  48. _t('Unhook your ringing phone'));
  49. var arg = {
  50. 'phone_number': phone_num,
  51. 'click2dial_model': self.view.dataset.model,
  52. 'click2dial_id': self.view.datarecord.id};
  53. self.rpc('/base_phone/click2dial', arg).done(function(r) {
  54. // console.log('Click2dial r=%s', JSON.stringify(r));
  55. if (r === false) {
  56. self.do_warn("Click2dial failed");
  57. } else if (typeof r === 'object') {
  58. self.do_notify(
  59. _t('Click2dial successfull'),
  60. _t('Number dialed:') + ' ' + r.dialed_number);
  61. if (r.action_model) {
  62. var context = {
  63. 'click2dial_model': self.view.dataset.model,
  64. 'click2dial_id': self.view.datarecord.id,
  65. 'phone_number': phone_num,
  66. };
  67. var action = {
  68. name: r.action_name,
  69. type: 'ir.actions.act_window',
  70. res_model: r.action_model,
  71. view_mode: 'form',
  72. views: [[false, 'form']],
  73. target: 'new',
  74. context: context,
  75. };
  76. web_client.action_manager.do_action(action);
  77. }
  78. }
  79. });
  80. });
  81. }
  82. },
  83. on_button_clicked: function() {
  84. location.href = 'tel:' + this.get('value');
  85. }
  86. });
  87. var FieldFax = formwidgets.FieldChar.extend({
  88. template: 'FieldFax',
  89. initialize_content: function() {
  90. this._super();
  91. var $button = this.$el.find('button');
  92. $button.click(this.on_button_clicked);
  93. this.setupFocus($button);
  94. },
  95. render_value: function() {
  96. if (!this.get('effective_readonly')) {
  97. this._super();
  98. } else {
  99. var fax_num = this.get('value');
  100. // console.log('BASE_PHONE fax_num = %s', fax_num);
  101. var href = '#';
  102. var href_text = '';
  103. if (fax_num) {
  104. href = 'fax:' + fax_num;
  105. href_text = formatInternational('', fax_num) || '';
  106. }
  107. if (href_text) {
  108. this.$el.find('a.oe_form_uri').attr('href', href).text(href_text);
  109. this.$el.find('span.oe_form_char_content').text('');
  110. } else {
  111. this.$el.find('a.oe_form_uri').attr('href', '').text('');
  112. this.$el.find('span.oe_form_char_content').text(fax_num || '');
  113. }
  114. }
  115. },
  116. on_button_clicked: function() {
  117. location.href = 'fax:' + this.get('value');
  118. }
  119. });
  120. // To avoid conflicts, we check that widgets do not exist before using
  121. if(!core.form_widget_registry.get('fax')){
  122. core.form_widget_registry.add('fax', FieldFax);
  123. }
  124. if(!core.form_widget_registry.get('phone')){
  125. core.form_widget_registry.add('phone', FieldPhone);
  126. }
  127. /*
  128. var Column = require('web.ListView');
  129. var ColumnPhone = Column.extend({
  130. // ability to add widget="phone" in TREE view
  131. _format: function(row_data, options) {
  132. console.log('row_data=' + row_data);
  133. console.log('options=');
  134. console.log(options);
  135. var value = row_data[this.id].value;
  136. if (value) {
  137. readable_space = formatInternational('', value);
  138. readable_no_break_space = readable_space.replace(/\s/g, ' ');
  139. console('special return');
  140. return readable_no_break_space;
  141. }
  142. console.log('return normal');
  143. return this._super(row_data, options);
  144. }
  145. });
  146. if (!core.list_widget_registry.get('phone')) {
  147. core.list_widget_registry.add('field.phone', ColumnPhone);
  148. // a mon avis, il y a une mauvaise compréhension : fields.phone signifiera fields.Phone dans le python
  149. } */
  150. });