2019-07-11 10:48:52 +02:00
|
|
|
/**
|
|
|
|
* @module converse-bookmark-views
|
2020-01-26 16:21:20 +01:00
|
|
|
* @description Converse.js plugin which adds views for XEP-0048 bookmarks
|
|
|
|
* @copyright 2020, the Converse.js contributors
|
|
|
|
* @license Mozilla Public License (MPLv2)
|
2016-06-27 23:50:38 +02:00
|
|
|
*/
|
2019-07-11 12:30:14 +02:00
|
|
|
import "@converse/headless/converse-muc";
|
2020-05-18 10:54:37 +02:00
|
|
|
import { _converse, api, converse } from "@converse/headless/converse-core";
|
2020-01-26 16:35:34 +01:00
|
|
|
import tpl_bookmarks_list from "templates/bookmarks_list.js"
|
|
|
|
import tpl_muc_bookmark_form from "templates/muc_bookmark_form.js";
|
2020-06-08 16:08:50 +02:00
|
|
|
import { Model } from '@converse/skeletor/src/model.js';
|
|
|
|
import { View } from '@converse/skeletor/src/view.js';
|
2020-09-10 07:33:00 +02:00
|
|
|
import { __ } from './i18n';
|
2020-06-08 16:08:50 +02:00
|
|
|
import { invokeMap } from 'lodash-es';
|
2018-10-23 03:41:38 +02:00
|
|
|
|
2020-03-24 12:26:34 +01:00
|
|
|
const { 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"],
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Configuration values for this plugin
|
|
|
|
// ====================================
|
|
|
|
// Refer to docs/source/configuration.rst for explanations of these
|
|
|
|
// configuration settings.
|
2020-06-03 09:17:13 +02:00
|
|
|
api.settings.extend({
|
2019-05-24 13:52:15 +02:00
|
|
|
hide_open_bookmarks: 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))) {
|
2020-03-24 12:26:34 +01:00
|
|
|
invokeMap(_converse.bookmarks.where({'jid': jid}), Model.prototype.destroy);
|
2019-05-24 13:52:15 +02:00
|
|
|
}
|
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');
|
2020-03-31 13:15:57 +02:00
|
|
|
api.rooms.open(jid, {'bring_to_foreground': true});
|
2019-05-24 13:52:15 +02:00
|
|
|
_converse.chatboxviews.get(jid).renderBookmarkForm();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const bookmarkableChatRoomView = {
|
2020-01-26 16:35:34 +01:00
|
|
|
/**
|
|
|
|
* Set whether the groupchat is bookmarked or not.
|
|
|
|
* @private
|
|
|
|
*/
|
2018-10-23 03:41:38 +02:00
|
|
|
setBookmarkState () {
|
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) {
|
2020-06-03 10:48:27 +02:00
|
|
|
ev?.preventDefault();
|
2018-10-23 03:41:38 +02:00
|
|
|
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
|
|
|
|
2020-01-30 12:57:11 +01:00
|
|
|
_converse.MUCBookmarkForm = View.extend({
|
2020-01-26 16:35:34 +01:00
|
|
|
className: 'muc-bookmark-form chatroom-form-container',
|
2019-04-24 11:03:27 +02:00
|
|
|
|
|
|
|
initialize (attrs) {
|
|
|
|
this.chatroomview = attrs.chatroomview;
|
|
|
|
this.render();
|
|
|
|
},
|
|
|
|
|
|
|
|
toHTML () {
|
2020-01-26 16:35:34 +01:00
|
|
|
return tpl_muc_bookmark_form(Object.assign(
|
|
|
|
this.model.toJSON(), {
|
|
|
|
'onCancel': ev => this.closeBookmarkForm(ev),
|
|
|
|
'onSubmit': ev => this.onBookmarkFormSubmitted(ev)
|
|
|
|
}
|
|
|
|
));
|
2019-04-24 11:03:27 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
onBookmarkFormSubmitted (ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
_converse.bookmarks.createBookmark({
|
|
|
|
'jid': this.model.get('jid'),
|
2020-03-24 12:26:34 +01:00
|
|
|
'autojoin': ev.target.querySelector('input[name="autojoin"]')?.checked || false,
|
|
|
|
'name': ev.target.querySelector('input[name=name]')?.value,
|
|
|
|
'nick': ev.target.querySelector('input[name=nick]')?.value
|
2019-04-24 11:03:27 +02:00
|
|
|
});
|
|
|
|
this.closeBookmarkForm(ev);
|
|
|
|
},
|
|
|
|
|
|
|
|
closeBookmarkForm (ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
this.chatroomview.closeForm();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
|
2020-01-30 12:57:11 +01:00
|
|
|
_converse.BookmarksView = View.extend({
|
2020-01-26 16:35:34 +01:00
|
|
|
tagName: 'span',
|
2018-10-23 03:41:38 +02:00
|
|
|
|
|
|
|
initialize () {
|
2019-10-13 17:44:29 +02:00
|
|
|
this.listenTo(this.model, 'add', this.render);
|
|
|
|
this.listenTo(this.model, 'remove', this.render);
|
2018-10-23 03:41:38 +02:00
|
|
|
|
2019-10-13 17:44:29 +02:00
|
|
|
this.listenTo(_converse.chatboxes, 'add', this.render);
|
|
|
|
this.listenTo(_converse.chatboxes, 'remove', this.render);
|
2018-10-23 03:41:38 +02:00
|
|
|
|
2019-10-13 17:44:29 +02:00
|
|
|
const id = `converse.room-bookmarks${_converse.bare_jid}-list-model`;
|
|
|
|
this.list_model = new _converse.BookmarksList({id});
|
|
|
|
this.list_model.browserStorage = _converse.createStore(id);
|
2019-03-27 08:34:58 +01:00
|
|
|
|
|
|
|
const render = () => {
|
|
|
|
this.render();
|
2019-10-13 17:44:29 +02:00
|
|
|
this.insertIntoControlBox();
|
2019-03-27 08:34:58 +01:00
|
|
|
}
|
|
|
|
this.list_model.fetch({'success': render, 'error': render});
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
|
|
|
|
2019-10-13 17:44:29 +02:00
|
|
|
toHTML () {
|
2020-03-31 13:15:57 +02:00
|
|
|
const is_hidden = b => !!(api.settings.get('hide_open_bookmarks') && _converse.chatboxes.get(b.get('jid')));
|
2019-10-13 17:44:29 +02:00
|
|
|
return tpl_bookmarks_list({
|
|
|
|
'_converse': _converse,
|
|
|
|
'bookmarks': this.model,
|
2020-01-26 17:55:24 +01:00
|
|
|
'hidden': this.model.getUnopenedBookmarks().length && true,
|
|
|
|
'is_hidden': is_hidden,
|
|
|
|
'openRoom': ev => this.openRoom(ev),
|
|
|
|
'removeBookmark': ev => this.removeBookmark(ev),
|
|
|
|
'toggleBookmarksList': ev => this.toggleBookmarksList(ev),
|
|
|
|
'toggle_state': this.list_model.get('toggle-state')
|
2018-10-23 03:41:38 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
insertIntoControlBox () {
|
|
|
|
const controlboxview = _converse.chatboxviews.get('controlbox');
|
2019-07-29 10:19:05 +02:00
|
|
|
if (controlboxview !== undefined && !u.rootContains(_converse.root, this.el)) {
|
2019-10-16 12:11:59 +02:00
|
|
|
const el = controlboxview.el.querySelector('.list-container--bookmarks');
|
|
|
|
el && el.parentNode.replaceChild(this.el, el);
|
2018-10-23 03:41:38 +02: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
|
|
|
|
}
|
2020-03-31 13:15:57 +02:00
|
|
|
api.rooms.open(jid, data, true);
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
removeBookmark: _converse.removeBookmarkViaEvent,
|
2018-02-22 13:55:17 +01:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
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 () {
|
2020-03-31 13:15:57 +02:00
|
|
|
await api.waitUntil('roomsPanelRendered');
|
2019-05-21 11:12:34 +02:00
|
|
|
_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
|
|
|
*/
|
2020-03-31 13:15:57 +02:00
|
|
|
api.trigger('bookmarkViewsInitialized');
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
2018-05-18 12:44:27 +02:00
|
|
|
|
2020-04-06 17:21:38 +02:00
|
|
|
api.listen.on('getHeadingButtons', (view, buttons) => {
|
|
|
|
if (_converse.allow_bookmarks && view.model.get('type') === _converse.CHATROOMS_TYPE) {
|
|
|
|
const bookmarked = view.model.get('bookmarked');
|
|
|
|
const data = {
|
|
|
|
'i18n_title': bookmarked ? __('Unbookmark this groupchat') : __('Bookmark this groupchat'),
|
|
|
|
'i18n_text': bookmarked ? __('Unbookmark') : __('Bookmark'),
|
|
|
|
'handler': ev => view.toggleBookmark(ev),
|
|
|
|
'a_class': 'toggle-bookmark',
|
|
|
|
'icon_class': 'fa-bookmark',
|
|
|
|
'name': 'bookmark'
|
|
|
|
}
|
|
|
|
const names = buttons.map(t => t.name);
|
|
|
|
const idx = names.indexOf('details');
|
|
|
|
const data_promise = _converse.checkBookmarksSupport().then(s => s ? data : '');
|
|
|
|
return idx > -1 ? [...buttons.slice(0, idx), data_promise, ...buttons.slice(idx)] : [data_promise, ...buttons];
|
|
|
|
}
|
|
|
|
return buttons;
|
|
|
|
});
|
|
|
|
|
2020-03-31 13:15:57 +02:00
|
|
|
api.listen.on('bookmarksInitialized', initBookmarkViews);
|
|
|
|
api.listen.on('chatRoomViewInitialized', view => view.setBookmarkState());
|
2019-05-24 13:52:15 +02:00
|
|
|
/************************ END Event Handlers ************************/
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
});
|