Commit 2b927f21 authored by JC Brand's avatar JC Brand

Big refactor. The RosterView is no longer an overview.

This is because we can no longer assume a one to one mapping between roster
contacts and their views. Roster contacts can belong to more than one group,
each group needs to show the contact, which means we need a view for each group
the contact belongs to.

updates #83
parent 6a82c087
...@@ -2724,9 +2724,7 @@ ...@@ -2724,9 +2724,7 @@
}, },
toggle: function (ev) { toggle: function (ev) {
if (ev && ev.preventDefault) { if (ev && ev.preventDefault) { ev.preventDefault(); }
ev.preventDefault();
}
this.toggleview.model.save({'collapsed': !this.toggleview.model.get('collapsed')}); this.toggleview.model.save({'collapsed': !this.toggleview.model.get('collapsed')});
this.$('.minimized-chats-flyout').toggle(); this.$('.minimized-chats-flyout').toggle();
}, },
...@@ -2830,6 +2828,9 @@ ...@@ -2830,6 +2828,9 @@
initialize: function () { initialize: function () {
this.model.on("change", this.onChange, this); this.model.on("change", this.onChange, this);
this.model.on("remove", this.remove, this);
this.model.on("destroy", this.remove, this);
this.model.on("open", this.openChat, this);
}, },
onChange: function () { onChange: function () {
...@@ -2845,7 +2846,7 @@ ...@@ -2845,7 +2846,7 @@
}, },
openChat: function (ev) { openChat: function (ev) {
ev.preventDefault(); if (ev && ev.preventDefault) { ev.preventDefault(); }
return converse.chatboxviews.showChat({ return converse.chatboxviews.showChat({
'id': this.model.get('jid'), 'id': this.model.get('jid'),
'jid': this.model.get('jid'), 'jid': this.model.get('jid'),
...@@ -2858,7 +2859,7 @@ ...@@ -2858,7 +2859,7 @@
}, },
removeContact: function (ev) { removeContact: function (ev) {
ev.preventDefault(); if (ev && ev.preventDefault) { ev.preventDefault(); }
var result = confirm(__("Are you sure you want to remove this contact?")); var result = confirm(__("Are you sure you want to remove this contact?"));
if (result === true) { if (result === true) {
var bare_jid = this.model.get('jid'); var bare_jid = this.model.get('jid');
...@@ -3250,7 +3251,7 @@ ...@@ -3250,7 +3251,7 @@
} }
}); });
this.RosterView = Backbone.Overview.extend({ this.RosterView = Backbone.View.extend({
tagName: 'dl', tagName: 'dl',
id: 'converse-roster', id: 'converse-roster',
events: { events: {
...@@ -3294,6 +3295,7 @@ ...@@ -3294,6 +3295,7 @@
} }
this.$fragment = $('<span>'); this.$fragment = $('<span>');
this.$fragment.append($(roster_markup)); this.$fragment.append($(roster_markup));
return this;
}, },
update: function (item) { update: function (item) {
...@@ -3301,8 +3303,8 @@ ...@@ -3301,8 +3303,8 @@
}, },
reset: function () { reset: function () {
this.removeAll(); this.$el.find('span').empty();
this.update(); this.render().update();
return this; return this;
}, },
...@@ -3396,34 +3398,34 @@ ...@@ -3396,34 +3398,34 @@
return $group; return $group;
}, },
addCurrentContact: function (view) { addCurrentContact: function (item) {
var item = view.model;
if (converse.roster_groups) { if (converse.roster_groups) {
if (item.get('groups').length === 0) { if (item.get('groups').length === 0) {
this.getRoster().find('.roster-group[data-group="'+HEADER_UNGROUPED+'"]').after(view.el); this.getRoster().find('.roster-group[data-group="'+HEADER_UNGROUPED+'"]')
.after((new converse.RosterItemView({model: item})).render().el);
} else { } else {
_.each(item.get('groups'), $.proxy(function (group) { _.each(item.get('groups'), $.proxy(function (group) {
this.getGroup(group).after(view.el); // We need a separate view per group
this.getGroup(group).after((new converse.RosterItemView({model: item})).render().el);
},this)); },this));
} }
} else { } else {
this.getRoster().find('.roster-group[data-group="'+HEADER_CURRENT_CONTACTS+'"]').after(view.el); this.getRoster().find('.roster-group[data-group="'+HEADER_CURRENT_CONTACTS+'"]')
.after((new converse.RosterItemView({model: item})).render().el);
} }
}, },
addRosterItem: function (item) { addRosterItem: function (item) {
var view = new converse.RosterItemView({model: item}); var view;
this.add(item.id, view); if (item.get('subscription') === 'both' || item.get('subscription') === 'to') {
if ((converse.show_only_online_users) && (item.get('chat_status') !== 'online')) { this.addCurrentContact(item);
return this; } else {
} view = (new converse.RosterItemView({model: item})).render();
view.render() if ((item.get('ask') === 'subscribe') || (item.get('subscription') === 'from')) {
if (view.$el.hasClass('current-xmpp-contact')) { this.getRoster().find('#pending-xmpp-contacts').after(view.el);
this.addCurrentContact(view); } else if (item.get('requesting') === true) {
} else if (view.$el.hasClass('pending-xmpp-contact')) { this.getRoster().find('#xmpp-contact-requests').after(view.el);
this.getRoster().find('#pending-xmpp-contacts').after(view.el); }
} else if (view.$el.hasClass('requesting-xmpp-contact')) {
this.getRoster().find('#xmpp-contact-requests').after(view.render().el);
} }
return this; return this;
}, },
...@@ -3496,9 +3498,7 @@ ...@@ -3496,9 +3498,7 @@
}, },
sortRoster: function (chat_status) { sortRoster: function (chat_status) {
/* XXX /* XXX See if the jquery detach method can be somehow used to avoid DOM reflows.
* 1). See if the jquery detach method can be somehow used to avoid DOM reflows.
* 2). Likewise see if documentFragment can be used.
*/ */
var $el = this.getRoster(); var $el = this.getRoster();
$el.find('.roster-group').each($.proxy(function (idx, group) { $el.find('.roster-group').each($.proxy(function (idx, group) {
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
}); });
it("is created when you click on a roster item", $.proxy(function () { it("is created when you click on a roster item", $.proxy(function () {
var i, $el, click, jid, view, chatboxview; var i, $el, click, jid, chatboxview;
// openControlBox was called earlier, so the controlbox is // openControlBox was called earlier, so the controlbox is
// visible, but no other chat boxes have been created. // visible, but no other chat boxes have been created.
expect(this.chatboxes.length).toEqual(1); expect(this.chatboxes.length).toEqual(1);
...@@ -33,12 +33,8 @@ ...@@ -33,12 +33,8 @@
for (i=0; i<online_contacts.length; i++) { for (i=0; i<online_contacts.length; i++) {
$el = $(online_contacts[i]); $el = $(online_contacts[i]);
jid = $el.text().replace(/ /g,'.').toLowerCase() + '@localhost'; jid = $el.text().replace(/ /g,'.').toLowerCase() + '@localhost';
view = this.rosterview.get(jid);
spyOn(view, 'openChat').andCallThrough();
view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
$el.click(); $el.click();
chatboxview = this.chatboxviews.get(jid); chatboxview = this.chatboxviews.get(jid);
expect(view.openChat).toHaveBeenCalled();
expect(this.chatboxes.length).toEqual(i+2); expect(this.chatboxes.length).toEqual(i+2);
expect(this.chatboxviews.trimChats).toHaveBeenCalled(); expect(this.chatboxviews.trimChats).toHaveBeenCalled();
// Check that new chat boxes are created to the left of the // Check that new chat boxes are created to the left of the
...@@ -49,7 +45,7 @@ ...@@ -49,7 +45,7 @@
}, converse)); }, converse));
it("can be trimmed to conserve space", $.proxy(function () { it("can be trimmed to conserve space", $.proxy(function () {
var i, $el, click, jid, key, view, chatbox, chatboxview; var i, $el, click, jid, key, chatbox, chatboxview;
// openControlBox was called earlier, so the controlbox is // openControlBox was called earlier, so the controlbox is
// visible, but no other chat boxes have been created. // visible, but no other chat boxes have been created.
var trimmed_chatboxes = converse.minimized_chats; var trimmed_chatboxes = converse.minimized_chats;
...@@ -64,7 +60,6 @@ ...@@ -64,7 +60,6 @@
for (i=0; i<online_contacts.length; i++) { for (i=0; i<online_contacts.length; i++) {
$el = $(online_contacts[i]); $el = $(online_contacts[i]);
jid = $el.text().replace(/ /g,'.').toLowerCase() + '@localhost'; jid = $el.text().replace(/ /g,'.').toLowerCase() + '@localhost';
view = this.rosterview.get(jid);
$el.click(); $el.click();
expect(this.chatboxviews.trimChats).toHaveBeenCalled(); expect(this.chatboxviews.trimChats).toHaveBeenCalled();
...@@ -96,7 +91,7 @@ ...@@ -96,7 +91,7 @@
it("is focused if its already open and you click on its corresponding roster item", $.proxy(function () { it("is focused if its already open and you click on its corresponding roster item", $.proxy(function () {
var contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost'; var contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
var i, $el, click, jid, view, chatboxview, chatbox; var i, $el, click, jid, chatboxview, chatbox;
// openControlBox was called earlier, so the controlbox is // openControlBox was called earlier, so the controlbox is
// visible, but no other chat boxes have been created. // visible, but no other chat boxes have been created.
expect(this.chatboxes.length).toEqual(1); expect(this.chatboxes.length).toEqual(1);
...@@ -105,11 +100,7 @@ ...@@ -105,11 +100,7 @@
spyOn(chatboxview, 'focus'); spyOn(chatboxview, 'focus');
$el = this.rosterview.$el.find('a.open-chat:contains("'+chatbox.get('fullname')+'")'); $el = this.rosterview.$el.find('a.open-chat:contains("'+chatbox.get('fullname')+'")');
jid = $el.text().replace(/ /g,'.').toLowerCase() + '@localhost'; jid = $el.text().replace(/ /g,'.').toLowerCase() + '@localhost';
view = this.rosterview.get(jid);
spyOn(view, 'openChat').andCallThrough();
view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
$el.click(); $el.click();
expect(view.openChat).toHaveBeenCalled();
expect(this.chatboxes.length).toEqual(2); expect(this.chatboxes.length).toEqual(2);
expect(chatboxview.focus).toHaveBeenCalled(); expect(chatboxview.focus).toHaveBeenCalled();
}, converse)); }, converse));
......
This diff is collapsed.
...@@ -80,13 +80,21 @@ ...@@ -80,13 +80,21 @@
var i = 0, jid, views = []; var i = 0, jid, views = [];
for (i; i<amount; i++) { for (i; i<amount; i++) {
jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost'; jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
views[i] = converse.rosterview.get(jid).openChat(mock.event); views[i] = converse.roster.get(jid).trigger("open");
} }
return views; return views;
}; };
utils.openChatBoxFor = function (jid) { utils.openChatBoxFor = function (jid) {
return converse.rosterview.get(jid).openChat(mock.event); return converse.roster.get(jid).trigger("open");
};
utils.removeRosterContacts = function () {
var model;
while (converse.rosterview.model.length) {
model = converse.rosterview.model.pop();
converse.rosterview.model.remove(model);
}
}; };
utils.clearBrowserStorage = function () { utils.clearBrowserStorage = function () {
......
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