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.

226 lines
8.4 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.share', 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 copy = require('muk_web_utils.copy');
  27. var _t = core._t;
  28. var QWeb = core.qweb;
  29. var ShareMixin = {
  30. getShareMessageValues: function(message) {
  31. var values = {
  32. name: session.partner_display_name,
  33. record: this.recordData.display_name,
  34. url: utils.isUrl(this.value) && this.value,
  35. value: this.value,
  36. };
  37. return {
  38. subject: _.template(this.shareOptions.subjectTemplate)(values),
  39. body: QWeb.render(this.shareOptions.bodyTemplate, values),
  40. text: _.template(this.shareOptions.textTemplate)(values),
  41. url: utils.isUrl(this.value) && this.value,
  42. }
  43. },
  44. openShareChat: function(note) {
  45. var values = this.getShareMessageValues();
  46. var context = {
  47. default_is_log: note,
  48. default_body: values.body,
  49. default_subject: values.subject,
  50. default_model: this.shareOptions.res_model,
  51. default_res_id: this.shareOptions.res_id,
  52. mail_post_autofollow: false,
  53. };
  54. this.do_action({
  55. type: 'ir.actions.act_window',
  56. res_model: 'mail.compose.message',
  57. view_mode: 'form',
  58. view_type: 'form',
  59. views: [[false, 'form']],
  60. target: 'new',
  61. context: context,
  62. });
  63. },
  64. _onShareMessageClick: function(event) {
  65. event.preventDefault();
  66. event.stopPropagation();
  67. this.openShareChat(false);
  68. },
  69. _onShareNoteClick: function(event) {
  70. event.preventDefault();
  71. event.stopPropagation();
  72. this.openShareChat(true);
  73. },
  74. _onShareMailClick: function(event) {
  75. event.preventDefault();
  76. event.stopPropagation();
  77. var values = this.getShareMessageValues();
  78. var subject = "subject=" + values.subject;
  79. var body = "&body=" + encodeURIComponent(values.text);
  80. window.location.href = "mailto:?" + subject + body;
  81. },
  82. _onShareSendClick: function(event) {
  83. event.preventDefault();
  84. event.stopPropagation();
  85. var values = this.getShareMessageValues();
  86. navigator.share({
  87. title: values.subject,
  88. text: values.text,
  89. url: values.url,
  90. });
  91. },
  92. };
  93. var CharShare = fields.CharCopyClipboard.extend(ShareMixin, {
  94. fieldDependencies: _.extend({}, fields.CharCopyClipboard.fieldDependencies, {
  95. display_name: {type: 'char'},
  96. }),
  97. events: _.extend({}, fields.CharCopyClipboard.events, {
  98. 'click .mk_share_dropdown_message': '_onShareMessageClick',
  99. 'click .mk_share_dropdown_note': '_onShareNoteClick',
  100. 'click .mk_share_dropdown_mail': '_onShareMailClick',
  101. 'click .mk_share_dropdown_send': '_onShareSendClick',
  102. }),
  103. init: function(parent, name, record) {
  104. this._super.apply(this, arguments);
  105. this.navigator = window.navigator.share;
  106. this.chatter = _.contains(odoo._modules, "mail");
  107. this.shareOptions = _.defaults(this.nodeOptions, {
  108. subjectTemplate: _t("<%= name %> shared a message!"),
  109. textTemplate: _t("<%= value %>"),
  110. bodyTemplate: 'muk_web_utils.ShareMessage',
  111. });
  112. this.shareOptions = _.extend({}, this.shareOptions, {
  113. res_model: this.recordData[this.nodeOptions.res_model] || this.model,
  114. res_id: this.recordData[this.nodeOptions.res_id] || this.res_id,
  115. });
  116. },
  117. _render: function() {
  118. this._super.apply(this, arguments);
  119. this.$el.addClass('mk_field_share');
  120. this.$el.prepend($(QWeb.render('muk_web_utils.CharShare', {
  121. navigator: !!this.navigator,
  122. chatter: !!this.chatter,
  123. })));
  124. },
  125. });
  126. var TextShare = fields.TextCopyClipboard.extend(ShareMixin, {
  127. fieldDependencies: _.extend({}, fields.TextCopyClipboard.fieldDependencies, {
  128. display_name: {type: 'char'},
  129. }),
  130. events: _.extend({}, fields.TextCopyClipboard.events, {
  131. 'click .mk_share_dropdown_message': '_onShareMessageClick',
  132. 'click .mk_share_dropdown_note': '_onShareNoteClick',
  133. 'click .mk_share_dropdown_mail': '_onShareMailClick',
  134. 'click .mk_share_dropdown_send': '_onShareSendClick',
  135. }),
  136. init: function(parent, name, record) {
  137. this._super.apply(this, arguments);
  138. this.navigator = window.navigator.share;
  139. this.chatter = _.contains(odoo._modules, "mail");
  140. this.shareOptions = _.defaults(this.nodeOptions, {
  141. subjectTemplate: _t("<%= name %> shared a message!"),
  142. textTemplate: _t("<%= value %>"),
  143. bodyTemplate: 'muk_web_utils.ShareMessage',
  144. });
  145. this.shareOptions = _.extend({}, this.shareOptions, {
  146. res_model: this.recordData[this.nodeOptions.res_model] || this.model,
  147. res_id: this.recordData[this.nodeOptions.res_id] || this.res_id,
  148. });
  149. },
  150. _render: function() {
  151. this._super.apply(this, arguments);
  152. this.$el.addClass('mk_field_share');
  153. this.$el.prepend($(QWeb.render('muk_web_utils.TextShare', {
  154. navigator: !!this.navigator,
  155. chatter: !!this.chatter,
  156. })));
  157. }
  158. });
  159. var BinaryFileShare = copy.BinaryFileCopy.extend(ShareMixin, {
  160. fieldDependencies: _.extend({}, fields.FieldBinaryFile.fieldDependencies, {
  161. display_name: {type: 'char'},
  162. }),
  163. events: _.extend({}, fields.FieldBinaryFile.events, {
  164. 'click .mk_share_dropdown_message': '_onShareMessageClick',
  165. 'click .mk_share_dropdown_note': '_onShareNoteClick',
  166. 'click .mk_share_dropdown_mail': '_onShareMailClick',
  167. 'click .mk_share_dropdown_send': '_onShareSendClick',
  168. }),
  169. init: function () {
  170. this._super.apply(this, arguments);
  171. this.navigator = window.navigator.share;
  172. this.chatter = _.contains(odoo._modules, "mail");
  173. this.shareOptions = _.defaults(this.nodeOptions, {
  174. subjectTemplate: _t("<%= name %> shared a file!"),
  175. textTemplate: _t("<%= value %>"),
  176. bodyTemplate: 'muk_web_utils.ShareBinaryMessage',
  177. });
  178. this.shareOptions = _.extend({}, this.shareOptions, {
  179. res_model: this.recordData[this.nodeOptions.res_model] || this.model,
  180. res_id: this.recordData[this.nodeOptions.res_id] || this.res_id,
  181. });
  182. },
  183. getShareMessageValues: function() {
  184. var values = {
  185. name: session.partner_display_name,
  186. record: this.recordData.display_name,
  187. url: this.shareUrl,
  188. value: this.shareUrl,
  189. };
  190. return {
  191. subject: _.template(this.shareOptions.subjectTemplate)(values),
  192. body: QWeb.render(this.shareOptions.bodyTemplate, values),
  193. text: _.template(this.shareOptions.textTemplate)(values),
  194. url: this.shareUrl,
  195. }
  196. },
  197. _renderReadonly: function () {
  198. this._super.apply(this, arguments);
  199. this.$el.addClass('mk_field_share');
  200. this.$el.append($(QWeb.render('muk_web_utils.BinaryShare', {
  201. navigator: !!this.navigator,
  202. chatter: !!this.chatter,
  203. share: !!this.shareUrl,
  204. })));
  205. },
  206. });
  207. registry.add('share_char', CharShare);
  208. registry.add('share_text', TextShare);
  209. registry.add('share_binary', BinaryFileShare);
  210. return {
  211. ShareMixin: ShareMixin,
  212. CharShare: CharShare,
  213. TextShare: TextShare,
  214. BinaryFileShare: BinaryFileShare,
  215. };
  216. });