Commit d0b9761d authored by JC Brand's avatar JC Brand

More work to remove the need for collection-view anti-pattern.

Remove createChatBox from chatboxesview and move the message received listener to chatboxes
parent 9d89779b
...@@ -332,7 +332,7 @@ ...@@ -332,7 +332,7 @@
$chat_content.scrollTop($chat_content[0].scrollHeight); $chat_content.scrollTop($chat_content[0].scrollHeight);
}, },
messageReceived: function (message) { showMessage: function (message) {
var $chat_content = this.$el.find('.chat-content'); var $chat_content = this.$el.find('.chat-content');
if (xmppchat.xmppstatus.getStatus() === 'offline') { if (xmppchat.xmppstatus.getStatus() === 'offline') {
// only update the UI if the user is not offline // only update the UI if the user is not offline
...@@ -479,7 +479,7 @@ ...@@ -479,7 +479,7 @@
initialize: function (){ initialize: function (){
// boxviewinit // boxviewinit
$('body').append(this.$el.hide()); $('body').append(this.$el.hide());
this.model.messages.on('add', this.messageReceived, this); this.model.messages.on('add', this.showMessage, this);
this.model.on('show', function() { this.model.on('show', function() {
this.show(); this.show();
...@@ -1056,55 +1056,13 @@ ...@@ -1056,55 +1056,13 @@
}, this) }, this)
}); });
}, },
});
xmppchat.ChatBoxesView = Backbone.View.extend({
el: '#collective-xmpp-chat-data',
createChatRoom: function (jid) {
var box = new xmppchat.ChatRoom(jid, xmppchat.fullname);
var view = new xmppchat.ChatRoomView({
'model': box
});
this.views[jid] = view.render();
view.$el.appendTo(this.$el);
this.options.model.add(box);
return view;
},
createChatBox: function (data) {
var model = new xmppchat.ChatBox({
'id': data['jid'],
'jid': data['jid'],
'fullname': data['fullname'],
'image_type': data['image_type'],
'image': data['image'],
'visible': data['visible'],
'url': data['url'],
});
this.options.model.add(model);
model.save();
return model;
},
openControlBox: function () {
var controlbox = this.model.get('controlbox')
if (controlbox) {
controlbox.trigger('show');
} else {
this.options.model.add({
id: 'controlbox',
box_id: 'controlbox',
visible: true
});
}
},
messageReceived: function (message) { messageReceived: function (message) {
var partner_jid, $message = $(message), var partner_jid, $message = $(message),
message_from = $message.attr('from'); message_from = $message.attr('from');
if ( message_from == xmppchat.connection.jid) { if (message_from == xmppchat.connection.jid) {
// FIXME: Forwarded messages should be sent to specific resources, not broadcasted // FIXME: Forwarded messages should be sent to specific resources,
// not broadcasted
return true; return true;
} }
var $forwarded = $message.children('forwarded'); var $forwarded = $message.children('forwarded');
...@@ -1113,7 +1071,7 @@ ...@@ -1113,7 +1071,7 @@
} }
var from = Strophe.getBareJidFromJid(message_from), var from = Strophe.getBareJidFromJid(message_from),
to = Strophe.getBareJidFromJid($message.attr('to')), to = Strophe.getBareJidFromJid($message.attr('to')),
view, resource, chatbox; resource, chatbox;
if (from == xmppchat.connection.bare_jid) { if (from == xmppchat.connection.bare_jid) {
// I am the sender, so this must be a forwarded message... // I am the sender, so this must be a forwarded message...
partner_jid = to; partner_jid = to;
...@@ -1123,33 +1081,60 @@ ...@@ -1123,33 +1081,60 @@
resource = Strophe.getResourceFromJid(message_from); resource = Strophe.getResourceFromJid(message_from);
} }
view = this.views[partner_jid]; chatbox = this.get(partner_jid);
if (!view) { if (!chatbox) {
xmppchat.getVCard( xmppchat.getVCard(
partner_jid, partner_jid,
$.proxy(function (jid, fullname, img, img_type, url) { $.proxy(function (jid, fullname, image, image_type, url) {
chatbox = this.createChatBox({ chatbox = this.create({
'id': jid,
'jid': jid, 'jid': jid,
'fullname': fullname, 'fullname': fullname,
'image': img, 'image_type': image_type,
'image_type': img_type, 'image': image,
'url': url, 'url': url,
}) });
chatbox.messageReceived(message); chatbox.messageReceived(message);
xmppchat.roster.addResource(partner_jid, resource); xmppchat.roster.addResource(partner_jid, resource);
}, this), }, this),
$.proxy(function () { $.proxy(function () {
// # TODO: call the function above // # FIXME
console.log("An error occured while fetching vcard"); console.log("An error occured while fetching vcard");
}, this)); }, this));
return true; return true;
} else if (!view.isVisible()) {
this.model.get(partner_jid).trigger('show');
} }
view.model.messageReceived(message); chatbox.messageReceived(message);
xmppchat.roster.addResource(partner_jid, resource); xmppchat.roster.addResource(partner_jid, resource);
return true; return true;
}, },
});
xmppchat.ChatBoxesView = Backbone.View.extend({
el: '#collective-xmpp-chat-data',
createChatRoom: function (jid) {
var box = new xmppchat.ChatRoom(jid, xmppchat.fullname);
var view = new xmppchat.ChatRoomView({
'model': box
});
this.views[jid] = view.render();
view.$el.appendTo(this.$el);
this.options.model.add(box);
return view;
},
openControlBox: function () {
var controlbox = this.model.get('controlbox')
if (controlbox) {
controlbox.trigger('show');
} else {
this.options.model.add({
id: 'controlbox',
box_id: 'controlbox',
visible: true
});
}
},
initialize: function () { initialize: function () {
// boxesviewinit // boxesviewinit
...@@ -1203,14 +1188,14 @@ ...@@ -1203,14 +1188,14 @@
if (chatbox) { if (chatbox) {
chatbox.trigger('show'); chatbox.trigger('show');
} else { } else {
xmppchat.chatboxesview.createChatBox({ xmppchat.chatboxes.create({
'jid': jid, 'id': this.model.get('jid'),
'jid': this.model.get('jid'),
'fullname': this.model.get('fullname'), 'fullname': this.model.get('fullname'),
'image': this.model.get('image'),
'image_type': this.model.get('image_type'), 'image_type': this.model.get('image_type'),
'visible': true, 'image': this.model.get('image'),
'url': this.model.get('url'), 'url': this.model.get('url'),
}) });
} }
}, },
...@@ -2052,7 +2037,7 @@ ...@@ -2052,7 +2037,7 @@
this.connection.addHandler( this.connection.addHandler(
$.proxy(function (message) { $.proxy(function (message) {
this.chatboxesview.messageReceived(message); this.chatboxes.messageReceived(message);
return true; return true;
}, this), null, 'message', 'chat'); }, this), null, 'message', 'chat');
......
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