Bläddra i källkod

New API method `converse.rooms.close()`.

JC Brand 9 år sedan
förälder
incheckning
6c99c51c74
5 ändrade filer med 155 tillägg och 98 borttagningar
  1. 1 0
      docs/CHANGES.md
  2. 6 0
      docs/source/development.rst
  3. 131 0
      spec/chatroom.js
  4. 0 97
      spec/converse.js
  5. 17 1
      src/converse-muc.js

+ 1 - 0
docs/CHANGES.md

@@ -10,6 +10,7 @@
 - Add processing hints to chat state notifications [jcbrand]
 - Don't use sound and desktop notifications for OTR messages (when setting up the session) [jcbrand]
 - New config option [default_state](https://conversejs.org/docs/html/configuration.html#default_state) [jcbrand]
+- New API method `converse.rooms.close()`
 - #553 Add processing hints to OTR messages [jcbrand]
 - #650 Don't ignore incoming messages with same JID as current user (might be MAM archived) [jcbrand]
 

+ 6 - 0
docs/source/development.rst

@@ -678,6 +678,12 @@ To setup a custom nickname when joining the room, provide the optional nick argu
 
     converse.rooms.open('group@muc.example.com', 'mycustomnick')
 
+close
+~~~~~
+
+Lets you close open chat rooms. You can call this method without any arguments
+to close all open chat rooms, or you can specify a single JID or an array of
+JIDs.
 
 The "settings" grouping
 -----------------------

+ 131 - 0
spec/chatroom.js

@@ -23,6 +23,137 @@
             });
         });
 
