Add closured get/set methods for views from chatboxesview

This commit is contained in:
JC Brand 2014-03-04 15:09:16 +02:00
parent ac0042fdf9
commit d20ca10638
5 changed files with 49 additions and 47 deletions

View File

@ -348,7 +348,7 @@
};
this.showLoginButton = function () {
var view = converse.chatboxesview.views.controlbox;
var view = converse.chatboxesview.get('controlbox');
if (typeof view.loginpanel !== 'undefined') {
view.loginpanel.showLoginButton();
}
@ -1759,7 +1759,7 @@
'box_id' : hex_sha1(jid)
});
if (!chatroom.get('connected')) {
converse.chatboxesview.views[jid].connect(null);
converse.chatboxesview.get(jid).connect(null);
}
}
});
@ -2351,7 +2351,7 @@
onChatRoomRoster: function (roster, room) {
this.renderChatArea();
var controlboxview = converse.chatboxesview.views.controlbox,
var controlboxview = converse.chatboxesview.get('controlbox'),
roster_size = _.size(roster),
$participant_list = this.$el.find('.participant-list'),
participants = [], keys = _.keys(roster), i;
@ -2466,10 +2466,12 @@
el: '#conversejs',
initialize: function () {
// boxesviewinit
this.views = {};
var views = {};
this.get = function (id) { return views[id]; };
this.set = function (id, view) { views[id] = view; };
this.model.on("add", function (item) {
var view = this.views[item.get('id')];
var view = this.get(item.get('id'));
if (!view) {
if (item.get('chatroom')) {
view = new converse.ChatRoomView({'model': item});
@ -2479,7 +2481,7 @@
} else {
view = new converse.ChatBoxView({model: item});
}
this.views[item.get('id')] = view;
this.set(item.get('id'), view);
} else {
delete view.model; // Remove ref to old model to help garbage collection
view.model = item;

View File

@ -79,8 +79,8 @@
it("can be closed by clicking a DOM element with class 'close-chatbox-button'", $.proxy(function () {
var chatbox = utils.openChatBoxes(1)[0],
controlview = this.chatboxesview.views.controlbox, // The controlbox is currently open
chatview = this.chatboxesview.views[chatbox.get('jid')];
controlview = this.chatboxesview.get('controlbox'), // The controlbox is currently open
chatview = this.chatboxesview.get(chatbox.get('jid'));
spyOn(chatview, 'closeChat').andCallThrough();
spyOn(controlview, 'closeChat').andCallThrough();
spyOn(converse, 'emit');
@ -141,7 +141,7 @@
var contact_jid = mock.cur_names[2].replace(' ','.').toLowerCase() + '@localhost';
utils.openChatBoxFor(contact_jid);
var chatbox = this.chatboxes.get(contact_jid);
var view = this.chatboxesview.views[contact_jid];
var view = this.chatboxesview.get(contact_jid);
expect(chatbox).toBeDefined();
expect(view).toBeDefined();
var $toolbar = view.$el.find('ul.chat-toolbar');
@ -153,7 +153,7 @@
var contact_jid = mock.cur_names[2].replace(' ','.').toLowerCase() + '@localhost';
utils.openChatBoxFor(contact_jid);
var chatbox = this.chatboxes.get(contact_jid);
var view = this.chatboxesview.views[contact_jid];
var view = this.chatboxesview.get(contact_jid);
var $toolbar = view.$el.find('ul.chat-toolbar');
var $textarea = view.$el.find('textarea.chat-textarea');
expect($toolbar.children('li.toggle-smiley').length).toBe(1);
@ -213,7 +213,7 @@
var contact_jid = mock.cur_names[2].replace(' ','.').toLowerCase() + '@localhost';
utils.openChatBoxFor(contact_jid);
var chatbox = this.chatboxes.get(contact_jid);
var view = this.chatboxesview.views[contact_jid];
var view = this.chatboxesview.get(contact_jid);
var $toolbar = view.$el.find('ul.chat-toolbar');
expect($toolbar.children('li.toggle-otr').length).toBe(1);
// Register spies
@ -239,7 +239,7 @@
var contact_jid = mock.cur_names[2].replace(' ','.').toLowerCase() + '@localhost';
utils.openChatBoxFor(contact_jid);
var chatbox = this.chatboxes.get(contact_jid);
var view = this.chatboxesview.views[contact_jid];
var view = this.chatboxesview.get(contact_jid);
var $toolbar = view.$el.find('ul.chat-toolbar');
var callButton = $toolbar.find('.toggle-call');
@ -278,7 +278,7 @@
runs($.proxy(function () {
// Check that the chatbox and its view now exist
var chatbox = this.chatboxes.get(sender_jid);
var chatboxview = this.chatboxesview.views[sender_jid];
var chatboxview = this.chatboxesview.get(sender_jid);
expect(chatbox).toBeDefined();
expect(chatboxview).toBeDefined();
// Check that the message was received and check the
@ -311,7 +311,7 @@
var one_day_ago = new Date(new Date().setDate(new Date().getDate()-1));
var message = 'This is a day old message';
var chatbox = this.chatboxes.get(contact_jid);
var chatboxview = this.chatboxesview.views[contact_jid];
var chatboxview = this.chatboxesview.get(contact_jid);
var $chat_content = chatboxview.$el.find('.chat-content');
var msg_obj;
var msg_txt;
@ -380,7 +380,7 @@
waits(250);
runs(function () {
expect(converse.emit).toHaveBeenCalledWith('onChatBoxFocused', jasmine.any(Object));
var view = this.chatboxesview.views[contact_jid];
var view = this.chatboxesview.get(contact_jid);
var message = 'This message is sent from this chatbox';
spyOn(view, 'sendMessage').andCallThrough();
view.$el.find('.chat-textarea').text(message);
@ -396,7 +396,7 @@
it("are sanitized to prevent Javascript injection attacks", $.proxy(function () {
var contact_jid = mock.cur_names[0].replace(' ','.').toLowerCase() + '@localhost';
utils.openChatBoxFor(contact_jid);
var view = this.chatboxesview.views[contact_jid];
var view = this.chatboxesview.get(contact_jid);
var message = 'This message contains <b>markup</b>';
spyOn(view, 'sendMessage').andCallThrough();
view.$el.find('.chat-textarea').text(message);
@ -413,7 +413,7 @@
it("'/clear' can be used to clear messages in a conversation", $.proxy(function () {
spyOn(converse, 'emit');
var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
var view = this.chatboxesview.views[contact_jid];
var view = this.chatboxesview.get(contact_jid);
var message = 'This message is another sent from this chatbox';
// Lets make sure there is at least one message already
// (e.g for when this test is run on its own).

View File

@ -24,7 +24,7 @@
waits(300);
runs(function () {
// Open a new chatroom
var roomspanel = converse.chatboxesview.views.controlbox.roomspanel;
var roomspanel = converse.chatboxesview.get('controlbox').roomspanel;
var $input = roomspanel.$el.find('input.new-chatroom-name');
var $nick = roomspanel.$el.find('input.new-chatroom-nick');
var $server = roomspanel.$el.find('input.new-chatroom-server');
@ -42,7 +42,7 @@
});
it("shows users currently present in the room", $.proxy(function () {
var chatroomview = this.chatboxesview.views['lounge@muc.localhost'],
var chatroomview = this.chatboxesview.get('lounge@muc.localhost'),
$participant_list;
var roster = {}, room = {}, i;
for (i=0; i<mock.chatroom_names.length-1; i++) {
@ -57,7 +57,7 @@
}, converse));
it("indicates moderators by means of a special css class and tooltip", $.proxy(function () {
var chatroomview = this.chatboxesview.views['lounge@muc.localhost'];
var chatroomview = this.chatboxesview.get('lounge@muc.localhost');
var roster = {}, idx = mock.chatroom_names.length-1;
roster[mock.chatroom_names[idx]] = {};
roster[mock.chatroom_names[idx]].role = 'moderator';
@ -71,7 +71,7 @@
it("shows received groupchat messages", $.proxy(function () {
spyOn(converse, 'emit');
var view = this.chatboxesview.views['lounge@muc.localhost'];
var view = this.chatboxesview.get('lounge@muc.localhost');
if (!view.$el.find('.chat-area').length) { view.renderChatArea(); }
var nick = mock.chatroom_names[0];
var text = 'This is a received message';
@ -90,7 +90,7 @@
it("shows sent groupchat messages", $.proxy(function () {
spyOn(converse, 'emit');
var view = this.chatboxesview.views['lounge@muc.localhost'];
var view = this.chatboxesview.get('lounge@muc.localhost');
if (!view.$el.find('.chat-area').length) { view.renderChatArea(); }
var nick = mock.chatroom_names[0];
var text = 'This is a sent message';
@ -133,7 +133,7 @@
}, converse));
it("can be closed again by clicking a DOM element with class 'close-chatbox-button'", $.proxy(function () {
var view = this.chatboxesview.views['lounge@muc.localhost'], chatroom = view.model, $el;
var view = this.chatboxesview.get('lounge@muc.localhost'), chatroom = view.model, $el;
spyOn(view, 'closeChat').andCallThrough();
spyOn(converse, 'emit');
spyOn(converse.connection.muc, 'leave');
@ -152,7 +152,7 @@
describe("When attempting to enter a chatroom", $.proxy(function () {
beforeEach($.proxy(function () {
var roomspanel = this.chatboxesview.views.controlbox.roomspanel;
var roomspanel = this.chatboxesview.get('controlbox').roomspanel;
var $input = roomspanel.$el.find('input.new-chatroom-name');
var $nick = roomspanel.$el.find('input.new-chatroom-nick');
var $server = roomspanel.$el.find('input.new-chatroom-server');
@ -163,7 +163,7 @@
}, converse));
afterEach($.proxy(function () {
var view = this.chatboxesview.views['problematic@muc.localhost'];
var view = this.chatboxesview.get('problematic@muc.localhost');
view.closeChat();
}, converse));
@ -177,7 +177,7 @@
.c('error').attrs({by:'coven@chat.shakespeare.lit', type:'auth'})
.c('not-authorized').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
var view = this.chatboxesview.views['problematic@muc.localhost'];
var view = this.chatboxesview.get('problematic@muc.localhost');
spyOn(view, 'renderPasswordForm').andCallThrough();
runs(function () {
view.onChatRoomPresence(presence, {'nick': 'dummy'});
@ -200,7 +200,7 @@
.c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
.c('error').attrs({by:'coven@chat.shakespeare.lit', type:'auth'})
.c('registration-required').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
var view = this.chatboxesview.views['problematic@muc.localhost'];
var view = this.chatboxesview.get('problematic@muc.localhost');
spyOn(view, 'showErrorMessage').andCallThrough();
view.onChatRoomPresence(presence, {'nick': 'dummy'});
expect(view.$el.find('.chat-body p').text()).toBe('You are not on the member list of this room');
@ -215,7 +215,7 @@
.c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
.c('error').attrs({by:'coven@chat.shakespeare.lit', type:'auth'})
.c('forbidden').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
var view = this.chatboxesview.views['problematic@muc.localhost'];
var view = this.chatboxesview.get('problematic@muc.localhost');
spyOn(view, 'showErrorMessage').andCallThrough();
view.onChatRoomPresence(presence, {'nick': 'dummy'});
expect(view.$el.find('.chat-body p').text()).toBe('You have been banned from this room');
@ -230,7 +230,7 @@
.c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
.c('error').attrs({by:'coven@chat.shakespeare.lit', type:'modify'})
.c('jid-malformed').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
var view = this.chatboxesview.views['problematic@muc.localhost'];
var view = this.chatboxesview.get('problematic@muc.localhost');
spyOn(view, 'showErrorMessage').andCallThrough();
view.onChatRoomPresence(presence, {'nick': 'dummy'});
expect(view.$el.find('.chat-body p').text()).toBe('No nickname was specified');
@ -245,7 +245,7 @@
.c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
.c('error').attrs({by:'coven@chat.shakespeare.lit', type:'cancel'})
.c('not-allowed').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
var view = this.chatboxesview.views['problematic@muc.localhost'];
var view = this.chatboxesview.get('problematic@muc.localhost');
spyOn(view, 'showErrorMessage').andCallThrough();
view.onChatRoomPresence(presence, {'nick': 'dummy'});
expect(view.$el.find('.chat-body p').text()).toBe('You are not allowed to create new rooms');
@ -260,7 +260,7 @@
.c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
.c('error').attrs({by:'coven@chat.shakespeare.lit', type:'cancel'})
.c('not-acceptable').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
var view = this.chatboxesview.views['problematic@muc.localhost'];
var view = this.chatboxesview.get('problematic@muc.localhost');
spyOn(view, 'showErrorMessage').andCallThrough();
view.onChatRoomPresence(presence, {'nick': 'dummy'});
expect(view.$el.find('.chat-body p').text()).toBe("Your nickname doesn't conform to this room's policies");
@ -275,7 +275,7 @@
.c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
.c('error').attrs({by:'coven@chat.shakespeare.lit', type:'cancel'})
.c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
var view = this.chatboxesview.views['problematic@muc.localhost'];
var view = this.chatboxesview.get('problematic@muc.localhost');
spyOn(view, 'showErrorMessage').andCallThrough();
view.onChatRoomPresence(presence, {'nick': 'dummy'});
expect(view.$el.find('.chat-body p').text()).toBe("Your nickname is already taken");
@ -290,7 +290,7 @@
.c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
.c('error').attrs({by:'coven@chat.shakespeare.lit', type:'cancel'})
.c('item-not-found').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
var view = this.chatboxesview.views['problematic@muc.localhost'];
var view = this.chatboxesview.get('problematic@muc.localhost');
spyOn(view, 'showErrorMessage').andCallThrough();
view.onChatRoomPresence(presence, {'nick': 'dummy'});
expect(view.$el.find('.chat-body p').text()).toBe("This room does not (yet) exist");
@ -305,7 +305,7 @@
.c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
.c('error').attrs({by:'coven@chat.shakespeare.lit', type:'cancel'})
.c('service-unavailable').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
var view = this.chatboxesview.views['problematic@muc.localhost'];
var view = this.chatboxesview.get('problematic@muc.localhost');
spyOn(view, 'showErrorMessage').andCallThrough();
view.onChatRoomPresence(presence, {'nick': 'dummy'});
expect(view.$el.find('.chat-body p').text()).toBe("This room has reached it's maximum number of occupants");

View File

@ -460,7 +460,7 @@
describe("The 'Add Contact' widget", $.proxy(function (mock, utils) {
it("opens up an add form when you click on it", $.proxy(function () {
var panel = this.chatboxesview.views.controlbox.contactspanel;
var panel = this.chatboxesview.get('controlbox').contactspanel;
spyOn(panel, 'toggleContactForm').andCallThrough();
panel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
panel.$el.find('a.toggle-xmpp-contact-form').click();
@ -485,7 +485,7 @@
}, converse));
it("contains two tabs, 'Contacts' and 'ChatRooms'", $.proxy(function () {
var cbview = this.chatboxesview.views.controlbox;
var cbview = this.chatboxesview.get('controlbox');
var $panels = cbview.$el.find('.controlbox-panes');
expect($panels.children().length).toBe(2);
expect($panels.children().first().attr('id')).toBe('users');
@ -508,7 +508,7 @@
}, converse));
it("is opened by clicking the 'Chatrooms' tab", $.proxy(function () {
var cbview = this.chatboxesview.views.controlbox;
var cbview = this.chatboxesview.get('controlbox');
var $tabs = cbview.$el.find('#controlbox-tabs');
var $panels = cbview.$el.find('.controlbox-panes');
var $contacts = $panels.children().first();
@ -527,7 +527,7 @@
}, converse));
it("contains a form through which a new chatroom can be created", $.proxy(function () {
var roomspanel = this.chatboxesview.views.controlbox.roomspanel;
var roomspanel = this.chatboxesview.get('controlbox').roomspanel;
var $input = roomspanel.$el.find('input.new-chatroom-name');
var $nick = roomspanel.$el.find('input.new-chatroom-nick');
var $server = roomspanel.$el.find('input.new-chatroom-server');

View File

@ -13,7 +13,7 @@
var i, chatbox;
for (i=converse.chatboxes.models.length-1; i>-1; i--) {
chatbox = converse.chatboxes.models[i];
converse.chatboxesview.views[chatbox.get('id')].closeChat();
converse.chatboxesview.get(chatbox.get('id')).closeChat();
}
return this;
};
@ -22,11 +22,11 @@
var i, chatbox, num_chatboxes = converse.chatboxes.models.length;
for (i=num_chatboxes-1; i>-1; i--) {
chatbox = converse.chatboxes.models[i];
converse.chatboxesview.views[chatbox.get('id')].closeChat();
converse.chatboxesview.views[chatbox.get('id')].$el.remove();
converse.chatboxesview.get(chatbox.get('id')).closeChat();
converse.chatboxesview.get(chatbox.get('id')).$el.remove();
}
converse.chatboxesview.views.controlbox.closeChat();
converse.chatboxesview.views.controlbox.$el.remove();
converse.chatboxesview.get('controlbox').closeChat();
converse.chatboxesview.get('controlbox').$el.remove();
return this;
};
@ -66,13 +66,13 @@
};
utils.openContactsPanel = function () {
var cbview = converse.chatboxesview.views.controlbox;
var cbview = converse.chatboxesview.get('controlbox');
var $tabs = cbview.$el.find('#controlbox-tabs');
$tabs.find('li').first().find('a').click();
};
utils.openRoomsPanel = function () {
var cbview = converse.chatboxesview.views.controlbox;
var cbview = converse.chatboxesview.get('controlbox');
var $tabs = cbview.$el.find('#controlbox-tabs');
$tabs.find('li').last().find('a').click();
};
@ -91,7 +91,7 @@
};
utils.clearChatBoxMessages = function (jid) {
var view = converse.chatboxesview.views[jid];
var view = converse.chatboxesview.get(jid);
view.$el.find('.chat-content').empty();
view.model.messages.reset().localStorage._clear();
};