Browse Source

Fixes concerning tab visibility

JC Brand 9 years ago
parent
commit
705c043852
5 changed files with 33 additions and 19 deletions
  1. 3 0
      dev.html
  2. 15 5
      spec/chatbox.js
  3. 1 1
      src/converse-chatview.js
  4. 12 11
      src/converse-core.js
  5. 2 2
      src/converse-notification.js

+ 3 - 0
dev.html

@@ -60,6 +60,9 @@
                 'prosody@conference.prosody.im',
                 'prosody@conference.prosody.im',
                 'jdev@conference.jabber.org'
                 'jdev@conference.jabber.org'
             ],
             ],
+            notify_all_room_messages: [
+                'discuss@conference.conversejs.org'
+            ],
             auto_reconnect: true,
             auto_reconnect: true,
             bosh_service_url: 'https://conversejs.org/http-bind/', // Please use this connection manager only for testing purposes
             bosh_service_url: 'https://conversejs.org/http-bind/', // Please use this connection manager only for testing purposes
             keepalive: true,
             keepalive: true,

+ 15 - 5
spec/chatbox.js

@@ -1276,13 +1276,20 @@
         describe("A Message Counter", function () {
         describe("A Message Counter", function () {
             beforeEach(function () {
             beforeEach(function () {
                 converse.clearMsgCounter();
                 converse.clearMsgCounter();
-            }.bind(converse));
+                test_utils.closeAllChatBoxes();
+                test_utils.removeControlBox();
+                converse.roster.browserStorage._clear();
+                test_utils.initConverse();
+                test_utils.createContacts('current');
+                test_utils.openControlBox();
+                test_utils.openContactsPanel();
+            });
 
 
             it("is incremented when the message is received and the window is not focused", function () {
             it("is incremented when the message is received and the window is not focused", function () {
                 spyOn(converse, 'emit');
                 spyOn(converse, 'emit');
                 expect(this.msg_counter).toBe(0);
                 expect(this.msg_counter).toBe(0);
                 spyOn(converse, 'incrementMsgCounter').andCallThrough();
                 spyOn(converse, 'incrementMsgCounter').andCallThrough();
-                $(window).trigger('blur');
+                var previous_state = converse.windowState;
                 var message = 'This message will increment the message counter';
                 var message = 'This message will increment the message counter';
                 var sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost',
                 var sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost',
                     msg = $msg({
                     msg = $msg({
@@ -1292,17 +1299,20 @@
                         id: (new Date()).getTime()
                         id: (new Date()).getTime()
                     }).c('body').t(message).up()
                     }).c('body').t(message).up()
                       .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
                       .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
+                converse.windowState = 'hidden';
                 this.chatboxes.onMessage(msg);
                 this.chatboxes.onMessage(msg);
                 expect(converse.incrementMsgCounter).toHaveBeenCalled();
                 expect(converse.incrementMsgCounter).toHaveBeenCalled();
                 expect(this.msg_counter).toBe(1);
                 expect(this.msg_counter).toBe(1);
                 expect(converse.emit).toHaveBeenCalledWith('message', msg);
                 expect(converse.emit).toHaveBeenCalledWith('message', msg);
+                converse.windowSate = previous_state;
             }.bind(converse));
             }.bind(converse));
 
 
             it("is cleared when the window is focused", function () {
             it("is cleared when the window is focused", function () {
+                converse.windowState = 'hidden';
                 spyOn(converse, 'clearMsgCounter').andCallThrough();
                 spyOn(converse, 'clearMsgCounter').andCallThrough();
                 runs(function () {
                 runs(function () {
-                    $(window).triggerHandler('blur');
-                    $(window).triggerHandler('focus');
+                    converse.saveWindowState(null, 'focus');
+                    converse.saveWindowState(null, 'blur');
                 });
                 });
                 waits(50);
                 waits(50);
                 runs(function () {
                 runs(function () {
@@ -1313,7 +1323,7 @@
             it("is not incremented when the message is received and the window is focused", function () {
             it("is not incremented when the message is received and the window is focused", function () {
                 expect(this.msg_counter).toBe(0);
                 expect(this.msg_counter).toBe(0);
                 spyOn(converse, 'incrementMsgCounter').andCallThrough();
                 spyOn(converse, 'incrementMsgCounter').andCallThrough();
-                $(window).trigger('focus');
+                converse.saveWindowState(null, 'focus');
                 var message = 'This message will not increment the message counter';
                 var message = 'This message will not increment the message counter';
                 var sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost',
                 var sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost',
                     msg = $msg({
                     msg = $msg({

+ 1 - 1
src/converse-chatview.js

@@ -342,7 +342,7 @@
                         if (this.model.get('scrolled', true)) {
                         if (this.model.get('scrolled', true)) {
                             this.$el.find('.new-msgs-indicator').removeClass('hidden');
                             this.$el.find('.new-msgs-indicator').removeClass('hidden');
                         }
                         }
-                        if (converse.windowState === 'blur' || this.model.get('scrolled', true)) {
+                        if (converse.windowState === 'hidden' || this.model.get('scrolled', true)) {
                             converse.incrementMsgCounter();
                             converse.incrementMsgCounter();
                         }
                         }
                     }
                     }

+ 12 - 11
src/converse-core.js

@@ -515,8 +515,6 @@
                 } else {
                 } else {
                     document.title = document.title.replace(/^Messages \(\d+\) /, "Messages (" + this.msg_counter + ") ");
                     document.title = document.title.replace(/^Messages \(\d+\) /, "Messages (" + this.msg_counter + ") ");
                 }
                 }
-                window.blur();
-                window.focus();
             } else if (document.title.search(/^Messages \(\d+\) /) !== -1) {
             } else if (document.title.search(/^Messages \(\d+\) /) !== -1) {
                 document.title = document.title.replace(/^Messages \(\d+\) /, "");
                 document.title = document.title.replace(/^Messages \(\d+\) /, "");
             }
             }
@@ -570,7 +568,10 @@
             }
             }
         };
         };
 
 
-        var saveWindowState = function (ev, hidden) {
+        this.saveWindowState = function (ev, hidden) {
+            // XXX: eventually we should be able to just use
+            // document.visibilityState (when we drop support for older
+            // browsers).
             var state;
             var state;
             var v = "visible", h = "hidden",
             var v = "visible", h = "hidden",
                 event_map = {
                 event_map = {
@@ -581,7 +582,7 @@
                     'focusout': h,
                     'focusout': h,
                     'pagehide': h
                     'pagehide': h
                 };
                 };
-            ev = ev || window.event;
+            ev = ev || document.createEvent('Events');
             if (ev.type in event_map) {
             if (ev.type in event_map) {
                 state = event_map[ev.type];
                 state = event_map[ev.type];
             } else {
             } else {
@@ -600,23 +601,23 @@
             var hidden = "hidden";
             var hidden = "hidden";
             // Standards:
             // Standards:
             if (hidden in document) {
             if (hidden in document) {
-                document.addEventListener("visibilitychange", _.partial(saveWindowState, _, hidden));
+                document.addEventListener("visibilitychange", _.partial(converse.saveWindowState, _, hidden));
             } else if ((hidden = "mozHidden") in document) {
             } else if ((hidden = "mozHidden") in document) {
-                document.addEventListener("mozvisibilitychange", _.partial(saveWindowState, _, hidden));
+                document.addEventListener("mozvisibilitychange", _.partial(converse.saveWindowState, _, hidden));
             } else if ((hidden = "webkitHidden") in document) {
             } else if ((hidden = "webkitHidden") in document) {
-                document.addEventListener("webkitvisibilitychange", _.partial(saveWindowState, _, hidden));
+                document.addEventListener("webkitvisibilitychange", _.partial(converse.saveWindowState, _, hidden));
             } else if ((hidden = "msHidden") in document) {
             } else if ((hidden = "msHidden") in document) {
-                document.addEventListener("msvisibilitychange", _.partial(saveWindowState, _, hidden));
+                document.addEventListener("msvisibilitychange", _.partial(converse.saveWindowState, _, hidden));
             } else if ("onfocusin" in document) {
             } else if ("onfocusin" in document) {
                 // IE 9 and lower:
                 // IE 9 and lower:
-                document.onfocusin = document.onfocusout = _.partial(saveWindowState, _, hidden);
+                document.onfocusin = document.onfocusout = _.partial(converse.saveWindowState, _, hidden);
             } else {
             } else {
                 // All others:
                 // All others:
-                window.onpageshow = window.onpagehide = window.onfocus = window.onblur = _.partial(saveWindowState, _, hidden);
+                window.onpageshow = window.onpagehide = window.onfocus = window.onblur = _.partial(converse.saveWindowState, _, hidden);
             }
             }
             // set the initial state (but only if browser supports the Page Visibility API)
             // set the initial state (but only if browser supports the Page Visibility API)
             if( document[hidden] !== undefined ) {
             if( document[hidden] !== undefined ) {
-                _.partial(saveWindowState, _, hidden)({type: document[hidden] ? "blur" : "focus"});
+                _.partial(converse.saveWindowState, _, hidden)({type: document[hidden] ? "blur" : "focus"});
             }
             }
         };
         };
 
 

+ 2 - 2
src/converse-notification.js

@@ -110,11 +110,11 @@
                 }
                 }
             };
             };
 
 
-            converse.areDesktopNotificationsEnabled = function (ignore_blur) {
+            converse.areDesktopNotificationsEnabled = function (ignore_hidden) {
                 var enabled = converse.supports_html5_notification &&
                 var enabled = converse.supports_html5_notification &&
                     converse.show_desktop_notifications &&
                     converse.show_desktop_notifications &&
                     Notification.permission === "granted";
                     Notification.permission === "granted";
-                if (ignore_blur) {
+                if (ignore_hidden) {
                     return enabled;
                     return enabled;
                 } else {
                 } else {
                     return enabled && converse.windowState === 'hidden';
                     return enabled && converse.windowState === 'hidden';