Rewrite and improve trimOpenChats. updates #61

This commit is contained in:
JC Brand 2014-05-05 23:37:52 +02:00
parent 65508b26ed
commit ad466b62a0

View File

@ -644,17 +644,21 @@
this.messages = new converse.Messages(); this.messages = new converse.Messages();
this.messages.localStorage = new Backbone.LocalStorage( this.messages.localStorage = new Backbone.LocalStorage(
b64_sha1('converse.messages'+this.get('jid')+converse.bare_jid)); b64_sha1('converse.messages'+this.get('jid')+converse.bare_jid));
this.save({ this.save({
'user_id' : Strophe.getNodeFromJid(this.get('jid')), 'user_id' : Strophe.getNodeFromJid(this.get('jid')),
'box_id' : b64_sha1(this.get('jid')), 'box_id' : b64_sha1(this.get('jid')),
'otr_status': this.get('otr_status') || UNENCRYPTED, 'otr_status': this.get('otr_status') || UNENCRYPTED,
'minimized': this.get('minimized') || false, 'minimized': this.get('minimized') || false,
'time_minimized': this.get('time_minimized') || moment(), 'time_minimized': this.get('time_minimized') || moment(),
'time_opened': this.get('time_opened') || moment(), 'time_opened': this.get('time_opened') || moment().format(),
'height': height 'height': height
}); });
} else { } else {
this.set({'height': height}); this.set({
'height': height,
'time_opened': moment(0).format()
});
} }
}, },
@ -1297,6 +1301,13 @@
return this; return this;
}, },
trimChat: function () {
// TODO: Instead of closing the chat, we should add it to
// div#offscreen-chatboxes
this.$el.hide(); // Hide it immediately to avoid flashes on the screen
this.closeChat();
},
saveToggleState: function () { saveToggleState: function () {
var flyout = this.$el.find('.box-flyout'); var flyout = this.$el.find('.box-flyout');
if (flyout.hasClass('minimized')) { if (flyout.hasClass('minimized')) {
@ -2380,6 +2391,7 @@
this.ChatBoxes = Backbone.Collection.extend({ this.ChatBoxes = Backbone.Collection.extend({
model: converse.ChatBox, model: converse.ChatBox,
comparator : 'time_opened',
registerMessageHandler: function () { registerMessageHandler: function () {
converse.connection.addHandler( converse.connection.addHandler(
@ -2480,7 +2492,6 @@
this.getAll = function () { return views; }; this.getAll = function () { return views; };
this.model.on("add", function (item) { this.model.on("add", function (item) {
this.trimOpenChats();
var view = this.get(item.get('id')); var view = this.get(item.get('id'));
if (!view) { if (!view) {
if (item.get('chatroom')) { if (item.get('chatroom')) {
@ -2497,51 +2508,34 @@
view.model = item; view.model = item;
view.initialize(); view.initialize();
} }
this.trimOpenChats(view);
}, this); }, this);
}, },
trimOpenChats: function () { trimOpenChats: function (view) {
/* This method is called before a new chat box will be opened. /* This method is called before a new chat box will be opened.
* *
* Check whether there is enough space in the page to show * Check whether there is enough space in the page to show
* another chat box. Otherwise, close the oldest chat box. * another chat box. Otherwise, close the oldest chat box.
*/ */
var toggle_width = 0; var toggle_width = 0,
var controlbox = this.get('controlbox'); boxes_width = view.$el.outerWidth(true),
controlbox = this.get('controlbox');
if (!controlbox || !controlbox.$el.is(':visible')) { if (!controlbox || !controlbox.$el.is(':visible')) {
toggle_width = converse.controlboxtoggle.$el.width(); toggle_width = converse.controlboxtoggle.$el.width();
} }
var views = this.getAll(); this.$el.find('.chatbox').addBack('.chatroom').each(function (idx, el) {
var oldest = moment(); var $el = $(el);
var oldest_view; if ($el.is(':visible')) {
var total_width = this.$el.width(); boxes_width += $el.outerWidth(true);
var view_list = _.values(views);
if (view_list.length === 0) {
return;
}
var box_width = view_list[0].$el.outerWidth();
var num_visible_views = 1; // Include view about to be opened
_.each(views, function (v) {
if (v.$el.is(':visible')) {
num_visible_views += 1;
} }
}); });
if (num_visible_views === 1) { if (this.model.length <= 1) {
return; return;
} }
if ((num_visible_views*box_width + toggle_width) > total_width) { if ((boxes_width + toggle_width) > this.$el.width()) {
_.each(views, function (v) { // trim oldest view (which is not controlbox)
if (v.id === 'controlbox' || !v.$el.is(':visible')) { this.get(this.model.at(1).get('id')).trimChat();
return;
}
var opened = v.model.get('time_opened');
if (moment(opened).isBefore(oldest)) {
oldest = opened;
oldest_view = v;
}
});
oldest_view.$el.hide(); // Hide it immediately to avoid flashes on the screen
oldest_view.closeChat();
} }
}, },