+        describe("The \"rooms\" API", function () {
+            beforeEach(function () {
+                test_utils.closeAllChatBoxes();
+                test_utils.clearBrowserStorage();
+                converse.rosterview.model.reset();
+                test_utils.createContacts('current');
+            });
+
+            it("has a method 'close' which closes rooms by JID or all rooms when called with no arguments", function () {
+                runs(function () {
+                    test_utils.openChatRoom('lounge', 'localhost', 'dummy');
+                    test_utils.openChatRoom('leisure', 'localhost', 'dummy');
+                    test_utils.openChatRoom('news', 'localhost', 'dummy');
+                    expect(converse.chatboxviews.get('lounge@localhost').$el.is(':visible')).toBeTruthy();
+                    expect(converse.chatboxviews.get('leisure@localhost').$el.is(':visible')).toBeTruthy();
+                    expect(converse.chatboxviews.get('news@localhost').$el.is(':visible')).toBeTruthy();
+                });
+                waits('100');
+                runs(function () {
+                    converse_api.rooms.close('lounge@localhost');
+                    expect(converse.chatboxviews.get('lounge@localhost')).toBeUndefined();
+                    expect(converse.chatboxviews.get('leisure@localhost').$el.is(':visible')).toBeTruthy();
+                    expect(converse.chatboxviews.get('news@localhost').$el.is(':visible')).toBeTruthy();
+                    converse_api.rooms.close(['leisure@localhost', 'news@localhost']);
+                    expect(converse.chatboxviews.get('lounge@localhost')).toBeUndefined();
+                    expect(converse.chatboxviews.get('leisure@localhost')).toBeUndefined();
+                    expect(converse.chatboxviews.get('news@localhost')).toBeUndefined();
+
+                    test_utils.openChatRoom('lounge', 'localhost', 'dummy');
+                    test_utils.openChatRoom('leisure', 'localhost', 'dummy');
+                    expect(converse.chatboxviews.get('lounge@localhost').$el.is(':visible')).toBeTruthy();
+                    expect(converse.chatboxviews.get('leisure@localhost').$el.is(':visible')).toBeTruthy();
+                });
+                waits('100');
+                runs(function () {
+                    converse_api.rooms.close();
+                    expect(converse.chatboxviews.get('lounge@localhost')).toBeUndefined();
+                    expect(converse.chatboxviews.get('leisure@localhost')).toBeUndefined();
+                });
+            });
+
+            it("has a method 'get' which returns a wrapped chat room (if it exists)", function () {
+                waits('300'); // ChatBox.show() is debounced for 250ms
+                runs(function () {
+                    test_utils.openChatRoom('lounge', 'localhost', 'dummy');
+                    var jid = 'lounge@localhost';
+                    var room = converse_api.rooms.get(jid);
+                    expect(room instanceof Object).toBeTruthy();
+                    expect(room.is_chatroom).toBeTruthy();
+                    var chatroomview = converse.chatboxviews.get(jid);
+                    expect(chatroomview.$el.is(':visible')).toBeTruthy();
+                    chatroomview.close();
+                });
+                waits('300'); // ChatBox.show() is debounced for 250ms
+                runs(function () {
+                    // Test with mixed case
+                    test_utils.openChatRoom('Leisure', 'localhost', 'dummy');
+                    var jid = 'Leisure@localhost';
+                    var room = converse_api.rooms.get(jid);
+                    expect(room instanceof Object).toBeTruthy();
+                    var chatroomview = converse.chatboxviews.get(jid.toLowerCase());
+                    expect(chatroomview.$el.is(':visible')).toBeTruthy();
+                });
+                waits('300'); // ChatBox.show() is debounced for 250ms
+                runs(function () {
+                    var jid = 'leisure@localhost';
+                    var room = converse_api.rooms.get(jid);
+                    expect(room instanceof Object).toBeTruthy();
+                    var chatroomview = converse.chatboxviews.get(jid.toLowerCase());
+                    expect(chatroomview.$el.is(':visible')).toBeTruthy();
+
+                    jid = 'leiSure@localhost';
+                    room = converse_api.rooms.get(jid);
+                    expect(room instanceof Object).toBeTruthy();
+                    chatroomview = converse.chatboxviews.get(jid.toLowerCase());
+                    expect(chatroomview.$el.is(':visible')).toBeTruthy();
+                    chatroomview.close();
+
+                    // Non-existing room
+                    jid = 'lounge2@localhost';
+                    room = converse_api.rooms.get(jid);
+                    expect(typeof room === 'undefined').toBeTruthy();
+                });
+            });
+
+           it("has a method 'open' which opens and returns a wrapped chat box", function () {
+                var chatroomview;
+                var jid = 'lounge@localhost';
+                var room = converse_api.rooms.open(jid);
+                runs(function () {
+                    // Test on chat room that doesn't exist.
+                    expect(room instanceof Object).toBeTruthy();
+                    expect(room.is_chatroom).toBeTruthy();
+                    chatroomview = converse.chatboxviews.get(jid);
+                    expect(chatroomview.$el.is(':visible')).toBeTruthy();
+                });
+                waits('300'); // ChatBox.show() is debounced for 250ms
+                runs(function () {
+                    // Test again, now that the room exists.
+                    room = converse_api.rooms.open(jid);
+                    expect(room instanceof Object).toBeTruthy();
+                    expect(room.is_chatroom).toBeTruthy();
+                    chatroomview = converse.chatboxviews.get(jid);
+                    expect(chatroomview.$el.is(':visible')).toBeTruthy();
+                });
+                waits('300'); // ChatBox.show() is debounced for 250ms
+                runs(function () {
+                    // Test with mixed case in JID
+                    jid = 'Leisure@localhost';
+                    room = converse_api.rooms.open(jid);
+                    expect(room instanceof Object).toBeTruthy();
+                    chatroomview = converse.chatboxviews.get(jid.toLowerCase());
+                    expect(chatroomview.$el.is(':visible')).toBeTruthy();
+
+                    jid = 'leisure@localhost';
+                    room = converse_api.rooms.open(jid);
+                    expect(room instanceof Object).toBeTruthy();
+                    chatroomview = converse.chatboxviews.get(jid.toLowerCase());
+                    expect(chatroomview.$el.is(':visible')).toBeTruthy();
+
+                    jid = 'leiSure@localhost';
+                    room = converse_api.rooms.open(jid);
+                    expect(room instanceof Object).toBeTruthy();
+                    chatroomview = converse.chatboxviews.get(jid.toLowerCase());
+                    expect(chatroomview.$el.is(':visible')).toBeTruthy();
+                    chatroomview.close();
+                });
+            });
+        });
+
+
         describe("A Chat Room", function () {
             beforeEach(function () {
                 runs(function () {

+ 0 - 97
spec/converse.js

@@ -313,103 +313,6 @@
             });
         }, converse));
 
-        describe("The \"rooms\" API", function () {
-            beforeEach(function () {
-                test_utils.closeAllChatBoxes();
-                test_utils.clearBrowserStorage();
-                converse.rosterview.model.reset();
-                test_utils.createContacts('current');
-            });
-
-            it("has a method 'get' which returns a wrapped chat room (if it exists)", function () {
-                waits('300'); // ChatBox.show() is debounced for 250ms
-                runs(function () {
-                    test_utils.openChatRoom('lounge', 'localhost', 'dummy');
-                    var jid = 'lounge@localhost';
-                    var room = converse_api.rooms.get(jid);
-                    expect(room instanceof Object).toBeTruthy();
-                    expect(room.is_chatroom).toBeTruthy();
-                    var chatroomview = converse.chatboxviews.get(jid);
-                    expect(chatroomview.$el.is(':visible')).toBeTruthy();
-                    chatroomview.close();
-                });
-                waits('300'); // ChatBox.show() is debounced for 250ms
-                runs(function () {
-                    // Test with mixed case
-                    test_utils.openChatRoom('Leisure', 'localhost', 'dummy');
-                    var jid = 'Leisure@localhost';
-                    var room = converse_api.rooms.get(jid);
-                    expect(room instanceof Object).toBeTruthy();
-                    var chatroomview = converse.chatboxviews.get(jid.toLowerCase());
-                    expect(chatroomview.$el.is(':visible')).toBeTruthy();
-                });
-                waits('300'); // ChatBox.show() is debounced for 250ms
-                runs(function () {
-                    var jid = 'leisure@localhost';
-                    var room = converse_api.rooms.get(jid);
-                    expect(room instanceof Object).toBeTruthy();
-                    var chatroomview = converse.chatboxviews.get(jid.toLowerCase());
-                    expect(chatroomview.$el.is(':visible')).toBeTruthy();
-
-                    jid = 'leiSure@localhost';
-                    room = converse_api.rooms.get(jid);
-                    expect(room instanceof Object).toBeTruthy();
-                    chatroomview = converse.chatboxviews.get(jid.toLowerCase());
-                    expect(chatroomview.$el.is(':visible')).toBeTruthy();
-                    chatroomview.close();
-
-                    // Non-existing room
-                    jid = 'lounge2@localhost';
-                    room = converse_api.rooms.get(jid);
-                    expect(typeof room === 'undefined').toBeTruthy();
-                });
-            });
-
-           it("has a method 'open' which opens and returns a wrapped chat box", function () {
-                var chatroomview;
-                var jid = 'lounge@localhost';
-                var room = converse_api.rooms.open(jid);
-                runs(function () {
-                    // Test on chat room that doesn't exist.
-                    expect(room instanceof Object).toBeTruthy();
-                    expect(room.is_chatroom).toBeTruthy();
-                    chatroomview = converse.chatboxviews.get(jid);
-                    expect(chatroomview.$el.is(':visible')).toBeTruthy();
-                });
-                waits('300'); // ChatBox.show() is debounced for 250ms
-                runs(function () {
-                    // Test again, now that the room exists.
-                    room = converse_api.rooms.open(jid);
-                    expect(room instanceof Object).toBeTruthy();
-                    expect(room.is_chatroom).toBeTruthy();
-                    chatroomview = converse.chatboxviews.get(jid);
-                    expect(chatroomview.$el.is(':visible')).toBeTruthy();
-                });
-                waits('300'); // ChatBox.show() is debounced for 250ms
-                runs(function () {
-                    // Test with mixed case in JID
-                    jid = 'Leisure@localhost';
-                    room = converse_api.rooms.open(jid);
-                    expect(room instanceof Object).toBeTruthy();
-                    chatroomview = converse.chatboxviews.get(jid.toLowerCase());
-                    expect(chatroomview.$el.is(':visible')).toBeTruthy();
-
-                    jid = 'leisure@localhost';
-                    room = converse_api.rooms.open(jid);
-                    expect(room instanceof Object).toBeTruthy();
-                    chatroomview = converse.chatboxviews.get(jid.toLowerCase());
-                    expect(chatroomview.$el.is(':visible')).toBeTruthy();
-
-                    jid = 'leiSure@localhost';
-                    room = converse_api.rooms.open(jid);
-                    expect(room instanceof Object).toBeTruthy();
-                    chatroomview = converse.chatboxviews.get(jid.toLowerCase());
-                    expect(chatroomview.$el.is(':visible')).toBeTruthy();
-                    chatroomview.close();
-                });
-            });
-        });
-
         describe("The \"settings\" API", $.proxy(function() {
             beforeEach($.proxy(function () {
                 test_utils.closeAllChatBoxes();

+ 17 - 1
src/converse-muc.js

@@ -1363,6 +1363,23 @@
              */
             _.extend(converse_api, {
                 'rooms': {
+                    'close': function (jids) {
+                        if (typeof jids === "undefined") {
+                            converse.chatboxviews.each(function (view) {
+                                if (view.is_chatroom && view.model) {
+                                    view.close();
+                                }
+                            });
+                        } else if (typeof jids === "string") {
+                            var view = converse.chatboxviews.get(jids);
+                            if (view) { view.close(); }
+                        } else {
+                            _.map(jids, function (jid) {
+                                var view = converse.chatboxviews.get(jid);
+                                if (view) { view.close(); }
+                            });
+                        }
+                    },
                     'open': function (jids, nick) {
                         if (!nick) {
                             nick = Strophe.getNodeFromJid(converse.bare_jid);
@@ -1373,7 +1390,6 @@
                         var _transform = function (jid) {
                             jid = jid.toLowerCase();
                             var chatroom = converse.chatboxes.get(jid);
-                            converse.log('jid');
                             if (!chatroom) {
                                 chatroom = converse.chatboxviews.showChat({
                                     'id': jid,