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.

225 lines
8.0 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. views: [[false, 'form']],
  68. target: 'new',
  69. context: context,
  70. });
  71. },
  72. _onShareMessageClick: function(event) {
  73. event.preventDefault();
  74. event.stopPropagation();
  75. this.openShareChat(false);
  76. },
  77. _onShareNoteClick: function(event) {
  78. event.preventDefault();
  79. event.stopPropagation();
  80. this.openShareChat(true);
  81. },
  82. _onShareMailClick: function(event) {
  83. event.preventDefault();
  84. event.stopPropagation();
  85. var values = this.getShareMessageValues();
  86. var subject = "subject=" + values.subject;
  87. var body = "&body=" + encodeURIComponent(values.text);
  88. window.location.href = "mailto:?" + subject + body;
  89. },
  90. _onShareSendClick: function(event) {
  91. event.preventDefault();
  92. event.stopPropagation();
  93. var values = this.getShareMessageValues();
  94. navigator.share({
  95. title: values.subject,
  96. text: values.text,
  97. url: values.url,
  98. });
  99. },
  100. };
  101. var CharShare = fields.CharCopyClipboard.extend(ShareMixin, {
  102. fieldDependencies: _.extend({}, fields.CharCopyClipboard.prototype.fieldDependencies, {
  103. display_name: {type: 'char'},
  104. }),
  105. events: _.extend({}, fields.CharCopyClipboard.prototype.events, ShareMixin.shareEvents),
  106. init: function(parent, name, record) {
  107. this._super.apply(this, arguments);
  108. this.navigator = window.navigator.share;
  109. this.chatter = _.contains(odoo._modules, "mail");
  110. this.shareOptions = _.defaults(this.nodeOptions, {
  111. subjectTemplate: _t("<%= name %> shared a message!"),
  112. textTemplate: _t("<%= value %>"),
  113. bodyTemplate: 'muk_web_utils.ShareMessage',
  114. });
  115. this.shareOptions = _.extend({}, this.shareOptions, {
  116. res_model: this.recordData[this.nodeOptions.res_model] || this.model,
  117. res_id: this.recordData[this.nodeOptions.res_id] || this.res_id,
  118. });
  119. },
  120. _render: function() {
  121. this._super.apply(this, arguments);
  122. this.$el.addClass('mk_field_share');
  123. this.$el.prepend($(QWeb.render('muk_web_utils.CharShare', {
  124. navigator: !!this.navigator,
  125. chatter: !!this.chatter,
  126. })));
  127. },
  128. });
  129. var TextShare = fields.TextCopyClipboard.extend(ShareMixin, {
  130. fieldDependencies: _.extend({}, fields.TextCopyClipboard.prototype.fieldDependencies, {
  131. display_name: {type: 'char'},
  132. }),
  133. events: _.extend({}, fields.TextCopyClipboard.prototype.events, ShareMixin.shareEvents),
  134. init: function(parent, name, record) {
  135. this._super.apply(this, arguments);
  136. this.navigator = window.navigator.share;
  137. this.chatter = _.contains(odoo._modules, "mail");
  138. this.shareOptions = _.defaults(this.nodeOptions, {
  139. subjectTemplate: _t("<%= name %> shared a message!"),
  140. textTemplate: _t("<%= value %>"),
  141. bodyTemplate: 'muk_web_utils.ShareMessage',
  142. });
  143. this.shareOptions = _.extend({}, this.shareOptions, {
  144. res_model: this.recordData[this.nodeOptions.res_model] || this.model,
  145. res_id: this.recordData[this.nodeOptions.res_id] || this.res_id,
  146. });
  147. },
  148. _render: function() {
  149. this._super.apply(this, arguments);
  150. this.$el.addClass('mk_field_share');
  151. this.$el.prepend($(QWeb.render('muk_web_utils.TextShare', {
  152. navigator: !!this.navigator,
  153. chatter: !!this.chatter,
  154. })));
  155. }
  156. });
  157. var BinaryFileShare = copy.BinaryFileCopy.extend(ShareMixin, {
  158. fieldDependencies: _.extend({}, fields.FieldBinaryFile.prototype.fieldDependencies, {
  159. display_name: {type: 'char'},
  160. }),
  161. events: _.extend({}, copy.BinaryFileCopy.prototype.events, ShareMixin.shareEvents, {
  162. 'click .mk_share_button': '_onShareDropdownClick',
  163. }),
  164. init: function () {
  165. this._super.apply(this, arguments);
  166. this.navigator = window.navigator.share;
  167. this.chatter = _.contains(odoo._modules, "mail");
  168. this.shareOptions = _.defaults(this.nodeOptions, {
  169. subjectTemplate: _t("<%= name %> shared a file!"),
  170. textTemplate: _t("<%= value %>"),
  171. bodyTemplate: 'muk_web_utils.ShareBinaryMessage',
  172. });
  173. this.shareOptions = _.extend({}, this.shareOptions, {
  174. res_model: this.recordData[this.nodeOptions.res_model] || this.model,
  175. res_id: this.recordData[this.nodeOptions.res_id] || this.res_id,
  176. });
  177. },
  178. getShareMessageValues: function() {
  179. var values = {
  180. name: session.partner_display_name,
  181. record: this.recordData.display_name,
  182. url: this.shareUrl,
  183. value: this.shareUrl,
  184. };
  185. return {
  186. subject: _.template(this.shareOptions.subjectTemplate)(values),
  187. body: QWeb.render(this.shareOptions.bodyTemplate, values),
  188. text: _.template(this.shareOptions.textTemplate)(values),
  189. url: this.shareUrl,
  190. }
  191. },
  192. _renderReadonly: function () {
  193. this._super.apply(this, arguments);
  194. this.$el.addClass('mk_field_share');
  195. this.$el.append($(QWeb.render('muk_web_utils.BinaryShare', {
  196. navigator: !!this.navigator,
  197. chatter: !!this.chatter,
  198. share: !!this.shareUrl,
  199. })));
  200. },
  201. _onShareDropdownClick: function(event) {
  202. $(event.currentTarget).dropdown("toggle");
  203. event.stopPropagation();
  204. },
  205. });
  206. registry.add('share_char', CharShare);
  207. registry.add('share_text', TextShare);
  208. registry.add('share_binary', BinaryFileShare);
  209. return {
  210. ShareMixin: ShareMixin,
  211. CharShare: CharShare,
  212. TextShare: TextShare,
  213. BinaryFileShare: BinaryFileShare,
  214. };
  215. });