Browse Source

Some refactoring to fix issues with how/when chat rooms are shown

- Don't call show in a room's initialize method (instead let the code be more
  similar to normal chats, in that it should listen to the "show" trigger).
- Rename chatBoxShouldBeShown to chatBoxMayBeShown
- Implement auto_join for rooms only once boxes have been fetched already.
JC Brand 9 years ago
parent
commit
94693f2d02
4 changed files with 30 additions and 15 deletions
  1. 2 2
      src/converse-controlbox.js
  2. 12 4
      src/converse-core.js
  3. 2 2
      src/converse-minimize.js
  4. 14 7
      src/converse-muc.js

+ 2 - 2
src/converse-controlbox.js

@@ -73,8 +73,8 @@
             },
 
             ChatBoxes: {
-                chatBoxShouldBeShown: function (chatbox) {
-                    return this._super.chatBoxShouldBeShown.apply(this, arguments) &&
+                chatBoxMayBeShown: function (chatbox) {
+                    return this._super.chatBoxMayBeShown.apply(this, arguments) &&
                            chatbox.get('id') !== 'controlbox';
                 },
 

+ 12 - 4
src/converse-core.js

@@ -1215,7 +1215,7 @@
                     }.bind(this), null, 'message', 'chat');
             },
 
-            chatBoxShouldBeShown: function (chatbox) {
+            chatBoxMayBeShown: function (chatbox) {
                 return true;
             },
 
@@ -1226,10 +1226,11 @@
                  * if the controlbox plugin is active.
                  */
                 collection.each(function (chatbox) {
-                    if (this.chatBoxShouldBeShown(chatbox)) {
+                    if (this.chatBoxMayBeShown(chatbox)) {
                         chatbox.trigger('show');
                     }
                 }.bind(this));
+                converse.emit('chatBoxesFetched');
             },
 
             onConnected: function () {
@@ -1385,8 +1386,13 @@
                 return this;
             },
 
+            chatBoxMayBeShown: function (chatbox) {
+                return this.model.chatBoxMayBeShown(chatbox);
+            },
+
             showChat: function (attrs) {
-                /* Find the chat box and show it. If it doesn't exist, create it.
+                /* Find the chat box and show it (if it may be shown).
+                 * If it doesn't exist, create it.
                  */
                 var chatbox  = this.model.get(attrs.jid);
                 if (!chatbox) {
@@ -1396,7 +1402,9 @@
                         }
                     });
                 }
-                chatbox.trigger('show', true);
+                if (this.chatBoxMayBeShown(chatbox)) {
+                    chatbox.trigger('show', true);
+                }
                 return chatbox;
             }
         });

+ 2 - 2
src/converse-minimize.js

@@ -191,8 +191,8 @@
             },
 
             ChatBoxes: {
-                chatBoxShouldBeShown: function (chatbox) {
-                    return this._super.chatBoxShouldBeShown.apply(this, arguments) &&
+                chatBoxMayBeShown: function (chatbox) {
+                    return this._super.chatBoxMayBeShown.apply(this, arguments) &&
                            !chatbox.get('minimized');
                 },
             },

+ 14 - 7
src/converse-muc.js

@@ -180,6 +180,9 @@
 
                 initialize: function () {
                     this.model.messages.on('add', this.onMessageAdded, this);
+                    this.model.on('show', this.show, this);
+                    this.model.on('destroy', this.hide, this);
+
                     this.occupantsview = new converse.ChatRoomOccupantsView({
                         model: new converse.ChatRoomOccupants({nick: this.model.get('nick')})
                     });
@@ -191,7 +194,6 @@
                     this.join(null, {'maxstanzas': converse.muc_history_max_stanzas});
                     this.fetchMessages();
                     this.$el.insertAfter(converse.chatboxviews.get("controlbox").$el);
-                    this.show();
                     converse.emit('chatRoomOpened', this);
                 },
 
@@ -1312,12 +1314,8 @@
                     }
                 }
             };
-            var onConnected = function () {
-                converse.connection.addHandler(
-                    function (message) {
-                        converse.onDirectMUCInvitation(message);
-                        return true;
-                    }, 'jabber:x:conference', 'message');
+
+            var autoJoinRooms = function () {
                 _.each(converse.auto_join_rooms, function (room) {
                     if (typeof room === 'string') {
                         converse_api.rooms.open(room);
@@ -1328,6 +1326,15 @@
                     }
                 });
             };
+            converse.on('chatBoxesFetched', autoJoinRooms);
+
+            var onConnected = function () {
+                converse.connection.addHandler(
+                    function (message) {
+                        converse.onDirectMUCInvitation(message);
+                        return true;
+                    }, 'jabber:x:conference', 'message');
+            };
             converse.on('connected', onConnected);
             converse.on('reconnected', onConnected);
             /* ------------------------------------------------------------ */