浏览代码

Refactor chat `close` methods

- Remove the shared base-class method.
- Trigger `chatBoxClosed` on the model
- Send CSN from the model, not the view
JC Brand 4 年之前
父节点
当前提交
0756af2b30
共有 5 个文件被更改,包括 25 次插入23 次删除
  1. 2 2
      CHANGES.md
  2. 14 1
      src/headless/plugins/chat/model.js
  3. 7 1
      src/plugins/chatview/view.js
  4. 2 1
      src/plugins/muc-views/muc.js
  5. 0 18
      src/shared/chat/baseview.js

+ 2 - 2
CHANGES.md

@@ -37,8 +37,8 @@ Removed events:
 * `rosterGroupsFetched`
 * `messageSend` (use `sendMessage` instead)
 
-The `chatBoxMaximized` and `chatBoxMinimized` events now have the `model` as
-payload and not the `view` since it might not be exist at that time.
+The `chatBoxClosed`, `chatBoxMaximized` and `chatBoxMinimized` events now have the `model` as
+payload and not the `view`.
 
 ## 7.0.5 (Unreleased)
 

+ 14 - 1
src/headless/plugins/chat/model.js

@@ -250,6 +250,12 @@ const ChatBox = ModelWithContact.extend({
     },
 
     async close () {
+        if (api.connection.connected()) {
+            // Immediately sending the chat state, because the
+            // model is going to be destroyed afterwards.
+            this.setChatState(_converse.INACTIVE);
+            this.sendChatState();
+        }
         try {
             await new Promise((success, reject) => {
                 return this.destroy({success, 'error': (m, e) => reject(e)})
@@ -261,6 +267,13 @@ const ChatBox = ModelWithContact.extend({
                 await this.clearMessages();
             }
         }
+        /**
+         * Triggered once a chatbox has been closed.
+         * @event _converse#chatBoxClosed
+         * @type {_converse.ChatBox | _converse.ChatRoom}
+         * @example _converse.api.listen.on('chatBoxClosed', chat => { ... });
+         */
+        api.trigger('chatBoxClosed', this);
     },
 
     announceReconnection () {
@@ -268,7 +281,7 @@ const ChatBox = ModelWithContact.extend({
          * Triggered whenever a `_converse.ChatBox` instance has reconnected after an outage
          * @event _converse#onChatReconnected
          * @type {_converse.ChatBox | _converse.ChatRoom}
-         * @example _converse.api.listen.on('onChatReconnected', chatbox => { ... });
+         * @example _converse.api.listen.on('onChatReconnected', chat => { ... });
          */
         api.trigger('chatReconnected', this);
     },

+ 7 - 1
src/plugins/chatview/view.js

@@ -133,11 +133,17 @@ export default class ChatView extends BaseChatView {
         }
     }
 
+    /**
+     * Closes this chat
+     * @private
+     * @method _converse.ChatBoxView#close
+     */
     close (ev) {
+        ev?.preventDefault?.();
         if (_converse.router.history.getFragment() === 'converse/chat?jid=' + this.model.get('jid')) {
             _converse.router.navigate('');
         }
-        return super.close(ev);
+        return this.model.close(ev);
     }
 
     afterShown () {

+ 2 - 1
src/plugins/muc-views/muc.js

@@ -67,10 +67,11 @@ export default class MUCView extends BaseChatView {
      * @method _converse.ChatRoomView#close
      */
     close (ev) {
+        ev?.preventDefault?.();
         if (_converse.router.history.getFragment() === 'converse/room?jid=' + this.model.get('jid')) {
             _converse.router.navigate('');
         }
-        return super.close(ev);
+        return this.model.close(ev);
     }
 
     async destroy () {

+ 0 - 18
src/shared/chat/baseview.js

@@ -50,24 +50,6 @@ export default class BaseChatView extends ElementView {
         this.afterShown();
     }
 
-    async close (ev) {
-        ev?.preventDefault?.();
-        if (api.connection.connected()) {
-            // Immediately sending the chat state, because the
-            // model is going to be destroyed afterwards.
-            this.model.setChatState(_converse.INACTIVE);
-            this.model.sendChatState();
-        }
-        await this.model.close(ev);
-        /**
-         * Triggered once a chatbox has been closed.
-         * @event _converse#chatBoxClosed
-         * @type { _converse.ChatBoxView | _converse.ChatRoomView }
-         * @example _converse.api.listen.on('chatBoxClosed', view => { ... });
-         */
-        api.trigger('chatBoxClosed', this);
-    }
-
     emitBlurred (ev) {
         if (this.contains(document.activeElement) || this.contains(ev.relatedTarget)) {
             // Something else in this chatbox is still focused