mirror of https://github.com/zulip/zulip.git
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:
parent
e36fd929bc
commit
7d67716c21
|
@ -12,31 +12,28 @@ var stream_assignment_colors = ["#76ce90", "#fae589", "#a6c7e5", "#e79ab5",
|
|||
"#9987e1", "#e4523d", "#c2c2c2", "#4f8de4",
|
||||
"#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.
|
||||
exports.color_classes = 'dark_background';
|
||||
|
||||
exports.pick_color = function () {
|
||||
if (available_colors.length === 0) {
|
||||
// We've used all the palette colors, so start re-using them.
|
||||
return stream_assignment_colors[exports.subscribed_streams().length
|
||||
% stream_assignment_colors.length];
|
||||
exports.pick_color = function (used_colors) {
|
||||
var used_color_hash = {};
|
||||
|
||||
_.each(used_colors, function (color) {
|
||||
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) {
|
||||
var color_class = exports.get_color_class(color);
|
||||
|
|
|
@ -30,6 +30,14 @@ exports.subscribed_streams = function () {
|
|||
.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 () {
|
||||
// Show or hide the "All messages" link, depending on whether
|
||||
// the user has any subscriptions hidden from home view.
|
||||
|
@ -176,10 +184,12 @@ function create_sub(stream_name, attrs) {
|
|||
subscribed: true,
|
||||
in_home_view: true,
|
||||
invite_only: false,
|
||||
notifications: false,
|
||||
color: stream_color.pick_color()
|
||||
notifications: false
|
||||
});
|
||||
stream_color.mark_color_used(sub.color);
|
||||
|
||||
if (!sub.color) {
|
||||
sub.color = get_color();
|
||||
}
|
||||
|
||||
add_sub(stream_name, 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);
|
||||
} else if (! sub.subscribed) {
|
||||
// Add yourself to an existing stream.
|
||||
var color = get_color();
|
||||
exports.set_color(stream_name, color);
|
||||
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 button = button_for_sub(sub);
|
||||
if (button.length !== 0) {
|
||||
|
|
Loading…
Reference in New Issue