2016-03-14 17:04:27 +01:00
|
|
|
// Converse.js (A browser based XMPP chat client)
|
|
|
|
// http://conversejs.org
|
|
|
|
//
|
2017-02-13 15:37:17 +01:00
|
|
|
// Copyright (c) 2012-2017, Jan-Carel Brand <jc@opkode.com>
|
2016-03-14 17:04:27 +01:00
|
|
|
// Licensed under the Mozilla Public License (MPLv2)
|
|
|
|
//
|
2018-02-09 16:02:56 +01:00
|
|
|
/*global define, window, document */
|
2016-03-14 17:04:27 +01:00
|
|
|
|
|
|
|
(function (root, factory) {
|
2018-01-03 14:58:05 +01:00
|
|
|
define(["converse-core",
|
2018-05-24 21:09:33 +02:00
|
|
|
"templates/chatbox_minimize.html",
|
|
|
|
"templates/toggle_chats.html",
|
|
|
|
"templates/trimmed_chat.html",
|
|
|
|
"templates/chats_panel.html",
|
2017-08-29 11:08:37 +02:00
|
|
|
"converse-chatview"
|
2016-03-14 17:04:27 +01:00
|
|
|
], factory);
|
2016-09-23 10:54:55 +02:00
|
|
|
}(this, function (
|
2016-12-20 11:42:20 +01:00
|
|
|
converse,
|
2016-09-23 10:54:55 +02:00
|
|
|
tpl_chatbox_minimize,
|
|
|
|
tpl_toggle_chats,
|
2016-11-22 17:36:39 +01:00
|
|
|
tpl_trimmed_chat,
|
|
|
|
tpl_chats_panel
|
2016-09-23 10:54:55 +02:00
|
|
|
) {
|
2016-03-14 17:04:27 +01:00
|
|
|
"use strict";
|
2017-06-12 20:30:58 +02:00
|
|
|
|
2018-01-03 14:58:05 +01:00
|
|
|
const { _ , Backbone, Promise, Strophe, b64_sha1, moment } = converse.env;
|
|
|
|
const u = converse.env.utils;
|
2016-09-23 10:54:55 +02:00
|
|
|
|
2016-12-20 11:42:20 +01:00
|
|
|
converse.plugins.add('converse-minimize', {
|
2017-08-29 11:08:37 +02:00
|
|
|
/* Optional dependencies are other plugins which might be
|
|
|
|
* overridden or relied upon, and therefore need to be loaded before
|
|
|
|
* this plugin. They are called "optional" because they might not be
|
|
|
|
* available, in which case any overrides applicable to them will be
|
|
|
|
* ignored.
|
|
|
|
*
|
|
|
|
* 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-03-04 06:37:11 +01:00
|
|
|
dependencies: ["converse-chatview", "converse-controlbox", "converse-muc", "converse-muc-views", "converse-headline"],
|
2017-08-29 11:08:37 +02:00
|
|
|
|
2017-11-02 15:30:24 +01:00
|
|
|
enabled (_converse) {
|
|
|
|
return _converse.view_mode == 'overlayed';
|
|
|
|
},
|
|
|
|
|
2016-03-14 17:04:27 +01: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.
|
|
|
|
|
|
|
|
ChatBox: {
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize () {
|
2016-08-31 12:06:17 +02:00
|
|
|
this.__super__.initialize.apply(this, arguments);
|
2018-02-21 22:40:51 +01:00
|
|
|
this.on('show', this.maximize, this);
|
|
|
|
|
2016-03-14 17:04:27 +01:00
|
|
|
if (this.get('id') === 'controlbox') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.save({
|
|
|
|
'minimized': this.get('minimized') || false,
|
|
|
|
'time_minimized': this.get('time_minimized') || moment(),
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
maximize () {
|
2018-01-03 14:58:05 +01:00
|
|
|
u.safeSave(this, {
|
2016-03-14 17:04:27 +01:00
|
|
|
'minimized': false,
|
|
|
|
'time_opened': moment().valueOf()
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
minimize () {
|
2018-01-03 14:58:05 +01:00
|
|
|
u.safeSave(this, {
|
2016-03-14 17:04:27 +01:00
|
|
|
'minimized': true,
|
|
|
|
'time_minimized': moment().format()
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2016-03-28 16:08:50 +02:00
|
|
|
ChatBoxView: {
|
|
|
|
events: {
|
|
|
|
'click .toggle-chatbox-button': 'minimize',
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize () {
|
2016-03-28 16:08:50 +02:00
|
|
|
this.model.on('change:minimized', this.onMinimizedChanged, this);
|
2016-08-31 12:06:17 +02:00
|
|
|
return this.__super__.initialize.apply(this, arguments);
|
2016-03-28 16:08:50 +02:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
_show () {
|
|
|
|
const { _converse } = this.__super__;
|
2016-03-28 16:52:00 +02:00
|
|
|
if (!this.model.get('minimized')) {
|
2017-04-23 15:38:48 +02:00
|
|
|
this.__super__._show.apply(this, arguments);
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.chatboxviews.trimChats(this);
|
2017-04-23 15:38:48 +02:00
|
|
|
} else {
|
|
|
|
this.minimize();
|
2016-03-28 16:52:00 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
isNewMessageHidden () {
|
2017-05-03 09:06:28 +02:00
|
|
|
return this.model.get('minimized') ||
|
|
|
|
this.__super__.isNewMessageHidden.apply(this, arguments);
|
|
|
|
},
|
|
|
|
|
2018-03-13 19:11:49 +01:00
|
|
|
shouldShowOnTextMessage () {
|
|
|
|
return !this.model.get('minimized') &&
|
|
|
|
this.__super__.shouldShowOnTextMessage.apply(this, arguments);
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
setChatBoxHeight (height) {
|
2016-03-28 16:08:50 +02:00
|
|
|
if (!this.model.get('minimized')) {
|
2016-08-31 12:06:17 +02:00
|
|
|
return this.__super__.setChatBoxHeight.apply(this, arguments);
|
2016-03-28 16:08:50 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
setChatBoxWidth (width) {
|
2016-03-28 16:08:50 +02:00
|
|
|
if (!this.model.get('minimized')) {
|
2016-08-31 12:06:17 +02:00
|
|
|
return this.__super__.setChatBoxWidth.apply(this, arguments);
|
2016-03-28 16:08:50 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
onMinimizedChanged (item) {
|
2016-03-28 16:08:50 +02:00
|
|
|
if (item.get('minimized')) {
|
|
|
|
this.minimize();
|
|
|
|
} else {
|
|
|
|
this.maximize();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
maximize () {
|
2016-03-31 10:40:25 +02:00
|
|
|
// Restores a minimized chat box
|
2017-07-10 17:46:22 +02:00
|
|
|
const { _converse } = this.__super__;
|
2017-08-29 11:08:37 +02:00
|
|
|
this.insertIntoDOM();
|
|
|
|
|
2017-05-03 09:06:28 +02:00
|
|
|
if (!this.model.isScrolledUp()) {
|
|
|
|
this.model.clearUnreadMsgCounter();
|
|
|
|
}
|
2016-11-22 17:42:58 +01:00
|
|
|
this.show();
|
2017-08-29 11:08:37 +02:00
|
|
|
this.__super__._converse.emit('chatBoxMaximized', this);
|
2016-03-28 16:08:50 +02:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
minimize (ev) {
|
|
|
|
const { _converse } = this.__super__;
|
2016-03-28 16:08:50 +02:00
|
|
|
if (ev && ev.preventDefault) { ev.preventDefault(); }
|
|
|
|
// save the scroll position to restore it on maximize
|
2017-06-14 20:14:45 +02:00
|
|
|
if (this.model.collection && this.model.collection.browserStorage) {
|
2018-01-02 21:44:46 +01:00
|
|
|
this.model.save({'scroll': this.content.scrollTop});
|
2017-06-14 20:14:45 +02:00
|
|
|
} else {
|
2018-01-02 21:44:46 +01:00
|
|
|
this.model.set({'scroll': this.content.scrollTop});
|
2017-06-14 20:14:45 +02:00
|
|
|
}
|
2016-12-20 10:30:20 +01:00
|
|
|
this.setChatState(_converse.INACTIVE).model.minimize();
|
2016-11-22 17:42:58 +01:00
|
|
|
this.hide();
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.emit('chatBoxMinimized', this);
|
2016-03-28 16:08:50 +02:00
|
|
|
},
|
2016-03-31 10:40:25 +02:00
|
|
|
},
|
2016-03-28 16:08:50 +02:00
|
|
|
|
2018-05-03 20:05:45 +02:00
|
|
|
ChatBoxHeading: {
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const { _converse } = this.__super__,
|
|
|
|
{ __ } = _converse;
|
|
|
|
const result = this.__super__.render.apply(this, arguments);
|
|
|
|
const new_html = tpl_chatbox_minimize(
|
|
|
|
{info_minimize: __('Minimize this chat box')}
|
|
|
|
);
|
|
|
|
const el = this.el.querySelector('.toggle-chatbox-button');
|
|
|
|
if (el) {
|
|
|
|
el.outerHTML = new_html;
|
|
|
|
} else {
|
|
|
|
const button = this.el.querySelector('.close-chatbox-button');
|
|
|
|
button.insertAdjacentHTML('afterEnd', new_html);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-03-31 10:40:25 +02:00
|
|
|
ChatRoomView: {
|
|
|
|
events: {
|
|
|
|
'click .toggle-chatbox-button': 'minimize',
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize () {
|
2016-03-31 10:40:25 +02:00
|
|
|
this.model.on('change:minimized', function (item) {
|
|
|
|
if (item.get('minimized')) {
|
|
|
|
this.hide();
|
|
|
|
} else {
|
|
|
|
this.maximize();
|
|
|
|
}
|
|
|
|
}, this);
|
2017-07-10 17:46:22 +02:00
|
|
|
const result = this.__super__.initialize.apply(this, arguments);
|
2016-03-31 10:40:25 +02:00
|
|
|
if (this.model.get('minimized')) {
|
|
|
|
this.hide();
|
|
|
|
}
|
|
|
|
return result;
|
2016-12-02 18:41:05 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
generateHeadingHTML () {
|
|
|
|
const { _converse } = this.__super__,
|
|
|
|
{ __ } = _converse;
|
|
|
|
const html = this.__super__.generateHeadingHTML.apply(this, arguments);
|
|
|
|
const div = document.createElement('div');
|
2016-12-02 18:41:05 +01:00
|
|
|
div.innerHTML = html;
|
2017-07-10 17:46:22 +02:00
|
|
|
const button = div.querySelector('.close-chatbox-button');
|
2017-08-29 11:08:37 +02:00
|
|
|
button.insertAdjacentHTML('afterend',
|
|
|
|
tpl_chatbox_minimize({
|
|
|
|
'info_minimize': __('Minimize this chat box')
|
|
|
|
})
|
|
|
|
);
|
2016-12-02 18:41:05 +01:00
|
|
|
return div.innerHTML;
|
2016-03-31 10:40:25 +02:00
|
|
|
}
|
2016-03-28 16:08:50 +02:00
|
|
|
},
|
|
|
|
|
2016-03-14 17:04:27 +01:00
|
|
|
ChatBoxes: {
|
2017-07-10 17:46:22 +02:00
|
|
|
chatBoxMayBeShown (chatbox) {
|
2016-08-31 12:06:17 +02:00
|
|
|
return this.__super__.chatBoxMayBeShown.apply(this, arguments) &&
|
2016-03-14 17:04:27 +01:00
|
|
|
!chatbox.get('minimized');
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
ChatBoxViews: {
|
2017-07-10 17:46:22 +02:00
|
|
|
getChatBoxWidth (view) {
|
2018-01-03 14:58:05 +01:00
|
|
|
if (!view.model.get('minimized') && u.isVisible(view.el)) {
|
|
|
|
return u.getOuterWidth(view.el, true);
|
2016-03-14 17:04:27 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
getShownChats () {
|
|
|
|
return this.filter((view) =>
|
2016-06-09 23:25:19 +02:00
|
|
|
// The controlbox can take a while to close,
|
|
|
|
// so we need to check its state. That's why we checked
|
|
|
|
// the 'closed' state.
|
2017-07-10 17:46:22 +02:00
|
|
|
!view.model.get('minimized') &&
|
2016-06-09 23:25:19 +02:00
|
|
|
!view.model.get('closed') &&
|
2018-01-03 14:58:05 +01:00
|
|
|
u.isVisible(view.el)
|
2017-07-10 17:46:22 +02:00
|
|
|
);
|
2016-03-30 14:03:15 +02:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
trimChats (newchat) {
|
2016-03-14 17:04:27 +01:00
|
|
|
/* This method is called when a newly created chat box will
|
2016-03-19 23:16:00 +01:00
|
|
|
* be shown.
|
|
|
|
*
|
|
|
|
* It checks whether there is enough space on the page to show
|
|
|
|
* another chat box. Otherwise it minimizes the oldest chat box
|
|
|
|
* to create space.
|
|
|
|
*/
|
2018-01-03 14:58:05 +01:00
|
|
|
const { _converse } = this.__super__,
|
|
|
|
shown_chats = this.getShownChats(),
|
|
|
|
body_width = u.getOuterWidth(document.querySelector('body'), true);
|
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
if (_converse.no_trimming || shown_chats.length <= 1) {
|
2016-03-30 14:03:15 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-01-03 14:58:05 +01:00
|
|
|
if (this.getChatBoxWidth(shown_chats[0]) === body_width) {
|
2016-03-30 14:03:15 +02:00
|
|
|
// If the chats shown are the same width as the body,
|
|
|
|
// then we're in responsive mode and the chats are
|
|
|
|
// fullscreen. In this case we don't trim.
|
2016-03-14 17:04:27 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-08-29 12:11:18 +02:00
|
|
|
_converse.api.waitUntil('minimizedChatsInitialized').then(() => {
|
2018-01-03 14:58:05 +01:00
|
|
|
const minimized_el = _.get(_converse.minimized_chats, 'el'),
|
|
|
|
new_id = newchat ? newchat.model.get('id') : null;
|
|
|
|
|
|
|
|
if (minimized_el) {
|
|
|
|
const minimized_width = _.includes(this.model.pluck('minimized'), true) ?
|
|
|
|
u.getOuterWidth(minimized_el, true) : 0;
|
|
|
|
|
|
|
|
const boxes_width = _.reduce(
|
|
|
|
this.xget(new_id),
|
|
|
|
(memo, view) => memo + this.getChatBoxWidth(view),
|
|
|
|
newchat ? u.getOuterWidth(newchat.el, true) : 0
|
|
|
|
);
|
|
|
|
if ((minimized_width + boxes_width) > body_width) {
|
|
|
|
const oldest_chat = this.getOldestMaximizedChat([new_id]);
|
|
|
|
if (oldest_chat) {
|
|
|
|
// We hide the chat immediately, because waiting
|
|
|
|
// for the event to fire (and letting the
|
|
|
|
// ChatBoxView hide it then) causes race
|
|
|
|
// conditions.
|
|
|
|
const view = this.get(oldest_chat.get('id'));
|
|
|
|
if (view) {
|
|
|
|
view.hide();
|
|
|
|
}
|
|
|
|
oldest_chat.minimize();
|
2017-08-16 14:16:24 +02:00
|
|
|
}
|
2016-03-28 16:52:00 +02:00
|
|
|
}
|
2016-03-14 17:04:27 +01:00
|
|
|
}
|
2017-08-16 14:16:24 +02:00
|
|
|
}).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
|
2016-03-14 17:04:27 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
getOldestMaximizedChat (exclude_ids) {
|
2016-03-14 17:04:27 +01:00
|
|
|
// Get oldest view (if its id is not excluded)
|
2016-03-31 10:40:25 +02:00
|
|
|
exclude_ids.push('controlbox');
|
2017-07-10 17:46:22 +02:00
|
|
|
let i = 0;
|
|
|
|
let model = this.model.sort().at(i);
|
2017-01-26 15:49:02 +01:00
|
|
|
while (_.includes(exclude_ids, model.get('id')) ||
|
2016-03-14 17:04:27 +01:00
|
|
|
model.get('minimized') === true) {
|
|
|
|
i++;
|
|
|
|
model = this.model.at(i);
|
|
|
|
if (!model) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return model;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize () {
|
2016-03-14 17:04:27 +01:00
|
|
|
/* The initialize function gets called as soon as the plugin is
|
2016-12-20 10:30:20 +01:00
|
|
|
* loaded by Converse.js's plugin machinery.
|
2016-03-14 17:04:27 +01:00
|
|
|
*/
|
2017-07-10 17:46:22 +02:00
|
|
|
const { _converse } = this,
|
|
|
|
{ __ } = _converse;
|
2016-12-20 11:42:20 +01:00
|
|
|
|
|
|
|
// Add new HTML templates.
|
|
|
|
_converse.templates.chatbox_minimize = tpl_chatbox_minimize;
|
|
|
|
_converse.templates.toggle_chats = tpl_toggle_chats;
|
|
|
|
_converse.templates.trimmed_chat = tpl_trimmed_chat;
|
|
|
|
_converse.templates.chats_panel = tpl_chats_panel;
|
|
|
|
|
2017-07-05 11:03:13 +02:00
|
|
|
_converse.api.settings.update({
|
2016-03-14 17:04:27 +01:00
|
|
|
no_trimming: false, // Set to true for phantomjs tests (where browser apparently has no width)
|
|
|
|
});
|
|
|
|
|
2017-08-16 14:16:24 +02:00
|
|
|
_converse.api.promises.add('minimizedChatsInitialized');
|
|
|
|
|
2018-01-03 20:02:05 +01:00
|
|
|
_converse.MinimizedChatBoxView = Backbone.NativeView.extend({
|
2016-03-14 17:04:27 +01:00
|
|
|
tagName: 'div',
|
2018-03-09 17:43:25 +01:00
|
|
|
className: 'chat-head row no-gutters',
|
2016-03-14 17:04:27 +01:00
|
|
|
events: {
|
|
|
|
'click .close-chatbox-button': 'close',
|
|
|
|
'click .restore-chat': 'restore'
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize () {
|
2017-05-03 09:06:28 +02:00
|
|
|
this.model.on('change:num_unread', this.render, this);
|
2016-03-14 17:04:27 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
render () {
|
|
|
|
const data = _.extend(
|
2016-03-14 17:04:27 +01:00
|
|
|
this.model.toJSON(),
|
|
|
|
{ 'tooltip': __('Click to restore this chat') }
|
|
|
|
);
|
2016-03-21 12:33:34 +01:00
|
|
|
if (this.model.get('type') === 'chatroom') {
|
2016-03-14 17:04:27 +01:00
|
|
|
data.title = this.model.get('name');
|
2018-01-03 17:49:08 +01:00
|
|
|
u.addClass('chat-head-chatroom', this.el);
|
2016-03-14 17:04:27 +01:00
|
|
|
} else {
|
|
|
|
data.title = this.model.get('fullname');
|
2018-01-03 17:49:08 +01:00
|
|
|
u.addClass('chat-head-chatbox', this.el);
|
2016-03-14 17:04:27 +01:00
|
|
|
}
|
2018-01-03 14:58:05 +01:00
|
|
|
this.el.innerHTML = tpl_trimmed_chat(data);
|
|
|
|
return this.el;
|
2016-03-14 17:04:27 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
close (ev) {
|
2016-03-14 17:04:27 +01:00
|
|
|
if (ev && ev.preventDefault) { ev.preventDefault(); }
|
|
|
|
this.remove();
|
2017-07-10 17:46:22 +02:00
|
|
|
const view = _converse.chatboxviews.get(this.model.get('id'));
|
2016-03-28 13:42:33 +02:00
|
|
|
if (view) {
|
|
|
|
// This will call model.destroy(), removing it from the
|
|
|
|
// collection and will also emit 'chatBoxClosed'
|
|
|
|
view.close();
|
|
|
|
} else {
|
|
|
|
this.model.destroy();
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.emit('chatBoxClosed', this);
|
2016-03-28 13:42:33 +02:00
|
|
|
}
|
2016-03-14 17:04:27 +01:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
restore: _.debounce(function (ev) {
|
|
|
|
if (ev && ev.preventDefault) { ev.preventDefault(); }
|
2017-05-03 09:06:28 +02:00
|
|
|
this.model.off('change:num_unread', null, this);
|
2016-03-14 17:04:27 +01:00
|
|
|
this.remove();
|
|
|
|
this.model.maximize();
|
2017-01-26 15:49:02 +01:00
|
|
|
}, 200, {'leading': true})
|
2016-03-14 17:04:27 +01:00
|
|
|
});
|
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
|
|
|
|
_converse.MinimizedChats = Backbone.Overview.extend({
|
2016-11-22 17:36:39 +01:00
|
|
|
tagName: 'div',
|
|
|
|
id: "minimized-chats",
|
|
|
|
className: 'hidden',
|
2016-03-14 17:04:27 +01:00
|
|
|
events: {
|
|
|
|
"click #toggle-minimized-chats": "toggle"
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize () {
|
2016-11-22 17:36:39 +01:00
|
|
|
this.render();
|
2016-03-14 17:04:27 +01:00
|
|
|
this.initToggle();
|
2017-08-29 12:11:18 +02:00
|
|
|
this.addMultipleChats(this.model.where({'minimized': true}));
|
2016-03-14 17:04:27 +01:00
|
|
|
this.model.on("add", this.onChanged, this);
|
|
|
|
this.model.on("destroy", this.removeChat, this);
|
|
|
|
this.model.on("change:minimized", this.onChanged, this);
|
|
|
|
this.model.on('change:num_unread', this.updateUnreadMessagesCounter, this);
|
|
|
|
},
|
|
|
|
|
2017-08-29 12:11:18 +02:00
|
|
|
render () {
|
|
|
|
if (!this.el.parentElement) {
|
|
|
|
this.el.innerHTML = tpl_chats_panel();
|
2018-03-09 17:43:25 +01:00
|
|
|
_converse.chatboxviews.insertRowColumn(this.el);
|
2017-08-29 12:11:18 +02:00
|
|
|
}
|
|
|
|
if (this.keys().length === 0) {
|
|
|
|
this.el.classList.add('hidden');
|
2018-01-03 14:58:05 +01:00
|
|
|
} else if (this.keys().length > 0 && !u.isVisible(this.el)) {
|
2017-08-29 12:11:18 +02:00
|
|
|
this.el.classList.remove('hidden');
|
|
|
|
_converse.chatboxviews.trimChats();
|
|
|
|
}
|
2018-01-03 14:58:05 +01:00
|
|
|
return this.el;
|
2017-08-29 12:11:18 +02:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
tearDown () {
|
2016-03-14 17:04:27 +01:00
|
|
|
this.model.off("add", this.onChanged);
|
|
|
|
this.model.off("destroy", this.removeChat);
|
|
|
|
this.model.off("change:minimized", this.onChanged);
|
|
|
|
this.model.off('change:num_unread', this.updateUnreadMessagesCounter);
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
initToggle () {
|
2016-12-20 10:30:20 +01:00
|
|
|
this.toggleview = new _converse.MinimizedChatsToggleView({
|
|
|
|
model: new _converse.MinimizedChatsToggle()
|
2016-03-14 17:04:27 +01:00
|
|
|
});
|
2017-07-10 17:46:22 +02:00
|
|
|
const id = b64_sha1(`converse.minchatstoggle${_converse.bare_jid}`);
|
2016-03-14 17:04:27 +01:00
|
|
|
this.toggleview.model.id = id; // Appears to be necessary for backbone.browserStorage
|
2016-12-20 10:30:20 +01:00
|
|
|
this.toggleview.model.browserStorage = new Backbone.BrowserStorage[_converse.storage](id);
|
2016-03-14 17:04:27 +01:00
|
|
|
this.toggleview.model.fetch();
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
toggle (ev) {
|
2016-03-14 17:04:27 +01:00
|
|
|
if (ev && ev.preventDefault) { ev.preventDefault(); }
|
|
|
|
this.toggleview.model.save({'collapsed': !this.toggleview.model.get('collapsed')});
|
2018-01-03 14:58:05 +01:00
|
|
|
u.slideToggleElement(this.el.querySelector('.minimized-chats-flyout'), 200);
|
2016-03-14 17:04:27 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
onChanged (item) {
|
2016-03-14 17:04:27 +01:00
|
|
|
if (item.get('id') === 'controlbox') {
|
|
|
|
// The ControlBox has it's own minimize toggle
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (item.get('minimized')) {
|
|
|
|
this.addChat(item);
|
|
|
|
} else if (this.get(item.get('id'))) {
|
|
|
|
this.removeChat(item);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-01-03 14:58:05 +01:00
|
|
|
addChatView (item) {
|
|
|
|
const existing = this.get(item.get('id'));
|
|
|
|
if (existing && existing.el.parentNode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const view = new _converse.MinimizedChatBoxView({model: item});
|
|
|
|
this.el.querySelector('.minimized-chats-flyout').insertAdjacentElement('beforeEnd', view.render());
|
|
|
|
this.add(item.get('id'), view);
|
|
|
|
},
|
|
|
|
|
2017-08-29 12:11:18 +02:00
|
|
|
addMultipleChats (items) {
|
2018-01-03 14:58:05 +01:00
|
|
|
_.each(items, this.addChatView.bind(this));
|
2017-08-29 12:11:18 +02:00
|
|
|
this.toggleview.model.set({'num_minimized': this.keys().length});
|
|
|
|
this.render();
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
addChat (item) {
|
2018-01-03 14:58:05 +01:00
|
|
|
this.addChatView(item);
|
2016-03-14 17:04:27 +01:00
|
|
|
this.toggleview.model.set({'num_minimized': this.keys().length});
|
|
|
|
this.render();
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
removeChat (item) {
|
2016-03-14 17:04:27 +01:00
|
|
|
this.remove(item.get('id'));
|
|
|
|
this.toggleview.model.set({'num_minimized': this.keys().length});
|
|
|
|
this.render();
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
updateUnreadMessagesCounter () {
|
|
|
|
const ls = this.model.pluck('num_unread');
|
|
|
|
let count = 0, i;
|
2016-03-14 17:04:27 +01:00
|
|
|
for (i=0; i<ls.length; i++) { count += ls[i]; }
|
2017-04-24 22:34:08 +02:00
|
|
|
this.toggleview.model.save({'num_unread': count});
|
2016-03-14 17:04:27 +01:00
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.MinimizedChatsToggle = Backbone.Model.extend({
|
2017-05-16 11:19:15 +02:00
|
|
|
defaults: {
|
|
|
|
'collapsed': false,
|
|
|
|
'num_minimized': 0,
|
|
|
|
'num_unread': 0
|
2016-03-14 17:04:27 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2018-01-03 20:02:05 +01:00
|
|
|
_converse.MinimizedChatsToggleView = Backbone.NativeView.extend({
|
2016-03-14 17:04:27 +01:00
|
|
|
el: '#toggle-minimized-chats',
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize () {
|
2016-03-14 17:04:27 +01:00
|
|
|
this.model.on('change:num_minimized', this.render, this);
|
|
|
|
this.model.on('change:num_unread', this.render, this);
|
2018-01-03 14:58:05 +01:00
|
|
|
this.flyout = this.el.parentElement.querySelector('.minimized-chats-flyout');
|
2016-03-14 17:04:27 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
render () {
|
2018-01-03 14:58:05 +01:00
|
|
|
this.el.innerHTML = tpl_toggle_chats(
|
2016-03-14 17:04:27 +01:00
|
|
|
_.extend(this.model.toJSON(), {
|
|
|
|
'Minimized': __('Minimized')
|
|
|
|
})
|
2018-01-03 14:58:05 +01:00
|
|
|
);
|
2016-03-14 17:04:27 +01:00
|
|
|
if (this.model.get('collapsed')) {
|
2018-01-03 14:58:05 +01:00
|
|
|
u.hideElement(this.flyout);
|
2016-03-14 17:04:27 +01:00
|
|
|
} else {
|
2018-01-03 14:58:05 +01:00
|
|
|
u.showElement(this.flyout);
|
2016-03-14 17:04:27 +01:00
|
|
|
}
|
2018-01-03 14:58:05 +01:00
|
|
|
return this.el;
|
2016-03-14 17:04:27 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-08-16 12:31:17 +02:00
|
|
|
Promise.all([
|
|
|
|
_converse.api.waitUntil('connectionInitialized'),
|
|
|
|
_converse.api.waitUntil('chatBoxesInitialized')
|
|
|
|
]).then(() => {
|
|
|
|
_converse.minimized_chats = new _converse.MinimizedChats({
|
|
|
|
model: _converse.chatboxes
|
|
|
|
});
|
2017-08-16 14:16:24 +02:00
|
|
|
_converse.emit('minimizedChatsInitialized');
|
2017-08-16 12:31:17 +02:00
|
|
|
}).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
|
|
|
|
|
2018-05-30 17:16:26 +02:00
|
|
|
|
|
|
|
_converse.on('registeredGlobalEventHandlers', function () {
|
|
|
|
window.addEventListener("resize", _.debounce(function (ev) {
|
|
|
|
if (_converse.connection.connected) {
|
|
|
|
_converse.chatboxviews.trimChats();
|
|
|
|
}
|
|
|
|
}, 200));
|
|
|
|
});
|
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.on('controlBoxOpened', function (chatbox) {
|
2016-03-14 18:03:14 +01:00
|
|
|
// Wrapped in anon method because at scan time, chatboxviews
|
|
|
|
// attr not set yet.
|
2016-12-20 10:30:20 +01:00
|
|
|
if (_converse.connection.connected) {
|
|
|
|
_converse.chatboxviews.trimChats(chatbox);
|
2016-03-28 16:52:00 +02:00
|
|
|
}
|
2016-03-14 18:03:14 +01:00
|
|
|
});
|
2016-03-14 17:04:27 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}));
|