2017-04-29 13:30:47 +02:00
|
|
|
// Converse.js (A browser based XMPP chat client)
|
|
|
|
// http://conversejs.org
|
|
|
|
//
|
|
|
|
// Copyright (c) 2012-2017, Jan-Carel Brand <jc@opkode.com>
|
|
|
|
// Licensed under the Mozilla Public License (MPLv2)
|
|
|
|
//
|
|
|
|
/*global define */
|
|
|
|
|
|
|
|
/* This is a non-core Converse.js plugin which shows a list of currently open
|
|
|
|
* rooms in the "Rooms Panel" of the ControlBox.
|
|
|
|
*/
|
|
|
|
(function (root, factory) {
|
2018-06-06 11:04:23 +02:00
|
|
|
define(["converse-core",
|
2017-04-29 13:30:47 +02:00
|
|
|
"converse-muc",
|
2018-05-24 21:09:33 +02:00
|
|
|
"templates/rooms_list.html",
|
|
|
|
"templates/rooms_list_item.html"
|
2017-04-29 13:30:47 +02:00
|
|
|
], factory);
|
2018-06-06 11:04:23 +02:00
|
|
|
}(this, function (converse, muc, tpl_rooms_list, tpl_rooms_list_item) {
|
2018-02-21 22:40:51 +01:00
|
|
|
const { Backbone, Promise, Strophe, b64_sha1, sizzle, _ } = converse.env;
|
2018-01-05 13:33:28 +01:00
|
|
|
const u = converse.env.utils;
|
2017-04-29 13:30:47 +02:00
|
|
|
|
|
|
|
converse.plugins.add('converse-roomslist', {
|
2017-08-09 15:50:24 +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-07-02 23:29:57 +02:00
|
|
|
dependencies: ["converse-singleton", "converse-controlbox", "converse-muc", "converse-bookmarks"],
|
2017-08-09 15:50:24 +02:00
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize () {
|
2017-04-29 13:30:47 +02:00
|
|
|
/* The initialize function gets called as soon as the plugin is
|
|
|
|
* loaded by converse.js's plugin machinery.
|
|
|
|
*/
|
2017-07-10 17:46:22 +02:00
|
|
|
const { _converse } = this,
|
2017-09-24 20:36:39 +02:00
|
|
|
{ __ } = _converse;
|
2017-04-29 13:30:47 +02:00
|
|
|
|
2018-01-05 13:33:28 +01:00
|
|
|
|
|
|
|
_converse.OpenRooms = Backbone.Collection.extend({
|
2018-06-04 19:53:33 +02:00
|
|
|
|
2018-01-05 13:33:28 +01:00
|
|
|
comparator (room) {
|
|
|
|
if (room.get('bookmarked')) {
|
|
|
|
const bookmark = _.head(_converse.bookmarksview.model.where({'jid': room.get('jid')}));
|
|
|
|
return bookmark.get('name');
|
|
|
|
} else {
|
|
|
|
return room.get('name');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize () {
|
|
|
|
_converse.chatboxes.on('add', this.onChatBoxAdded, this);
|
2018-07-02 23:29:57 +02:00
|
|
|
_converse.chatboxes.on('change:hidden', this.onChatBoxChanged, this);
|
2018-01-05 13:33:28 +01:00
|
|
|
_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);
|
|
|
|
this.reset(_.map(_converse.chatboxes.where({'type': 'chatroom'}), 'attributes'));
|
|
|
|
},
|
|
|
|
|
|
|
|
onChatBoxAdded (item) {
|
|
|
|
if (item.get('type') === 'chatroom') {
|
|
|
|
this.create(item.attributes);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onChatBoxChanged (item) {
|
|
|
|
if (item.get('type') === 'chatroom') {
|
|
|
|
const room = this.get(item.get('jid'));
|
|
|
|
if (!_.isNil(room)) {
|
|
|
|
room.set(item.attributes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onChatBoxRemoved (item) {
|
|
|
|
if (item.get('type') === 'chatroom') {
|
|
|
|
const room = this.get(item.get('jid'))
|
|
|
|
this.remove(room);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-04-29 13:30:47 +02:00
|
|
|
_converse.RoomsList = Backbone.Model.extend({
|
|
|
|
defaults: {
|
|
|
|
"toggle-state": _converse.OPENED
|
2017-07-14 10:01:00 +02:00
|
|
|
}
|
2017-04-29 13:30:47 +02:00
|
|
|
});
|
|
|
|
|
2018-01-05 13:33:28 +01:00
|
|
|
_converse.RoomsListElementView = Backbone.VDOMView.extend({
|
2018-06-04 19:53:33 +02:00
|
|
|
events: {
|
2018-07-02 23:29:57 +02:00
|
|
|
'click .room-info': 'showRoomDetailsModal'
|
2018-06-04 19:53:33 +02:00
|
|
|
},
|
|
|
|
|
2018-01-05 13:33:28 +01:00
|
|
|
initialize () {
|
|
|
|
this.model.on('destroy', this.remove, this);
|
|
|
|
this.model.on('remove', this.remove, this);
|
|
|
|
this.model.on('change:bookmarked', this.render, this);
|
2018-07-02 23:29:57 +02:00
|
|
|
this.model.on('change:hidden', this.render, this);
|
2018-01-05 13:33:28 +01:00
|
|
|
this.model.on('change:name', this.render, this);
|
|
|
|
this.model.on('change:num_unread', this.render, this);
|
|
|
|
this.model.on('change:num_unread_general', this.render, this);
|
|
|
|
},
|
|
|
|
|
|
|
|
toHTML () {
|
|
|
|
return tpl_rooms_list_item(
|
|
|
|
_.extend(this.model.toJSON(), {
|
2018-02-22 13:55:17 +01: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-07-02 23:29:57 +02:00
|
|
|
'currently_open': _converse.isSingleton() && !this.model.get('hidden'),
|
2018-07-02 15:54:51 +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'),
|
2018-01-05 13:33:28 +01:00
|
|
|
'name': this.getRoomsListElementName(),
|
2018-07-02 15:54:51 +02:00
|
|
|
'open_title': __('Click to open this groupchat')
|
2018-01-05 13:33:28 +01:00
|
|
|
}));
|
2018-06-04 19:53:33 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
showRoomDetailsModal (ev) {
|
|
|
|
const room = _converse.chatboxes.get(this.model.get('jid'));
|
|
|
|
ev.preventDefault();
|
|
|
|
if (_.isUndefined(room.room_details_modal)) {
|
|
|
|
room.room_details_modal = new _converse.RoomDetailsModal({'model': room});
|
|
|
|
}
|
|
|
|
room.room_details_modal.show(ev);
|
|
|
|
},
|
|
|
|
|
|
|
|
getRoomsListElementName () {
|
|
|
|
if (this.model.get('bookmarked') && _converse.bookmarksview) {
|
|
|
|
const bookmark = _.head(_converse.bookmarksview.model.where({'jid': this.model.get('jid')}));
|
|
|
|
return bookmark.get('name');
|
|
|
|
} else {
|
|
|
|
return this.model.get('name');
|
|
|
|
}
|
2018-01-05 13:33:28 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-04 19:53:33 +02:00
|
|
|
|
2018-01-05 13:33:28 +01:00
|
|
|
_converse.RoomsListView = Backbone.OrderedListView.extend({
|
2017-04-29 13:30:47 +02:00
|
|
|
tagName: 'div',
|
2018-02-15 15:50:55 +01:00
|
|
|
className: 'open-rooms-list list-container rooms-list-container',
|
2017-04-29 13:30:47 +02:00
|
|
|
events: {
|
2017-08-09 15:50:24 +02:00
|
|
|
'click .add-bookmark': 'addBookmark',
|
2017-04-29 13:30:47 +02:00
|
|
|
'click .close-room': 'closeRoom',
|
2018-07-04 10:46:52 +02:00
|
|
|
'click .list-toggle': 'toggleRoomsList',
|
2017-08-09 15:50:24 +02:00
|
|
|
'click .remove-bookmark': 'removeBookmark',
|
2018-03-05 16:45:02 +01:00
|
|
|
'click .open-room': 'openRoom',
|
2017-04-29 13:30:47 +02:00
|
|
|
},
|
2018-01-05 13:33:28 +01:00
|
|
|
listSelector: '.rooms-list',
|
|
|
|
ItemView: _converse.RoomsListElementView,
|
|
|
|
subviewIndex: 'jid',
|
2017-04-29 13:30:47 +02:00
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize () {
|
2018-01-05 13:33:28 +01:00
|
|
|
Backbone.OrderedListView.prototype.initialize.apply(this, arguments);
|
2017-07-14 10:01:00 +02:00
|
|
|
|
2018-01-05 13:33:28 +01:00
|
|
|
this.model.on('add', this.showOrHide, this);
|
|
|
|
this.model.on('remove', this.showOrHide, this);
|
2017-04-29 13:30:47 +02:00
|
|
|
|
2018-08-23 09:41:39 +02:00
|
|
|
const storage = _converse.config.get('storage'),
|
2018-08-22 22:20:15 +02:00
|
|
|
id = b64_sha1(`converse.roomslist${_converse.bare_jid}`);
|
|
|
|
|
|
|
|
this.list_model = new _converse.RoomsList({'id': id});
|
|
|
|
this.list_model.browserStorage = new Backbone.BrowserStorage[storage](id);
|
2017-04-29 13:30:47 +02:00
|
|
|
this.list_model.fetch();
|
|
|
|
this.render();
|
2018-01-05 13:33:28 +01:00
|
|
|
this.sortAndPositionAllItems();
|
2017-04-29 13:30:47 +02:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
render () {
|
2018-01-15 21:59:39 +01:00
|
|
|
this.el.innerHTML = tpl_rooms_list({
|
2017-04-29 13:30:47 +02:00
|
|
|
'toggle_state': this.list_model.get('toggle-state'),
|
2018-07-02 15:54:51 +02:00
|
|
|
'desc_rooms': __('Click to toggle the list of open groupchats'),
|
|
|
|
'label_rooms': __('Open Groupchats'),
|
2018-03-17 12:42:17 +01:00
|
|
|
'_converse': _converse
|
2017-06-12 20:30:58 +02:00
|
|
|
});
|
2017-04-29 13:30:47 +02:00
|
|
|
if (this.list_model.get('toggle-state') !== _converse.OPENED) {
|
2017-07-15 11:03:22 +02:00
|
|
|
this.el.querySelector('.open-rooms-list').classList.add('collapsed');
|
2017-04-29 13:30:47 +02:00
|
|
|
}
|
2018-01-05 13:33:28 +01:00
|
|
|
this.showOrHide();
|
|
|
|
this.insertIntoControlBox();
|
|
|
|
return this;
|
|
|
|
},
|
2017-04-29 13:30:47 +02:00
|
|
|
|
2018-01-05 13:33:28 +01:00
|
|
|
insertIntoControlBox () {
|
|
|
|
const controlboxview = _converse.chatboxviews.get('controlbox');
|
2018-05-23 04:22:47 +02:00
|
|
|
if (!_.isUndefined(controlboxview) && !u.rootContains(_converse.root, this.el)) {
|
2018-02-15 15:50:55 +01:00
|
|
|
const el = controlboxview.el.querySelector('.open-rooms-list');
|
|
|
|
if (!_.isNull(el)) {
|
|
|
|
el.parentNode.replaceChild(this.el, el);
|
2017-04-29 13:30:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
hide () {
|
2018-01-05 13:33:28 +01:00
|
|
|
u.hideElement(this.el);
|
2017-04-29 13:30:47 +02:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
show () {
|
2018-01-05 13:33:28 +01:00
|
|
|
u.showElement(this.el);
|
2017-04-29 13:30:47 +02:00
|
|
|
},
|
|
|
|
|
2018-02-21 22:40:51 +01:00
|
|
|
openRoom (ev) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
_converse.api.rooms.open(jid, data);
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
closeRoom (ev) {
|
2017-04-29 13:30:47 +02:00
|
|
|
ev.preventDefault();
|
2017-07-13 23:55:40 +02:00
|
|
|
const name = ev.target.getAttribute('data-room-name');
|
|
|
|
const jid = ev.target.getAttribute('data-room-jid');
|
2018-07-02 15:54:51 +02:00
|
|
|
if (confirm(__("Are you sure you want to leave the groupchat %1$s?", name))) {
|
2018-04-08 19:44:53 +02:00
|
|
|
// TODO: replace with API call
|
|
|
|
_converse.chatboxviews.get(jid).close();
|
2017-04-29 13:30:47 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-01-05 13:33:28 +01:00
|
|
|
showOrHide (item) {
|
|
|
|
if (!this.model.models.length) {
|
|
|
|
u.hideElement(this.el);
|
|
|
|
} else {
|
|
|
|
u.showElement(this.el);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-08-09 15:50:24 +02:00
|
|
|
removeBookmark: _converse.removeBookmarkViaEvent,
|
|
|
|
addBookmark: _converse.addBookmarkViaEvent,
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
toggleRoomsList (ev) {
|
2017-04-29 13:30:47 +02:00
|
|
|
if (ev && ev.preventDefault) { ev.preventDefault(); }
|
2018-03-17 12:42:17 +01:00
|
|
|
const icon_el = ev.target.querySelector('.fa');
|
|
|
|
if (icon_el.classList.contains("fa-caret-down")) {
|
2018-06-06 11:04:23 +02:00
|
|
|
u.slideIn(this.el.querySelector('.open-rooms-list')).then(() => {
|
2017-07-14 10:01:00 +02:00
|
|
|
this.list_model.save({'toggle-state': _converse.CLOSED});
|
2018-03-17 12:42:17 +01:00
|
|
|
icon_el.classList.remove("fa-caret-down");
|
|
|
|
icon_el.classList.add("fa-caret-right");
|
2017-07-14 10:01:00 +02:00
|
|
|
});
|
2017-04-29 13:30:47 +02:00
|
|
|
} else {
|
2018-06-06 11:04:23 +02:00
|
|
|
u.slideOut(this.el.querySelector('.open-rooms-list')).then(() => {
|
2017-07-14 10:01:00 +02:00
|
|
|
this.list_model.save({'toggle-state': _converse.OPENED});
|
2018-03-17 12:42:17 +01:00
|
|
|
icon_el.classList.remove("fa-caret-right");
|
|
|
|
icon_el.classList.add("fa-caret-down");
|
2017-07-14 10:01:00 +02:00
|
|
|
});
|
2017-04-29 13:30:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
const initRoomsListView = function () {
|
2018-08-23 09:41:39 +02:00
|
|
|
const storage = _converse.config.get('storage'),
|
2018-08-22 22:20:15 +02:00
|
|
|
id = b64_sha1(`converse.open-rooms-{_converse.bare_jid}`),
|
|
|
|
model = new _converse.OpenRooms();
|
|
|
|
|
|
|
|
model.browserStorage = new Backbone.BrowserStorage[storage](id);
|
|
|
|
_converse.rooms_list_view = new _converse.RoomsListView({'model': model});
|
2017-04-29 13:30:47 +02:00
|
|
|
};
|
2017-07-05 11:40:54 +02:00
|
|
|
|
2018-02-22 12:46:10 +01:00
|
|
|
if (_converse.allow_bookmarks) {
|
|
|
|
u.onMultipleEvents([
|
|
|
|
{'object': _converse, 'event': 'chatBoxesFetched'},
|
|
|
|
{'object': _converse, 'event': 'roomsPanelRendered'},
|
|
|
|
{'object': _converse, 'event': 'bookmarksInitialized'}
|
|
|
|
], initRoomsListView);
|
|
|
|
} else {
|
|
|
|
u.onMultipleEvents([
|
|
|
|
{'object': _converse, 'event': 'chatBoxesFetched'},
|
|
|
|
{'object': _converse, 'event': 'roomsPanelRendered'}
|
|
|
|
], initRoomsListView);
|
|
|
|
}
|
2017-04-29 13:30:47 +02:00
|
|
|
|
2018-01-15 21:59:39 +01:00
|
|
|
_converse.api.listen.on('reconnected', initRoomsListView);
|
2017-04-29 13:30:47 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}));
|