2018-05-30 17:23:35 +02:00
|
|
|
// Converse.js
|
2017-04-20 21:24:20 +02:00
|
|
|
// http://conversejs.org
|
|
|
|
//
|
2018-05-30 17:23:35 +02:00
|
|
|
// Copyright (c) 2012-2018, 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
|
|
|
*/
|
|
|
|
(function (root, factory) {
|
|
|
|
define(
|
|
|
|
["converse-core", "converse-chatview"],
|
|
|
|
factory);
|
|
|
|
}(this, function (converse) {
|
|
|
|
"use strict";
|
2017-07-10 17:46:22 +02:00
|
|
|
const { _, Strophe } = converse.env;
|
2018-09-06 09:44:44 +02:00
|
|
|
const u = converse.env.utils;
|
|
|
|
|
2017-04-20 21:24:20 +02:00
|
|
|
|
2017-06-10 20:39:09 +02:00
|
|
|
function hideChat (view) {
|
|
|
|
if (view.model.get('id') === 'controlbox') { return; }
|
2018-09-06 09:44:44 +02:00
|
|
|
u.safeSave(view.model, {'hidden': true});
|
2017-06-10 20:39:09 +02:00
|
|
|
view.hide();
|
|
|
|
}
|
|
|
|
|
2018-09-06 09:44:44 +02:00
|
|
|
|
2017-04-20 21:24:20 +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.
|
2018-05-24 09:09:09 +02:00
|
|
|
dependencies: ['converse-chatboxes', 'converse-muc', 'converse-muc-views', 'converse-controlbox', 'converse-rosterview'],
|
2017-04-20 21:24:20 +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.
|
2017-04-21 12:20:26 +02:00
|
|
|
ChatBoxes: {
|
2018-05-30 17:23:35 +02:00
|
|
|
|
2018-02-21 22:40:51 +01:00
|
|
|
chatBoxMayBeShown (chatbox) {
|
2018-06-07 14:09:19 +02:00
|
|
|
const { _converse } = this.__super__;
|
|
|
|
if (chatbox.get('id') === 'controlbox') {
|
|
|
|
return true;
|
|
|
|
}
|
2018-07-02 23:29:57 +02:00
|
|
|
if (_converse.isSingleton()) {
|
2018-06-07 14:09:19 +02:00
|
|
|
const any_chats_visible = _converse.chatboxes
|
|
|
|
.filter((cb) => cb.get('id') != 'controlbox')
|
|
|
|
.filter((cb) => !cb.get('hidden')).length > 0;
|
|
|
|
|
|
|
|
if (any_chats_visible) {
|
|
|
|
return !chatbox.get('hidden');
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
2018-05-30 17:23:35 +02:00
|
|
|
} else {
|
|
|
|
return this.__super__.chatBoxMayBeShown.apply(this, arguments);
|
|
|
|
}
|
2018-02-21 22:40:51 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
createChatBox (jid, attrs) {
|
2018-02-21 22:40:51 +01:00
|
|
|
/* Make sure new chat boxes are hidden by default. */
|
2018-07-02 23:29:57 +02:00
|
|
|
const { _converse } = this.__super__;
|
|
|
|
if (_converse.isSingleton()) {
|
2018-05-30 17:23:35 +02:00
|
|
|
attrs = attrs || {};
|
|
|
|
attrs.hidden = true;
|
|
|
|
}
|
2017-04-21 12:20:26 +02:00
|
|
|
return this.__super__.createChatBox.call(this, jid, attrs);
|
|
|
|
}
|
|
|
|
},
|
2017-06-15 16:22:49 +02:00
|
|
|
|
2017-04-20 21:24:20 +02:00
|
|
|
ChatBoxView: {
|
2018-03-13 19:11:49 +01:00
|
|
|
shouldShowOnTextMessage () {
|
2018-07-02 23:29:57 +02:00
|
|
|
const { _converse } = this.__super__;
|
|
|
|
if (_converse.isSingleton()) {
|
2018-05-30 17:23:35 +02:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return this.__super__.shouldShowOnTextMessage.apply(this, arguments);
|
|
|
|
}
|
2018-03-13 19:11:49 +01:00
|
|
|
},
|
|
|
|
|
2018-02-21 22:40:51 +01:00
|
|
|
_show (focus) {
|
2017-04-20 21:24:20 +02:00
|
|
|
/* We only have one chat visible at any one
|
|
|
|
* time. So before opening a chat, we make sure all other
|
|
|
|
* chats are hidden.
|
|
|
|
*/
|
2018-07-02 23:29:57 +02:00
|
|
|
const { _converse } = this.__super__;
|
|
|
|
if (_converse.isSingleton()) {
|
2018-05-30 17:23:35 +02:00
|
|
|
_.each(this.__super__._converse.chatboxviews.xget(this.model.get('id')), hideChat);
|
2018-09-06 09:44:44 +02:00
|
|
|
u.safeSave(this.model, {'hidden': false});
|
2018-05-30 17:23:35 +02:00
|
|
|
}
|
2018-02-21 22:40:51 +01:00
|
|
|
return this.__super__._show.apply(this, arguments);
|
2017-04-20 21:24:20 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-14 13:44:17 +01:00
|
|
|
ChatRoomView: {
|
|
|
|
show (focus) {
|
2018-07-02 23:29:57 +02:00
|
|
|
const { _converse } = this.__super__;
|
|
|
|
if (_converse.isSingleton()) {
|
2018-05-30 17:23:35 +02:00
|
|
|
_.each(this.__super__._converse.chatboxviews.xget(this.model.get('id')), hideChat);
|
2018-09-06 09:44:44 +02:00
|
|
|
u.safeSave(this.model, {'hidden': false});
|
2018-05-30 17:23:35 +02:00
|
|
|
}
|
2018-02-21 22:40:51 +01:00
|
|
|
return this.__super__.show.apply(this, arguments);
|
2018-02-14 13:44:17 +01:00
|
|
|
}
|
2017-04-20 21:24:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}));
|