Add event handler for closing chatrooms. Some refactoring.

This commit is contained in:
JC Brand 2012-07-21 18:39:32 +02:00
parent b088f997e8
commit d8c68449b9

48
chat.js
View File

@ -87,7 +87,7 @@ xmppchat.ChatBox = Backbone.Model.extend({
initialize: function () { initialize: function () {
this.set({ this.set({
'user_id' : Strophe.getNodeFromJid(this.get('jid')), 'user_id' : Strophe.getNodeFromJid(this.get('jid')),
'chat_id' : this.hash(this.get('jid')) 'box_id' : this.hash(this.get('jid'))
}); });
} }
@ -295,7 +295,7 @@ xmppchat.ChatBoxView = Backbone.View.extend({
closeChat: function () { closeChat: function () {
var that = this; var that = this;
$('#'+this.model.get('chat_id')).hide('fast', function () { $('#'+this.model.get('box_id')).hide('fast', function () {
that.removeChatFromCookie(that.model.get('id')); that.removeChatFromCookie(that.model.get('id'));
}); });
}, },
@ -335,7 +335,7 @@ xmppchat.ChatBoxView = Backbone.View.extend({
'</form>'), '</form>'),
render: function () { render: function () {
$(this.el).attr('id', this.model.get('chat_id')); $(this.el).attr('id', this.model.get('box_id'));
$(this.el).html(this.template(this.model.toJSON())); $(this.el).html(this.template(this.model.toJSON()));
this.insertClientStoredMessages(); this.insertClientStoredMessages();
return this; return this;
@ -422,7 +422,7 @@ xmppchat.RoomsPanel = Backbone.View.extend({
// FIXME: // FIXME:
var name = $(ev.target).find('input.new-chatroom-name').val(), var name = $(ev.target).find('input.new-chatroom-name').val(),
jid = name + '@conference.devbox'; jid = name + '@conference.devbox';
xmppchat.chatboxesview.createChatRoom(jid); xmppchat.chatboxesview.renderChat(jid);
} }
}); });
@ -434,7 +434,7 @@ xmppchat.SettingsPanel = Backbone.View.extend({
xmppchat.ControlBox = xmppchat.ChatBox.extend({ xmppchat.ControlBox = xmppchat.ChatBox.extend({
initialize: function () { initialize: function () {
this.set({ this.set({
'chat_id' : 'online-users-container' 'box_id' : 'online-users-container'
}); });
} }
}); });
@ -483,7 +483,7 @@ xmppchat.ChatRoom = xmppchat.ChatBox.extend({
'id': jid, 'id': jid,
'name': Strophe.getNodeFromJid(jid), 'name': Strophe.getNodeFromJid(jid),
'jid': jid, 'jid': jid,
'room_id' : this.hash(jid) 'box_id' : this.hash(jid)
}, {'silent': true}); }, {'silent': true});
var result = xmppchat.connection.muc.join(jid, nick, this.onMessage, this.onPresence, this.onRoomMessage); var result = xmppchat.connection.muc.join(jid, nick, this.onMessage, this.onPresence, this.onRoomMessage);
} }
@ -494,6 +494,13 @@ xmppchat.ChatRoomView = xmppchat.ChatBoxView.extend({
length: 300, length: 300,
tagName: 'div', tagName: 'div',
className: 'chatroom', className: 'chatroom',
events: {
'click .close-chatbox-button': 'closeChatRoom'
},
closeChatRoom: function () {
this.closeChat();
},
template: _.template( template: _.template(
'<div class="chat-head chat-head-chatroom">' + '<div class="chat-head chat-head-chatroom">' +
@ -520,7 +527,7 @@ xmppchat.ChatRoomView = xmppchat.ChatBoxView.extend({
'</div>'), '</div>'),
render: function () { render: function () {
$(this.el).attr('id', this.model.get('room_id')); $(this.el).attr('id', this.model.get('box_id'));
$(this.el).html(this.template(this.model.toJSON())); $(this.el).html(this.template(this.model.toJSON()));
return this; return this;
} }
@ -593,30 +600,20 @@ xmppchat.ChatBoxesView = Backbone.View.extend({
if (!this.model.get(jid)) { if (!this.model.get(jid)) {
this.renderChat(jid); this.renderChat(jid);
} else { } else {
this.positionNewChat(jid); this.showChat(jid);
} }
}, },
positionNewChat: function (jid) { showChat: function (jid) {
var view = this.views[jid], var view = this.views[jid];
that = this,
offset = 0;
if (view.isVisible()) { if (view.isVisible()) {
view.focus(); view.focus();
} else { } else {
if (jid === 'online-users-container') { if (jid === 'online-users-container') {
$(view.el).css({'right': (offset+'px')});
$(view.el).show('fast', function () { $(view.el).show('fast', function () {
view.el.focus(); view.el.focus();
}); });
} else { } else {
for (var i=0; i<this.model.models.length; i++) {
if ($("#"+this.model.models[i].get('chat_id')).is(':visible')) {
offset += this.views[this.model.models[i].get('jid')].length;
}
}
// $(view.el).css({'right': (offset+'px')});
$(view.el).show('fast', function () { $(view.el).show('fast', function () {
view.el.focus(); view.el.focus();
view.scrolldown(); view.scrolldown();
@ -641,18 +638,9 @@ xmppchat.ChatBoxesView = Backbone.View.extend({
xmppchat.roster.addResource(bare_jid, resource); xmppchat.roster.addResource(bare_jid, resource);
}, },
createChatRoom: function (jid) {
var chatroom = new xmppchat.ChatRoom(jid),
chatroomview = new xmppchat.ChatRoomView({
'model': chatroom
});
this.views[chatroom.get('jid')] = chatroomview.render();
this.options.model.add(chatroom);
},
initialize: function () { initialize: function () {
this.options.model.on("add", function (item) { this.options.model.on("add", function (item) {
this.positionNewChat(item.get('id')); this.showChat(item.get('id'));
}, this); }, this);
this.views = {}; this.views = {};