Simplify code to generate new stream colors.

Instead of splicing up a cloned copy of stream_assignment_colors
every time somebody uses a color, we just rebuild a hash
of used_colors from our subscribed streams when we need to assign
a color, and we avoid calling into stream_color.pick_color() when
a stream already has a color.

This change has a slight functional impact in the situation where
a user unsubscribes some streams during their session, because
we weren't "reclaiming" colors before on unsubscription, but the
simple approach gets that for free.

(imported from commit adf360365bdf1ae9db99c533a0bde62d91f5dfe8)
This commit is contained in:
Steve Howell 2013-08-07 14:23:12 -04:00
parent e36fd929bc
commit 7d67716c21
2 changed files with 30 additions and 23 deletions

View File

@ -12,31 +12,28 @@ var stream_assignment_colors = ["#76ce90", "#fae589", "#a6c7e5", "#e79ab5",
"#9987e1", "#e4523d", "#c2c2c2", "#4f8de4", "#9987e1", "#e4523d", "#c2c2c2", "#4f8de4",
"#c6a8ad", "#e7cc4d", "#c8bebf", "#a47462"]; "#c6a8ad", "#e7cc4d", "#c8bebf", "#a47462"];
// Clone stream_assignement_colors
var available_colors = stream_assignment_colors.slice(0);
// Classes which could be returned by get_color_class. // Classes which could be returned by get_color_class.
exports.color_classes = 'dark_background'; exports.color_classes = 'dark_background';
exports.pick_color = function () { exports.pick_color = function (used_colors) {
if (available_colors.length === 0) { var used_color_hash = {};
// We've used all the palette colors, so start re-using them.
return stream_assignment_colors[exports.subscribed_streams().length _.each(used_colors, function (color) {
% stream_assignment_colors.length]; used_color_hash[color] = true;
});
var color = _.find(stream_assignment_colors, function (color) {
return !_.has(used_color_hash, color);
});
if (color) {
return color;
} }
return available_colors[0]; // All available colors were used.
return stream_assignment_colors[0];
}; };
exports.mark_color_used = function (color) {
var i;
for (i = 0; i < available_colors.length; ++i) {
if (available_colors[i] === color) {
available_colors.splice(i, 1);
return;
}
}
};
function update_table_stream_color(table, stream_name, color) { function update_table_stream_color(table, stream_name, color) {
var color_class = exports.get_color_class(color); var color_class = exports.get_color_class(color);

View File

@ -30,6 +30,14 @@ exports.subscribed_streams = function () {
.value(); .value();
}; };
function get_color() {
var streams = _.values(stream_info);
var subscribed_streams = _.where(streams, {subscribed: true});
var used_colors = _.pluck(subscribed_streams, 'color');
var color = stream_color.pick_color(used_colors);
return color;
}
exports.update_all_messages_link = function () { exports.update_all_messages_link = function () {
// Show or hide the "All messages" link, depending on whether // Show or hide the "All messages" link, depending on whether
// the user has any subscriptions hidden from home view. // the user has any subscriptions hidden from home view.
@ -176,10 +184,12 @@ function create_sub(stream_name, attrs) {
subscribed: true, subscribed: true,
in_home_view: true, in_home_view: true,
invite_only: false, invite_only: false,
notifications: false, notifications: false
color: stream_color.pick_color()
}); });
stream_color.mark_color_used(sub.color);
if (!sub.color) {
sub.color = get_color();
}
add_sub(stream_name, sub); add_sub(stream_name, sub);
$(document).trigger($.Event('sub_obj_created.zulip', {sub: sub})); $(document).trigger($.Event('sub_obj_created.zulip', {sub: sub}));
@ -229,9 +239,9 @@ function mark_subscribed(stream_name, attrs) {
add_sub_to_table(sub); add_sub_to_table(sub);
} else if (! sub.subscribed) { } else if (! sub.subscribed) {
// Add yourself to an existing stream. // Add yourself to an existing stream.
var color = get_color();
exports.set_color(stream_name, color);
sub.subscribed = true; sub.subscribed = true;
exports.set_color(stream_name, stream_color.pick_color());
stream_color.mark_color_used(sub.color);
var settings = settings_for_sub(sub); var settings = settings_for_sub(sub);
var button = button_for_sub(sub); var button = button_for_sub(sub);
if (button.length !== 0) { if (button.length !== 0) {