utils.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import { _converse, api, converse } from '@converse/headless/core';
  2. import { __ } from 'i18n';
  3. const u = converse.env.utils;
  4. function getChatBoxWidth (view) {
  5. if (view.model.get('id') === 'controlbox') {
  6. // We return the width of the controlbox or its toggle,
  7. // depending on which is visible.
  8. if (u.isVisible(view)) {
  9. return u.getOuterWidth(view, true);
  10. } else {
  11. return u.getOuterWidth(_converse.controlboxtoggle.el, true);
  12. }
  13. } else if (!view.model.get('minimized') && u.isVisible(view)) {
  14. return u.getOuterWidth(view, true);
  15. }
  16. return 0;
  17. }
  18. function getShownChats () {
  19. return _converse.chatboxviews.filter(el =>
  20. // The controlbox can take a while to close,
  21. // so we need to check its state. That's why we checked the 'closed' state.
  22. !el.model.get('minimized') && !el.model.get('closed') && u.isVisible(el)
  23. );
  24. }
  25. function getMinimizedWidth () {
  26. const minimized_el = _converse.minimized_chats?.el;
  27. return _converse.chatboxes.pluck('minimized').includes(true) ? u.getOuterWidth(minimized_el, true) : 0;
  28. }
  29. function getBoxesWidth (newchat) {
  30. const new_id = newchat ? newchat.model.get('id') : null;
  31. const newchat_width = newchat ? u.getOuterWidth(newchat.el, true) : 0;
  32. return Object.values(_converse.chatboxviews.xget(new_id))
  33. .reduce((memo, view) => memo + getChatBoxWidth(view), newchat_width);
  34. }
  35. /**
  36. * This method is called when a newly created chat box will be shown.
  37. * It checks whether there is enough space on the page to show
  38. * another chat box. Otherwise it minimizes the oldest chat box
  39. * to create space.
  40. * @private
  41. * @method _converse.ChatBoxViews#trimChats
  42. * @param { _converse.ChatBoxView|_converse.ChatRoomView|_converse.ControlBoxView|_converse.HeadlinesBoxView } [newchat]
  43. */
  44. export async function trimChats (newchat) {
  45. if (_converse.isTestEnv() || api.settings.get('no_trimming') || !api.connection.connected() || api.settings.get("view_mode") !== 'overlayed') {
  46. return;
  47. }
  48. const shown_chats = getShownChats();
  49. if (shown_chats.length <= 1) {
  50. return;
  51. }
  52. const body_width = u.getOuterWidth(document.querySelector('body'), true);
  53. if (getChatBoxWidth(shown_chats[0]) === body_width) {
  54. // If the chats shown are the same width as the body,
  55. // then we're in responsive mode and the chats are
  56. // fullscreen. In this case we don't trim.
  57. return;
  58. }
  59. await api.waitUntil('minimizedChatsInitialized');
  60. const minimized_el = _converse.minimized_chats?.el;
  61. if (minimized_el) {
  62. while ((getMinimizedWidth() + getBoxesWidth(newchat)) > body_width) {
  63. const new_id = newchat ? newchat.model.get('id') : null;
  64. const oldest_chat = getOldestMaximizedChat([new_id]);
  65. if (oldest_chat) {
  66. // We hide the chat immediately, because waiting
  67. // for the event to fire (and letting the
  68. // ChatBoxView hide it then) causes race
  69. // conditions.
  70. const view = _converse.chatboxviews.get(oldest_chat.get('id'));
  71. if (view) {
  72. view.hide();
  73. }
  74. minimize(oldest_chat);
  75. } else {
  76. break;
  77. }
  78. }
  79. }
  80. }
  81. function getOldestMaximizedChat (exclude_ids) {
  82. // Get oldest view (if its id is not excluded)
  83. exclude_ids.push('controlbox');
  84. let i = 0;
  85. let model = _converse.chatboxes.sort().at(i);
  86. while (exclude_ids.includes(model.get('id')) || model.get('minimized') === true) {
  87. i++;
  88. model = _converse.chatboxes.at(i);
  89. if (!model) {
  90. return null;
  91. }
  92. }
  93. return model;
  94. }
  95. export function addMinimizeButtonToChat (view, buttons) {
  96. const data = {
  97. 'a_class': 'toggle-chatbox-button',
  98. 'handler': ev => minimize(ev, view.model),
  99. 'i18n_text': __('Minimize'),
  100. 'i18n_title': __('Minimize this chat'),
  101. 'icon_class': "fa-minus",
  102. 'name': 'minimize',
  103. 'standalone': _converse.api.settings.get("view_mode") === 'overlayed'
  104. }
  105. const names = buttons.map(t => t.name);
  106. const idx = names.indexOf('close');
  107. return idx > -1 ? [...buttons.slice(0, idx), data, ...buttons.slice(idx)] : [data, ...buttons];
  108. }
  109. export function addMinimizeButtonToMUC (view, buttons) {
  110. const data = {
  111. 'a_class': 'toggle-chatbox-button',
  112. 'handler': ev => minimize(ev, view.model),
  113. 'i18n_text': __('Minimize'),
  114. 'i18n_title': __('Minimize this groupchat'),
  115. 'icon_class': "fa-minus",
  116. 'name': 'minimize',
  117. 'standalone': _converse.api.settings.get("view_mode") === 'overlayed'
  118. }
  119. const names = buttons.map(t => t.name);
  120. const idx = names.indexOf('signout');
  121. return idx > -1 ? [...buttons.slice(0, idx), data, ...buttons.slice(idx)] : [data, ...buttons];
  122. }
  123. export function maximize (ev, chatbox) {
  124. if (ev?.preventDefault) {
  125. ev.preventDefault();
  126. } else {
  127. chatbox = ev;
  128. }
  129. u.safeSave(chatbox, {
  130. 'hidden': false,
  131. 'minimized': false,
  132. 'time_opened': new Date().getTime()
  133. });
  134. }
  135. export function minimize (ev, model) {
  136. if (ev?.preventDefault) {
  137. ev.preventDefault();
  138. } else {
  139. model = ev;
  140. }
  141. model.setChatState(_converse.INACTIVE);
  142. u.safeSave(model, {
  143. 'hidden': true,
  144. 'minimized': true,
  145. 'time_minimized': new Date().toISOString()
  146. });
  147. }
  148. /**
  149. * Handler which gets called when a {@link _converse#ChatBox} has it's
  150. * `minimized` property set to false.
  151. *
  152. * Will trigger {@link _converse#chatBoxMaximized}
  153. * @returns {_converse.ChatBoxView|_converse.ChatRoomView}
  154. */
  155. function onMaximized (model) {
  156. if (!model.isScrolledUp()) {
  157. model.clearUnreadMsgCounter();
  158. }
  159. model.setChatState(_converse.ACTIVE);
  160. /**
  161. * Triggered when a previously minimized chat gets maximized
  162. * @event _converse#chatBoxMaximized
  163. * @type { _converse.ChatBoxView }
  164. * @example _converse.api.listen.on('chatBoxMaximized', view => { ... });
  165. */
  166. api.trigger('chatBoxMaximized', model);
  167. }
  168. /**
  169. * Handler which gets called when a {@link _converse#ChatBox} has it's
  170. * `minimized` property set to true.
  171. *
  172. * Will trigger {@link _converse#chatBoxMinimized}
  173. * @returns {_converse.ChatBoxView|_converse.ChatRoomView}
  174. */
  175. function onMinimized (model) {
  176. /**
  177. * Triggered when a previously maximized chat gets Minimized
  178. * @event _converse#chatBoxMinimized
  179. * @type { _converse.ChatBoxView }
  180. * @example _converse.api.listen.on('chatBoxMinimized', view => { ... });
  181. */
  182. api.trigger('chatBoxMinimized', model);
  183. }
  184. export function onMinimizedChanged (model) {
  185. if (model.get('minimized')) {
  186. onMinimized(model);
  187. } else {
  188. onMaximized(model);
  189. }
  190. }