2016-06-27 23:50:38 +02:00
|
|
|
// Converse.js (A browser based XMPP chat client)
|
2019-03-04 17:47:45 +01:00
|
|
|
// https://conversejs.org
|
2016-06-27 23:50:38 +02:00
|
|
|
//
|
2019-05-21 11:12:34 +02:00
|
|
|
// Copyright (c) 2019, Jan-Carel Brand <jc@opkode.com>
|
2016-06-27 23:50:38 +02:00
|
|
|
// Licensed under the Mozilla Public License (MPLv2)
|
|
|
|
//
|
2019-07-11 10:48:52 +02:00
|
|
|
/**
|
|
|
|
* @module converse-bookmark-views
|
|
|
|
* @description
|
|
|
|
* Converse.js plugin which adds views for XEP-0048 bookmarks
|
2016-06-27 23:50:38 +02:00
|
|
|
*/
|
2019-05-23 14:26:20 +02:00
|
|
|
import "backbone.nativeview";
|
2019-07-11 12:30:14 +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_bookmark from "templates/bookmark.html";
|
|
|
|
import tpl_bookmarks_list from "templates/bookmarks_list.html"
|
|
|
|
import tpl_chatroom_bookmark_form from "templates/chatroom_bookmark_form.html";
|
|
|
|
import tpl_chatroom_bookmark_toggle from "templates/chatroom_bookmark_toggle.html";
|
|
|
|
|
2019-07-11 12:30:14 +02:00
|
|
|
const { Backbone, Strophe, _ } = converse.env;
|
2018-10-23 03:41:38 +02:00
|
|
|
const u = converse.env.utils;
|
|
|
|
|
|
|
|
|
2019-05-21 11:12:34 +02:00
|
|
|
converse.plugins.add('converse-bookmark-views', {
|
2018-10-23 03:41:38 +02:00
|
|
|
|
|
|
|
/* Plugin dependencies are other plugins which might be
|
|
|
|
* overridden or relied upon, and therefore need to be loaded before
|
|
|
|
* this plugin.
|
|
|
|
*
|
|
|
|
* If the setting "strict_plugin_dependencies" is set to true,
|
|
|
|
* an error will be raised if the plugin is not found. By default it's
|
|
|
|
* false, which means these plugins are only loaded opportunistically.
|
|
|
|
*
|
|
|
|
* NB: These plugins need to have already been loaded via require.js.
|
|
|
|
*/
|
|
|
|
dependencies: ["converse-chatboxes", "converse-muc", "converse-muc-views"],
|
|
|
|
|
|
|
|
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.
|
|
|
|
ChatRoomView: {
|
|
|
|
events: {
|
|
|
|
'click .toggle-bookmark': 'toggleBookmark'
|
|
|
|
},
|
2019-05-24 13:52:15 +02:00
|
|
|
async renderHeading () {
|
|
|
|
this.__super__.renderHeading.apply(this, arguments);
|
|
|
|
const { _converse } = this.__super__;
|
|
|
|
if (_converse.allow_bookmarks) {
|
|
|
|
const supported = await _converse.checkBookmarksSupport();
|
|
|
|
if (supported) {
|
|
|
|
this.renderBookmarkToggle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2018-10-23 03:41:38 +02:00
|
|
|
|
2019-05-24 13:52:15 +02:00
|
|
|
initialize () {
|
|
|
|
/* The initialize function gets called as soon as the plugin is
|
|
|
|
* loaded by converse.js's plugin machinery.
|
|
|
|
*/
|
|
|
|
const { _converse } = this,
|
|
|
|
{ __ } = _converse;
|
|
|
|
|
|
|
|
// Configuration values for this plugin
|
|
|
|
// ====================================
|
|
|
|
// Refer to docs/source/configuration.rst for explanations of these
|
|
|
|
// configuration settings.
|
|
|
|
_converse.api.settings.update({
|
|
|
|
hide_open_bookmarks: true,
|
|
|
|
muc_respect_autojoin: true
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
Object.assign(_converse, {
|
|
|
|
|
|
|
|
removeBookmarkViaEvent (ev) {
|
|
|
|
/* Remove a bookmark as determined by the passed in
|
|
|
|
* event.
|
|
|
|
*/
|
|
|
|
ev.preventDefault();
|
|
|
|
const name = ev.target.getAttribute('data-bookmark-name');
|
|
|
|
const jid = ev.target.getAttribute('data-room-jid');
|
|
|
|
if (confirm(__("Are you sure you want to remove the bookmark \"%1$s\"?", name))) {
|
|
|
|
_.invokeMap(_converse.bookmarks.where({'jid': jid}), Backbone.Model.prototype.destroy);
|
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
|
|
|
|
2019-05-24 13:52:15 +02:00
|
|
|
addBookmarkViaEvent (ev) {
|
|
|
|
/* Add a bookmark as determined by the passed in
|
|
|
|
* event.
|
|
|
|
*/
|
|
|
|
ev.preventDefault();
|
|
|
|
const jid = ev.target.getAttribute('data-room-jid');
|
2019-10-09 16:01:38 +02:00
|
|
|
_converse.api.rooms.open(jid, {'bring_to_foreground': true});
|
2019-05-24 13:52:15 +02:00
|
|
|
_converse.chatboxviews.get(jid).renderBookmarkForm();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const bookmarkableChatRoomView = {
|
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
renderBookmarkToggle () {
|
|
|
|
if (this.el.querySelector('.chat-head .toggle-bookmark')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const { _converse } = this.__super__,
|
|
|
|
{ __ } = _converse;
|
|
|
|
|
|
|
|
const bookmark_button = tpl_chatroom_bookmark_toggle(
|
|
|
|
_.assignIn(this.model.toJSON(), {
|
2019-02-26 12:08:48 +01:00
|
|
|
'info_toggle_bookmark': this.model.get('bookmarked') ?
|
|
|
|
__('Unbookmark this groupchat') :
|
|
|
|
__('Bookmark this groupchat'),
|
|
|
|
'bookmarked': this.model.get('bookmarked')
|
2018-10-23 03:41:38 +02:00
|
|
|
}));
|
2019-04-05 14:04:00 +02:00
|
|
|
|
|
|
|
const buttons_row = this.el.querySelector('.chatbox-buttons')
|
|
|
|
const close_button = buttons_row.querySelector('.close-chatbox-button');
|
|
|
|
if (close_button) {
|
|
|
|
close_button.insertAdjacentHTML('afterend', bookmark_button);
|
|
|
|
} else {
|
|
|
|
buttons_row.insertAdjacentHTML('beforeEnd', bookmark_button);
|
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
setBookmarkState () {
|
|
|
|
/* Set whether the groupchat is bookmarked or not.
|
|
|
|
*/
|
2019-07-29 10:19:05 +02:00
|
|
|
if (_converse.bookmarks !== undefined) {
|
2017-07-10 17:46:22 +02:00
|
|
|
const models = _converse.bookmarks.where({'jid': this.model.get('jid')});
|
2016-09-22 13:02:16 +02:00
|
|
|
if (!models.length) {
|
2018-10-23 03:41:38 +02:00
|
|
|
this.model.save('bookmarked', false);
|
2016-06-27 23:50:38 +02:00
|
|
|
} else {
|
2018-10-23 03:41:38 +02:00
|
|
|
this.model.save('bookmarked', true);
|
2016-06-27 23:50:38 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
renderBookmarkForm () {
|
2019-04-24 11:03:27 +02:00
|
|
|
this.hideChatRoomContents();
|
|
|
|
if (!this.bookmark_form) {
|
|
|
|
this.bookmark_form = new _converse.MUCBookmarkForm({
|
|
|
|
'model': this.model,
|
|
|
|
'chatroomview': this
|
|
|
|
});
|
|
|
|
const container_el = this.el.querySelector('.chatroom-body');
|
|
|
|
container_el.insertAdjacentElement('beforeend', this.bookmark_form.el);
|
|
|
|
}
|
|
|
|
u.showElement(this.bookmark_form.el);
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
2017-08-09 15:50:24 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
toggleBookmark (ev) {
|
|
|
|
if (ev) {
|
2017-08-09 15:50:24 +02:00
|
|
|
ev.preventDefault();
|
2018-10-23 03:41:38 +02:00
|
|
|
ev.stopPropagation();
|
|
|
|
}
|
|
|
|
const models = _converse.bookmarks.where({'jid': this.model.get('jid')});
|
|
|
|
if (!models.length) {
|
|
|
|
this.renderBookmarkForm();
|
|
|
|
} else {
|
2019-05-21 11:12:34 +02:00
|
|
|
models.forEach(model => model.destroy());
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-24 13:52:15 +02:00
|
|
|
Object.assign(_converse.ChatRoomView.prototype, bookmarkableChatRoomView);
|
2018-10-23 03:41:38 +02:00
|
|
|
|
2016-10-06 09:02:29 +02:00
|
|
|
|
2019-04-24 11:03:27 +02:00
|
|
|
_converse.MUCBookmarkForm = Backbone.VDOMView.extend({
|
|
|
|
className: 'muc-bookmark-form',
|
|
|
|
|
|
|
|
events: {
|
|
|
|
'submit form': 'onBookmarkFormSubmitted',
|
|
|
|
'click .button-cancel': 'closeBookmarkForm'
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize (attrs) {
|
|
|
|
this.chatroomview = attrs.chatroomview;
|
|
|
|
this.render();
|
|
|
|
},
|
|
|
|
|
|
|
|
toHTML () {
|
|
|
|
return tpl_chatroom_bookmark_form({
|
|
|
|
'default_nick': this.model.get('nick'),
|
|
|
|
'heading': __('Bookmark this groupchat'),
|
|
|
|
'label_autojoin': __('Would you like this groupchat to be automatically joined upon startup?'),
|
|
|
|
'label_cancel': __('Cancel'),
|
|
|
|
'label_name': __('The name for this bookmark:'),
|
|
|
|
'label_nick': __('What should your nickname for this groupchat be?'),
|
|
|
|
'label_submit': __('Save'),
|
|
|
|
'name': this.model.get('name')
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
onBookmarkFormSubmitted (ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
_converse.bookmarks.createBookmark({
|
|
|
|
'jid': this.model.get('jid'),
|
|
|
|
'autojoin': _.get(ev.target.querySelector('input[name="autojoin"]'), 'checked') || false,
|
|
|
|
'name': _.get(ev.target.querySelector('input[name=name]'), 'value'),
|
|
|
|
'nick': _.get(ev.target.querySelector('input[name=nick]'), 'value')
|
|
|
|
});
|
|
|
|
this.closeBookmarkForm(ev);
|
|
|
|
},
|
|
|
|
|
|
|
|
closeBookmarkForm (ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
this.chatroomview.closeForm();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
|
|
|
|
_converse.BookmarkView = Backbone.VDOMView.extend({
|
|
|
|
toHTML () {
|
|
|
|
return tpl_bookmark({
|
|
|
|
'hidden': _converse.hide_open_bookmarks &&
|
|
|
|
_converse.chatboxes.where({'jid': this.model.get('jid')}).length,
|
|
|
|
'bookmarked': true,
|
|
|
|
'info_leave_room': __('Leave this groupchat'),
|
|
|
|
'info_remove': __('Remove this bookmark'),
|
|
|
|
'info_remove_bookmark': __('Unbookmark this groupchat'),
|
|
|
|
'info_title': __('Show more information on this groupchat'),
|
|
|
|
'jid': this.model.get('jid'),
|
|
|
|
'name': Strophe.xmlunescape(this.model.get('name')),
|
|
|
|
'open_title': __('Click to open this groupchat')
|
|
|
|
});
|
2018-02-22 13:55:17 +01:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
});
|
|
|
|
|
2019-05-21 11:12:34 +02:00
|
|
|
|
2019-05-23 14:26:20 +02:00
|
|
|
_converse.BookmarksView = OrderedListView.extend({
|
2018-10-23 03:41:38 +02:00
|
|
|
tagName: 'div',
|
|
|
|
className: 'bookmarks-list list-container rooms-list-container',
|
|
|
|
events: {
|
|
|
|
'click .add-bookmark': 'addBookmark',
|
|
|
|
'click .bookmarks-toggle': 'toggleBookmarksList',
|
|
|
|
'click .remove-bookmark': 'removeBookmark',
|
|
|
|
'click .open-room': 'openRoom',
|
|
|
|
},
|
|
|
|
listSelector: '.rooms-list',
|
|
|
|
ItemView: _converse.BookmarkView,
|
|
|
|
subviewIndex: 'jid',
|
|
|
|
|
|
|
|
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
|
|
|
|
2019-09-06 14:34:59 +02:00
|
|
|
this.listenTo(_converse.chatboxes, 'add', this.renderBookmarkListElement);
|
|
|
|
this.listenTo(_converse.chatboxes, 'remove', this.renderBookmarkListElement);
|
2018-10-23 03:41:38 +02:00
|
|
|
|
|
|
|
const storage = _converse.config.get('storage'),
|
2019-02-18 19:06:11 +01:00
|
|
|
id = `converse.room-bookmarks${_converse.bare_jid}-list-model`;
|
2018-10-23 03:41:38 +02:00
|
|
|
this.list_model = new _converse.BookmarksList({'id': id});
|
2019-05-23 14:26:20 +02:00
|
|
|
this.list_model.browserStorage = new BrowserStorage[storage](id);
|
2019-03-27 08:34:58 +01:00
|
|
|
|
|
|
|
const render = () => {
|
|
|
|
this.render();
|
|
|
|
this.sortAndPositionAllItems();
|
|
|
|
}
|
|
|
|
this.list_model.fetch({'success': render, 'error': render});
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
render () {
|
|
|
|
this.el.innerHTML = tpl_bookmarks_list({
|
|
|
|
'toggle_state': this.list_model.get('toggle-state'),
|
|
|
|
'desc_bookmarks': __('Click to toggle the bookmarks list'),
|
|
|
|
'label_bookmarks': __('Bookmarks'),
|
|
|
|
'_converse': _converse
|
|
|
|
});
|
|
|
|
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('.bookmarks-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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2019-04-16 16:36:58 +02:00
|
|
|
_converse.api.rooms.open(jid, data, true);
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
removeBookmark: _converse.removeBookmarkViaEvent,
|
|
|
|
addBookmark: _converse.addBookmarkViaEvent,
|
2018-02-22 13:55:17 +01:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
renderBookmarkListElement (chatbox) {
|
|
|
|
const bookmarkview = this.get(chatbox.get('jid'));
|
2019-08-05 01:39:57 +02:00
|
|
|
if (bookmarkview) {
|
|
|
|
bookmarkview.render();
|
|
|
|
this.showOrHide();
|
2016-11-24 09:51:01 +01:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
|
|
|
|
2019-10-09 16:01:38 +02:00
|
|
|
showOrHide () {
|
2018-10-23 03:41:38 +02:00
|
|
|
if (_converse.hide_open_bookmarks) {
|
|
|
|
const bookmarks = this.model.filter((bookmark) =>
|
|
|
|
!_converse.chatboxes.get(bookmark.get('jid')));
|
|
|
|
if (!bookmarks.length) {
|
|
|
|
u.hideElement(this.el);
|
|
|
|
return;
|
2018-02-07 17:30:44 +01:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
if (this.model.models.length) {
|
|
|
|
u.showElement(this.el);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
toggleBookmarksList (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 (u.hasClass('fa-caret-down', icon_el)) {
|
|
|
|
u.slideIn(this.el.querySelector('.bookmarks'));
|
|
|
|
this.list_model.save({'toggle-state': _converse.CLOSED});
|
|
|
|
icon_el.classList.remove("fa-caret-down");
|
|
|
|
icon_el.classList.add("fa-caret-right");
|
|
|
|
} else {
|
|
|
|
icon_el.classList.remove("fa-caret-right");
|
|
|
|
icon_el.classList.add("fa-caret-down");
|
|
|
|
u.slideOut(this.el.querySelector('.bookmarks'));
|
|
|
|
this.list_model.save({'toggle-state': _converse.OPENED});
|
|
|
|
}
|
2018-02-22 12:46:10 +01:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
});
|
|
|
|
|
2019-05-24 13:52:15 +02:00
|
|
|
/************************ BEGIN Event Handlers ************************/
|
2019-05-21 11:12:34 +02:00
|
|
|
const initBookmarkViews = async function () {
|
|
|
|
await _converse.api.waitUntil('roomsPanelRendered');
|
|
|
|
_converse.bookmarksview = new _converse.BookmarksView({'model': _converse.bookmarks});
|
2019-03-29 15:47:23 +01:00
|
|
|
/**
|
|
|
|
* Triggered once the _converse.Bookmarks collection and _converse.BookmarksView view
|
|
|
|
* has been created and cached bookmarks have been fetched.
|
2019-05-21 11:12:34 +02:00
|
|
|
* @event _converse#bookmarkViewsInitialized
|
|
|
|
* @example _converse.api.listen.on('bookmarkViewsInitialized', () => { ... });
|
2019-03-29 15:47:23 +01:00
|
|
|
*/
|
2019-05-21 11:12:34 +02:00
|
|
|
_converse.api.trigger('bookmarkViewsInitialized');
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
2018-05-18 12:44:27 +02:00
|
|
|
|
2019-05-21 11:12:34 +02:00
|
|
|
_converse.api.listen.on('bookmarksInitialized', initBookmarkViews);
|
2019-05-24 13:52:15 +02:00
|
|
|
|
2019-05-29 12:57:54 +02:00
|
|
|
_converse.api.listen.on('chatRoomOpened', view => view.setBookmarkState());
|
2019-05-24 13:52:15 +02:00
|
|
|
/************************ END Event Handlers ************************/
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
});
|