Commit 6de07c5c authored by Ariel Fuggini's avatar Ariel Fuggini Committed by JC Brand

Adds setting to toggle showing unread message counter in page title

parent cf94a519
...@@ -1954,6 +1954,13 @@ Converse render the emojis with `Twemoji <https://twemoji.twitter.com/>`_. ...@@ -1954,6 +1954,13 @@ Converse render the emojis with `Twemoji <https://twemoji.twitter.com/>`_.
See also `emoji_image_path`_. See also `emoji_image_path`_.
update_title
------------
* Default: ``true``
Determines whether to shows unread messages counter in the page title.
visible_toolbar_buttons visible_toolbar_buttons
----------------------- -----------------------
......
...@@ -1058,16 +1058,16 @@ describe("Chatboxes", function () { ...@@ -1058,16 +1058,16 @@ describe("Chatboxes", function () {
await mock.openControlBox(_converse); await mock.openControlBox(_converse);
expect(document.title).toBe('Converse Tests'); expect(document.title).toBe('Converse Tests');
_converse.api.settings.set('update_title', false);
const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit'; const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
const previous_state = _converse.windowState; const previous_state = _converse.windowState;
const message = 'This message will increment the message counter';
const msg = $msg({ const msg = $msg({
from: sender_jid, from: sender_jid,
to: _converse.connection.jid, to: _converse.connection.jid,
type: 'chat', type: 'chat',
id: u.getUniqueId() id: u.getUniqueId()
}).c('body').t(message).up() }).c('body').t('This message will increment the message counter').up()
.c('active', {'xmlns': Strophe.NS.CHATSTATES}).tree(); .c('active', {'xmlns': Strophe.NS.CHATSTATES}).tree();
_converse.windowState = 'hidden'; _converse.windowState = 'hidden';
...@@ -1078,8 +1078,21 @@ describe("Chatboxes", function () { ...@@ -1078,8 +1078,21 @@ describe("Chatboxes", function () {
await _converse.handleMessageStanza(msg); await _converse.handleMessageStanza(msg);
expect(_converse.incrementMsgCounter).toHaveBeenCalled(); expect(_converse.incrementMsgCounter).toHaveBeenCalled();
expect(_converse.clearMsgCounter).not.toHaveBeenCalled(); expect(_converse.clearMsgCounter).not.toHaveBeenCalled();
expect(document.title).toBe('Messages (1) Converse Tests'); expect(document.title).toBe('Converse Tests');
expect(_converse.api.trigger).toHaveBeenCalledWith('message', jasmine.any(Object)); expect(_converse.api.trigger).toHaveBeenCalledWith('message', jasmine.any(Object));
_converse.api.settings.set('update_title', true);
const msg2 = $msg({
from: sender_jid,
to: _converse.connection.jid,
type: 'chat',
id: u.getUniqueId()
}).c('body').t('This message increment the message counter AND update the page title').up()
.c('active', {'xmlns': Strophe.NS.CHATSTATES}).tree();
await _converse.handleMessageStanza(msg2);
expect(document.title).toBe('Messages (2) Converse Tests');
_converse.windowSate = previous_state; _converse.windowSate = previous_state;
done(); done();
})); }));
......
...@@ -32,32 +32,40 @@ converse.plugins.add('converse-chatboxes', { ...@@ -32,32 +32,40 @@ converse.plugins.add('converse-chatboxes', {
'privateChatsAutoJoined' 'privateChatsAutoJoined'
]); ]);
api.settings.extend({
'update_title': true
});
let msg_counter = 0; let msg_counter = 0;
const favicon = new Favico({type : 'circle', position: 'up', animation: 'none'}); const favicon = new Favico({type : 'circle', position: 'up', animation: 'none'});
_converse.incrementMsgCounter = function () { _converse.incrementMsgCounter = function () {
msg_counter += 1; msg_counter += 1;
favicon.badge(msg_counter); favicon.badge(msg_counter);
const title = document.title; if (api.settings.get('update_title')) {
if (!title) { const title = document.title;
return; if (!title) {
} return;
if (title.search(/^Messages \(\d+\) /) === -1) { }
document.title = `Messages (${msg_counter}) ${title}`; if (title.search(/^Messages \(\d+\) /) === -1) {
} else { document.title = `Messages (${msg_counter}) ${title}`;
document.title = title.replace(/^Messages \(\d+\) /, `Messages (${msg_counter}) `); } else {
document.title = title.replace(/^Messages \(\d+\) /, `Messages (${msg_counter}) `);
}
} }
}; };
_converse.clearMsgCounter = function () { _converse.clearMsgCounter = function () {
msg_counter = 0; msg_counter = 0;
favicon.badge(msg_counter); favicon.badge(msg_counter);
const title = document.title; if (api.settings.get('update_title')) {
if (!title) { const title = document.title;
return; if (!title) {
} return;
if (title.search(/^Messages \(\d+\) /) !== -1) { }
document.title = title.replace(/^Messages \(\d+\) /, ""); if (title.search(/^Messages \(\d+\) /) !== -1) {
document.title = title.replace(/^Messages \(\d+\) /, "");
}
} }
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment