Commit 4b28f3f8 authored by JC Brand's avatar JC Brand

Bookmarks fixes.

- Remove bookmark from list when removed from collection
- Only render list after all bookmarks have been fetched
- Properly remove bookmarks from sessionStorage
parent aabd2cc0
...@@ -51,6 +51,13 @@ ...@@ -51,6 +51,13 @@
// //
// New functions which don't exist yet can also be added. // New functions which don't exist yet can also be added.
clearSession: function () {
this.__super__.clearSession.apply(this, arguments);
if (!_.isUndefined(this.bookmarks)) {
this.bookmarks.browserStorage._clear();
}
},
ChatRoomView: { ChatRoomView: {
events: { events: {
'click .toggle-bookmark': 'toggleBookmark' 'click .toggle-bookmark': 'toggleBookmark'
...@@ -97,6 +104,8 @@ ...@@ -97,6 +104,8 @@
}, },
setBookmarkState: function () { setBookmarkState: function () {
/* Set whether the room is bookmarked or not.
*/
if (!_.isUndefined(converse.bookmarks)) { if (!_.isUndefined(converse.bookmarks)) {
var models = converse.bookmarks.where({'jid': this.model.get('jid')}); var models = converse.bookmarks.where({'jid': this.model.get('jid')});
if (!models.length) { if (!models.length) {
...@@ -188,15 +197,16 @@ ...@@ -188,15 +197,16 @@
}, },
fetchBookmarks: function () { fetchBookmarks: function () {
var deferred = new $.Deferred();
this.fetch({ this.fetch({
'add': true, 'silent': true,
'success': this.onCachedBookmarksFetched.bind(this), 'success': _.bind(this.onCachedBookmarksFetched, this, deferred),
'error': this.onCachedBookmarksFetched.bind(this) 'error': _.bind(this.onCachedBookmarksFetched, this, deferred)
}); });
return deferred.promise();
}, },
sendBookmarkStanza: function () { sendBookmarkStanza: function () {
var deferred = new $.Deferred();
var stanza = $iq({ var stanza = $iq({
'type': 'set', 'type': 'set',
'from': converse.connection.jid, 'from': converse.connection.jid,
...@@ -222,34 +232,30 @@ ...@@ -222,34 +232,30 @@
.c('value').t('true').up().up() .c('value').t('true').up().up()
.c('field', {'var':'pubsub#access_model'}) .c('field', {'var':'pubsub#access_model'})
.c('value').t('whitelist'); .c('value').t('whitelist');
converse.connection.sendIQ( converse.connection.sendIQ(stanza, null, this.onBookmarkError.bind(this));
stanza,
deferred.resolve,
_.bind(this.onBookmarkError, this, deferred)
);
return deferred.promise();
}, },
onBookmarkError: function (deferred, iq) { onBookmarkError: function (iq) {
converse.log("Error while trying to add bookmark", "error"); converse.log("Error while trying to add bookmark", "error");
converse.log(iq); converse.log(iq);
// We remove all locally cached bookmarks and fetch them // We remove all locally cached bookmarks and fetch them
// again from the server. // again from the server.
this.reset(); this.reset();
this.fetchBookmarksFromServer(); this.fetchBookmarksFromServer(null);
window.alert(__("Sorry, something went wrong while trying to save your bookmark.")); window.alert(__("Sorry, something went wrong while trying to save your bookmark."));
return deferred.reject();
}, },
onCachedBookmarksFetched: function () { onCachedBookmarksFetched: function (deferred) {
if (!window.sessionStorage.getItem(this.browserStorage.name)) { if (!window.sessionStorage.getItem(this.browserStorage.name)) {
// There aren't any cached bookmarks, so we query the // There aren't any cached bookmarks, so we query the
// XMPP server. // XMPP server.
this.fetchBookmarksFromServer(); this.fetchBookmarksFromServer(deferred);
} else {
return deferred.resolve();
} }
}, },
fetchBookmarksFromServer: function () { fetchBookmarksFromServer: function (deferred) {
var stanza = $iq({ var stanza = $iq({
'from': converse.connection.jid, 'from': converse.connection.jid,
'type': 'get', 'type': 'get',
...@@ -257,8 +263,8 @@ ...@@ -257,8 +263,8 @@
.c('items', {'node': 'storage:bookmarks'}); .c('items', {'node': 'storage:bookmarks'});
converse.connection.sendIQ( converse.connection.sendIQ(
stanza, stanza,
this.onBookmarksReceived.bind(this), _.bind(this.onBookmarksReceived, this, deferred),
this.onBookmarksReceivedError _.partial(this.onBookmarksReceivedError, deferred)
); );
}, },
...@@ -276,23 +282,30 @@ ...@@ -276,23 +282,30 @@
} }
}, },
onBookmarksReceived: function (iq) { onBookmarksReceived: function (deferred, iq) {
var bookmarks = $(iq).find( var bookmarks = $(iq).find(
'items[node="storage:bookmarks"] item[id="current"] storage conference' 'items[node="storage:bookmarks"] item[id="current"] storage conference'
); );
var that = this;
_.each(bookmarks, function (bookmark) { _.each(bookmarks, function (bookmark) {
this.create({ that.create({
'jid': bookmark.getAttribute('jid'), 'jid': bookmark.getAttribute('jid'),
'name': bookmark.getAttribute('name'), 'name': bookmark.getAttribute('name'),
'autojoin': bookmark.getAttribute('autojoin') === 'true', 'autojoin': bookmark.getAttribute('autojoin') === 'true',
'nick': bookmark.querySelector('nick').textContent 'nick': bookmark.querySelector('nick').textContent
}, {'silent':true});
}); });
}.bind(this)); if (!_.isUndefined(deferred)) {
return deferred.resolve();
}
}, },
onBookmarksReceivedError: function (iq) { onBookmarksReceivedError: function (deferred, iq) {
converse.log('Error while fetching bookmarks'); converse.log('Error while fetching bookmarks');
converse.log(iq); converse.log(iq);
if (!_.isUndefined(deferred)) {
return deferred.reject();
}
} }
}); });
...@@ -300,12 +313,14 @@ ...@@ -300,12 +313,14 @@
tagName: 'div', tagName: 'div',
className: 'bookmarks-list', className: 'bookmarks-list',
events: { events: {
'click .remove-bookmark': 'removebookmark', 'click .remove-bookmark': 'removeBookmark',
'click .bookmarks-toggle': 'toggleBookmarksList' 'click .bookmarks-toggle': 'toggleBookmarksList'
}, },
initialize: function () { initialize: function () {
this.model.on('add', this.onBookmarkAdded, this); this.model.on('add', this.addBookmarkListElement, this);
this.model.on('remove', this.removeBookmarkListElement, this);
this.render();
}, },
render: function (cfg) { render: function (cfg) {
...@@ -314,7 +329,9 @@ ...@@ -314,7 +329,9 @@
'desc_bookmarks': __('Click to toggle the bookmarks list'), 'desc_bookmarks': __('Click to toggle the bookmarks list'),
'label_bookmarks': __('Bookmarked Rooms') 'label_bookmarks': __('Bookmarked Rooms')
})); }));
this.$bookmarks = this.$('.bookmarks'); this.model.each(this.addBookmarkListElement, this);
var controlboxview = converse.chatboxviews.get('controlbox');
this.$el.prependTo(controlboxview.$('#chatrooms'));
return this.$el; return this.$el;
}, },
...@@ -323,13 +340,11 @@ ...@@ -323,13 +340,11 @@
var name = $(ev.target).data('bookmarkName'); var name = $(ev.target).data('bookmarkName');
var jid = $(ev.target).data('roomJid'); var jid = $(ev.target).data('roomJid');
if (confirm(__(___("Are you sure you want to remove the bookmark \"%1$s\"?"), name))) { if (confirm(__(___("Are you sure you want to remove the bookmark \"%1$s\"?"), name))) {
var models = converse.bookmarks.where({'jid': jid}); _.each(converse.bookmarks.where({'jid': jid}), function (item) { item.destroy(); });
converse.bookmarks.remove(models);
} }
}, },
onBookmarkAdded: function (item) { addBookmarkListElement: function (item) {
// TODO: Try to come up with a way to avoid DOM reflows.
var $bookmark = $(converse.templates.bookmark({ var $bookmark = $(converse.templates.bookmark({
'name': item.get('name'), 'name': item.get('name'),
'jid': item.get('jid'), 'jid': item.get('jid'),
...@@ -337,12 +352,11 @@ ...@@ -337,12 +352,11 @@
'info_title': __('Show more information on this room'), 'info_title': __('Show more information on this room'),
'info_remove': __('Remove this bookmark') 'info_remove': __('Remove this bookmark')
})); }));
if (_.isUndefined(this.$bookmarks)) { this.$('.bookmarks').append($bookmark);
this.render(); },
var controlboxview = converse.chatboxviews.get('controlbox');
this.$el.prependTo(controlboxview.$('#chatrooms')); removeBookmarkListElement: function (item) {
} this.$('[data-room-jid="'+item.get('jid')+'"]:first').parent().remove();
this.$bookmarks.append($bookmark);
}, },
toggleBookmarksList: function (ev) { toggleBookmarksList: function (ev) {
...@@ -360,10 +374,11 @@ ...@@ -360,10 +374,11 @@
converse.initBookmarks = function () { converse.initBookmarks = function () {
converse.bookmarks = new converse.Bookmarks(); converse.bookmarks = new converse.Bookmarks();
converse.bookmarks.fetchBookmarks().then(function () {
converse.bookmarksview = new converse.BookmarksView( converse.bookmarksview = new converse.BookmarksView(
{'model': converse.bookmarks} {'model': converse.bookmarks}
); );
converse.bookmarks.fetchBookmarks(); });
}; };
converse.on('chatBoxesFetched', converse.initBookmarks); converse.on('chatBoxesFetched', converse.initBookmarks);
} }
......
...@@ -552,7 +552,7 @@ ...@@ -552,7 +552,7 @@
}; };
this.clearSession = function () { this.clearSession = function () {
if (this.roster) { if (!_.isUndefined(this.roster)) {
this.roster.browserStorage._clear(); this.roster.browserStorage._clear();
} }
this.session.browserStorage._clear(); this.session.browserStorage._clear();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment