converse-headline.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Converse.js (A browser based XMPP chat client)
  2. // http://conversejs.org
  3. //
  4. // Copyright (c) 2012-2017, Jan-Carel Brand <jc@opkode.com>
  5. // Licensed under the Mozilla Public License (MPLv2)
  6. import "converse-chatview";
  7. import converse from "@converse/headless/converse-core";
  8. import tpl_chatbox from "templates/chatbox.html";
  9. const { _, utils } = converse.env;
  10. converse.plugins.add('converse-headline', {
  11. /* Plugin dependencies are other plugins which might be
  12. * overridden or relied upon, and therefore need to be loaded before
  13. * this plugin.
  14. *
  15. * If the setting "strict_plugin_dependencies" is set to true,
  16. * an error will be raised if the plugin is not found. By default it's
  17. * false, which means these plugins are only loaded opportunistically.
  18. *
  19. * NB: These plugins need to have already been loaded via require.js.
  20. */
  21. dependencies: ["converse-chatview"],
  22. overrides: {
  23. // Overrides mentioned here will be picked up by converse.js's
  24. // plugin architecture they will replace existing methods on the
  25. // relevant objects or classes.
  26. //
  27. // New functions which don't exist yet can also be added.
  28. ChatBoxes: {
  29. model (attrs, options) {
  30. const { _converse } = this.__super__;
  31. if (attrs.type == _converse.HEADLINES_TYPE) {
  32. return new _converse.HeadlinesBox(attrs, options);
  33. } else {
  34. return this.__super__.model.apply(this, arguments);
  35. }
  36. },
  37. }
  38. },
  39. initialize () {
  40. /* The initialize function gets called as soon as the plugin is
  41. * loaded by converse.js's plugin machinery.
  42. */
  43. const { _converse } = this,
  44. { __ } = _converse;
  45. _converse.HeadlinesBox = _converse.ChatBox.extend({
  46. defaults: {
  47. 'type': _converse.HEADLINES_TYPE,
  48. 'bookmarked': false,
  49. 'chat_state': undefined,
  50. 'num_unread': 0,
  51. 'url': ''
  52. },
  53. });
  54. _converse.HeadlinesBoxView = _converse.ChatBoxView.extend({
  55. className: 'chatbox headlines',
  56. events: {
  57. 'click .close-chatbox-button': 'close',
  58. 'click .toggle-chatbox-button': 'minimize',
  59. 'keypress textarea.chat-textarea': 'keyPressed'
  60. },
  61. initialize () {
  62. this.initDebounced();
  63. this.disable_mam = true; // Don't do MAM queries for this box
  64. this.model.messages.on('add', this.onMessageAdded, this);
  65. this.model.on('show', this.show, this);
  66. this.model.on('destroy', this.hide, this);
  67. this.model.on('change:minimized', this.onMinimizedChanged, this);
  68. this.render().insertHeading().fetchMessages().insertIntoDOM().hide();
  69. _converse.emit('chatBoxOpened', this);
  70. _converse.emit('chatBoxInitialized', this);
  71. },
  72. render () {
  73. this.el.setAttribute('id', this.model.get('box_id'))
  74. this.el.innerHTML = tpl_chatbox(
  75. _.extend(this.model.toJSON(), {
  76. info_close: '',
  77. label_personal_message: '',
  78. show_send_button: false,
  79. show_toolbar: false,
  80. unread_msgs: ''
  81. }
  82. ));
  83. this.content = this.el.querySelector('.chat-content');
  84. return this;
  85. },
  86. // Override to avoid the methods in converse-chatview.js
  87. 'renderMessageForm': _.noop,
  88. 'afterShown': _.noop
  89. });
  90. function onHeadlineMessage (message) {
  91. /* Handler method for all incoming messages of type "headline". */
  92. const from_jid = message.getAttribute('from');
  93. if (utils.isHeadlineMessage(_converse, message)) {
  94. if (_.includes(from_jid, '@') &&
  95. !_converse.api.contacts.get(from_jid) &&
  96. !_converse.allow_non_roster_messaging) {
  97. return;
  98. }
  99. if (_.isNull(message.querySelector('body'))) {
  100. // Avoid creating a chat box if we have nothing to show
  101. // inside it.
  102. return;
  103. }
  104. const chatbox = _converse.chatboxes.create({
  105. 'id': from_jid,
  106. 'jid': from_jid,
  107. 'type': _converse.HEADLINES_TYPE,
  108. 'from': from_jid
  109. });
  110. chatbox.createMessage(message, message);
  111. _converse.emit('message', {'chatbox': chatbox, 'stanza': message});
  112. }
  113. return true;
  114. }
  115. function registerHeadlineHandler () {
  116. _converse.connection.addHandler(onHeadlineMessage, null, 'message');
  117. }
  118. _converse.on('connected', registerHeadlineHandler);
  119. _converse.on('reconnected', registerHeadlineHandler);
  120. _converse.on('chatBoxViewsInitialized', () => {
  121. const that = _converse.chatboxviews;
  122. _converse.chatboxes.on('add', item => {
  123. if (!that.get(item.get('id')) && item.get('type') === _converse.HEADLINES_TYPE) {
  124. that.add(item.get('id'), new _converse.HeadlinesBoxView({model: item}));
  125. }
  126. });
  127. });
  128. }
  129. });