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.1 KiB

  1. /**********************************************************************************
  2. *
  3. * Copyright (c) 2017-2019 MuK IT GmbH.
  4. *
  5. * This file is part of MuK Web Utils
  6. * (see https://mukit.at).
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. **********************************************************************************/
  22. odoo.define('muk_web_utils.share', function (require) {
  23. "use strict";
  24. var core = require('web.core');
  25. var session = require('web.session');
  26. var fields = require('web.basic_fields');
  27. var registry = require('web.field_registry');
  28. var utils = require('muk_web_utils.utils');
  29. var copy = require('muk_web_utils.copy');
  30. var _t = core._t;
  31. var QWeb = core.qweb;
  32. var ShareMixin = {
  33. shareEvents: {
  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. getShareMessageValues: function(message) {
  40. var values = {
  41. name: session.partner_display_name,
  42. record: this.recordData.display_name,
  43. url: utils.isUrl(this.value) && this.value,
  44. value: this.value,
  45. };
  46. return {
  47. subject: _.template(this.shareOptions.subjectTemplate)(values),
  48. body: QWeb.render(this.shareOptions.bodyTemplate, values),
  49. text: _.template(this.shareOptions.textTemplate)(values),
  50. url: utils.isUrl(this.value) && this.value,
  51. }
  52. },
  53. openShareChat: function(note) {
  54. var values = this.getShareMessageValues();
  55. var context = {
  56. default_is_log: note,
  57. default_body: values.body,
  58. default_subject: values.subject,
  59. default_model: this.shareOptions.res_model,
  60. default_res_id: this.shareOptions.res_id,
  61. mail_post_autofollow: false,
  62. };
  63. this.do_action({
  64. type: 'ir.actions.act_window',
  65. res_model: 'mail.compose.message',
  66. view_mode: 'form',
  67. view_type: 'form',
  68. views: [[false, 'form']],
  69. target: 'new',
  70. context: context,
  71. });
  72. },
  73. _onShareMessageClick: function(event) {
  74. event.preventDefault();
  75. event.stopPropagation();
  76. this.openShareChat(false);
  77. },
  78. _onShareNoteClick: function(event) {
  79. event.preventDefault();
  80. event.stopPropagation();
  81. this.openShareChat(true);
  82. },
  83. _onShareMailClick: function(event) {
  84. event.preventDefault();
  85. event.stopPropagation();
  86. var values = this.getShareMessageValues();
  87. var subject = "subject=" + values.subject;
  88. var body = "&body=" + encodeURIComponent(values.text);
  89. window.location.href = "mailto:?" + subject + body;
  90. },
  91. _onShareSendClick: function(event) {
  92. event.preventDefault();
  93. event.stopPropagation();
  94. var values = this.getShareMessageValues();
  95. navigator.share({
  96. title: values.subject,
  97. text: values.text,
  98. url: values.url,
  99. });
  100. },
  101. };
  102. var CharShare = fields.CharCopyClipboard.extend(ShareMixin, {
  103. fieldDependencies: _.extend({}, fields.CharCopyClipboard.prototype.fieldDependencies, {
  104. display_name: {type: 'char'},
  105. }),
  106. events: _.extend({}, fields.CharCopyClipboard.prototype.events, ShareMixin.shareEvents),
  107. init: function(parent, name, record) {
  108. this._super.apply(this, arguments);
  109. this.navigator = window.navigator.share;
  110. this.chatter = _.contains(odoo._modules, "mail");
  111. this.shareOptions = _.defaults(this.nodeOptions, {
  112. subjectTemplate: _t("<%= name %> shared a message!"),
  113. textTemplate: _t("<%= value %>"),
  114. bodyTemplate: 'muk_web_utils.ShareMessage',
  115. });
  116. this.shareOptions = _.extend({}, this.shareOptions, {
  117. res_model: this.recordData[this.nodeOptions.res_model] || this.model,
  118. res_id: this.recordData[this.nodeOptions.res_id] || this.res_id,
  119. });
  120. },
  121. _render: function() {
  122. this._super.apply(this, arguments);
  123. this.$el.addClass('mk_field_share');
  124. this.$el.prepend($(QWeb.render('muk_web_utils.CharShare', {
  125. navigator: !!this.navigator,
  126. chatter: !!this.chatter,
  127. })));
  128. },
  129. });
  130. var TextShare = fields.TextCopyClipboard.extend(ShareMixin, {
  131. fieldDependencies: _.extend({}, fields.TextCopyClipboard.prototype.fieldDependencies, {
  132. display_name: {type: 'char'},
  133. }),
  134. events: _.extend({}, fields.TextCopyClipboard.prototype.events, ShareMixin.shareEvents),
  135. init: function(parent, name, record) {
  136. this._super.apply(this, arguments);
  137. this.navigator = window.navigator.share;
  138. this.chatter = _.contains(odoo._modules, "mail");
  139. this.shareOptions = _.defaults(this.nodeOptions, {
  140. subjectTemplate: _t("<%= name %> shared a message!"),
  141. textTemplate: _t("<%= value %>"),
  142. bodyTemplate: 'muk_web_utils.ShareMessage',
  143. });
  144. this.shareOptions = _.extend({}, this.shareOptions, {
  145. res_model: this.recordData[this.nodeOptions.res_model] || this.model,
  146. res_id: this.recordData[this.nodeOptions.res_id] || this.res_id,
  147. });
  148. },
  149. _render: function() {
  150. this._super.apply(this, arguments);
  151. this.$el.addClass('mk_field_share');
  152. this.$el.prepend($(QWeb.render('muk_web_utils.TextShare', {
  153. navigator: !!this.navigator,
  154. chatter: !!this.chatter,
  155. })));
  156. }
  157. });
  158. var BinaryFileShare = copy.BinaryFileCopy.extend(ShareMixin, {
  159. fieldDependencies: _.extend({}, fields.FieldBinaryFile.prototype.fieldDependencies, {
  160. display_name: {type: 'char'},
  161. }),
  162. events: _.extend({}, copy.BinaryFileCopy.prototype.events, ShareMixin.shareEvents, {
  163. 'click .mk_share_button': '_onShareDropdownClick',
  164. }),
  165. init: function () {
  166. this._super.apply(this, arguments);
  167. this.navigator = window.navigator.share;
  168. this.chatter = _.contains(odoo._modules, "mail");
  169. this.shareOptions = _.defaults(this.nodeOptions, {
  170. subjectTemplate: _t("<%= name %> shared a file!"),
  171. textTemplate: _t("<%= value %>"),
  172. bodyTemplate: 'muk_web_utils.ShareBinaryMessage',
  173. });
  174. this.shareOptions = _.extend({}, this.shareOptions, {
  175. res_model: this.recordData[this.nodeOptions.res_model] || this.model,
  176. res_id: this.recordData[this.nodeOptions.res_id] || this.res_id,
  177. });
  178. },
  179. getShareMessageValues: function() {
  180. var values = {
  181. name: session.partner_display_name,
  182. record: this.recordData.display_name,
  183. url: this.shareUrl,
  184. value: this.shareUrl,
  185. };
  186. return {
  187. subject: _.template(this.shareOptions.subjectTemplate)(values),
  188. body: QWeb.render(this.shareOptions.bodyTemplate, values),
  189. text: _.template(this.shareOptions.textTemplate)(values),
  190. url: this.shareUrl,
  191. }
  192. },
  193. _renderReadonly: function () {
  194. this._super.apply(this, arguments);
  195. this.$el.addClass('mk_field_share');
  196. this.$el.append($(QWeb.render('muk_web_utils.BinaryShare', {
  197. navigator: !!this.navigator,
  198. chatter: !!this.chatter,
  199. share: !!this.shareUrl,
  200. })));
  201. },
  202. _onShareDropdownClick: function(event) {
  203. $(event.currentTarget).dropdown("toggle");
  204. event.stopPropagation();
  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. });