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.

210 lines
8.0 KiB

  1. /**********************************************************************************
  2. *
  3. * Copyright (c) 2017-2019 MuK IT GmbH.
  4. *
  5. * This file is part of MuK Backend Theme Mail
  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_theme_mail.Discuss', function (require) {
  23. "use strict";
  24. const config = require("web.config");
  25. const core = require("web.core");
  26. const Discuss = require("mail.Discuss");
  27. const QWeb = core.qweb;
  28. if (!config.device.isMobile) {
  29. return;
  30. }
  31. Discuss.include({
  32. contentTemplate: "mail.discuss_mobile",
  33. events: Object.assign({}, Discuss.prototype.events, {
  34. "click .o_mail_mobile_tab": "_onClickMobileTab",
  35. "click .o_mailbox_inbox_item": "_onClickMobileMailboxItem",
  36. "click .o_mail_preview": "_onClickMobileMailPreview",
  37. }),
  38. init: function() {
  39. this._super.apply(this, arguments);
  40. this._currentState = this._defaultThreadID;
  41. },
  42. start: function() {
  43. this._$mainContent = this.$(".o_mail_discuss_content");
  44. return this._super
  45. .apply(this, arguments)
  46. .then(this._updateControlPanel.bind(this));
  47. },
  48. on_attach_callback: function() {
  49. if (this._thread && this._isInInboxTab()) {
  50. this._threadWidget.scrollToPosition(
  51. this._threadsScrolltop[this._thread.getID()]
  52. );
  53. }
  54. },
  55. on_detach_callback: function() {
  56. if (this._isInInboxTab()) {
  57. this._threadsScrolltop[
  58. this._thread.getID()
  59. ] = this._threadWidget.getScrolltop();
  60. }
  61. },
  62. _isInInboxTab: function() {
  63. return _.contains(["mailbox_inbox", "mailbox_starred"], this._currentState);
  64. },
  65. _renderButtons: function() {
  66. this._super.apply(this, arguments);
  67. _.each(["dm_chat", "multi_user_channel"], type => {
  68. const selector = ".o_mail_discuss_button_" + type;
  69. this.$buttons.on("click", selector, this._onAddThread.bind(this));
  70. });
  71. },
  72. _restoreThreadState: function() {
  73. if (this._isInInboxTab()) {
  74. this._super.apply(this, arguments);
  75. }
  76. },
  77. _selectMessage: function() {
  78. this._super.apply(this, arguments);
  79. this.$(".o_mail_mobile_tabs").addClass("o_hidden");
  80. },
  81. _setThread: function(threadID) {
  82. const thread = this.call("mail_service", "getThread", threadID);
  83. this._thread = thread;
  84. if (thread.getType() !== "mailbox") {
  85. this.call("mail_service", "openThreadWindow", threadID);
  86. return Promise.resolve();
  87. }
  88. return this._super.apply(this, arguments);
  89. },
  90. _storeThreadState: function() {
  91. if (this._thread && this._isInInboxTab()) {
  92. this._super.apply(this, arguments);
  93. }
  94. },
  95. _unselectMessage: function() {
  96. this._super.apply(this, arguments);
  97. this.$(".o_mail_mobile_tabs").removeClass("o_hidden");
  98. },
  99. _updateThreads: function() {
  100. return this._updateContent(this._currentState);
  101. },
  102. _updateContent: function(type) {
  103. const inMailbox = type === "mailbox_inbox" || type === "mailbox_starred";
  104. if (!inMailbox && this._isInInboxTab()) {
  105. this._storeThreadState();
  106. }
  107. const previouslyInInbox = this._isInInboxTab();
  108. this._currentState = type;
  109. let def = false;
  110. if (inMailbox) {
  111. def = this._fetchAndRenderThread();
  112. } else {
  113. const allChannels = this.call("mail_service", "getChannels");
  114. const channels = _.filter(allChannels, function(channel) {
  115. return channel.getType() === type;
  116. });
  117. def = this.call("mail_service", "getChannelPreviews", channels);
  118. }
  119. return def.then(previews => {
  120. if (inMailbox) {
  121. if (!previouslyInInbox) {
  122. this.$(".o_mail_discuss_tab_pane").remove();
  123. this._$mainContent.append(this._threadWidget.$el);
  124. this._$mainContent.append(this._extendedComposer.$el);
  125. }
  126. this._restoreThreadState();
  127. } else {
  128. this._threadWidget.$el.detach();
  129. this._extendedComposer.$el.detach();
  130. const $content = $(
  131. QWeb.render("mail.discuss.MobileTabPane", {
  132. previews: previews,
  133. type: type,
  134. })
  135. );
  136. this._prepareAddThreadInput(
  137. $content.find(".o_mail_add_thread input"),
  138. type
  139. );
  140. this._$mainContent.html($content);
  141. }
  142. this.$buttons
  143. .find("button")
  144. .removeClass("d-block")
  145. .addClass("d-none");
  146. this.$buttons
  147. .find(".o_mail_discuss_button_" + type)
  148. .removeClass("d-none")
  149. .addClass("d-block");
  150. this.$buttons
  151. .find(".o_mail_discuss_button_mark_all_read")
  152. .toggleClass("d-none", type !== "mailbox_inbox")
  153. .toggleClass("d-block", type === "mailbox_inbox");
  154. this.$buttons
  155. .find(".o_mail_discuss_button_unstar_all")
  156. .toggleClass("d-none", type !== "mailbox_starred")
  157. .toggleClass("d-block", type === "mailbox_starred");
  158. if (inMailbox) {
  159. this.$(".o_mail_discuss_mobile_mailboxes_buttons").removeClass(
  160. "o_hidden"
  161. );
  162. this.$(".o_mailbox_inbox_item")
  163. .removeClass("btn-primary")
  164. .addClass("btn-secondary");
  165. this.$(".o_mailbox_inbox_item[data-type=" + type + "]")
  166. .removeClass("btn-secondary")
  167. .addClass("btn-primary");
  168. } else {
  169. this.$(".o_mail_discuss_mobile_mailboxes_buttons").addClass(
  170. "o_hidden"
  171. );
  172. }
  173. this.$(".o_mail_mobile_tab").removeClass("active");
  174. const type_n = type === "mailbox_starred" ? "mailbox_inbox" : type;
  175. this.$(".o_mail_mobile_tab[data-type=" + type_n + "]").addClass(
  176. "active"
  177. );
  178. });
  179. },
  180. _onAddThread: function() {
  181. this.$(".o_mail_add_thread")
  182. .show()
  183. .find("input")
  184. .focus();
  185. },
  186. _onClickMobileMailboxItem: function(ev) {
  187. const mailboxID = $(ev.currentTarget).data("type");
  188. this._setThread(mailboxID);
  189. this._updateContent(this._thread.getID());
  190. },
  191. _onClickMobileTab: function(ev) {
  192. const type = $(ev.currentTarget).data("type");
  193. if (type === "mailbox") {
  194. const inbox = this.call("mail_service", "getMailbox", "inbox");
  195. this._setThread(inbox);
  196. }
  197. this._updateContent(type);
  198. },
  199. _onClickMobileMailPreview: function(ev) {
  200. const threadID = $(ev.currentTarget).data("preview-id");
  201. this.call("mail_service", "openThreadWindow", threadID);
  202. },
  203. });
  204. });