2018-05-30 17:23:35 +02:00
|
|
|
// Converse.js
|
2019-03-04 17:47:45 +01:00
|
|
|
// https://conversejs.org
|
2017-04-20 21:24:20 +02:00
|
|
|
//
|
2019-02-18 19:17:06 +01:00
|
|
|
// Copyright (c) 2013-2019, the Converse.js developers
|
2017-04-20 21:24:20 +02:00
|
|
|
// Licensed under the Mozilla Public License (MPLv2)
|
|
|
|
|
|
|
|
/* converse-singleton
|
2017-11-01 10:37:24 +01:00
|
|
|
* ******************
|
2017-04-20 21:24:20 +02:00
|
|
|
*
|
2017-11-01 10:37:24 +01:00
|
|
|
* A plugin which ensures that only one chat (private or groupchat) is
|
2017-04-20 21:24:20 +02:00
|
|
|
* visible at any one time. All other ongoing chats are hidden and kept in the
|
|
|
|
* background.
|
|
|
|
*
|
2017-11-01 10:37:24 +01:00
|
|
|
* This plugin makes sense in mobile or fullscreen chat environments (as
|
|
|
|
* configured by the `view_mode` setting).
|
2017-04-20 21:24:20 +02:00
|
|
|
*/
|
2018-09-06 09:44:44 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
import "converse-chatview";
|
|
|
|
import converse from "@converse/headless/converse-core";
|
2017-04-20 21:24:20 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
const { _, Strophe } = converse.env;
|
|
|
|
const u = converse.env.utils;
|
2017-06-10 20:39:09 +02:00
|
|
|
|
2018-09-06 09:44:44 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
function hideChat (view) {
|
|
|
|
if (view.model.get('id') === 'controlbox') { return; }
|
|
|
|
u.safeSave(view.model, {'hidden': true});
|
|
|
|
view.hide();
|
|
|
|
}
|
2017-04-20 21:24:20 +02:00
|
|
|
|
2018-05-30 17:23:35 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
converse.plugins.add('converse-singleton', {
|
|
|
|
// It's possible however to make optional dependencies non-optional.
|
|
|
|
// If the setting "strict_plugin_dependencies" is set to true,
|
|
|
|
// an error will be raised if the plugin is not found.
|
|
|
|
//
|
|
|
|
// NB: These plugins need to have already been loaded via require.js.
|
|
|
|
dependencies: ['converse-chatboxes', 'converse-muc', 'converse-muc-views', 'converse-controlbox', 'converse-rosterview'],
|
2018-06-07 14:09:19 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
overrides: {
|
|
|
|
// overrides mentioned here will be picked up by converse.js's
|
|
|
|
// plugin architecture they will replace existing methods on the
|
|
|
|
// relevant objects or classes.
|
|
|
|
//
|
|
|
|
// new functions which don't exist yet can also be added.
|
2018-02-21 22:40:51 +01:00
|
|
|
|
2019-04-16 13:38:46 +02:00
|
|
|
ChatBox: {
|
2019-04-16 16:36:58 +02:00
|
|
|
maybeShow (force) {
|
|
|
|
// This method must return the chatbox
|
2018-10-23 03:41:38 +02:00
|
|
|
const { _converse } = this.__super__;
|
2019-04-16 16:36:58 +02:00
|
|
|
if (!force && _converse.isUniView()) {
|
2019-04-16 13:38:46 +02:00
|
|
|
if (this.get('id') === 'controlbox') {
|
2019-04-16 16:36:58 +02:00
|
|
|
return this.trigger('show');
|
2019-04-16 13:38:46 +02:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
const any_chats_visible = _converse.chatboxes
|
|
|
|
.filter(cb => cb.get('id') != 'controlbox')
|
|
|
|
.filter(cb => !cb.get('hidden')).length > 0;
|
|
|
|
|
2019-04-16 16:36:58 +02:00
|
|
|
if (!any_chats_visible || !this.get('hidden')) {
|
|
|
|
return this.trigger('show');
|
2018-05-30 17:23:35 +02:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
} else {
|
2019-04-16 16:36:58 +02:00
|
|
|
return this.__super__.maybeShow.apply(this, arguments);
|
2017-04-21 12:20:26 +02:00
|
|
|
}
|
2019-04-16 13:38:46 +02:00
|
|
|
}
|
|
|
|
},
|
2017-06-15 16:22:49 +02:00
|
|
|
|
2019-04-16 13:38:46 +02:00
|
|
|
ChatBoxes: {
|
2018-10-23 03:41:38 +02:00
|
|
|
createChatBox (jid, attrs) {
|
|
|
|
/* Make sure new chat boxes are hidden by default. */
|
|
|
|
const { _converse } = this.__super__;
|
2018-12-04 12:14:52 +01:00
|
|
|
if (_converse.isUniView()) {
|
2018-10-23 03:41:38 +02:00
|
|
|
attrs = attrs || {};
|
|
|
|
attrs.hidden = true;
|
|
|
|
}
|
|
|
|
return this.__super__.createChatBox.call(this, jid, attrs);
|
|
|
|
}
|
|
|
|
},
|
2018-03-13 19:11:49 +01:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
ChatBoxView: {
|
|
|
|
shouldShowOnTextMessage () {
|
|
|
|
const { _converse } = this.__super__;
|
2018-12-04 12:14:52 +01:00
|
|
|
if (_converse.isUniView()) {
|
2018-10-23 03:41:38 +02:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return this.__super__.shouldShowOnTextMessage.apply(this, arguments);
|
2017-04-20 21:24:20 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
_show (focus) {
|
|
|
|
/* We only have one chat visible at any one
|
|
|
|
* time. So before opening a chat, we make sure all other
|
|
|
|
* chats are hidden.
|
|
|
|
*/
|
|
|
|
const { _converse } = this.__super__;
|
2018-12-04 12:14:52 +01:00
|
|
|
if (_converse.isUniView()) {
|
2018-10-23 03:41:38 +02:00
|
|
|
_.each(this.__super__._converse.chatboxviews.xget(this.model.get('id')), hideChat);
|
|
|
|
u.safeSave(this.model, {'hidden': false});
|
2018-02-14 13:44:17 +01:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
return this.__super__._show.apply(this, arguments);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
ChatRoomView: {
|
|
|
|
show (focus) {
|
|
|
|
const { _converse } = this.__super__;
|
2018-12-04 12:14:52 +01:00
|
|
|
if (_converse.isUniView()) {
|
2018-10-23 03:41:38 +02:00
|
|
|
_.each(this.__super__._converse.chatboxviews.xget(this.model.get('id')), hideChat);
|
|
|
|
u.safeSave(this.model, {'hidden': false});
|
|
|
|
}
|
|
|
|
return this.__super__.show.apply(this, arguments);
|
2017-04-20 21:24:20 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|