2016-06-27 23:50:38 +02:00
|
|
|
// Converse.js (A browser based XMPP chat client)
|
|
|
|
// http://conversejs.org
|
|
|
|
//
|
2017-02-02 19:29:38 +01:00
|
|
|
// Copyright (c) 2012-2017, Jan-Carel Brand <jc@opkode.com>
|
2016-06-27 23:50:38 +02:00
|
|
|
// Licensed under the Mozilla Public License (MPLv2)
|
|
|
|
//
|
2017-04-21 15:52:16 +02:00
|
|
|
/*global define */
|
2016-06-27 23:50:38 +02:00
|
|
|
|
|
|
|
/* This is a Converse.js plugin which add support for bookmarks specified
|
|
|
|
* in XEP-0048.
|
|
|
|
*/
|
|
|
|
(function (root, factory) {
|
2018-01-10 14:26:35 +01:00
|
|
|
define(["converse-core",
|
2016-06-27 23:50:38 +02:00
|
|
|
"converse-muc",
|
2018-05-24 21:09:33 +02:00
|
|
|
"templates/chatroom_bookmark_form.html",
|
|
|
|
"templates/chatroom_bookmark_toggle.html",
|
|
|
|
"templates/bookmark.html",
|
|
|
|
"templates/bookmarks_list.html"
|
2016-06-27 23:50:38 +02:00
|
|
|
],
|
|
|
|
factory);
|
2016-10-06 09:02:29 +02:00
|
|
|
}(this, function (
|
2017-03-30 17:00:24 +02:00
|
|
|
converse,
|
|
|
|
muc,
|
2016-10-06 09:02:29 +02:00
|
|
|
tpl_chatroom_bookmark_form,
|
2016-12-02 18:41:05 +01:00
|
|
|
tpl_chatroom_bookmark_toggle,
|
2016-10-06 09:02:29 +02:00
|
|
|
tpl_bookmark,
|
|
|
|
tpl_bookmarks_list
|
|
|
|
) {
|
2016-06-27 23:50:38 +02:00
|
|
|
|
2017-07-10 21:14:48 +02:00
|
|
|
const { Backbone, Promise, Strophe, $iq, b64_sha1, sizzle, _ } = converse.env;
|
2018-01-10 14:26:35 +01:00
|
|
|
const u = converse.env.utils;
|
2016-06-27 23:50:38 +02:00
|
|
|
|
2016-12-20 11:42:20 +01:00
|
|
|
converse.plugins.add('converse-bookmarks', {
|
2017-12-17 16:01:45 +01:00
|
|
|
|
2018-01-10 14:26:35 +01: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.
|
|
|
|
*/
|
2018-02-22 11:49:59 +01:00
|
|
|
dependencies: ["converse-chatboxes", "converse-muc", "converse-muc-views"],
|
2017-12-17 16:01:45 +01:00
|
|
|
|
2016-06-27 23:50:38 +02:00
|
|
|
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.
|
|
|
|
//
|
|
|
|
// New functions which don't exist yet can also be added.
|
2016-09-22 13:02:16 +02:00
|
|
|
|
2016-06-27 23:50:38 +02:00
|
|
|
ChatRoomView: {
|
|
|
|
events: {
|
|
|
|
'click .toggle-bookmark': 'toggleBookmark'
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize () {
|
2016-09-22 11:30:06 +02:00
|
|
|
this.__super__.initialize.apply(this, arguments);
|
|
|
|
this.model.on('change:bookmarked', this.onBookmarked, this);
|
2016-09-22 14:03:42 +02:00
|
|
|
this.setBookmarkState();
|
2016-09-22 11:30:06 +02:00
|
|
|
},
|
|
|
|
|
2018-02-08 09:49:05 +01:00
|
|
|
renderBookmarkToggle () {
|
2017-07-10 17:46:22 +02:00
|
|
|
const { _converse } = this.__super__,
|
2018-02-07 17:30:44 +01:00
|
|
|
{ __ } = _converse;
|
2018-02-08 09:49:05 +01:00
|
|
|
const bookmark_button = tpl_chatroom_bookmark_toggle(
|
|
|
|
_.assignIn(this.model.toJSON(), {
|
2018-07-02 15:54:51 +02:00
|
|
|
info_toggle_bookmark: __('Bookmark this groupchat'),
|
2018-02-08 09:49:05 +01:00
|
|
|
bookmarked: this.model.get('bookmarked')
|
|
|
|
}));
|
|
|
|
const close_button = this.el.querySelector('.close-chatbox-button');
|
|
|
|
close_button.insertAdjacentHTML('afterend', bookmark_button);
|
|
|
|
},
|
2018-02-07 17:30:44 +01:00
|
|
|
|
2018-02-08 09:49:05 +01:00
|
|
|
renderHeading () {
|
|
|
|
this.__super__.renderHeading.apply(this, arguments);
|
|
|
|
const { _converse } = this.__super__;
|
2016-12-20 10:30:20 +01:00
|
|
|
if (_converse.allow_bookmarks) {
|
2018-02-22 13:55:17 +01:00
|
|
|
_converse.checkBookmarksSupport().then((supported) => {
|
|
|
|
if (supported) {
|
|
|
|
this.renderBookmarkToggle();
|
2018-02-07 17:30:44 +01:00
|
|
|
}
|
2018-02-08 09:49:05 +01:00
|
|
|
}).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
|
2016-11-24 09:51:01 +01:00
|
|
|
}
|
2016-06-27 23:50:38 +02:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
checkForReservedNick () {
|
2016-09-23 09:41:55 +02:00
|
|
|
/* Check if the user has a bookmark with a saved nickanme
|
2018-07-22 10:58:04 +02:00
|
|
|
* for this groupchat, and if so use it.
|
2016-09-23 09:41:55 +02:00
|
|
|
* Otherwise delegate to the super method.
|
|
|
|
*/
|
2017-07-10 17:46:22 +02:00
|
|
|
const { _converse } = this.__super__;
|
2016-12-20 10:30:20 +01:00
|
|
|
if (_.isUndefined(_converse.bookmarks) || !_converse.allow_bookmarks) {
|
2016-11-24 09:45:17 +01:00
|
|
|
return this.__super__.checkForReservedNick.apply(this, arguments);
|
2016-10-06 14:21:08 +02:00
|
|
|
}
|
2017-06-12 20:30:58 +02:00
|
|
|
const model = _converse.bookmarks.findWhere({'jid': this.model.get('jid')});
|
2016-09-23 09:41:55 +02:00
|
|
|
if (!_.isUndefined(model) && model.get('nick')) {
|
2017-02-16 11:17:38 +01:00
|
|
|
this.join(model.get('nick'));
|
2016-09-23 09:41:55 +02:00
|
|
|
} else {
|
2016-12-05 07:03:44 +01:00
|
|
|
return this.__super__.checkForReservedNick.apply(this, arguments);
|
2016-09-23 09:41:55 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
onBookmarked () {
|
2018-03-06 19:03:23 +01:00
|
|
|
const icon = this.el.querySelector('.toggle-bookmark');
|
2018-02-07 17:30:44 +01:00
|
|
|
if (_.isNull(icon)) {
|
|
|
|
return;
|
|
|
|
}
|
2016-09-22 14:03:42 +02:00
|
|
|
if (this.model.get('bookmarked')) {
|
2017-08-14 22:06:32 +02:00
|
|
|
icon.classList.add('button-on');
|
2016-09-22 14:03:42 +02:00
|
|
|
} else {
|
2017-08-14 22:06:32 +02:00
|
|
|
icon.classList.remove('button-on');
|
2016-09-22 14:03:42 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
setBookmarkState () {
|
2018-07-22 10:58:04 +02:00
|
|
|
/* Set whether the groupchat is bookmarked or not.
|
2016-10-17 10:34:09 +02:00
|
|
|
*/
|
2017-07-10 17:46:22 +02:00
|
|
|
const { _converse } = this.__super__;
|
2016-12-20 10:30:20 +01:00
|
|
|
if (!_.isUndefined(_converse.bookmarks)) {
|
2017-07-10 17:46:22 +02:00
|
|
|
const models = _converse.bookmarks.where({'jid': this.model.get('jid')});
|
2016-09-22 14:03:42 +02:00
|
|
|
if (!models.length) {
|
|
|
|
this.model.save('bookmarked', false);
|
|
|
|
} else {
|
|
|
|
this.model.save('bookmarked', true);
|
|
|
|
}
|
|
|
|
}
|
2016-09-22 11:30:06 +02:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
renderBookmarkForm () {
|
|
|
|
const { _converse } = this.__super__,
|
|
|
|
{ __ } = _converse,
|
2017-08-14 22:06:32 +02:00
|
|
|
body = this.el.querySelector('.chatroom-body');
|
|
|
|
|
|
|
|
_.each(body.children, function (child) {
|
|
|
|
child.classList.add('hidden');
|
|
|
|
});
|
2016-12-04 14:06:34 +01:00
|
|
|
// Remove any existing forms
|
2017-12-23 21:56:20 +01:00
|
|
|
_.each(body.querySelectorAll('.chatroom-form-container'), u.removeElement);
|
2017-12-06 14:59:01 +01:00
|
|
|
|
2017-08-14 22:06:32 +02:00
|
|
|
body.insertAdjacentHTML(
|
2018-02-07 21:59:45 +01:00
|
|
|
'beforeend',
|
2017-02-19 10:58:30 +01:00
|
|
|
tpl_chatroom_bookmark_form({
|
2018-07-02 15:54:51 +02:00
|
|
|
heading: __('Bookmark this groupchat'),
|
2016-06-27 23:50:38 +02:00
|
|
|
label_name: __('The name for this bookmark:'),
|
2018-07-02 15:54:51 +02:00
|
|
|
label_autojoin: __('Would you like this groupchat to be automatically joined upon startup?'),
|
|
|
|
label_nick: __('What should your nickname for this groupchat be?'),
|
2016-06-27 23:50:38 +02:00
|
|
|
default_nick: this.model.get('nick'),
|
|
|
|
label_submit: __('Save'),
|
|
|
|
label_cancel: __('Cancel')
|
2017-08-14 22:06:32 +02:00
|
|
|
})
|
|
|
|
);
|
2017-12-06 14:59:01 +01:00
|
|
|
const form = body.querySelector('form.chatroom-form');
|
2017-08-14 22:06:32 +02:00
|
|
|
form.addEventListener(
|
|
|
|
'submit',
|
|
|
|
this.onBookmarkFormSubmitted.bind(this)
|
|
|
|
);
|
|
|
|
form.querySelector('.button-cancel').addEventListener(
|
|
|
|
'click',
|
2017-12-06 14:59:01 +01:00
|
|
|
this.closeForm.bind(this)
|
2017-08-14 22:06:32 +02:00
|
|
|
);
|
2016-06-27 23:50:38 +02:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
onBookmarkFormSubmitted (ev) {
|
2016-06-27 23:50:38 +02:00
|
|
|
ev.preventDefault();
|
2017-07-10 17:46:22 +02:00
|
|
|
const { _converse } = this.__super__;
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.bookmarks.createBookmark({
|
2016-09-22 11:30:06 +02:00
|
|
|
'jid': this.model.get('jid'),
|
2017-12-23 21:56:20 +01:00
|
|
|
'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')
|
2016-06-27 23:50:38 +02:00
|
|
|
});
|
2017-12-23 21:56:20 +01:00
|
|
|
u.removeElement(this.el.querySelector('div.chatroom-form-container'));
|
|
|
|
this.renderAfterTransition();
|
2016-06-27 23:50:38 +02:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
toggleBookmark (ev) {
|
2016-06-27 23:50:38 +02:00
|
|
|
if (ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
}
|
2017-07-10 17:46:22 +02:00
|
|
|
const { _converse } = this.__super__;
|
|
|
|
const models = _converse.bookmarks.where({'jid': this.model.get('jid')});
|
2016-09-22 13:02:16 +02:00
|
|
|
if (!models.length) {
|
2016-06-27 23:50:38 +02:00
|
|
|
this.renderBookmarkForm();
|
|
|
|
} else {
|
2017-01-26 10:08:43 +01:00
|
|
|
_.forEach(models, function (model) {
|
2016-10-27 12:49:09 +02:00
|
|
|
model.destroy();
|
|
|
|
});
|
2018-03-06 19:03:23 +01:00
|
|
|
this.el.querySelector('.toggle-bookmark').classList.remove('button-on');
|
2016-06-27 23:50:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize () {
|
2016-06-27 23:50:38 +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;
|
2016-12-20 11:42:20 +01:00
|
|
|
|
2016-11-24 09:51:01 +01:00
|
|
|
// Configuration values for this plugin
|
|
|
|
// ====================================
|
|
|
|
// Refer to docs/source/configuration.rst for explanations of these
|
|
|
|
// configuration settings.
|
2017-07-05 11:03:13 +02:00
|
|
|
_converse.api.settings.update({
|
2017-05-07 18:58:52 +02:00
|
|
|
allow_bookmarks: true,
|
2018-02-08 15:39:27 +01:00
|
|
|
allow_public_bookmarks: false,
|
2018-02-07 21:59:45 +01:00
|
|
|
hide_open_bookmarks: true
|
2016-11-24 09:51:01 +01:00
|
|
|
});
|
2017-07-05 11:03:13 +02:00
|
|
|
// Promises exposed by this plugin
|
|
|
|
_converse.api.promises.add('bookmarksInitialized');
|
2016-09-22 11:30:06 +02:00
|
|
|
|
2017-08-09 15:50:24 +02:00
|
|
|
// Pure functions on the _converse object
|
|
|
|
_.extend(_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');
|
2017-09-24 20:36:39 +02:00
|
|
|
if (confirm(__("Are you sure you want to remove the bookmark \"%1$s\"?", name))) {
|
2017-08-09 15:50:24 +02:00
|
|
|
_.invokeMap(_converse.bookmarks.where({'jid': jid}), Backbone.Model.prototype.destroy);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
addBookmarkViaEvent (ev) {
|
|
|
|
/* Add a bookmark as determined by the passed in
|
|
|
|
* event.
|
|
|
|
*/
|
|
|
|
ev.preventDefault();
|
|
|
|
const jid = ev.target.getAttribute('data-room-jid');
|
2017-12-15 21:51:07 +01:00
|
|
|
const chatroom = _converse.api.rooms.open(jid, {'bring_to_foreground': true});
|
2017-08-09 15:50:24 +02:00
|
|
|
_converse.chatboxviews.get(jid).renderBookmarkForm();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.Bookmark = Backbone.Model;
|
2016-09-22 14:03:42 +02:00
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.Bookmarks = Backbone.Collection.extend({
|
|
|
|
model: _converse.Bookmark,
|
2017-12-23 21:56:20 +01:00
|
|
|
comparator: 'name',
|
2016-09-22 13:02:16 +02:00
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize () {
|
2017-01-26 15:49:02 +01:00
|
|
|
this.on('add', _.flow(this.openBookmarkedRoom, this.markRoomAsBookmarked));
|
2016-09-22 13:02:16 +02:00
|
|
|
this.on('remove', this.markRoomAsUnbookmarked, this);
|
2016-10-06 09:02:29 +02:00
|
|
|
this.on('remove', this.sendBookmarkStanza, this);
|
2016-09-22 14:03:42 +02:00
|
|
|
|
2018-08-23 09:41:39 +02:00
|
|
|
const storage = _converse.config.get('storage'),
|
2018-08-22 22:20:15 +02:00
|
|
|
cache_key = `converse.room-bookmarks${_converse.bare_jid}`;
|
2017-07-10 10:43:37 +02:00
|
|
|
this.fetched_flag = b64_sha1(cache_key+'fetched');
|
2018-08-22 22:20:15 +02:00
|
|
|
this.browserStorage = new Backbone.BrowserStorage[storage](b64_sha1(cache_key));
|
2016-09-22 14:03:42 +02:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
openBookmarkedRoom (bookmark) {
|
2016-09-22 14:03:42 +02:00
|
|
|
if (bookmark.get('autojoin')) {
|
2018-07-22 10:58:04 +02:00
|
|
|
const groupchat = _converse.api.rooms.create(bookmark.get('jid'), bookmark.get('nick'));
|
|
|
|
if (!groupchat.get('hidden')) {
|
|
|
|
groupchat.trigger('show');
|
2018-02-22 11:49:59 +01:00
|
|
|
}
|
2016-09-22 14:03:42 +02:00
|
|
|
}
|
2016-10-27 10:00:48 +02:00
|
|
|
return bookmark;
|
2016-09-22 13:02:16 +02:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
fetchBookmarks () {
|
2017-12-23 21:56:20 +01:00
|
|
|
const deferred = u.getResolveablePromise();
|
2017-07-10 10:43:37 +02:00
|
|
|
if (this.browserStorage.records.length > 0) {
|
2016-10-27 12:49:09 +02:00
|
|
|
this.fetch({
|
|
|
|
'success': _.bind(this.onCachedBookmarksFetched, this, deferred),
|
|
|
|
'error': _.bind(this.onCachedBookmarksFetched, this, deferred)
|
|
|
|
});
|
2017-07-10 10:43:37 +02:00
|
|
|
} else if (! window.sessionStorage.getItem(this.fetched_flag)) {
|
|
|
|
// There aren't any cached bookmarks and the
|
|
|
|
// `fetched_flag` is off, so we query the XMPP server.
|
2016-10-27 12:49:09 +02:00
|
|
|
// If nothing is returned from the XMPP server, we set
|
2017-07-10 10:43:37 +02:00
|
|
|
// the `fetched_flag` to avoid calling the server again.
|
2016-10-27 12:49:09 +02:00
|
|
|
this.fetchBookmarksFromServer(deferred);
|
|
|
|
} else {
|
|
|
|
deferred.resolve();
|
|
|
|
}
|
2017-11-10 15:52:50 +01:00
|
|
|
return deferred;
|
2016-10-27 12:49:09 +02:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
onCachedBookmarksFetched (deferred) {
|
2016-10-27 12:49:09 +02:00
|
|
|
return deferred.resolve();
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
createBookmark (options) {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.bookmarks.create(options);
|
|
|
|
_converse.bookmarks.sendBookmarkStanza();
|
2016-09-22 13:02:16 +02:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
sendBookmarkStanza () {
|
|
|
|
let stanza = $iq({
|
2016-09-22 13:02:16 +02:00
|
|
|
'type': 'set',
|
2016-12-20 10:30:20 +01:00
|
|
|
'from': _converse.connection.jid,
|
2016-09-22 13:02:16 +02:00
|
|
|
})
|
|
|
|
.c('pubsub', {'xmlns': Strophe.NS.PUBSUB})
|
|
|
|
.c('publish', {'node': 'storage:bookmarks'})
|
|
|
|
.c('item', {'id': 'current'})
|
|
|
|
.c('storage', {'xmlns':'storage:bookmarks'});
|
|
|
|
this.each(function (model) {
|
|
|
|
stanza = stanza.c('conference', {
|
|
|
|
'name': model.get('name'),
|
|
|
|
'autojoin': model.get('autojoin'),
|
|
|
|
'jid': model.get('jid'),
|
|
|
|
}).c('nick').t(model.get('nick')).up().up();
|
|
|
|
});
|
|
|
|
stanza.up().up().up();
|
|
|
|
stanza.c('publish-options')
|
|
|
|
.c('x', {'xmlns': Strophe.NS.XFORM, 'type':'submit'})
|
|
|
|
.c('field', {'var':'FORM_TYPE', 'type':'hidden'})
|
|
|
|
.c('value').t('http://jabber.org/protocol/pubsub#publish-options').up().up()
|
|
|
|
.c('field', {'var':'pubsub#persist_items'})
|
|
|
|
.c('value').t('true').up().up()
|
|
|
|
.c('field', {'var':'pubsub#access_model'})
|
|
|
|
.c('value').t('whitelist');
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.connection.sendIQ(stanza, null, this.onBookmarkError.bind(this));
|
2016-09-22 13:02:16 +02:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
onBookmarkError (iq) {
|
2017-07-05 11:33:55 +02:00
|
|
|
_converse.log("Error while trying to add bookmark", Strophe.LogLevel.ERROR);
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.log(iq);
|
2016-09-22 13:02:16 +02:00
|
|
|
// We remove all locally cached bookmarks and fetch them
|
|
|
|
// again from the server.
|
|
|
|
this.reset();
|
2016-10-17 10:34:09 +02:00
|
|
|
this.fetchBookmarksFromServer(null);
|
2016-09-22 13:02:16 +02:00
|
|
|
window.alert(__("Sorry, something went wrong while trying to save your bookmark."));
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
fetchBookmarksFromServer (deferred) {
|
|
|
|
const stanza = $iq({
|
2016-12-20 10:30:20 +01:00
|
|
|
'from': _converse.connection.jid,
|
2016-09-22 13:02:16 +02:00
|
|
|
'type': 'get',
|
|
|
|
}).c('pubsub', {'xmlns': Strophe.NS.PUBSUB})
|
|
|
|
.c('items', {'node': 'storage:bookmarks'});
|
2018-06-30 09:44:53 +02:00
|
|
|
_converse.api.sendIQ(stanza)
|
|
|
|
.then((iq) => this.onBookmarksReceived(deferred, iq))
|
|
|
|
.catch((iq) => this.onBookmarksReceivedError(deferred, iq)
|
2016-09-22 13:02:16 +02:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
markRoomAsBookmarked (bookmark) {
|
2018-07-22 10:58:04 +02:00
|
|
|
const groupchat = _converse.chatboxes.get(bookmark.get('jid'));
|
|
|
|
if (!_.isUndefined(groupchat)) {
|
|
|
|
groupchat.save('bookmarked', true);
|
2016-06-27 23:50:38 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
markRoomAsUnbookmarked (bookmark) {
|
2018-07-22 10:58:04 +02:00
|
|
|
const groupchat = _converse.chatboxes.get(bookmark.get('jid'));
|
|
|
|
if (!_.isUndefined(groupchat)) {
|
|
|
|
groupchat.save('bookmarked', false);
|
2016-09-22 13:02:16 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-07 21:59:45 +01:00
|
|
|
createBookmarksFromStanza (stanza) {
|
2017-12-23 21:56:20 +01:00
|
|
|
const bookmarks = sizzle(
|
2018-02-07 21:59:45 +01:00
|
|
|
'items[node="storage:bookmarks"] '+
|
|
|
|
'item#current '+
|
|
|
|
'storage[xmlns="storage:bookmarks"] '+
|
|
|
|
'conference',
|
|
|
|
stanza
|
2017-12-23 21:56:20 +01:00
|
|
|
)
|
|
|
|
_.forEach(bookmarks, (bookmark) => {
|
|
|
|
this.create({
|
2016-09-22 11:30:06 +02:00
|
|
|
'jid': bookmark.getAttribute('jid'),
|
|
|
|
'name': bookmark.getAttribute('name'),
|
2016-09-23 09:41:55 +02:00
|
|
|
'autojoin': bookmark.getAttribute('autojoin') === 'true',
|
2018-03-28 13:00:46 +02:00
|
|
|
'nick': _.get(bookmark.querySelector('nick'), 'textContent')
|
2016-10-27 12:49:09 +02:00
|
|
|
});
|
2016-10-17 10:34:09 +02:00
|
|
|
});
|
2018-02-07 21:59:45 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
onBookmarksReceived (deferred, iq) {
|
|
|
|
this.createBookmarksFromStanza(iq);
|
2016-10-17 10:34:09 +02:00
|
|
|
if (!_.isUndefined(deferred)) {
|
|
|
|
return deferred.resolve();
|
|
|
|
}
|
2016-06-27 23:50:38 +02:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
onBookmarksReceivedError (deferred, iq) {
|
2017-07-10 10:43:37 +02:00
|
|
|
window.sessionStorage.setItem(this.fetched_flag, true);
|
2017-10-31 18:06:39 +01:00
|
|
|
_converse.log('Error while fetching bookmarks', Strophe.LogLevel.WARN);
|
|
|
|
_converse.log(iq.outerHTML, Strophe.LogLevel.DEBUG);
|
2017-06-27 13:02:40 +02:00
|
|
|
if (!_.isNil(deferred)) {
|
2018-01-15 21:59:39 +01:00
|
|
|
if (iq.querySelector('error[type="cancel"] item-not-found')) {
|
|
|
|
// Not an exception, the user simply doesn't have
|
|
|
|
// any bookmarks.
|
|
|
|
return deferred.resolve();
|
|
|
|
} else {
|
|
|
|
return deferred.reject(new Error("Could not fetch bookmarks"));
|
|
|
|
}
|
2016-10-17 10:34:09 +02:00
|
|
|
}
|
2016-06-27 23:50:38 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-01-04 22:51:42 +01:00
|
|
|
_converse.BookmarksList = Backbone.Model.extend({
|
|
|
|
defaults: {
|
|
|
|
"toggle-state": _converse.OPENED
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
_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,
|
2018-07-02 15:54:51 +02:00
|
|
|
'info_leave_room': __('Leave this groupchat'),
|
2018-01-04 22:51:42 +01:00
|
|
|
'info_remove': __('Remove this bookmark'),
|
2018-07-02 15:54:51 +02:00
|
|
|
'info_remove_bookmark': __('Unbookmark this groupchat'),
|
|
|
|
'info_title': __('Show more information on this groupchat'),
|
2018-01-04 22:51:42 +01:00
|
|
|
'jid': this.model.get('jid'),
|
2018-03-16 12:49:39 +01:00
|
|
|
'name': Strophe.xmlunescape(this.model.get('name')),
|
2018-07-02 15:54:51 +02:00
|
|
|
'open_title': __('Click to open this groupchat')
|
2018-01-04 22:51:42 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
_converse.BookmarksView = Backbone.OrderedListView.extend({
|
2016-10-06 09:02:29 +02:00
|
|
|
tagName: 'div',
|
2018-02-16 15:08:16 +01:00
|
|
|
className: 'bookmarks-list list-container rooms-list-container',
|
2016-10-06 09:02:29 +02:00
|
|
|
events: {
|
2017-08-09 15:50:24 +02:00
|
|
|
'click .add-bookmark': 'addBookmark',
|
|
|
|
'click .bookmarks-toggle': 'toggleBookmarksList',
|
2018-03-05 16:51:51 +01:00
|
|
|
'click .remove-bookmark': 'removeBookmark',
|
|
|
|
'click .open-room': 'openRoom',
|
2016-10-06 09:02:29 +02:00
|
|
|
},
|
2018-01-04 22:51:42 +01:00
|
|
|
listSelector: '.rooms-list',
|
|
|
|
ItemView: _converse.BookmarkView,
|
|
|
|
subviewIndex: 'jid',
|
2016-10-06 09:02:29 +02:00
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize () {
|
2018-01-04 22:51:42 +01:00
|
|
|
Backbone.OrderedListView.prototype.initialize.apply(this, arguments);
|
|
|
|
|
2018-01-05 13:33:28 +01:00
|
|
|
this.model.on('add', this.showOrHide, this);
|
|
|
|
this.model.on('remove', this.showOrHide, this);
|
2018-01-06 20:02:50 +01:00
|
|
|
|
2017-05-07 18:58:52 +02:00
|
|
|
_converse.chatboxes.on('add', this.renderBookmarkListElement, this);
|
|
|
|
_converse.chatboxes.on('remove', this.renderBookmarkListElement, this);
|
2016-11-03 16:12:52 +01: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.room-bookmarks${_converse.bare_jid}-list-model`);
|
|
|
|
this.list_model = new _converse.BookmarksList({'id': id});
|
|
|
|
this.list_model.browserStorage = new Backbone.BrowserStorage[storage](id);
|
2016-11-03 16:12:52 +01:00
|
|
|
this.list_model.fetch();
|
2016-10-17 10:34:09 +02:00
|
|
|
this.render();
|
2018-01-04 22:51:42 +01:00
|
|
|
this.sortAndPositionAllItems();
|
2016-10-06 09:02:29 +02:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
render () {
|
2017-12-23 21:56:20 +01:00
|
|
|
this.el.innerHTML = tpl_bookmarks_list({
|
2016-11-03 16:12:52 +01:00
|
|
|
'toggle_state': this.list_model.get('toggle-state'),
|
2016-10-06 09:02:29 +02:00
|
|
|
'desc_bookmarks': __('Click to toggle the bookmarks list'),
|
2017-12-23 21:56:20 +01:00
|
|
|
'label_bookmarks': __('Bookmarks'),
|
|
|
|
'_converse': _converse
|
|
|
|
});
|
2018-01-05 13:33:28 +01:00
|
|
|
this.showOrHide();
|
2018-01-06 20:02:50 +01:00
|
|
|
this.insertIntoControlBox();
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
insertIntoControlBox () {
|
2017-07-10 17:46:22 +02:00
|
|
|
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-16 15:08:16 +01:00
|
|
|
const el = controlboxview.el.querySelector('.bookmarks-list');
|
|
|
|
if (!_.isNull(el)) {
|
|
|
|
el.parentNode.replaceChild(this.el, el);
|
2018-01-15 21:59:39 +01:00
|
|
|
}
|
2016-10-27 15:22:57 +02:00
|
|
|
}
|
2016-10-06 09:02:29 +02:00
|
|
|
},
|
|
|
|
|
2018-03-05 16:51: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-08-09 15:50:24 +02:00
|
|
|
removeBookmark: _converse.removeBookmarkViaEvent,
|
|
|
|
addBookmark: _converse.addBookmarkViaEvent,
|
2016-10-06 09:02:29 +02:00
|
|
|
|
2018-01-04 22:51:42 +01:00
|
|
|
renderBookmarkListElement (chatbox) {
|
|
|
|
const bookmarkview = this.get(chatbox.get('jid'));
|
|
|
|
if (_.isNil(bookmarkview)) {
|
|
|
|
// A chat box has been closed, but we don't have a
|
|
|
|
// bookmark for it, so nothing further to do here.
|
2017-05-07 18:58:52 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-01-04 22:51:42 +01:00
|
|
|
bookmarkview.render();
|
2018-01-05 13:33:28 +01:00
|
|
|
this.showOrHide();
|
2017-05-07 12:36:22 +02:00
|
|
|
},
|
|
|
|
|
2018-01-05 13:33:28 +01:00
|
|
|
showOrHide (item) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.model.models.length) {
|
|
|
|
u.showElement(this.el);
|
2016-10-27 12:49:09 +02:00
|
|
|
}
|
2016-10-06 09:02:29 +02:00
|
|
|
},
|
|
|
|
|
2018-01-05 13:33:28 +01:00
|
|
|
toggleBookmarksList (ev) {
|
2016-10-06 09:02:29 +02:00
|
|
|
if (ev && ev.preventDefault) { ev.preventDefault(); }
|
2018-03-17 12:42:17 +01:00
|
|
|
const icon_el = ev.target.querySelector('.fa');
|
|
|
|
if (u.hasClass('fa-caret-down', icon_el)) {
|
2017-12-23 21:56:20 +01:00
|
|
|
u.slideIn(this.el.querySelector('.bookmarks'));
|
2016-12-20 10:30:20 +01: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");
|
2016-10-06 09:02:29 +02:00
|
|
|
} else {
|
2018-03-17 12:42:17 +01:00
|
|
|
icon_el.classList.remove("fa-caret-right");
|
|
|
|
icon_el.classList.add("fa-caret-down");
|
2017-12-23 21:56:20 +01:00
|
|
|
u.slideOut(this.el.querySelector('.bookmarks'));
|
2016-12-20 10:30:20 +01:00
|
|
|
this.list_model.save({'toggle-state': _converse.OPENED});
|
2016-10-06 09:02:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-02-22 13:55:17 +01:00
|
|
|
_converse.checkBookmarksSupport = function () {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Promise.all([
|
|
|
|
_converse.api.disco.getIdentity('pubsub', 'pep', _converse.bare_jid),
|
|
|
|
_converse.api.disco.supports(Strophe.NS.PUBSUB+'#publish-options', _converse.bare_jid)
|
|
|
|
]).then((args) => {
|
2018-03-29 16:12:19 +02:00
|
|
|
resolve(args[0] && (args[1].length || _converse.allow_public_bookmarks));
|
2018-03-02 16:30:00 +01:00
|
|
|
}).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
|
|
|
|
}).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
|
2018-02-22 13:55:17 +01:00
|
|
|
}
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
const initBookmarks = function () {
|
2016-12-20 10:30:20 +01:00
|
|
|
if (!_converse.allow_bookmarks) {
|
2016-11-24 09:51:01 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-02-22 13:55:17 +01:00
|
|
|
_converse.checkBookmarksSupport().then((supported) => {
|
|
|
|
if (supported) {
|
|
|
|
_converse.bookmarks = new _converse.Bookmarks();
|
|
|
|
_converse.bookmarksview = new _converse.BookmarksView({'model': _converse.bookmarks});
|
|
|
|
_converse.bookmarks.fetchBookmarks()
|
|
|
|
.catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
|
|
|
|
.then(() => _converse.emit('bookmarksInitialized'));
|
|
|
|
} else {
|
2018-02-07 17:30:44 +01:00
|
|
|
_converse.emit('bookmarksInitialized');
|
|
|
|
}
|
2016-10-17 10:34:09 +02:00
|
|
|
});
|
2018-02-22 12:46:10 +01:00
|
|
|
}
|
2017-07-10 21:14:48 +02:00
|
|
|
|
2018-02-22 12:46:10 +01:00
|
|
|
u.onMultipleEvents([
|
|
|
|
{'object': _converse, 'event': 'chatBoxesFetched'},
|
|
|
|
{'object': _converse, 'event': 'roomsPanelRendered'}
|
|
|
|
], initBookmarks);
|
2016-10-27 10:38:06 +02:00
|
|
|
|
2018-05-18 12:44:27 +02:00
|
|
|
|
|
|
|
_converse.on('clearSession', () => {
|
|
|
|
if (!_.isUndefined(_converse.bookmarks)) {
|
|
|
|
_converse.bookmarks.browserStorage._clear();
|
|
|
|
window.sessionStorage.removeItem(_converse.bookmarks.fetched_flag);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-02-22 11:49:59 +01:00
|
|
|
_converse.on('reconnected', initBookmarks);
|
|
|
|
|
2018-02-07 21:59:45 +01:00
|
|
|
_converse.on('connected', () => {
|
|
|
|
// Add a handler for bookmarks pushed from other connected clients
|
|
|
|
// (from the same user obviously)
|
|
|
|
_converse.connection.addHandler((message) => {
|
2018-05-20 13:41:16 +02:00
|
|
|
if (sizzle('event[xmlns="'+Strophe.NS.PUBSUB+'#event"] items[node="storage:bookmarks"]', message).length) {
|
|
|
|
_converse.api.waitUntil('bookmarksInitialized')
|
|
|
|
.then(() => _converse.bookmarks.createBookmarksFromStanza(message))
|
|
|
|
.catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
|
2018-02-07 21:59:45 +01:00
|
|
|
}
|
|
|
|
}, null, 'message', 'headline', null, _converse.bare_jid);
|
|
|
|
});
|
|
|
|
|
2016-06-27 23:50:38 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}));
|