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) {
|
2017-07-14 10:01:00 +02:00
|
|
|
define(["utils",
|
2017-04-29 13:30:47 +02:00
|
|
|
"converse-core",
|
|
|
|
"converse-muc",
|
2017-05-15 19:08:08 +02:00
|
|
|
"tpl!rooms_list",
|
|
|
|
"tpl!rooms_list_item"
|
2017-04-29 13:30:47 +02:00
|
|
|
], factory);
|
2017-07-14 10:01:00 +02:00
|
|
|
}(this, function (utils, converse, muc, tpl_rooms_list, tpl_rooms_list_item) {
|
2017-07-10 21:14:48 +02:00
|
|
|
const { Backbone, Promise, b64_sha1, sizzle, _ } = converse.env;
|
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.
|
|
|
|
*/
|
|
|
|
optional_dependencies: ["converse-bookmarks"],
|
|
|
|
|
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
|
|
|
|
|
|
|
_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
|
|
|
});
|
|
|
|
|
|
|
|
_converse.RoomsListView = Backbone.View.extend({
|
|
|
|
tagName: 'div',
|
2017-05-16 13:31:31 +02:00
|
|
|
className: 'open-rooms-list 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',
|
2017-08-09 15:50:24 +02:00
|
|
|
'click .open-rooms-toggle': 'toggleRoomsList',
|
|
|
|
'click .remove-bookmark': 'removeBookmark',
|
2017-04-29 13:30:47 +02:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize () {
|
2017-07-14 10:01:00 +02:00
|
|
|
this.toggleRoomsList = _.debounce(this.toggleRoomsList, 600, {'leading': true});
|
|
|
|
|
2017-04-29 13:30:47 +02:00
|
|
|
this.model.on('add', this.renderRoomsListElement, this);
|
|
|
|
this.model.on('change:bookmarked', this.renderRoomsListElement, this);
|
|
|
|
this.model.on('change:name', this.renderRoomsListElement, this);
|
2017-05-15 19:08:08 +02:00
|
|
|
this.model.on('change:num_unread', this.renderRoomsListElement, this);
|
2017-05-24 08:40:09 +02:00
|
|
|
this.model.on('change:num_unread_general', this.renderRoomsListElement, this);
|
2017-04-29 13:30:47 +02:00
|
|
|
this.model.on('remove', this.removeRoomsListElement, this);
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
const cachekey = `converse.roomslist${_converse.bare_jid}`;
|
2017-04-29 13:30:47 +02:00
|
|
|
this.list_model = new _converse.RoomsList();
|
|
|
|
this.list_model.id = cachekey;
|
|
|
|
this.list_model.browserStorage = new Backbone.BrowserStorage[_converse.storage](
|
|
|
|
b64_sha1(cachekey)
|
|
|
|
);
|
|
|
|
this.list_model.fetch();
|
|
|
|
this.render();
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
render () {
|
2017-04-29 13:30:47 +02:00
|
|
|
this.el.innerHTML =
|
|
|
|
tpl_rooms_list({
|
|
|
|
'toggle_state': this.list_model.get('toggle-state'),
|
|
|
|
'desc_rooms': __('Click to toggle the rooms list'),
|
|
|
|
'label_rooms': __('Open Rooms')
|
2017-06-12 20:30:58 +02:00
|
|
|
});
|
2017-04-29 13:30:47 +02:00
|
|
|
this.hide();
|
|
|
|
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
|
|
|
}
|
|
|
|
this.model.each(this.renderRoomsListElement.bind(this));
|
2017-07-10 17:46:22 +02:00
|
|
|
const controlboxview = _converse.chatboxviews.get('controlbox');
|
2017-04-29 13:30:47 +02:00
|
|
|
|
|
|
|
if (!_.isUndefined(controlboxview) &&
|
|
|
|
!document.body.contains(this.el)) {
|
2017-07-10 17:46:22 +02:00
|
|
|
const container = controlboxview.el.querySelector('#chatrooms');
|
2017-04-29 13:30:47 +02:00
|
|
|
if (!_.isNull(container)) {
|
|
|
|
container.insertBefore(this.el, container.firstChild);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this.el;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
hide () {
|
2017-04-29 13:30:47 +02:00
|
|
|
this.el.classList.add('hidden');
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
show () {
|
2017-04-29 13:30:47 +02:00
|
|
|
this.el.classList.remove('hidden');
|
|
|
|
},
|
|
|
|
|
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');
|
2017-09-24 20:36:39 +02:00
|
|
|
if (confirm(__("Are you sure you want to leave the room \"%1$s\"?", name))) {
|
2017-04-29 13:30:47 +02:00
|
|
|
_converse.chatboxviews.get(jid).leave();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
renderRoomsListElement (item) {
|
2017-04-29 13:30:47 +02:00
|
|
|
if (item.get('type') !== 'chatroom') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.removeRoomsListElement(item);
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
let name, bookmark;
|
2017-04-29 13:30:47 +02:00
|
|
|
if (item.get('bookmarked')) {
|
|
|
|
bookmark = _.head(_converse.bookmarksview.model.where({'jid': item.get('jid')}));
|
|
|
|
name = bookmark.get('name');
|
|
|
|
} else {
|
|
|
|
name = item.get('name');
|
|
|
|
}
|
2017-07-10 17:46:22 +02:00
|
|
|
const div = document.createElement('div');
|
2017-05-15 19:08:08 +02:00
|
|
|
div.innerHTML = tpl_rooms_list_item(_.extend(item.toJSON(), {
|
2017-07-19 09:28:44 +02:00
|
|
|
'allow_bookmarks': _converse.allow_bookmarks,
|
2017-04-29 13:30:47 +02:00
|
|
|
'info_leave_room': __('Leave this room'),
|
|
|
|
'info_remove_bookmark': __('Unbookmark this room'),
|
2017-08-09 15:50:24 +02:00
|
|
|
'info_add_bookmark': __('Bookmark this room'),
|
2017-04-29 13:30:47 +02:00
|
|
|
'info_title': __('Show more information on this room'),
|
|
|
|
'name': name,
|
|
|
|
'open_title': __('Click to open this room')
|
|
|
|
}));
|
|
|
|
this.el.querySelector('.open-rooms-list').appendChild(div.firstChild);
|
|
|
|
this.show();
|
|
|
|
},
|
|
|
|
|
2017-08-09 15:50:24 +02:00
|
|
|
removeBookmark: _converse.removeBookmarkViaEvent,
|
|
|
|
addBookmark: _converse.addBookmarkViaEvent,
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
removeRoomsListElement (item) {
|
|
|
|
const list_el = this.el.querySelector('.open-rooms-list');
|
|
|
|
const el = _.head(sizzle(`.available-chatroom[data-room-jid="${item.get('jid')}"]`, list_el));
|
2017-04-29 13:30:47 +02:00
|
|
|
if (el) {
|
|
|
|
list_el.removeChild(el);
|
|
|
|
}
|
|
|
|
if (list_el.childElementCount === 0) {
|
|
|
|
this.hide();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
toggleRoomsList (ev) {
|
2017-04-29 13:30:47 +02:00
|
|
|
if (ev && ev.preventDefault) { ev.preventDefault(); }
|
2017-07-10 17:46:22 +02:00
|
|
|
const el = ev.target;
|
2017-04-29 13:30:47 +02:00
|
|
|
if (el.classList.contains("icon-opened")) {
|
2017-07-15 11:03:22 +02:00
|
|
|
utils.slideIn(this.el.querySelector('.open-rooms-list')).then(() => {
|
2017-07-14 10:01:00 +02:00
|
|
|
this.list_model.save({'toggle-state': _converse.CLOSED});
|
|
|
|
el.classList.remove("icon-opened");
|
|
|
|
el.classList.add("icon-closed");
|
|
|
|
});
|
2017-04-29 13:30:47 +02:00
|
|
|
} else {
|
2017-07-15 11:03:22 +02:00
|
|
|
utils.slideOut(this.el.querySelector('.open-rooms-list')).then(() => {
|
2017-07-14 10:01:00 +02:00
|
|
|
this.list_model.save({'toggle-state': _converse.OPENED});
|
|
|
|
el.classList.remove("icon-closed");
|
|
|
|
el.classList.add("icon-opened");
|
|
|
|
});
|
2017-04-29 13:30:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
const initRoomsListView = function () {
|
2017-04-29 13:30:47 +02:00
|
|
|
_converse.rooms_list_view = new _converse.RoomsListView(
|
|
|
|
{'model': _converse.chatboxes}
|
|
|
|
);
|
|
|
|
};
|
2017-07-05 11:40:54 +02:00
|
|
|
|
2017-07-10 21:14:48 +02:00
|
|
|
Promise.all([
|
|
|
|
_converse.api.waitUntil('chatBoxesFetched'),
|
|
|
|
_converse.api.waitUntil('roomsPanelRendered')
|
|
|
|
]).then(() => {
|
|
|
|
if (_converse.allow_bookmarks) {
|
|
|
|
_converse.api.waitUntil('bookmarksInitialized').then(
|
|
|
|
initRoomsListView
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
initRoomsListView();
|
|
|
|
}
|
|
|
|
});
|
2017-04-29 13:30:47 +02:00
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
const afterReconnection = function () {
|
2017-04-29 13:30:47 +02:00
|
|
|
if (_.isUndefined(_converse.rooms_list_view)) {
|
|
|
|
initRoomsListView();
|
|
|
|
} else {
|
|
|
|
_converse.rooms_list_view.render();
|
|
|
|
}
|
|
|
|
};
|
2017-05-15 19:08:08 +02:00
|
|
|
_converse.api.listen.on('reconnected', afterReconnection);
|
2017-04-29 13:30:47 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}));
|