Various bookmarks fixes.

Including:
- Always render bookmarks list
- Hide bookmarks list if no bookmarks available
- Only send bookmarks stanza when appropriate
This commit is contained in:
JC Brand 2016-10-27 10:49:09 +00:00
parent b1c2a71927
commit bc5cd95a98

View File

@ -129,15 +129,14 @@
label_submit: __('Save'), label_submit: __('Save'),
label_cancel: __('Cancel') label_cancel: __('Cancel')
})); }));
this.$('.chatroom-form').submit(this.addBookmark.bind(this)); this.$('.chatroom-form').submit(this.onBookmarkFormSubmitted.bind(this));
this.$('.chatroom-form .button-cancel').on('click', this.cancelConfiguration.bind(this)); this.$('.chatroom-form .button-cancel').on('click', this.cancelConfiguration.bind(this));
}, },
addBookmark: function (ev) { onBookmarkFormSubmitted: function (ev) {
ev.preventDefault(); ev.preventDefault();
var $form = $(ev.target), var $form = $(ev.target), that = this;
that = this; converse.bookmarks.createBookmark({
converse.bookmarks.create({
'jid': this.model.get('jid'), 'jid': this.model.get('jid'),
'autojoin': $form.find('input[name="autojoin"]').prop('checked'), 'autojoin': $form.find('input[name="autojoin"]').prop('checked'),
'name': $form.find('input[name=name]').val(), 'name': $form.find('input[name=name]').val(),
@ -159,7 +158,9 @@
if (!models.length) { if (!models.length) {
this.renderBookmarkForm(); this.renderBookmarkForm();
} else { } else {
converse.bookmarks.remove(models); _.each(models, function (model) {
model.destroy();
});
this.$('.icon-pushpin').removeClass('button-on'); this.$('.icon-pushpin').removeClass('button-on');
} }
} }
@ -178,22 +179,17 @@
model: converse.Bookmark, model: converse.Bookmark,
initialize: function () { initialize: function () {
this.on('bookmarksFetched', this.onBookmarksFetched, this); this.on('add', _.compose(this.markRoomAsBookmarked, this.openBookmarkedRoom));
this.on('add', this.markRoomAsBookmarked, this);
this.on('add', this.openBookmarkedRoom, this);
this.on('add', this.sendBookmarkStanza, this);
this.on('remove', this.markRoomAsUnbookmarked, this); this.on('remove', this.markRoomAsUnbookmarked, this);
this.on('remove', this.sendBookmarkStanza, this); this.on('remove', this.sendBookmarkStanza, this);
var cache_key = 'converse.room-bookmarks'+converse.bare_jid;
this.cached_flag = b64_sha1(cache_key+'fetched');
this.browserStorage = new Backbone.BrowserStorage[converse.storage]( this.browserStorage = new Backbone.BrowserStorage[converse.storage](
b64_sha1('converse.room-bookmarks'+converse.bare_jid) b64_sha1(cache_key)
); );
}, },
onBookmarksFetched: function () {
this.each(_.compose(this.markRoomAsBookmarked, this.openBookmarkedRoom));
},
openBookmarkedRoom: function (bookmark) { openBookmarkedRoom: function (bookmark) {
if (bookmark.get('autojoin')) { if (bookmark.get('autojoin')) {
converse_api.rooms.open(bookmark.get('jid'), bookmark.get('nick')); converse_api.rooms.open(bookmark.get('jid'), bookmark.get('nick'));
@ -203,12 +199,31 @@
fetchBookmarks: function () { fetchBookmarks: function () {
var deferred = new $.Deferred(); var deferred = new $.Deferred();
this.fetch({ var promise = deferred.promise();
'silent': true, if (window.sessionStorage.getItem(this.browserStorage.name)) {
'success': _.bind(this.onCachedBookmarksFetched, this, deferred), this.fetch({
'error': _.bind(this.onCachedBookmarksFetched, this, deferred) 'success': _.bind(this.onCachedBookmarksFetched, this, deferred),
}); 'error': _.bind(this.onCachedBookmarksFetched, this, deferred)
return deferred.promise(); });
} else if (! window.sessionStorage.getItem(this.cached_flag)) {
// There aren't any cached bookmarks, and the cache is
// not set to null. So we query the XMPP server.
// If nothing is returned from the XMPP server, we set
// the cache to null to avoid calling the server again.
this.fetchBookmarksFromServer(deferred);
} else {
deferred.resolve();
}
return promise;
},
onCachedBookmarksFetched: function (deferred) {
return deferred.resolve();
},
createBookmark: function (options) {
converse.bookmarks.create(options);
converse.bookmarks.sendBookmarkStanza();
}, },
sendBookmarkStanza: function () { sendBookmarkStanza: function () {
@ -220,7 +235,6 @@
.c('publish', {'node': 'storage:bookmarks'}) .c('publish', {'node': 'storage:bookmarks'})
.c('item', {'id': 'current'}) .c('item', {'id': 'current'})
.c('storage', {'xmlns':'storage:bookmarks'}); .c('storage', {'xmlns':'storage:bookmarks'});
this.each(function (model) { this.each(function (model) {
stanza = stanza.c('conference', { stanza = stanza.c('conference', {
'name': model.get('name'), 'name': model.get('name'),
@ -250,17 +264,6 @@
window.alert(__("Sorry, something went wrong while trying to save your bookmark.")); window.alert(__("Sorry, something went wrong while trying to save your bookmark."));
}, },
onCachedBookmarksFetched: function (deferred) {
if (!window.sessionStorage.getItem(this.browserStorage.name)) {
// There aren't any cached bookmarks, so we query the
// XMPP server.
this.fetchBookmarksFromServer(deferred);
} else {
this.trigger('bookmarksFetched');
return deferred.resolve();
}
},
fetchBookmarksFromServer: function (deferred) { fetchBookmarksFromServer: function (deferred) {
var stanza = $iq({ var stanza = $iq({
'from': converse.connection.jid, 'from': converse.connection.jid,
@ -270,7 +273,7 @@
converse.connection.sendIQ( converse.connection.sendIQ(
stanza, stanza,
_.bind(this.onBookmarksReceived, this, deferred), _.bind(this.onBookmarksReceived, this, deferred),
_.partial(this.onBookmarksReceivedError, deferred) _.bind(this.onBookmarksReceivedError, this, deferred)
); );
}, },
@ -299,15 +302,15 @@
'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}); });
}); });
this.trigger('bookmarksFetched');
if (!_.isUndefined(deferred)) { if (!_.isUndefined(deferred)) {
return deferred.resolve(); return deferred.resolve();
} }
}, },
onBookmarksReceivedError: function (deferred, iq) { onBookmarksReceivedError: function (deferred, iq) {
window.sessionStorage.setItem(this.cached_flag, true);
converse.log('Error while fetching bookmarks'); converse.log('Error while fetching bookmarks');
converse.log(iq); converse.log(iq);
if (!_.isUndefined(deferred)) { if (!_.isUndefined(deferred)) {
@ -325,7 +328,7 @@
}, },
initialize: function () { initialize: function () {
this.model.on('add', this.addBookmarkListElement, this); this.model.on('add', this.renderBookmarkListElement, this);
this.model.on('remove', this.removeBookmarkListElement, this); this.model.on('remove', this.removeBookmarkListElement, this);
this.render(); this.render();
}, },
@ -335,8 +338,8 @@
'toggle_state': converse.OPENED, 'toggle_state': converse.OPENED,
'desc_bookmarks': __('Click to toggle the bookmarks list'), 'desc_bookmarks': __('Click to toggle the bookmarks list'),
'label_bookmarks': __('Bookmarked Rooms') 'label_bookmarks': __('Bookmarked Rooms')
})); })).hide();
this.model.each(this.addBookmarkListElement, this); this.model.each(this.renderBookmarkListElement, this);
var controlboxview = converse.chatboxviews.get('controlbox'); var controlboxview = converse.chatboxviews.get('controlbox');
this.$el.prependTo(controlboxview.$('#chatrooms')); this.$el.prependTo(controlboxview.$('#chatrooms'));
return this.$el; return this.$el;
@ -351,7 +354,7 @@
} }
}, },
addBookmarkListElement: function (item) { renderBookmarkListElement: function (item) {
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'),
@ -360,10 +363,16 @@
'info_remove': __('Remove this bookmark') 'info_remove': __('Remove this bookmark')
})); }));
this.$('.bookmarks').append($bookmark); this.$('.bookmarks').append($bookmark);
if (!this.$el.is(':visible')) {
this.$el.show();
}
}, },
removeBookmarkListElement: function (item) { removeBookmarkListElement: function (item) {
this.$('[data-room-jid="'+item.get('jid')+'"]:first').parent().remove(); this.$('[data-room-jid="'+item.get('jid')+'"]:first').parent().remove();
if (this.model.length === 0) {
this.$el.hide();
}
}, },
toggleBookmarksList: function (ev) { toggleBookmarksList: function (ev) {
@ -381,7 +390,7 @@
var initBookmarks = function () { var initBookmarks = function () {
converse.bookmarks = new converse.Bookmarks(); converse.bookmarks = new converse.Bookmarks();
converse.bookmarks.fetchBookmarks().then(function () { converse.bookmarks.fetchBookmarks().always(function () {
converse.bookmarksview = new converse.BookmarksView( converse.bookmarksview = new converse.BookmarksView(
{'model': converse.bookmarks} {'model': converse.bookmarks}
); );