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) {
|
2017-12-23 21:56:20 +01:00
|
|
|
define(["utils",
|
2017-02-14 15:08:39 +01:00
|
|
|
"converse-core",
|
2016-06-27 23:50:38 +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
|
|
|
],
|
|
|
|
factory);
|
2016-10-06 09:02:29 +02:00
|
|
|
}(this, function (
|
2017-12-23 21:56:20 +01:00
|
|
|
u,
|
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;
|
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
|
|
|
|
|
|
|
optional_dependencies: ["converse-chatboxes", "converse-muc"],
|
|
|
|
|
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
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
clearSession () {
|
2016-10-17 10:34:09 +02:00
|
|
|
this.__super__.clearSession.apply(this, arguments);
|
|
|
|
if (!_.isUndefined(this.bookmarks)) {
|
2017-07-03 09:57:11 +02:00
|
|
|
this.bookmarks.reset();
|
2016-10-17 10:34:09 +02:00
|
|
|
this.bookmarks.browserStorage._clear();
|
2017-07-10 10:43:37 +02:00
|
|
|
window.sessionStorage.removeItem(this.bookmarks.fetched_flag);
|
2016-10-17 10:34:09 +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
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
generateHeadingHTML () {
|
|
|
|
const { _converse } = this.__super__,
|
|
|
|
{ __ } = _converse,
|
2016-12-20 11:42:20 +01:00
|
|
|
html = this.__super__.generateHeadingHTML.apply(this, arguments);
|
2016-12-20 10:30:20 +01:00
|
|
|
if (_converse.allow_bookmarks) {
|
2017-07-10 17:46:22 +02:00
|
|
|
const div = document.createElement('div');
|
2016-12-02 18:41:05 +01:00
|
|
|
div.innerHTML = html;
|
2017-07-10 17:46:22 +02:00
|
|
|
const bookmark_button = tpl_chatroom_bookmark_toggle(
|
2017-01-26 10:08:43 +01:00
|
|
|
_.assignIn(
|
2016-12-02 18:41:05 +01:00
|
|
|
this.model.toJSON(),
|
|
|
|
{
|
|
|
|
info_toggle_bookmark: __('Bookmark this room'),
|
|
|
|
bookmarked: this.model.get('bookmarked')
|
|
|
|
}
|
|
|
|
));
|
2017-07-10 17:46:22 +02:00
|
|
|
const close_button = div.querySelector('.close-chatbox-button');
|
2016-12-02 18:41:05 +01:00
|
|
|
close_button.insertAdjacentHTML('afterend', bookmark_button);
|
|
|
|
return div.innerHTML;
|
2016-11-24 09:51:01 +01:00
|
|
|
}
|
2016-12-02 18:41:05 +01:00
|
|
|
return html;
|
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
|
|
|
|
* for this room, and if so use it.
|
|
|
|
* 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 () {
|
2017-08-14 22:06:32 +02:00
|
|
|
const icon = this.el.querySelector('.icon-pushpin');
|
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 () {
|
2016-10-17 10:34:09 +02:00
|
|
|
/* Set whether the room is bookmarked or not.
|
|
|
|
*/
|
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(
|
|
|
|
'beforeend',
|
2017-02-19 10:58:30 +01:00
|
|
|
tpl_chatroom_bookmark_form({
|
2016-06-27 23:50:38 +02:00
|
|
|
heading: __('Bookmark this room'),
|
|
|
|
label_name: __('The name for this bookmark:'),
|
|
|
|
label_autojoin: __('Would you like this room to be automatically joined upon startup?'),
|
|
|
|
label_nick: __('What should your nickname for this room be?'),
|
|
|
|
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();
|
|
|
|
});
|
2017-08-14 22:06:32 +02:00
|
|
|
this.el.querySelector('.icon-pushpin').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,
|
|
|
|
hide_open_bookmarks: false
|
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.BookmarksList = Backbone.Model.extend({
|
2016-11-03 16:12:52 +01:00
|
|
|
defaults: {
|
2016-12-20 10:30:20 +01:00
|
|
|
"toggle-state": _converse.OPENED
|
2016-11-03 16:12:52 +01: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
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
const cache_key = `converse.room-bookmarks${_converse.bare_jid}`;
|
2017-07-10 10:43:37 +02:00
|
|
|
this.fetched_flag = b64_sha1(cache_key+'fetched');
|
2016-12-20 10:30:20 +01:00
|
|
|
this.browserStorage = new Backbone.BrowserStorage[_converse.storage](
|
2016-10-27 12:49:09 +02:00
|
|
|
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')) {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.api.rooms.open(bookmark.get('jid'), bookmark.get('nick'));
|
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'});
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.connection.sendIQ(
|
2016-09-22 13:02:16 +02:00
|
|
|
stanza,
|
2016-10-17 10:34:09 +02:00
|
|
|
_.bind(this.onBookmarksReceived, this, deferred),
|
2016-10-27 12:49:09 +02:00
|
|
|
_.bind(this.onBookmarksReceivedError, this, deferred)
|
2016-09-22 13:02:16 +02:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
markRoomAsBookmarked (bookmark) {
|
|
|
|
const room = _converse.chatboxes.get(bookmark.get('jid'));
|
2016-09-22 11:30:06 +02:00
|
|
|
if (!_.isUndefined(room)) {
|
|
|
|
room.save('bookmarked', true);
|
2016-06-27 23:50:38 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
markRoomAsUnbookmarked (bookmark) {
|
|
|
|
const room = _converse.chatboxes.get(bookmark.get('jid'));
|
2016-09-22 13:02:16 +02:00
|
|
|
if (!_.isUndefined(room)) {
|
|
|
|
room.save('bookmarked', false);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
onBookmarksReceived (deferred, iq) {
|
2017-12-23 21:56:20 +01:00
|
|
|
const bookmarks = sizzle(
|
|
|
|
'items[node="storage:bookmarks"] item[id="current"] storage conference',
|
|
|
|
iq
|
|
|
|
)
|
|
|
|
_.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',
|
2016-09-22 11:30:06 +02:00
|
|
|
'nick': bookmark.querySelector('nick').textContent
|
2016-10-27 12:49:09 +02:00
|
|
|
});
|
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)) {
|
2017-12-19 14:56:32 +01:00
|
|
|
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-03 20:02:05 +01:00
|
|
|
_converse.BookmarksView = Backbone.NativeView.extend({
|
2016-10-06 09:02:29 +02:00
|
|
|
tagName: 'div',
|
2017-12-23 21:56:20 +01:00
|
|
|
className: 'bookmarks-list rooms-list-container hidden',
|
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',
|
|
|
|
'click .remove-bookmark': 'removeBookmark'
|
2016-10-06 09:02:29 +02:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize () {
|
2016-10-27 12:49:09 +02:00
|
|
|
this.model.on('add', this.renderBookmarkListElement, this);
|
2016-10-17 10:34:09 +02:00
|
|
|
this.model.on('remove', this.removeBookmarkListElement, this);
|
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
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
const cachekey = `converse.room-bookmarks${_converse.bare_jid}-list-model`;
|
2016-12-20 10:30:20 +01:00
|
|
|
this.list_model = new _converse.BookmarksList();
|
2016-11-03 16:12:52 +01:00
|
|
|
this.list_model.id = cachekey;
|
2016-12-20 10:30:20 +01:00
|
|
|
this.list_model.browserStorage = new Backbone.BrowserStorage[_converse.storage](
|
2016-11-03 16:12:52 +01:00
|
|
|
b64_sha1(cachekey)
|
|
|
|
);
|
|
|
|
this.list_model.fetch();
|
2016-10-17 10:34:09 +02:00
|
|
|
this.render();
|
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
|
|
|
|
});
|
2017-01-26 15:49:02 +01:00
|
|
|
this.model.each(this.renderBookmarkListElement.bind(this));
|
2017-07-10 17:46:22 +02:00
|
|
|
const controlboxview = _converse.chatboxviews.get('controlbox');
|
2016-10-27 15:22:57 +02:00
|
|
|
if (!_.isUndefined(controlboxview)) {
|
2017-12-23 21:56:20 +01:00
|
|
|
const chatrooms_el = controlboxview.el.querySelector('#chatrooms');
|
|
|
|
chatrooms_el.insertAdjacentElement('afterbegin', this.el);
|
2016-10-27 15:22:57 +02:00
|
|
|
}
|
2017-12-23 21:56:20 +01:00
|
|
|
return this;
|
2016-10-06 09:02:29 +02:00
|
|
|
},
|
|
|
|
|
2017-08-09 15:50:24 +02:00
|
|
|
removeBookmark: _converse.removeBookmarkViaEvent,
|
|
|
|
addBookmark: _converse.addBookmarkViaEvent,
|
2016-10-06 09:02:29 +02:00
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
renderBookmarkListElement (item) {
|
2017-05-07 20:36:40 +02:00
|
|
|
if (item instanceof _converse.ChatBox) {
|
|
|
|
item = _.head(this.model.where({'jid': item.get('jid')}));
|
|
|
|
if (_.isNil(item)) {
|
2017-05-15 17:39:46 +02:00
|
|
|
// A chat box has been closed, but we don't have a
|
|
|
|
// bookmark for it, so nothing further to do here.
|
2017-05-07 20:36:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-05-07 18:58:52 +02:00
|
|
|
if (_converse.hide_open_bookmarks &&
|
|
|
|
_converse.chatboxes.where({'jid': item.get('jid')}).length) {
|
2017-05-15 17:39:46 +02:00
|
|
|
// A chat box has been opened, and we don't show
|
|
|
|
// bookmarks for open chats, so we remove it.
|
2017-05-07 18:58:52 +02:00
|
|
|
this.removeBookmarkListElement(item);
|
|
|
|
return;
|
|
|
|
}
|
2017-05-15 17:39:46 +02:00
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
const list_el = this.el.querySelector('.bookmarks');
|
|
|
|
const div = document.createElement('div');
|
2017-05-07 12:36:22 +02:00
|
|
|
div.innerHTML = tpl_bookmark({
|
|
|
|
'bookmarked': true,
|
|
|
|
'info_leave_room': __('Leave this room'),
|
|
|
|
'info_remove': __('Remove this bookmark'),
|
|
|
|
'info_remove_bookmark': __('Unbookmark this room'),
|
|
|
|
'info_title': __('Show more information on this room'),
|
|
|
|
'jid': item.get('jid'),
|
|
|
|
'name': item.get('name'),
|
|
|
|
'open_title': __('Click to open this room')
|
|
|
|
});
|
2017-07-10 17:46:22 +02:00
|
|
|
const el = _.head(sizzle(
|
|
|
|
`.available-chatroom[data-room-jid="${item.get('jid')}"]`,
|
2017-05-15 17:39:46 +02:00
|
|
|
list_el));
|
|
|
|
|
|
|
|
if (el) {
|
|
|
|
el.innerHTML = div.firstChild.innerHTML;
|
|
|
|
} else {
|
|
|
|
list_el.appendChild(div.firstChild);
|
|
|
|
}
|
2017-05-07 12:36:22 +02:00
|
|
|
this.show();
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
show () {
|
2017-12-23 21:56:20 +01:00
|
|
|
u.showElement(this.el);
|
2016-10-17 10:34:09 +02:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
hide () {
|
2017-12-23 21:56:20 +01:00
|
|
|
u.hideElement(this.el);
|
2017-05-07 12:36:22 +02:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
removeBookmarkListElement (item) {
|
|
|
|
const list_el = this.el.querySelector('.bookmarks');
|
|
|
|
const el = _.head(sizzle(`.available-chatroom[data-room-jid="${item.get('jid')}"]`, list_el));
|
2017-05-07 12:36:22 +02:00
|
|
|
if (el) {
|
|
|
|
list_el.removeChild(el);
|
|
|
|
}
|
2017-05-07 18:58:52 +02:00
|
|
|
if (list_el.childElementCount === 0) {
|
2017-05-07 12:36:22 +02:00
|
|
|
this.hide();
|
2016-10-27 12:49:09 +02:00
|
|
|
}
|
2016-10-06 09:02:29 +02:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
toggleBookmarksList (ev) {
|
2016-10-06 09:02:29 +02:00
|
|
|
if (ev && ev.preventDefault) { ev.preventDefault(); }
|
2018-01-03 20:02:05 +01:00
|
|
|
if (u.hasClass('icon-opened', ev.target)) {
|
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});
|
2017-12-23 21:56:20 +01:00
|
|
|
ev.target.classList.remove("icon-opened");
|
|
|
|
ev.target.classList.add("icon-closed");
|
2016-10-06 09:02:29 +02:00
|
|
|
} else {
|
2017-12-23 21:56:20 +01:00
|
|
|
ev.target.classList.remove("icon-closed");
|
|
|
|
ev.target.classList.add("icon-opened");
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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;
|
|
|
|
}
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.bookmarks = new _converse.Bookmarks();
|
2017-12-19 14:56:32 +01:00
|
|
|
_converse.bookmarks.fetchBookmarks().then(() => {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.bookmarksview = new _converse.BookmarksView(
|
|
|
|
{'model': _converse.bookmarks}
|
2016-10-17 10:34:09 +02:00
|
|
|
);
|
2017-12-19 14:56:32 +01:00
|
|
|
})
|
|
|
|
.catch(_.partial(_converse.log, _, Strophe.LogLevel.ERROR))
|
|
|
|
.then(() => {
|
2017-05-15 17:40:34 +02:00
|
|
|
_converse.emit('bookmarksInitialized');
|
2016-10-17 10:34:09 +02:00
|
|
|
});
|
2016-06-27 23:50:38 +02:00
|
|
|
};
|
2017-07-10 21:14:48 +02:00
|
|
|
|
|
|
|
Promise.all([
|
|
|
|
_converse.api.waitUntil('chatBoxesFetched'),
|
|
|
|
_converse.api.waitUntil('roomsPanelRendered')
|
2017-09-16 19:17:54 +02:00
|
|
|
]).then(initBookmarks)
|
|
|
|
.catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
|
2016-10-27 10:38:06 +02:00
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
const afterReconnection = function () {
|
2016-12-20 10:30:20 +01:00
|
|
|
if (!_converse.allow_bookmarks) {
|
2016-11-24 09:51:01 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-12-20 10:30:20 +01:00
|
|
|
if (_.isUndefined(_converse.bookmarksview)) {
|
2016-10-27 10:38:06 +02:00
|
|
|
initBookmarks();
|
|
|
|
} else {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.bookmarksview.render();
|
2016-10-27 10:38:06 +02:00
|
|
|
}
|
|
|
|
};
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.on('reconnected', afterReconnection);
|
2016-06-27 23:50:38 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}));
|