user-details.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { api } from "@converse/headless";
  2. import { blockContact, removeContact, unblockContact } from 'plugins/rosterview/utils.js';
  3. import BaseModal from "plugins/modal/modal.js";
  4. import { __ } from 'i18n';
  5. import { tplUserDetailsModal } from "./templates/user-details.js";
  6. export default class UserDetailsModal extends BaseModal {
  7. initialize () {
  8. super.initialize();
  9. this.model.rosterContactAdded.then(() => {
  10. this.registerContactEventHandlers();
  11. api.vcard.update(this.model.contact.vcard, true);
  12. });
  13. this.listenTo(this.model, 'change', this.render);
  14. if (this.model.contact !== undefined) {
  15. this.registerContactEventHandlers();
  16. // Refresh the vcard
  17. api.vcard.update(this.model.contact.vcard, true);
  18. }
  19. /**
  20. * Triggered once the UserDetailsModal has been initialized
  21. * @event _converse#userDetailsModalInitialized
  22. * @type {import('@converse/headless').ChatBox}
  23. * @example _converse.api.listen.on('userDetailsModalInitialized', (chatbox) => { ... });
  24. */
  25. api.trigger('userDetailsModalInitialized', this.model);
  26. }
  27. renderModal () {
  28. return tplUserDetailsModal(this);
  29. }
  30. getModalTitle () {
  31. return this.model.getDisplayName();
  32. }
  33. registerContactEventHandlers () {
  34. this.listenTo(this.model.contact, 'change', this.render);
  35. this.listenTo(this.model.contact.vcard, 'change', this.render);
  36. this.model.contact.on('destroy', () => {
  37. delete this.model.contact;
  38. this.render();
  39. });
  40. // Refresh the vcard
  41. api.vcard.update(this.model.contact.vcard, true);
  42. }
  43. /**
  44. * @param {MouseEvent} ev
  45. */
  46. async removeContact (ev) {
  47. ev?.preventDefault?.();
  48. setTimeout(() => removeContact(this.model.contact), 1);
  49. this.modal.hide();
  50. }
  51. /**
  52. * @param {MouseEvent} ev
  53. */
  54. async blockContact(ev) {
  55. ev?.preventDefault?.();
  56. setTimeout(() => blockContact(this.model.contact), 1);
  57. this.modal.hide();
  58. }
  59. /**
  60. * @param {MouseEvent} ev
  61. */
  62. async unblockContact(ev) {
  63. ev?.preventDefault?.();
  64. setTimeout(() => unblockContact(this.model.contact), 1);
  65. this.modal.hide();
  66. }
  67. }
  68. api.elements.define('converse-user-details-modal', UserDetailsModal);