Converse converse.js

Source: plugins/muc-views/heading.js

  1. import './modals/muc-details.js';
  2. import './modals/muc-invite.js';
  3. import './modals/nickname.js';
  4. import tplMUCHead from './templates/muc-head.js';
  5. import { CustomElement } from 'shared/components/element.js';
  6. import { Model } from '@converse/skeletor/src/model.js';
  7. import { __ } from 'i18n';
  8. import { _converse, api, converse } from "@converse/headless/core.js";
  9. import { destroyMUC, showModeratorToolsModal } from './utils.js';
  10. import './styles/muc-head.scss';
  11. export default class MUCHeading extends CustomElement {
  12. async initialize () {
  13. this.model = _converse.chatboxes.get(this.getAttribute('jid'));
  14. this.listenTo(this.model, 'change', () => this.requestUpdate());
  15. this.listenTo(this.model, 'vcard:add', () => this.requestUpdate());
  16. this.listenTo(this.model, 'vcard:change', () => this.requestUpdate());
  17. this.user_settings = await _converse.api.user.settings.getModel();
  18. this.listenTo(this.user_settings, 'change:mucs_with_hidden_subject', () => this.requestUpdate());
  19. await this.model.initialized;
  20. this.listenTo(this.model.features, 'change:open', () => this.requestUpdate());
  21. this.model.occupants.forEach(o => this.onOccupantAdded(o));
  22. this.listenTo(this.model.occupants, 'add', this.onOccupantAdded);
  23. this.listenTo(this.model.occupants, 'change:affiliation', this.onOccupantAffiliationChanged);
  24. this.requestUpdate();
  25. }
  26. render () {
  27. return (this.model && this.user_settings) ? tplMUCHead(this) : '';
  28. }
  29. onOccupantAdded (occupant) {
  30. if (occupant.get('jid') === _converse.bare_jid) {
  31. this.requestUpdate();
  32. }
  33. }
  34. onOccupantAffiliationChanged (occupant) {
  35. if (occupant.get('jid') === _converse.bare_jid) {
  36. this.requestUpdate();
  37. }
  38. }
  39. showRoomDetailsModal (ev) {
  40. ev.preventDefault();
  41. api.modal.show('converse-muc-details-modal', { 'model': this.model }, ev);
  42. }
  43. showInviteModal (ev) {
  44. ev.preventDefault();
  45. api.modal.show('converse-muc-invite-modal', { 'model': new Model(), 'chatroomview': this }, ev);
  46. }
  47. toggleTopic (ev) {
  48. ev?.preventDefault?.();
  49. this.model.toggleSubjectHiddenState();
  50. }
  51. getAndRenderConfigurationForm () {
  52. this.model.session.set('view', converse.MUC.VIEWS.CONFIG);
  53. }
  54. close (ev) {
  55. ev.preventDefault();
  56. this.model.close();
  57. }
  58. destroy (ev) {
  59. ev.preventDefault();
  60. destroyMUC(this.model);
  61. }
  62. /**
  63. * Returns a list of objects which represent buttons for the groupchat header.
  64. * @emits _converse#getHeadingButtons
  65. */
  66. getHeadingButtons (subject_hidden) {
  67. const buttons = [];
  68. buttons.push({
  69. 'i18n_text': __('Details'),
  70. 'i18n_title': __('Show more information about this groupchat'),
  71. 'handler': ev => this.showRoomDetailsModal(ev),
  72. 'a_class': 'show-muc-details-modal',
  73. 'icon_class': 'fa-info-circle',
  74. 'name': 'details'
  75. });
  76. if (this.model.getOwnAffiliation() === 'owner') {
  77. buttons.push({
  78. 'i18n_text': __('Configure'),
  79. 'i18n_title': __('Configure this groupchat'),
  80. 'handler': () => this.getAndRenderConfigurationForm(),
  81. 'a_class': 'configure-chatroom-button',
  82. 'icon_class': 'fa-wrench',
  83. 'name': 'configure'
  84. });
  85. }
  86. buttons.push({
  87. 'i18n_text': __('Nickname'),
  88. 'i18n_title': __("Change the nickname you're using in this groupchat"),
  89. 'handler': ev => api.modal.show('converse-muc-nickname-modal', { 'model': this.model }, ev),
  90. 'a_class': 'open-nickname-modal',
  91. 'icon_class': 'fa-smile',
  92. 'name': 'nickname'
  93. });
  94. if (this.model.invitesAllowed()) {
  95. buttons.push({
  96. 'i18n_text': __('Invite'),
  97. 'i18n_title': __('Invite someone to join this groupchat'),
  98. 'handler': ev => this.showInviteModal(ev),
  99. 'a_class': 'open-invite-modal',
  100. 'icon_class': 'fa-user-plus',
  101. 'name': 'invite'
  102. });
  103. }
  104. const subject = this.model.get('subject');
  105. if (subject && subject.text) {
  106. buttons.push({
  107. 'i18n_text': subject_hidden ? __('Show topic') : __('Hide topic'),
  108. 'i18n_title': subject_hidden
  109. ? __('Show the topic message in the heading')
  110. : __('Hide the topic in the heading'),
  111. 'handler': ev => this.toggleTopic(ev),
  112. 'a_class': 'hide-topic',
  113. 'icon_class': 'fa-minus-square',
  114. 'name': 'toggle-topic'
  115. });
  116. }
  117. const conn_status = this.model.session.get('connection_status');
  118. if (conn_status === converse.ROOMSTATUS.ENTERED) {
  119. const allowed_commands = this.model.getAllowedCommands();
  120. if (allowed_commands.includes('modtools')) {
  121. buttons.push({
  122. 'i18n_text': __('Moderate'),
  123. 'i18n_title': __('Moderate this groupchat'),
  124. 'handler': () => showModeratorToolsModal(this.model),
  125. 'a_class': 'moderate-chatroom-button',
  126. 'icon_class': 'fa-user-cog',
  127. 'name': 'moderate'
  128. });
  129. }
  130. if (allowed_commands.includes('destroy')) {
  131. buttons.push({
  132. 'i18n_text': __('Destroy'),
  133. 'i18n_title': __('Remove this groupchat'),
  134. 'handler': ev => this.destroy(ev),
  135. 'a_class': 'destroy-chatroom-button',
  136. 'icon_class': 'fa-trash',
  137. 'name': 'destroy'
  138. });
  139. }
  140. }
  141. if (!api.settings.get('singleton')) {
  142. buttons.push({
  143. 'i18n_text': __('Leave'),
  144. 'i18n_title': __('Leave and close this groupchat'),
  145. 'handler': async ev => {
  146. ev.stopPropagation();
  147. const messages = [__('Are you sure you want to leave this groupchat?')];
  148. const result = await api.confirm(__('Confirm'), messages);
  149. result && this.close(ev);
  150. },
  151. 'a_class': 'close-chatbox-button',
  152. 'standalone': api.settings.get('view_mode') === 'overlayed',
  153. 'icon_class': 'fa-sign-out-alt',
  154. 'name': 'signout'
  155. });
  156. }
  157. const el = _converse.chatboxviews.get(this.getAttribute('jid'));
  158. if (el) {
  159. // This hook is described in src/plugins/chatview/heading.js
  160. return _converse.api.hook('getHeadingButtons', el, buttons);
  161. } else {
  162. return Promise.resolve(buttons); // Happens during tests
  163. }
  164. }
  165. }
  166. api.elements.define('converse-muc-heading', MUCHeading);