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.

152 lines
5.3 KiB

  1. /**********************************************************************************
  2. *
  3. * Copyright (C) 2017 MuK IT GmbH
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. **********************************************************************************/
  19. odoo.define('muk_web_utils.CharShare', function (require) {
  20. "use strict";
  21. var core = require('web.core');
  22. var session = require('web.session');
  23. var fields = require('web.basic_fields');
  24. var registry = require('web.field_registry');
  25. var utils = require('muk_web_utils.utils');
  26. var AbstractField = require('web.AbstractField');
  27. var _t = core._t;
  28. var QWeb = core.qweb;
  29. var ShareMixin = {
  30. fieldDependencies: _.extend({}, AbstractField.prototype.fieldDependencies, {
  31. display_name: {type: 'char'},
  32. }),
  33. events: _.extend({}, fields.InputField.prototype.events, {
  34. 'click .mk_share_dropdown_message': '_onShareMessageClick',
  35. 'click .mk_share_dropdown_note': '_onShareNoteClick',
  36. 'click .mk_share_dropdown_mail': '_onShareMailClick',
  37. 'click .mk_share_dropdown_send': '_onShareSendClick',
  38. }),
  39. init: function(parent, name, record) {
  40. this._super.apply(this, arguments);
  41. this.navigator = window.navigator.share;
  42. this.chatter = _.contains(odoo._modules, "mail");
  43. this.shareOptions = _.defaults(this.nodeOptions, {
  44. subjectTemplate: _t("<%= name %> shared a message!"),
  45. textTemplate: _t("<%= value %>"),
  46. bodyTemplate: 'muk_web_utils.ShareMessage',
  47. });
  48. this.shareOptions = _.extend({}, this.shareOptions, {
  49. res_model: this.recordData[this.nodeOptions.res_model] || this.model,
  50. res_id: this.recordData[this.nodeOptions.res_id] || this.res_id,
  51. });
  52. },
  53. getShareMessageValues: function(message) {
  54. var values = {
  55. name: session.partner_display_name,
  56. record: this.recordData.display_name,
  57. url: utils.isUrl(this.value) && this.value,
  58. value: this.value,
  59. };
  60. return {
  61. subject: _.template(this.shareOptions.subjectTemplate)(values),
  62. body: QWeb.render(this.shareOptions.bodyTemplate, values),
  63. text: _.template(this.shareOptions.textTemplate)(values),
  64. }
  65. },
  66. openShareChat: function(note) {
  67. var values = this.getShareMessageValues();
  68. var context = {
  69. default_is_log: note,
  70. default_body: values.body,
  71. default_subject: values.subject,
  72. default_model: this.shareOptions.res_model,
  73. default_res_id: this.shareOptions.res_id,
  74. mail_post_autofollow: false,
  75. };
  76. this.do_action({
  77. type: 'ir.actions.act_window',
  78. res_model: 'mail.compose.message',
  79. view_mode: 'form',
  80. view_type: 'form',
  81. views: [[false, 'form']],
  82. target: 'new',
  83. context: context,
  84. });
  85. },
  86. _onShareMessageClick: function(event) {
  87. event.preventDefault();
  88. event.stopPropagation();
  89. this.openShareChat(false);
  90. },
  91. _onShareNoteClick: function(event) {
  92. event.preventDefault();
  93. event.stopPropagation();
  94. this.openShareChat(true);
  95. },
  96. _onShareMailClick: function(event) {
  97. event.preventDefault();
  98. event.stopPropagation();
  99. var values = this.getShareMessageValues();
  100. var subject = "subject=" + values.subject;
  101. var body = "&body=" + encodeURIComponent(values.text);
  102. window.location.href = "mailto:?" + subject + body;
  103. },
  104. _onShareSendClick: function(event) {
  105. event.preventDefault();
  106. event.stopPropagation();
  107. var values = this.getShareMessageValues();
  108. navigator.share({
  109. title: values.subject,
  110. text: values.text,
  111. url: utils.isUrl(this.value) && this.value
  112. });
  113. },
  114. };
  115. var CharShare = fields.CharCopyClipboard.extend(ShareMixin, {
  116. _render: function() {
  117. this._super.apply(this, arguments);
  118. this.$el.addClass('mk_field_share');
  119. this.$el.prepend($(QWeb.render('muk_web_utils.CharShare', {
  120. navigator: !!this.navigator,
  121. chatter: !!this.chatter,
  122. })));
  123. },
  124. });
  125. var TextShare = fields.TextCopyClipboard.extend(ShareMixin, {
  126. _render: function() {
  127. this._super.apply(this, arguments);
  128. this.$el.addClass('mk_field_share');
  129. this.$el.prepend($(QWeb.render('muk_web_utils.TextShare', {
  130. navigator: !!this.navigator,
  131. chatter: !!this.chatter,
  132. })));
  133. }
  134. });
  135. registry.add('share_char', CharShare);
  136. registry.add('share_text', TextShare);
  137. return {
  138. ShareMixin: ShareMixin,
  139. CharShare: CharShare,
  140. TextShare: TextShare,
  141. };
  142. });