2017-04-20 21:24:20 +02:00
|
|
|
// Converse.js (A browser based XMPP chat client)
|
|
|
|
// http://conversejs.org
|
|
|
|
//
|
|
|
|
// Copyright (c) 2012-2017, JC Brand <jc@opkode.com>
|
|
|
|
// Licensed under the Mozilla Public License (MPLv2)
|
|
|
|
//
|
|
|
|
/*global Backbone, define, window, document, JSON */
|
|
|
|
|
|
|
|
/* converse-singleton
|
|
|
|
/* ******************
|
|
|
|
*
|
|
|
|
* A non-core plugin which ensures that only one chat, private or group, is
|
|
|
|
* visible at any one time. All other ongoing chats are hidden and kept in the
|
|
|
|
* background.
|
|
|
|
*
|
|
|
|
* This plugin makes sense in mobile or fullscreen chat environments.
|
|
|
|
*/
|
|
|
|
(function (root, factory) {
|
|
|
|
define(
|
|
|
|
["converse-core", "converse-chatview"],
|
|
|
|
factory);
|
|
|
|
}(this, function (converse) {
|
|
|
|
"use strict";
|
|
|
|
var _ = converse.env._,
|
|
|
|
Strophe = converse.env.Strophe;
|
|
|
|
|
2017-06-10 20:39:09 +02:00
|
|
|
function hideChat (view) {
|
|
|
|
if (view.model.get('id') === 'controlbox') { return; }
|
|
|
|
view.model.save({'hidden': true});
|
|
|
|
view.hide();
|
|
|
|
}
|
|
|
|
|
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.
|
2017-06-13 19:17:45 +02:00
|
|
|
optional_dependencies: ['converse-muc', '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: {
|
|
|
|
createChatBox: function (jid, attrs) {
|
|
|
|
/* Make sure new chat boxes are hidden by default.
|
|
|
|
*/
|
|
|
|
attrs = attrs || {};
|
|
|
|
attrs.hidden = true;
|
|
|
|
return this.__super__.createChatBox.call(this, jid, attrs);
|
|
|
|
}
|
|
|
|
},
|
2017-06-15 16:22:49 +02:00
|
|
|
|
|
|
|
RoomsPanel: {
|
|
|
|
parseRoomDataFromEvent: function (ev) {
|
|
|
|
/* We set hidden to false for rooms opened manually by the
|
|
|
|
* user. They should always be shown.
|
|
|
|
*/
|
|
|
|
var result = this.__super__.parseRoomDataFromEvent.apply(this, arguments);
|
|
|
|
result.hidden = false;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
},
|
2017-04-20 21:24:20 +02:00
|
|
|
|
|
|
|
ChatBoxViews: {
|
2017-04-21 19:52:27 +02:00
|
|
|
showChat: function (attrs, force) {
|
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.
|
|
|
|
*/
|
2017-06-12 20:30:58 +02:00
|
|
|
const _converse = this.__super__._converse;
|
|
|
|
const chatbox = this.getChatBox(attrs, true);
|
|
|
|
const hidden = _.isUndefined(attrs.hidden) ? chatbox.get('hidden') : attrs.hidden;
|
2017-06-15 16:22:49 +02:00
|
|
|
if ((force || !hidden) && _converse.connection.authenticated) {
|
2017-06-10 20:39:09 +02:00
|
|
|
_.each(_converse.chatboxviews.xget(chatbox.get('id')), hideChat);
|
2017-04-20 21:24:20 +02:00
|
|
|
chatbox.save({'hidden': false});
|
|
|
|
}
|
|
|
|
return this.__super__.showChat.apply(this, arguments);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
ChatBoxView: {
|
|
|
|
_show: function (focus) {
|
|
|
|
/* We only have one chat visible at any one
|
|
|
|
* time. So before opening a chat, we make sure all other
|
|
|
|
* chats are hidden.
|
|
|
|
*/
|
|
|
|
if (!this.model.get('hidden')) {
|
2017-06-10 20:39:09 +02:00
|
|
|
_.each(this.__super__._converse.chatboxviews.xget(this.model.get('id')), hideChat);
|
2017-04-20 21:24:20 +02:00
|
|
|
return this.__super__._show.apply(this, arguments);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
RosterContactView: {
|
|
|
|
openChat: function (ev) {
|
|
|
|
/* We only have one chat visible at any one
|
|
|
|
* time. So before opening a chat, we make sure all other
|
|
|
|
* chats are hidden.
|
|
|
|
*/
|
2017-06-10 20:39:09 +02:00
|
|
|
_.each(this.__super__._converse.chatboxviews.xget('controlbox'), hideChat);
|
2017-04-20 21:24:20 +02:00
|
|
|
this.model.save({'hidden': false});
|
|
|
|
return this.__super__.openChat.apply(this, arguments);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}));
|