From 2669a179f7c821774743f19075e12898c6504f9f Mon Sep 17 00:00:00 2001 From: JC Brand Date: Wed, 11 Jul 2012 16:16:17 +0200 Subject: [PATCH] Add a controlbox model and view and fixed ordering of chatboxes. --- chat.js | 253 +++++++++++++++++++++++++++++++++++++++++++----------- chatui.js | 39 --------- 2 files changed, 203 insertions(+), 89 deletions(-) diff --git a/chat.js b/chat.js index 1a24c94c4..7ac672936 100644 --- a/chat.js +++ b/chat.js @@ -211,14 +211,57 @@ xmppchat.ChatBoxView = Backbone.View.extend({ className: 'chatbox', events: { - 'click .close-chatbox-button': 'closeChatBox' + 'click .close-chatbox-button': 'closeChat' }, - closeChatBox: function () { + addChatToCookie: function () { + var cookie = jQuery.cookie('chats-open-'+xmppchat.username), + jid = this.model.get('jid'), + new_cookie, + open_chats = []; + + if (cookie) { + open_chats = cookie.split('|'); + } + if (!_.has(open_chats, jid)) { + // Update the cookie if this new chat is not yet in it. + open_chats.push(jid); + new_cookie = open_chats.join('|'); + jQuery.cookie('chats-open-'+xmppchat.username, new_cookie, {path: '/'}); + console.log('updated cookie = ' + new_cookie + '\n'); + } + }, + + removeChatFromCookie: function () { + var cookie = jQuery.cookie('chats-open-'+xmppchat.username), + open_chats = [], + new_chats = []; + + if (cookie) { + open_chats = cookie.split('|'); + } + for (var i=0; i < open_chats.length; i++) { + if (open_chats[i] != this.model.get('jid')) { + new_chats.push(open_chats[i]); + } + } + if (new_chats.length) { + jQuery.cookie('chats-open-'+xmppchat.username, new_chats.join('|'), {path: '/'}); + } + else { + jQuery.cookie('chats-open-'+xmppchat.username, null, {path: '/'}); + } + }, + + closeChat: function () { var that = this; $('#'+this.model.get('chat_id')).hide('fast', function () { - // TODO: that.removeChatFromCookie(jid); - xmppchat.chatboxesview.reorderChatBoxes(); + that.removeChatFromCookie(that.model.get('id')); + // Only reorder chats if it wasn't the last chat being closed. + var offset = parseInt($(that.el).css('right'), 10) + xmppchat.chatboxesview.chatbox_width; + if ($("div[style*='right: "+offset+"px']").length > 0) { + xmppchat.chatboxesview.reorderChats(); + } }); }, @@ -253,13 +296,41 @@ xmppchat.ChatBoxView = Backbone.View.extend({ return this; }, - show: function (callback) { + show: function () { var that = this; $(this.el).show('fast', function () { that.focus(); }); return this; } + +}); + +xmppchat.ControlBox = xmppchat.ChatBox.extend({ + + initialize: function () { + this.set({ + 'chat_id' : 'online-users-container' + }); + } + +}); + +xmppchat.ControlBoxView = xmppchat.ChatBoxView.extend({ + + el: '#online-users-container', + events: { + 'click a.close-controlbox-button': 'closeChat' + }, + + render: function () { + return this; + }, + + show: function () { + $(this.el).show(); + return this; + } }); xmppchat.ChatBoxes = Backbone.Collection.extend({ @@ -267,39 +338,120 @@ xmppchat.ChatBoxes = Backbone.Collection.extend({ xmppchat.ChatBoxesView = Backbone.View.extend({ - chatbox_width: 205, + chatbox_width: 212, + chatbox_padding: 15, - positionNewChatBox: function (view) { - var open_chats = 0, - offset; - if ($('#online-users-container').is(':visible')) { - open_chats++; - } - for (var i=0; i div'); $('#fancy-xmpp-status-select').click(function (ev) {