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.

203 lines
7.2 KiB

7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. /*******************************************************************************
  2. *
  3. * Copyright (C) 2017 MuK IT GmbH
  4. *
  5. * This program is free software: you can redistribute it and/or modify it under
  6. * the terms of the GNU Affero General Public License as published by the Free
  7. * Software Foundation, either version 3 of the License, or (at your option) any
  8. * later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. * 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_share.dialog', function(require) {
  20. "use strict";
  21. var core = require('web.core');
  22. var session = require('web.session');
  23. var config = require('web.config');
  24. var chat_manager = require('mail.chat_manager');
  25. var Dialog = require('web.Dialog');
  26. var QWeb = core.qweb;
  27. var _t = core._t;
  28. var ShareDialog = Dialog.extend({
  29. init: function (parent, options) {
  30. var self = this;
  31. this.url = options.url || window.location.href;
  32. options = _.defaults(options || {}, {
  33. title: _t("Share"), subtitle: '',
  34. size: 'smale',
  35. dialogClass: 'muk_share_dialog_body',
  36. $content: $(QWeb.render('muk_web_share.ShareDialog', {
  37. widget: this,
  38. isMobile: config.device.isMobile,
  39. })),
  40. buttons: [{
  41. text: _t("Close"),
  42. classes: 'btn-default',
  43. close: true,
  44. }],
  45. technical: true,
  46. });
  47. this._super.apply(this, arguments);
  48. },
  49. willStart: function () {
  50. var self = this;
  51. return this._super.apply(this, arguments).then(function () {
  52. self.$modal.find('.modal-dialog').addClass("muk_share_dialog");
  53. self.$modal.find('.modal-title').prepend($('<i class="fa fa-share-alt"/>'));
  54. self.$content.find('.muk_share_buttons_whatsapp').attr("href",
  55. 'whatsapp://send?text=' + encodeURIComponent(self.url));
  56. self.$content.find('.muk_share_buttons_email').attr("href",
  57. 'mailto:?subject=' + _t("Share") + '&body=' + encodeURIComponent(self.url));
  58. self.$content.find('.muk_share_buttons_chat').click(function(e) {
  59. e.preventDefault();
  60. e.stopPropagation();
  61. self.newMessage(self.url);
  62. });
  63. self.$content.find('.muk_share_buttons_copy').click(function(e) {
  64. e.preventDefault();
  65. e.stopPropagation();
  66. self.copyClipboard(self.url);
  67. });
  68. self.$content.find('.muk_share_chat_send').click(function(e) {
  69. e.preventDefault();
  70. e.stopPropagation();
  71. self.newMessage(self.url);
  72. });
  73. self.update();
  74. });
  75. },
  76. update: function() {
  77. this.updateUser();
  78. this.updateChannels();
  79. },
  80. updateUser: function() {
  81. var self = this;
  82. self.$content.find('#user').empty();
  83. self._rpc({
  84. fields: [
  85. 'name', 'login', 'image',
  86. 'partner_id', 'active', 'share'
  87. ],
  88. domain: [['active', '=', true]],
  89. model: 'res.users',
  90. method: 'search_read',
  91. context: session.user_context,
  92. }).then(function(result) {
  93. var users = _.filter(result, function (user) {
  94. return !user.share && user.id !== session.uid;
  95. });
  96. self.$content.find('#user').append($(QWeb.render('muk_web_share.ShareUsers', {
  97. widget: self,
  98. users: users,
  99. })));
  100. self.$content.find('.muk_share_chat_user').click(function(e) {
  101. var $partner_channel = $.Deferred();
  102. var partner_id = $(e.currentTarget).data('partner_id');
  103. var channel = chat_manager.get_dm_from_partner_id(partner_id);
  104. if(!channel) {
  105. chat_manager.create_channel(partner_id, "dm").then(function() {
  106. $partner_channel.resolve(chat_manager.get_dm_from_partner_id(partner_id));
  107. });
  108. } else {
  109. $partner_channel.resolve(channel);
  110. }
  111. $.when($partner_channel).then(function(partner_channel) {
  112. self.postMessage(self.formatUrl(self.url), partner_channel.id);
  113. });
  114. });
  115. });
  116. },
  117. updateChannels: function() {
  118. var self = this;
  119. self.$content.find('#channel').empty();
  120. self.$content.find('#chat').empty();
  121. var channels = _.filter(chat_manager.get_channels(), function (channel) {
  122. return !channel.is_chat && channel.type !== 'static';
  123. });
  124. chat_manager.get_channels_preview(channels).then(function(channels) {
  125. self.$content.find('#channel').append( $(QWeb.render('muk_web_share.ShareChannels', {
  126. widget: self,
  127. channels: channels,
  128. })));
  129. self.$content.find('.muk_share_chat_channel').click(function(e) {
  130. var channel_id = $(e.currentTarget).data('channel_id');
  131. self.postMessage(self.formatUrl(self.url), channel_id);
  132. });
  133. });
  134. var chats = _.filter(chat_manager.get_channels(), function (channel) {
  135. return channel.is_chat && channel.type !== 'static';
  136. });
  137. chat_manager.get_channels_preview(chats).then(function(channels) {
  138. self.$content.find('#chat').append( $(QWeb.render('muk_web_share.ShareChatItems', {
  139. widget: self,
  140. channels: channels,
  141. })));
  142. self.$content.find('.muk_share_chat_chat').click(function(e) {
  143. var channel_id = $(e.currentTarget).data('channel_id');
  144. self.postMessage(self.formatUrl(self.url), channel_id);
  145. });
  146. });
  147. },
  148. postMessage: function(message, channel_id) {
  149. chat_manager.post_message({
  150. content: message,
  151. }, {
  152. channel_id: channel_id,
  153. });
  154. chat_manager.open_channel(chat_manager.get_channel(channel_id));
  155. this.updateChannels();
  156. },
  157. newMessage: function(message) {
  158. this.copyClipboard(message);
  159. this.openChat();
  160. this.close();
  161. },
  162. openChat: function() {
  163. chat_manager.bus.trigger('open_chat');
  164. },
  165. copyClipboard: function(text) {
  166. var $temp = $("<input>");
  167. $("body").append($temp);
  168. $temp.val(text).select();
  169. document.execCommand("copy");
  170. $temp.remove();
  171. this.do_notify(_t("Link has been copied to clipboard!"));
  172. },
  173. formatUrl: function(url) {
  174. if(this.__parentedParent && this.__parentedParent.state) {
  175. var data = this.__parentedParent.state.data;
  176. return $('<div>').append($('<a>',{
  177. text: data.name || url,
  178. title: _t('Odoo Link'),
  179. href: url,
  180. })).html();
  181. }
  182. return $('<div>').append($('<a>',{
  183. text: url,
  184. title: _t('Odoo Link'),
  185. href: url,
  186. })).html();
  187. }
  188. });
  189. ShareDialog.share = function (owner) {
  190. return new ShareDialog(owner, _.extend({
  191. url: window.location.href,
  192. })).open();
  193. };
  194. return ShareDialog;
  195. });