Add closure get/set methods for rosteritem views

This commit is contained in:
JC Brand 2014-03-04 15:48:16 +02:00
parent d20ca10638
commit 1567b212c3
4 changed files with 44 additions and 18 deletions

View File

@ -442,7 +442,7 @@
return converse.default_box_height;
}
var resistance = 10;
if ((height !== converse.default_box_height) &&
if ((height !== converse.default_box_height) &&
(Math.abs(height - converse.default_box_height) < resistance)) {
return converse.default_box_height;
}
@ -2925,9 +2925,14 @@
this.RosterView = Backbone.View.extend({
tagName: 'dl',
id: 'converse-roster',
rosteritemviews: {},
initialize: function () {
var views = {};
this.get = function (id) {
return views[id];
};
this.set = function (id, view) { views[id] = view; };
this.model.on("add", function (item) {
this.addRosterItemView(item).render(item);
if (!item.get('vcard_updated')) {
@ -2983,15 +2988,15 @@
addRosterItemView: function (item) {
var view = new converse.RosterItemView({model: item});
this.rosteritemviews[item.id] = view;
this.set(item.id, view);
return this;
},
removeRosterItemView: function (item) {
var view = this.rosteritemviews[item.id];
var view = this.get(item.id);
if (view) {
view.$el.remove();
delete this.rosteritemviews[item.id];
delete this.get(item.id);
this.render();
}
return this;
@ -3018,7 +3023,7 @@
$count, changed_presence;
if (item) {
var jid = item.id,
view = this.rosteritemviews[item.id],
view = this.get(item.id),
ask = item.get('ask'),
subscription = item.get('subscription'),
requesting = item.get('requesting'),

View File

@ -39,7 +39,7 @@
for (i=0; i<online_contacts.length; i++) {
$el = $(online_contacts[i]);
jid = $el.text().replace(' ','.').toLowerCase() + '@localhost';
view = this.rosterview.rosteritemviews[jid];
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();

View File

@ -9,6 +9,14 @@
} (this, function (mock, utils) {
describe("The Control Box", $.proxy(function (mock, utils) {
beforeEach(function () {
runs(function () {
utils.openControlBox();
});
waits(250);
runs(function () {});
});
it("can be opened by clicking a DOM element with class 'toggle-online-users'", $.proxy(function () {
runs(function () {
utils.closeControlBox();
@ -35,6 +43,15 @@
}, converse));
describe("The Status Widget", $.proxy(function () {
beforeEach(function () {
runs(function () {
utils.openControlBox();
});
waits(250);
runs(function () {});
});
it("shows the user's chat status, which is online by default", $.proxy(function () {
var view = this.xmppstatusview;
expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(true);
@ -96,6 +113,9 @@
}, converse, mock, utils));
describe("The Contacts Roster", $.proxy(function (mock, utils) {
// FIXME: These tests are dependent on being run in order and cannot be
// run independently
describe("Pending Contacts", $.proxy(function () {
beforeEach(function () {
runs(function () {
@ -134,7 +154,8 @@
}, converse));
it("can be removed by the user", $.proxy(function () {
var view = _.toArray(this.rosterview.rosteritemviews).pop();
var jid = mock.pend_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
var view = this.rosterview.get(jid);
spyOn(window, 'confirm').andReturn(true);
spyOn(converse, 'emit');
spyOn(this.connection.roster, 'remove').andCallThrough();
@ -234,7 +255,7 @@
spyOn(this.rosterview, 'render').andCallThrough();
for (i=0; i<3; i++) {
jid = mock.cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
view = this.rosterview.rosteritemviews[jid];
view = this.rosterview.get(jid);
spyOn(view, 'render').andCallThrough();
item = view.model;
item.set('chat_status', 'online');
@ -253,7 +274,7 @@
spyOn(this.rosterview, 'render').andCallThrough();
for (i=3; i<6; i++) {
jid = mock.cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
view = this.rosterview.rosteritemviews[jid];
view = this.rosterview.get(jid);
spyOn(view, 'render').andCallThrough();
item = view.model;
item.set('chat_status', 'dnd');
@ -272,7 +293,7 @@
spyOn(this.rosterview, 'render').andCallThrough();
for (i=6; i<9; i++) {
jid = mock.cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
view = this.rosterview.rosteritemviews[jid];
view = this.rosterview.get(jid);
spyOn(view, 'render').andCallThrough();
item = view.model;
item.set('chat_status', 'away');
@ -291,7 +312,7 @@
spyOn(this.rosterview, 'render').andCallThrough();
for (i=9; i<12; i++) {
jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
view = this.rosterview.rosteritemviews[jid];
view = this.rosterview.get(jid);
spyOn(view, 'render').andCallThrough();
item = view.model;
item.set('chat_status', 'xa');
@ -310,7 +331,7 @@
spyOn(this.rosterview, 'render').andCallThrough();
for (i=12; i<15; i++) {
jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
view = this.rosterview.rosteritemviews[jid];
view = this.rosterview.get(jid);
spyOn(view, 'render').andCallThrough();
item = view.model;
item.set('chat_status', 'unavailable');
@ -388,7 +409,7 @@
// actually not accepted/authorized because of
// mock_connection.
var jid = mock.req_names.sort()[0].replace(' ','.').toLowerCase() + '@localhost';
var view = this.rosterview.rosteritemviews[jid];
var view = this.rosterview.get(jid);
spyOn(this.connection.roster, 'authorize');
spyOn(view, 'acceptRequest').andCallThrough();
view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
@ -400,7 +421,7 @@
it("can have their requests denied by the user", $.proxy(function () {
var jid = mock.req_names.sort()[1].replace(/ /g,'.').toLowerCase() + '@localhost';
var view = this.rosterview.rosteritemviews[jid];
var view = this.rosterview.get(jid);
spyOn(converse, 'emit');
spyOn(this.connection.roster, 'unauthorize');
spyOn(this.rosterview, 'removeRosterItemView').andCallThrough();
@ -451,7 +472,7 @@
// we make some online now
for (i=0; i<5; i++) {
jid = mock.cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
view = this.rosterview.rosteritemviews[jid];
view = this.rosterview.get(jid);
view.model.set('chat_status', 'online');
}
}, converse));

View File

@ -81,13 +81,13 @@
var i = 0, jid, views = [];
for (i; i<amount; i++) {
jid = mock.cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
views[i] = converse.rosterview.rosteritemviews[jid].openChat(mock.event);
views[i] = converse.rosterview.get(jid).openChat(mock.event);
}
return views;
};
utils.openChatBoxFor = function (jid) {
converse.rosterview.rosteritemviews[jid].openChat(mock.event);
converse.rosterview.get(jid).openChat(mock.event);
};
utils.clearChatBoxMessages = function (jid) {