Add a clearSession event to avoid using overrides.

This commit is contained in:
JC Brand 2018-05-18 12:44:27 +02:00
parent 2508ef0e9e
commit c920725832
6 changed files with 42 additions and 42 deletions

View File

@ -149,6 +149,11 @@ When a chatbox has been minimized or maximized. Relevant to converse-chatview.js
``_converse.api.listen.on('chatBoxToggled', function (chatbox) { ... });`` ``_converse.api.listen.on('chatBoxToggled', function (chatbox) { ... });``
clearSession
~~~~~~~~~~~~
Called when the user is logging out and provides the opportunity to remove session data.
connected connected
~~~~~~~~~ ~~~~~~~~~

View File

@ -105,10 +105,6 @@
expect($(view.el).find('.chat-msg-text:last').text()).toBe(' wrote a 3rd person message'); expect($(view.el).find('.chat-msg-text:last').text()).toBe(' wrote a 3rd person message');
expect($(view.el).find('.chat-msg-author:last').is(':visible')).toBeTruthy(); expect($(view.el).find('.chat-msg-author:last').is(':visible')).toBeTruthy();
expect(u.hasClass('chat-msg-followup', message_el)).toBeFalsy(); expect(u.hasClass('chat-msg-followup', message_el)).toBeFalsy();
message = 'This a normal message';
done(); done();
}); });
}); });

View File

@ -51,15 +51,6 @@
// //
// New functions which don't exist yet can also be added. // New functions which don't exist yet can also be added.
clearSession () {
if (!_.isUndefined(this.bookmarks)) {
this.bookmarks.reset();
this.bookmarks.browserStorage._clear();
window.sessionStorage.removeItem(this.bookmarks.fetched_flag);
}
this.__super__.clearSession.apply(this, arguments);
},
ChatRoomView: { ChatRoomView: {
events: { events: {
'click .toggle-bookmark': 'toggleBookmark' 'click .toggle-bookmark': 'toggleBookmark'
@ -575,6 +566,14 @@
{'object': _converse, 'event': 'roomsPanelRendered'} {'object': _converse, 'event': 'roomsPanelRendered'}
], initBookmarks); ], initBookmarks);
_converse.on('clearSession', () => {
if (!_.isUndefined(_converse.bookmarks)) {
_converse.bookmarks.browserStorage._clear();
window.sessionStorage.removeItem(_converse.bookmarks.fetched_flag);
}
});
_converse.on('reconnected', initBookmarks); _converse.on('reconnected', initBookmarks);
_converse.on('connected', () => { _converse.on('connected', () => {

View File

@ -104,19 +104,6 @@
} }
}, },
clearSession () {
this.__super__.clearSession.apply(this, arguments);
const chatboxes = _.get(this, 'chatboxes', null);
if (!_.isNil(chatboxes)) {
const controlbox = chatboxes.get('controlbox');
if (controlbox &&
controlbox.collection &&
controlbox.collection.browserStorage) {
controlbox.save({'connected': false});
}
}
},
ChatBoxes: { ChatBoxes: {
chatBoxMayBeShown (chatbox) { chatBoxMayBeShown (chatbox) {
return this.__super__.chatBoxMayBeShown.apply(this, arguments) && return this.__super__.chatBoxMayBeShown.apply(this, arguments) &&
@ -590,6 +577,20 @@
} }
}); });
_converse.on('clearSession', () => {
if (_converse.trusted) {
const chatboxes = _.get(_converse, 'chatboxes', null);
if (!_.isNil(chatboxes)) {
const controlbox = chatboxes.get('controlbox');
if (controlbox &&
controlbox.collection &&
controlbox.collection.browserStorage) {
controlbox.save({'connected': false});
}
}
}
});
Promise.all([ Promise.all([
_converse.api.waitUntil('connectionInitialized'), _converse.api.waitUntil('connectionInitialized'),
_converse.api.waitUntil('chatBoxesInitialized') _converse.api.waitUntil('chatBoxesInitialized')

View File

@ -649,13 +649,13 @@
}; };
this.clearSession = function () { this.clearSession = function () {
if (!_.isUndefined(this.session) && this.session.browserStorage) {
this.session.browserStorage._clear();
}
if (!_converse.trusted) { if (!_converse.trusted) {
window.localStorage.clear(); window.localStorage.clear();
window.sessionStorage.clear(); window.sessionStorage.clear();
} else if (!_.isUndefined(this.session) && this.session.browserStorage) {
this.session.browserStorage._clear();
} }
_converse.emit('clearSession');
}; };
this.logOut = function () { this.logOut = function () {

View File

@ -16,13 +16,6 @@
dependencies: ["converse-vcard"], dependencies: ["converse-vcard"],
overrides: { overrides: {
clearSession () {
this.__super__.clearSession.apply(this, arguments);
if (!_.isUndefined(this.roster)) {
this.roster.browserStorage._clear();
}
},
_tearDown () { _tearDown () {
this.__super__._tearDown.apply(this, arguments); this.__super__._tearDown.apply(this, arguments);
if (this.roster) { if (this.roster) {
@ -360,12 +353,12 @@
fetchRosterContacts () { fetchRosterContacts () {
/* Fetches the roster contacts, first by trying the /* Fetches the roster contacts, first by trying the
* sessionStorage cache, and if that's empty, then by querying * sessionStorage cache, and if that's empty, then by querying
* the XMPP server. * the XMPP server.
* *
* Returns a promise which resolves once the contacts have been * Returns a promise which resolves once the contacts have been
* fetched. * fetched.
*/ */
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.fetch({ this.fetch({
'add': true, 'add': true,
@ -735,6 +728,12 @@
/********** Event Handlers *************/ /********** Event Handlers *************/
_converse.api.listen.on('clearSession', () => {
if (!_.isUndefined(this.roster)) {
this.roster.browserStorage._clear();
}
});
_converse.api.listen.on('statusInitialized', (reconnecting) => { _converse.api.listen.on('statusInitialized', (reconnecting) => {
if (reconnecting) { if (reconnecting) {
// No need to recreate the roster, otherwise we lose our // No need to recreate the roster, otherwise we lose our