index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * @module converse-minimize
  3. * @copyright 2020, the Converse.js contributors
  4. * @license Mozilla Public License (MPLv2)
  5. */
  6. import './components/minimized-chat.js';
  7. import 'plugins/chatview/index.js';
  8. import MinimizedChats from './view.js';
  9. import MinimizedChatsToggle from './toggle.js';
  10. import { _converse, api, converse } from '@converse/headless/core';
  11. import { addMinimizeButtonToChat, addMinimizeButtonToMUC, trimChats } from './utils.js';
  12. import { debounce } from 'lodash-es';
  13. import { minimizableChatBox, minimizableChatBoxView } from './mixins.js';
  14. const { dayjs } = converse.env;
  15. converse.plugins.add('converse-minimize', {
  16. /* Optional dependencies are other plugins which might be
  17. * overridden or relied upon, and therefore need to be loaded before
  18. * this plugin. They are called "optional" because they might not be
  19. * available, in which case any overrides applicable to them will be
  20. * ignored.
  21. *
  22. * It's possible however to make optional dependencies non-optional.
  23. * If the setting "strict_plugin_dependencies" is set to true,
  24. * an error will be raised if the plugin is not found.
  25. *
  26. * NB: These plugins need to have already been loaded via require.js.
  27. */
  28. dependencies: [
  29. "converse-chatview",
  30. "converse-controlbox",
  31. "converse-muc-views",
  32. "converse-headlines-view",
  33. "converse-dragresize"
  34. ],
  35. enabled (_converse) {
  36. return _converse.api.settings.get("view_mode") === 'overlayed';
  37. },
  38. overrides: {
  39. // Overrides mentioned here will be picked up by converse.js's
  40. // plugin architecture they will replace existing methods on the
  41. // relevant objects or classes.
  42. //
  43. // New functions which don't exist yet can also be added.
  44. ChatBox: {
  45. initialize () {
  46. this.__super__.initialize.apply(this, arguments);
  47. this.on('change:hidden', m => !m.get('hidden') && this.maximize(), this);
  48. if (this.get('id') === 'controlbox') {
  49. return;
  50. }
  51. this.save({
  52. 'minimized': this.get('minimized') || false,
  53. 'time_minimized': this.get('time_minimized') || dayjs(),
  54. });
  55. },
  56. maybeShow (force) {
  57. if (!force && this.get('minimized')) {
  58. // Must return the chatbox
  59. return this;
  60. }
  61. return this.__super__.maybeShow.apply(this, arguments);
  62. },
  63. isHidden () {
  64. return this.__super__.isHidden.call(this) || this.get('minimized');
  65. }
  66. },
  67. ChatBoxView: {
  68. isNewMessageHidden () {
  69. return this.model.get('minimized') ||
  70. this.__super__.isNewMessageHidden.apply(this, arguments);
  71. },
  72. setChatBoxHeight (height) {
  73. if (!this.model.get('minimized')) {
  74. return this.__super__.setChatBoxHeight.call(this, height);
  75. }
  76. },
  77. setChatBoxWidth (width) {
  78. if (!this.model.get('minimized')) {
  79. return this.__super__.setChatBoxWidth.call(this, width);
  80. }
  81. }
  82. }
  83. },
  84. initialize () {
  85. /* The initialize function gets called as soon as the plugin is
  86. * loaded by Converse.js's plugin machinery.
  87. */
  88. api.settings.extend({'no_trimming': false});
  89. Object.assign(_converse.ChatBox.prototype, minimizableChatBox);
  90. Object.assign(_converse.ChatBoxView.prototype, minimizableChatBoxView);
  91. api.promises.add('minimizedChatsInitialized');
  92. _converse.MinimizedChatsToggle = MinimizedChatsToggle;
  93. _converse.MinimizedChats = MinimizedChats;
  94. _converse.minimize = {};
  95. _converse.minimize.trimChats = trimChats;
  96. /************************ BEGIN Event Handlers ************************/
  97. api.listen.on('chatBoxViewInitialized', view => _converse.minimize.trimChats(view));
  98. api.listen.on('chatRoomViewInitialized', view => _converse.minimize.trimChats(view));
  99. api.listen.on('controlBoxOpened', view => _converse.minimize.trimChats(view));
  100. api.listen.on('chatBoxViewInitialized', v => v.listenTo(v.model, 'change:minimized', v.onMinimizedChanged));
  101. api.listen.on('chatRoomViewInitialized', view => {
  102. view.listenTo(view.model, 'change:minimized', view.onMinimizedChanged)
  103. view.model.get('minimized') && view.hide();
  104. });
  105. api.listen.on('getHeadingButtons', (view, buttons) => {
  106. if (view.model.get('type') === _converse.CHATROOMS_TYPE) {
  107. return addMinimizeButtonToMUC(view, buttons);
  108. } else {
  109. return addMinimizeButtonToChat(view, buttons);
  110. }
  111. });
  112. const debouncedTrimChats = debounce(() => _converse.minimize.trimChats(), 250);
  113. api.listen.on('registeredGlobalEventHandlers', () => window.addEventListener("resize", debouncedTrimChats));
  114. api.listen.on('unregisteredGlobalEventHandlers', () => window.removeEventListener("resize", debouncedTrimChats));
  115. }
  116. });