Fixes concerning tab visibility
This commit is contained in:
parent
10ca2900d4
commit
705c043852
3
dev.html
3
dev.html
|
@ -60,6 +60,9 @@
|
|||
'prosody@conference.prosody.im',
|
||||
'jdev@conference.jabber.org'
|
||||
],
|
||||
notify_all_room_messages: [
|
||||
'discuss@conference.conversejs.org'
|
||||
],
|
||||
auto_reconnect: true,
|
||||
bosh_service_url: 'https://conversejs.org/http-bind/', // Please use this connection manager only for testing purposes
|
||||
keepalive: true,
|
||||
|
|
|
@ -1276,13 +1276,20 @@
|
|||
describe("A Message Counter", function () {
|
||||
beforeEach(function () {
|
||||
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 () {
|
||||
spyOn(converse, 'emit');
|
||||
expect(this.msg_counter).toBe(0);
|
||||
spyOn(converse, 'incrementMsgCounter').andCallThrough();
|
||||
$(window).trigger('blur');
|
||||
var previous_state = converse.windowState;
|
||||
var message = 'This message will increment the message counter';
|
||||
var sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost',
|
||||
msg = $msg({
|
||||
|
@ -1292,17 +1299,20 @@
|
|||
id: (new Date()).getTime()
|
||||
}).c('body').t(message).up()
|
||||
.c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
|
||||
converse.windowState = 'hidden';
|
||||
this.chatboxes.onMessage(msg);
|
||||
expect(converse.incrementMsgCounter).toHaveBeenCalled();
|
||||
expect(this.msg_counter).toBe(1);
|
||||
expect(converse.emit).toHaveBeenCalledWith('message', msg);
|
||||
converse.windowSate = previous_state;
|
||||
}.bind(converse));
|
||||
|
||||
it("is cleared when the window is focused", function () {
|
||||
converse.windowState = 'hidden';
|
||||
spyOn(converse, 'clearMsgCounter').andCallThrough();
|
||||
runs(function () {
|
||||
$(window).triggerHandler('blur');
|
||||
$(window).triggerHandler('focus');
|
||||
converse.saveWindowState(null, 'focus');
|
||||
converse.saveWindowState(null, 'blur');
|
||||
});
|
||||
waits(50);
|
||||
runs(function () {
|
||||
|
@ -1313,7 +1323,7 @@
|
|||
it("is not incremented when the message is received and the window is focused", function () {
|
||||
expect(this.msg_counter).toBe(0);
|
||||
spyOn(converse, 'incrementMsgCounter').andCallThrough();
|
||||
$(window).trigger('focus');
|
||||
converse.saveWindowState(null, 'focus');
|
||||
var message = 'This message will not increment the message counter';
|
||||
var sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost',
|
||||
msg = $msg({
|
||||
|
|
|
@ -342,7 +342,7 @@
|
|||
if (this.model.get('scrolled', true)) {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -515,8 +515,6 @@
|
|||
} else {
|
||||
document.title = document.title.replace(/^Messages \(\d+\) /, "Messages (" + this.msg_counter + ") ");
|
||||
}
|
||||
window.blur();
|
||||
window.focus();
|
||||
} else if (document.title.search(/^Messages \(\d+\) /) !== -1) {
|
||||
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 v = "visible", h = "hidden",
|
||||
event_map = {
|
||||
|
@ -581,7 +582,7 @@
|
|||
'focusout': h,
|
||||
'pagehide': h
|
||||
};
|
||||
ev = ev || window.event;
|
||||
ev = ev || document.createEvent('Events');
|
||||
if (ev.type in event_map) {
|
||||
state = event_map[ev.type];
|
||||
} else {
|
||||
|
@ -600,23 +601,23 @@
|
|||
var hidden = "hidden";
|
||||
// Standards:
|
||||
if (hidden in document) {
|
||||
document.addEventListener("visibilitychange", _.partial(saveWindowState, _, hidden));
|
||||
document.addEventListener("visibilitychange", _.partial(converse.saveWindowState, _, hidden));
|
||||
} 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) {
|
||||
document.addEventListener("webkitvisibilitychange", _.partial(saveWindowState, _, hidden));
|
||||
document.addEventListener("webkitvisibilitychange", _.partial(converse.saveWindowState, _, hidden));
|
||||
} else if ((hidden = "msHidden") in document) {
|
||||
document.addEventListener("msvisibilitychange", _.partial(saveWindowState, _, hidden));
|
||||
document.addEventListener("msvisibilitychange", _.partial(converse.saveWindowState, _, hidden));
|
||||
} else if ("onfocusin" in document) {
|
||||
// IE 9 and lower:
|
||||
document.onfocusin = document.onfocusout = _.partial(saveWindowState, _, hidden);
|
||||
document.onfocusin = document.onfocusout = _.partial(converse.saveWindowState, _, hidden);
|
||||
} else {
|
||||
// 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)
|
||||
if( document[hidden] !== undefined ) {
|
||||
_.partial(saveWindowState, _, hidden)({type: document[hidden] ? "blur" : "focus"});
|
||||
_.partial(converse.saveWindowState, _, hidden)({type: document[hidden] ? "blur" : "focus"});
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -110,11 +110,11 @@
|
|||
}
|
||||
};
|
||||
|
||||
converse.areDesktopNotificationsEnabled = function (ignore_blur) {
|
||||
converse.areDesktopNotificationsEnabled = function (ignore_hidden) {
|
||||
var enabled = converse.supports_html5_notification &&
|
||||
converse.show_desktop_notifications &&
|
||||
Notification.permission === "granted";
|
||||
if (ignore_blur) {
|
||||
if (ignore_hidden) {
|
||||
return enabled;
|
||||
} else {
|
||||
return enabled && converse.windowState === 'hidden';
|
||||
|
|
Loading…
Reference in New Issue
Block a user