2017-04-29 13:30:47 +02:00
|
|
|
// Converse.js (A browser based XMPP chat client)
|
2019-03-04 17:47:45 +01:00
|
|
|
// https://conversejs.org
|
2017-04-29 13:30:47 +02:00
|
|
|
//
|
2019-02-18 19:17:06 +01:00
|
|
|
// Copyright (c) 2013-2019, Jan-Carel Brand <jc@opkode.com>
|
2017-04-29 13:30:47 +02:00
|
|
|
// Licensed under the Mozilla Public License (MPLv2)
|
2019-07-11 10:48:52 +02:00
|
|
|
/**
|
|
|
|
* @module converse-roomslist
|
|
|
|
* @description
|
|
|
|
* Converse.js plugin which shows a list of currently open
|
2017-04-29 13:30:47 +02:00
|
|
|
* rooms in the "Rooms Panel" of the ControlBox.
|
|
|
|
*/
|
2019-10-09 16:01:38 +02:00
|
|
|
import "@converse/headless/converse-muc";
|
2019-05-23 14:26:20 +02:00
|
|
|
import BrowserStorage from "backbone.browserStorage";
|
|
|
|
import { OrderedListView } from "backbone.overview";
|
2018-10-23 03:41:38 +02:00
|
|
|
import converse from "@converse/headless/converse-core";
|
|
|
|
import tpl_rooms_list from "templates/rooms_list.html";
|
|
|
|
import tpl_rooms_list_item from "templates/rooms_list_item.html"
|
|
|
|
|
2019-10-09 16:01:38 +02:00
|
|
|
const { Backbone, Strophe, } = converse.env;
|
2018-10-23 03:41:38 +02:00
|
|
|
const u = converse.env.utils;
|
|
|
|
|
|
|
|
|
|
|
|
converse.plugins.add('converse-roomslist', {
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
dependencies: ["converse-singleton", "converse-controlbox", "converse-muc", "converse-bookmarks"],
|
|
|
|
|
|
|
|
initialize () {
|
|
|
|
/* The initialize function gets called as soon as the plugin is
|
|
|
|
* loaded by converse.js's plugin machinery.
|
2017-08-09 15:50:24 +02:00
|
|
|
*/
|
2018-10-23 03:41:38 +02:00
|
|
|
const { _converse } = this,
|
|
|
|
{ __ } = _converse;
|
2017-08-09 15:50:24 +02:00
|
|
|
|
2018-12-15 19:30:20 +01:00
|
|
|
// Promises exposed by this plugin
|
|
|
|
_converse.api.promises.add('roomsListInitialized');
|
|
|
|
|
2017-04-29 13:30:47 +02:00
|
|
|
|
2019-08-01 10:26:35 +02:00
|
|
|
_converse.OpenRooms = _converse.Collection.extend({
|
2018-01-05 13:33:28 +01:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
comparator (room) {
|
2019-05-22 19:28:09 +02:00
|
|
|
if (_converse.bookmarks && room.get('bookmarked')) {
|
2019-10-09 16:01:38 +02:00
|
|
|
const bookmark = _converse.bookmarks.findWhere({'jid': room.get('jid')});
|
2018-10-23 03:41:38 +02:00
|
|
|
return bookmark.get('name');
|
|
|
|
} else {
|
|
|
|
return room.get('name');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize () {
|
|
|
|
_converse.chatboxes.on('add', this.onChatBoxAdded, this);
|
|
|
|
_converse.chatboxes.on('change:hidden', this.onChatBoxChanged, this);
|
|
|
|
_converse.chatboxes.on('change:bookmarked', this.onChatBoxChanged, this);
|
|
|
|
_converse.chatboxes.on('change:name', this.onChatBoxChanged, this);
|
|
|
|
_converse.chatboxes.on('change:num_unread', this.onChatBoxChanged, this);
|
|
|
|
_converse.chatboxes.on('change:num_unread_general', this.onChatBoxChanged, this);
|
|
|
|
_converse.chatboxes.on('remove', this.onChatBoxRemoved, this);
|
2019-09-10 15:47:30 +02:00
|
|
|
this.reset(_converse.chatboxes.where({'type': _converse.CHATROOMS_TYPE}).map(cb => cb.attributes));
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
onChatBoxAdded (item) {
|
2019-08-05 01:39:57 +02:00
|
|
|
if (item.get('type') === _converse.CHATROOMS_TYPE) {
|
2018-10-23 03:41:38 +02:00
|
|
|
this.create(item.attributes);
|
|
|
|
}
|
|
|
|
},
|
2018-06-04 19:53:33 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
onChatBoxChanged (item) {
|
2019-08-05 01:39:57 +02:00
|
|
|
if (item.get('type') === _converse.CHATROOMS_TYPE) {
|
2018-10-23 03:41:38 +02:00
|
|
|
const room = this.get(item.get('jid'));
|
2019-08-05 01:39:57 +02:00
|
|
|
room && room.set(item.attributes);
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
},
|
2018-01-05 13:33:28 +01:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
onChatBoxRemoved (item) {
|
2019-08-05 01:39:57 +02:00
|
|
|
if (item.get('type') === _converse.CHATROOMS_TYPE) {
|
2018-10-23 03:41:38 +02:00
|
|
|
const room = this.get(item.get('jid'))
|
|
|
|
this.remove(room);
|
2018-01-05 13:33:28 +01:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
});
|
2018-01-05 13:33:28 +01:00
|
|
|
|
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
_converse.RoomsList = Backbone.Model.extend({
|
|
|
|
defaults: {
|
|
|
|
"toggle-state": _converse.OPENED
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
_converse.RoomsListElementView = Backbone.VDOMView.extend({
|
|
|
|
events: {
|
|
|
|
'click .room-info': 'showRoomDetailsModal'
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize () {
|
2019-09-06 14:34:59 +02:00
|
|
|
this.listenTo(this.model, 'destroy', this.remove)
|
|
|
|
this.listenTo(this.model, 'remove', this.remove)
|
|
|
|
this.listenTo(this.model, 'change:bookmarked', this.render)
|
|
|
|
this.listenTo(this.model, 'change:hidden', this.render)
|
|
|
|
this.listenTo(this.model, 'change:name', this.render)
|
|
|
|
this.listenTo(this.model, 'change:num_unread', this.render)
|
|
|
|
this.listenTo(this.model, 'change:num_unread_general', this.render)
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
toHTML () {
|
|
|
|
return tpl_rooms_list_item(
|
2019-04-29 09:07:15 +02:00
|
|
|
Object.assign(this.model.toJSON(), {
|
2018-10-23 03:41:38 +02:00
|
|
|
// XXX: By the time this renders, the _converse.bookmarks
|
|
|
|
// collection should already exist if bookmarks are
|
|
|
|
// supported by the XMPP server. So we can use it
|
|
|
|
// as a check for support (other ways of checking are async).
|
|
|
|
'allow_bookmarks': _converse.allow_bookmarks && _converse.bookmarks,
|
2018-12-04 12:14:52 +01:00
|
|
|
'currently_open': _converse.isUniView() && !this.model.get('hidden'),
|
2018-10-23 03:41:38 +02:00
|
|
|
'info_leave_room': __('Leave this groupchat'),
|
|
|
|
'info_remove_bookmark': __('Unbookmark this groupchat'),
|
|
|
|
'info_add_bookmark': __('Bookmark this groupchat'),
|
|
|
|
'info_title': __('Show more information on this groupchat'),
|
|
|
|
'name': this.getRoomsListElementName(),
|
|
|
|
'open_title': __('Click to open this groupchat')
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
|
|
|
showRoomDetailsModal (ev) {
|
|
|
|
const room = _converse.chatboxes.get(this.model.get('jid'));
|
|
|
|
ev.preventDefault();
|
2019-07-29 10:19:05 +02:00
|
|
|
if (room.room_details_modal === undefined) {
|
2018-10-23 03:41:38 +02:00
|
|
|
room.room_details_modal = new _converse.RoomDetailsModal({'model': room});
|
2017-07-14 10:01:00 +02:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
room.room_details_modal.show(ev);
|
|
|
|
},
|
|
|
|
|
|
|
|
getRoomsListElementName () {
|
2019-05-21 11:12:34 +02:00
|
|
|
if (this.model.get('bookmarked') && _converse.bookmarks) {
|
2019-08-10 14:05:21 +02:00
|
|
|
const bookmark = _converse.bookmarks.findWhere({'jid': this.model.get('jid')});
|
|
|
|
if (bookmark) {
|
|
|
|
return bookmark.get('name');
|
|
|
|
}
|
2018-01-05 13:33:28 +01:00
|
|
|
}
|
2019-08-10 14:05:21 +02:00
|
|
|
return this.model.get('name');
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
});
|
2017-04-29 13:30:47 +02:00
|
|
|
|
|
|
|
|
2019-05-23 14:26:20 +02:00
|
|
|
_converse.RoomsListView = OrderedListView.extend({
|
2018-10-23 03:41:38 +02:00
|
|
|
tagName: 'div',
|
|
|
|
className: 'open-rooms-list list-container rooms-list-container',
|
|
|
|
events: {
|
|
|
|
'click .add-bookmark': 'addBookmark',
|
|
|
|
'click .close-room': 'closeRoom',
|
|
|
|
'click .list-toggle': 'toggleRoomsList',
|
|
|
|
'click .remove-bookmark': 'removeBookmark',
|
|
|
|
'click .open-room': 'openRoom',
|
|
|
|
},
|
|
|
|
listSelector: '.rooms-list',
|
|
|
|
ItemView: _converse.RoomsListElementView,
|
|
|
|
subviewIndex: 'jid',
|
2017-04-29 13:30:47 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
initialize () {
|
2019-05-23 14:26:20 +02:00
|
|
|
OrderedListView.prototype.initialize.apply(this, arguments);
|
2018-10-23 03:41:38 +02:00
|
|
|
|
2019-09-06 14:34:59 +02:00
|
|
|
this.listenTo(this.model, 'add', this.showOrHide)
|
|
|
|
this.listenTo(this.model, 'remove', this.showOrHide)
|
2018-10-23 03:41:38 +02:00
|
|
|
|
|
|
|
const storage = _converse.config.get('storage'),
|
2019-02-18 19:06:11 +01:00
|
|
|
id = `converse.roomslist${_converse.bare_jid}`;
|
2018-10-23 03:41:38 +02:00
|
|
|
|
|
|
|
this.list_model = new _converse.RoomsList({'id': id});
|
2019-05-23 14:26:20 +02:00
|
|
|
this.list_model.browserStorage = new BrowserStorage[storage](id);
|
2018-10-23 03:41:38 +02:00
|
|
|
this.list_model.fetch();
|
|
|
|
this.render();
|
|
|
|
this.sortAndPositionAllItems();
|
|
|
|
},
|
|
|
|
|
|
|
|
render () {
|
|
|
|
this.el.innerHTML = tpl_rooms_list({
|
|
|
|
'toggle_state': this.list_model.get('toggle-state'),
|
|
|
|
'desc_rooms': __('Click to toggle the list of open groupchats'),
|
2019-08-07 10:52:20 +02:00
|
|
|
// Note to translators, "Open Groupchats" refers to groupchats that are open, NOT a command.
|
2018-10-23 03:41:38 +02:00
|
|
|
'label_rooms': __('Open Groupchats'),
|
|
|
|
'_converse': _converse
|
|
|
|
});
|
|
|
|
if (this.list_model.get('toggle-state') !== _converse.OPENED) {
|
|
|
|
this.el.querySelector('.open-rooms-list').classList.add('collapsed');
|
|
|
|
}
|
|
|
|
this.showOrHide();
|
|
|
|
this.insertIntoControlBox();
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
insertIntoControlBox () {
|
|
|
|
const controlboxview = _converse.chatboxviews.get('controlbox');
|
2019-07-29 10:19:05 +02:00
|
|
|
if (controlboxview !== undefined && !u.rootContains(_converse.root, this.el)) {
|
2018-10-23 03:41:38 +02:00
|
|
|
const el = controlboxview.el.querySelector('.open-rooms-list');
|
2019-08-05 01:39:57 +02:00
|
|
|
if (el !== null) {
|
2018-10-23 03:41:38 +02:00
|
|
|
el.parentNode.replaceChild(this.el, el);
|
2017-04-29 13:30:47 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
hide () {
|
|
|
|
u.hideElement(this.el);
|
|
|
|
},
|
|
|
|
|
|
|
|
show () {
|
|
|
|
u.showElement(this.el);
|
|
|
|
},
|
|
|
|
|
2019-01-04 05:48:23 +01:00
|
|
|
async openRoom (ev) {
|
2018-10-23 03:41:38 +02:00
|
|
|
ev.preventDefault();
|
|
|
|
const name = ev.target.textContent;
|
|
|
|
const jid = ev.target.getAttribute('data-room-jid');
|
|
|
|
const data = {
|
|
|
|
'name': name || Strophe.unescapeNode(Strophe.getNodeFromJid(jid)) || jid
|
|
|
|
}
|
2019-04-16 16:36:58 +02:00
|
|
|
await _converse.api.rooms.open(jid, data, true);
|
2019-01-04 05:48:23 +01:00
|
|
|
_converse.api.chatviews.get(jid).focus();
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
closeRoom (ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
const name = ev.target.getAttribute('data-room-name');
|
|
|
|
const jid = ev.target.getAttribute('data-room-jid');
|
|
|
|
if (confirm(__("Are you sure you want to leave the groupchat %1$s?", name))) {
|
|
|
|
// TODO: replace with API call
|
|
|
|
_converse.chatboxviews.get(jid).close();
|
|
|
|
}
|
|
|
|
},
|
2017-04-29 13:30:47 +02:00
|
|
|
|
2019-10-09 16:01:38 +02:00
|
|
|
showOrHide () {
|
2018-10-23 03:41:38 +02:00
|
|
|
if (!this.model.models.length) {
|
|
|
|
u.hideElement(this.el);
|
|
|
|
} else {
|
|
|
|
u.showElement(this.el);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
removeBookmark: _converse.removeBookmarkViaEvent,
|
|
|
|
addBookmark: _converse.addBookmarkViaEvent,
|
|
|
|
|
|
|
|
toggleRoomsList (ev) {
|
|
|
|
if (ev && ev.preventDefault) { ev.preventDefault(); }
|
2019-01-03 11:45:52 +01:00
|
|
|
const icon_el = ev.target.matches('.fa') ? ev.target : ev.target.querySelector('.fa');
|
2018-10-23 03:41:38 +02:00
|
|
|
if (icon_el.classList.contains("fa-caret-down")) {
|
|
|
|
u.slideIn(this.el.querySelector('.open-rooms-list')).then(() => {
|
|
|
|
this.list_model.save({'toggle-state': _converse.CLOSED});
|
|
|
|
icon_el.classList.remove("fa-caret-down");
|
|
|
|
icon_el.classList.add("fa-caret-right");
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
u.slideOut(this.el.querySelector('.open-rooms-list')).then(() => {
|
|
|
|
this.list_model.save({'toggle-state': _converse.OPENED});
|
|
|
|
icon_el.classList.remove("fa-caret-right");
|
|
|
|
icon_el.classList.add("fa-caret-down");
|
|
|
|
});
|
|
|
|
}
|
2018-02-22 12:46:10 +01:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const initRoomsListView = function () {
|
|
|
|
const storage = _converse.config.get('storage'),
|
2019-02-18 19:06:11 +01:00
|
|
|
id = `converse.open-rooms-{_converse.bare_jid}`,
|
2018-10-23 03:41:38 +02:00
|
|
|
model = new _converse.OpenRooms();
|
|
|
|
|
2019-05-23 14:26:20 +02:00
|
|
|
model.browserStorage = new BrowserStorage[storage](id);
|
2018-10-23 03:41:38 +02:00
|
|
|
_converse.rooms_list_view = new _converse.RoomsListView({'model': model});
|
2019-03-29 21:36:49 +01:00
|
|
|
/**
|
|
|
|
* Triggered once the _converse.RoomsListView has been created and initialized.
|
|
|
|
* @event _converse#roomsListInitialized
|
|
|
|
* @example _converse.api.listen.on('roomsListInitialized', status => { ... });
|
|
|
|
*/
|
|
|
|
_converse.api.trigger('roomsListInitialized');
|
2018-10-23 03:41:38 +02:00
|
|
|
};
|
|
|
|
|
2019-03-29 14:15:03 +01:00
|
|
|
_converse.api.listen.on('connected', async () => {
|
2019-02-20 22:58:59 +01:00
|
|
|
if (_converse.allow_bookmarks) {
|
|
|
|
await _converse.api.waitUntil('bookmarksInitialized');
|
|
|
|
} else {
|
|
|
|
await Promise.all([
|
|
|
|
_converse.api.waitUntil('chatBoxesFetched'),
|
|
|
|
_converse.api.waitUntil('roomsPanelRendered')
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
initRoomsListView();
|
|
|
|
});
|
2018-10-23 03:41:38 +02:00
|
|
|
|
|
|
|
_converse.api.listen.on('reconnected', initRoomsListView);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|