瀏覽代碼

Move method from view to model

JC Brand 4 年之前
父節點
當前提交
05abb1dae9
共有 2 個文件被更改,包括 20 次插入20 次删除
  1. 20 0
      src/headless/plugins/chat/model.js
  2. 0 20
      src/plugins/chatview/view.js

+ 20 - 0
src/headless/plugins/chat/model.js

@@ -58,8 +58,11 @@ const ChatBox = ModelWithContact.extend({
         if (this.get('type') === _converse.PRIVATE_CHAT_TYPE) {
         if (this.get('type') === _converse.PRIVATE_CHAT_TYPE) {
             this.presence = _converse.presences.findWhere({'jid': jid}) || _converse.presences.create({'jid': jid});
             this.presence = _converse.presences.findWhere({'jid': jid}) || _converse.presences.create({'jid': jid});
             await this.setRosterContact(jid);
             await this.setRosterContact(jid);
+            this.presence.on('change:show', this.onPresenceChanged);
         }
         }
         this.on('change:chat_state', this.sendChatState, this);
         this.on('change:chat_state', this.sendChatState, this);
+
+
         await this.fetchMessages();
         await this.fetchMessages();
         /**
         /**
          * Triggered once a {@link _converse.ChatBox} has been created and initialized.
          * Triggered once a {@link _converse.ChatBox} has been created and initialized.
@@ -275,6 +278,23 @@ const ChatBox = ModelWithContact.extend({
         this.announceReconnection();
         this.announceReconnection();
     },
     },
 
 
+    onPresenceChanged (item) {
+        const { __ } = _converse;
+        const show = item.get('show');
+        const fullname = this.getDisplayName();
+        let text;
+        if (show === 'offline') {
+            text = __('%1$s has gone offline', fullname);
+        } else if (show === 'away') {
+            text = __('%1$s has gone away', fullname);
+        } else if (show === 'dnd') {
+            text = __('%1$s is busy', fullname);
+        } else if (show === 'online') {
+            text = __('%1$s is online', fullname);
+        }
+        text && this.createMessage({ 'message': text, 'type': 'info' });
+    },
+
     validate (attrs) {
     validate (attrs) {
         if (!attrs.jid) {
         if (!attrs.jid) {
             return 'Ignored ChatBox without JID';
             return 'Ignored ChatBox without JID';

+ 0 - 20
src/plugins/chatview/view.js

@@ -29,7 +29,6 @@ export default class ChatView extends BaseChatView {
         this.listenTo(_converse, 'windowStateChanged', this.onWindowStateChanged);
         this.listenTo(_converse, 'windowStateChanged', this.onWindowStateChanged);
         this.listenTo(this.model, 'change:hidden', () => !this.model.get('hidden') && this.afterShown());
         this.listenTo(this.model, 'change:hidden', () => !this.model.get('hidden') && this.afterShown());
         this.listenTo(this.model, 'change:status', this.onStatusMessageChanged);
         this.listenTo(this.model, 'change:status', this.onStatusMessageChanged);
-        this.listenTo(this.model.presence, 'change:show', this.onPresenceChanged);
         this.render();
         this.render();
 
 
         // Need to be registered after render has been called.
         // Need to be registered after render has been called.
@@ -134,25 +133,6 @@ export default class ChatView extends BaseChatView {
         }
         }
     }
     }
 
 
-    onPresenceChanged (item) {
-        const show = item.get('show');
-        const fullname = this.model.getDisplayName();
-
-        let text;
-        if (u.isVisible(this)) {
-            if (show === 'offline') {
-                text = __('%1$s has gone offline', fullname);
-            } else if (show === 'away') {
-                text = __('%1$s has gone away', fullname);
-            } else if (show === 'dnd') {
-                text = __('%1$s is busy', fullname);
-            } else if (show === 'online') {
-                text = __('%1$s is online', fullname);
-            }
-            text && this.model.createMessage({ 'message': text, 'type': 'info' });
-        }
-    }
-
     async close (ev) {
     async close (ev) {
         ev?.preventDefault?.();
         ev?.preventDefault?.();
         if (_converse.router.history.getFragment() === 'converse/chat?jid=' + this.model.get('jid')) {
         if (_converse.router.history.getFragment() === 'converse/chat?jid=' + this.model.get('jid')) {