Bookmarked rooms will now be automatically opened

If configured for it.
This commit is contained in:
JC Brand 2016-09-22 14:03:42 +02:00
parent e80f001b35
commit 6972066076
3 changed files with 62 additions and 10 deletions

View File

@ -2,11 +2,12 @@
(function (root, factory) { (function (root, factory) {
define([ define([
"jquery", "jquery",
"underscore",
"utils", "utils",
"mock", "mock",
"test_utils" "test_utils"
], factory); ], factory);
} (this, function ($, utils, mock, test_utils) { } (this, function ($, _, utils, mock, test_utils) {
"use strict"; "use strict";
var $iq = converse_api.env.$iq, var $iq = converse_api.env.$iq,
Strophe = converse_api.env.Strophe; Strophe = converse_api.env.Strophe;
@ -122,6 +123,26 @@
// nothing to test for here. // nothing to test for here.
}); });
it("will be automatilly opened if 'autojoin' is set on the bookmark", function () {
var jid = 'lounge@localhost';
converse.bookmarks.create({
'jid': jid,
'autojoin': false,
'name': 'The Lounge',
'nick': ' Othello'
});
expect(_.isUndefined(converse.chatboxviews.get(jid))).toBeTruthy();
jid = 'theplay@conference.shakespeare.lit';
converse.bookmarks.create({
'jid': jid,
'autojoin': true,
'name': 'The Play',
'nick': ' Othello'
});
expect(_.isUndefined(converse.chatboxviews.get(jid))).toBeFalsy();
});
describe("when bookmarked", function () { describe("when bookmarked", function () {
beforeEach(function () { beforeEach(function () {
test_utils.closeAllChatBoxes(); test_utils.closeAllChatBoxes();
@ -145,7 +166,7 @@
}); });
it("can be unbookmarked", function () { it("can be unbookmarked", function () {
var sent_stanza, IQ_id; var view, sent_stanza, IQ_id;
var sendIQ = converse.connection.sendIQ; var sendIQ = converse.connection.sendIQ;
spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) { spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) {
sent_stanza = iq; sent_stanza = iq;
@ -159,7 +180,7 @@
waits(100); waits(100);
runs(function () { runs(function () {
var jid = 'theplay@conference.shakespeare.lit'; var jid = 'theplay@conference.shakespeare.lit';
var view = converse.chatboxviews.get(jid); view = converse.chatboxviews.get(jid);
spyOn(view, 'toggleBookmark').andCallThrough(); spyOn(view, 'toggleBookmark').andCallThrough();
spyOn(converse.bookmarks, 'sendBookmarkStanza').andCallThrough(); spyOn(converse.bookmarks, 'sendBookmarkStanza').andCallThrough();
view.delegateEvents(); view.delegateEvents();
@ -170,7 +191,10 @@
'nick': ' Othello' 'nick': ' Othello'
}); });
expect(converse.bookmarks.length).toBe(1); expect(converse.bookmarks.length).toBe(1);
view.model.save('bookmarked', true); });
waits(100);
runs(function () {
expect(view.model.get('bookmarked')).toBeTruthy();
var $bookmark_icon = view.$('.icon-pushpin'); var $bookmark_icon = view.$('.icon-pushpin');
expect($bookmark_icon.hasClass('button-on')).toBeTruthy(); expect($bookmark_icon.hasClass('button-on')).toBeTruthy();
$bookmark_icon.click(); $bookmark_icon.click();

View File

@ -52,6 +52,7 @@
initialize: function () { initialize: function () {
this.__super__.initialize.apply(this, arguments); this.__super__.initialize.apply(this, arguments);
this.model.on('change:bookmarked', this.onBookmarked, this); this.model.on('change:bookmarked', this.onBookmarked, this);
this.setBookmarkState();
}, },
render: function (options) { render: function (options) {
@ -65,7 +66,22 @@
}, },
onBookmarked: function () { onBookmarked: function () {
this.$('.icon-pushpin').toggleClass('button-on'); if (this.model.get('bookmarked')) {
this.$('.icon-pushpin').addClass('button-on');
} else {
this.$('.icon-pushpin').removeClass('button-on');
}
},
setBookmarkState: function () {
if (!_.isUndefined(converse.bookmarks)) {
var models = converse.bookmarks.where({'jid': this.model.get('jid')});
if (!models.length) {
this.model.save('bookmarked', false);
} else {
this.model.save('bookmarked', true);
}
}
}, },
renderBookmarkForm: function () { renderBookmarkForm: function () {
@ -129,15 +145,29 @@
*/ */
var converse = this.converse; var converse = this.converse;
converse.Bookmark = Backbone.Model;
converse.Bookmarks = Backbone.Collection.extend({ converse.Bookmarks = Backbone.Collection.extend({
model: converse.Bookmark,
initialize: function () { initialize: function () {
this.on('add', this.markRoomAsBookmarked, this); this.on('add', this.markRoomAsBookmarked, this);
this.on('add', this.openBookmarkedRoom, this);
this.on('remove', this.markRoomAsUnbookmarked, this); this.on('remove', this.markRoomAsUnbookmarked, this);
this.browserStorage = new Backbone.BrowserStorage[converse.storage](
b64_sha1('converse.room-bookmarks'+converse.bare_jid)
);
},
openBookmarkedRoom: function (bookmark) {
if (bookmark.get('autojoin')) {
converse_api.rooms.open(bookmark.get('jid'), bookmark.get('nick'));
}
}, },
fetchBookmarks: function () { fetchBookmarks: function () {
converse.bookmarks.fetch({ this.fetch({
'add': true, 'add': true,
'success': this.onCachedBookmarksFetched.bind(this), 'success': this.onCachedBookmarksFetched.bind(this),
'error': this.onCachedBookmarksFetched.bind(this) 'error': this.onCachedBookmarksFetched.bind(this)
@ -247,9 +277,6 @@
converse.initBookmarks = function () { converse.initBookmarks = function () {
converse.bookmarks = new converse.Bookmarks(); converse.bookmarks = new converse.Bookmarks();
var id = b64_sha1('converse.room-bookmarks');
converse.bookmarks.id = id;
converse.bookmarks.browserStorage = new Backbone.BrowserStorage[converse.storage](id);
converse.bookmarks.fetchBookmarks(); converse.bookmarks.fetchBookmarks();
}; };
converse.on('connected', converse.initBookmarks); converse.on('connected', converse.initBookmarks);

View File

@ -1676,7 +1676,8 @@
initialize: function () { initialize: function () {
this.addClientIdentities().addClientFeatures(); this.addClientIdentities().addClientFeatures();
this.browserStorage = new Backbone.BrowserStorage[converse.storage]( this.browserStorage = new Backbone.BrowserStorage[converse.storage](
b64_sha1('converse.features'+converse.bare_jid)); b64_sha1('converse.features'+converse.bare_jid)
);
this.on('add', this.onFeatureAdded, this); this.on('add', this.onFeatureAdded, this);
if (this.browserStorage.records.length === 0) { if (this.browserStorage.records.length === 0) {
// browserStorage is empty, so we've likely never queried this // browserStorage is empty, so we've likely never queried this