Extract color_data.js.

This code is pretty distinct from all the color-picking UI,
and we want to get it to 100% coverage and optimize it
more.
This commit is contained in:
Steve Howell 2018-11-28 21:07:14 +00:00 committed by Tim Abbott
parent 5ef86b6d22
commit 6035304619
10 changed files with 53 additions and 40 deletions

View File

@ -38,6 +38,7 @@
"buddy_list": false, "buddy_list": false,
"channel": false, "channel": false,
"click_handlers": false, "click_handlers": false,
"color_data": false,
"colorspace": false, "colorspace": false,
"common": false, "common": false,
"components": false, "components": false,

View File

@ -1,6 +1,4 @@
global.stub_out_jquery(); zrequire('color_data');
zrequire('stream_color');
run_test('pick_color', () => { run_test('pick_color', () => {
var used_colors = ["#76ce90", "#fae589"]; var used_colors = ["#76ce90", "#fae589"];
@ -8,7 +6,7 @@ run_test('pick_color', () => {
// Colors are assigned randomly, so this test is a little vague and brittle, // Colors are assigned randomly, so this test is a little vague and brittle,
// but we can verify a few things, like not reusing colors and making sure // but we can verify a few things, like not reusing colors and making sure
// the color has length 7. // the color has length 7.
var color = stream_color.pick_color(used_colors); var color = color_data.pick_color(used_colors);
assert.notEqual(color, "#76ce90"); assert.notEqual(color, "#76ce90");
assert.notEqual(color, "#fae589"); assert.notEqual(color, "#fae589");
assert.equal(color.length, 7); assert.equal(color.length, 7);

View File

@ -8,6 +8,8 @@ set_global('$', function () {
set_global('blueslip', global.make_zblueslip()); set_global('blueslip', global.make_zblueslip());
set_global('color_data', {});
zrequire('util'); zrequire('util');
zrequire('hash_util'); zrequire('hash_util');
zrequire('topic_data'); zrequire('topic_data');
@ -609,7 +611,7 @@ run_test('create_sub', () => {
color: '#76ce90', color: '#76ce90',
}; };
global.stream_color.pick_color = function () { color_data.pick_color = function () {
return '#bd86e5'; return '#bd86e5';
}; };

View File

@ -219,7 +219,7 @@ run_test('marked_subscribed', () => {
// Test assigning generated color // Test assigning generated color
with_overrides(function (override) { with_overrides(function (override) {
frontend.color = undefined; frontend.color = undefined;
override('stream_color.pick_color', function () { override('color_data.pick_color', function () {
return 'green'; return 'green';
}); });
var warnings = 0; var warnings = 0;

View File

@ -23,7 +23,7 @@ exports.initialize = function () {
var recipient_and_topic = $('#display_recipient').html(); var recipient_and_topic = $('#display_recipient').html();
var stream_name = recipient_and_topic.split('-')[0]; var stream_name = recipient_and_topic.split('-')[0];
var topic = recipient_and_topic.split('-')[1]; var topic = recipient_and_topic.split('-')[1];
var recipient_color = stream_color.pick_color([]); var recipient_color = color_data.pick_color([]);
current_message_group.message_containers = []; current_message_group.message_containers = [];
current_message_group.show_date_separator = false; current_message_group.show_date_separator = false;
current_message_group.display_recipient = stream_name; current_message_group.display_recipient = stream_name;

View File

@ -94,6 +94,7 @@ import "js/transmit.js";
import "js/zcommand.js"; import "js/zcommand.js";
import "js/compose.js"; import "js/compose.js";
import "js/upload.js"; import "js/upload.js";
import "js/color_data.js";
import "js/stream_color.js"; import "js/stream_color.js";
import "js/stream_data.js"; import "js/stream_data.js";
import "js/topic_data.js"; import "js/topic_data.js";

42
static/js/color_data.js Normal file
View File

@ -0,0 +1,42 @@
var color_data = (function () {
var exports = {};
// Auto-assigned colors should be from the default palette so it's easy to undo
// changes, so if that palette changes, change these colors.
var stream_assignment_colors = [
"#76ce90", "#fae589", "#a6c7e5", "#e79ab5",
"#bfd56f", "#f4ae55", "#b0a5fd", "#addfe5",
"#f5ce6e", "#c2726a", "#94c849", "#bd86e5",
"#ee7e4a", "#a6dcbf", "#95a5fd", "#53a063",
"#9987e1", "#e4523d", "#c2c2c2", "#4f8de4",
"#c6a8ad", "#e7cc4d", "#c8bebf", "#a47462",
];
exports.pick_color = function (used_colors) {
var colors = _.shuffle(stream_assignment_colors);
var used_color_hash = {};
_.each(used_colors, function (color) {
used_color_hash[color] = true;
});
var color = _.find(colors, function (color) {
return !_.has(used_color_hash, color);
});
if (color) {
return color;
}
// All available colors were used.
return colors[0];
};
return exports;
}());
if (typeof module !== 'undefined') {
module.exports = color_data;
}
window.color_data = color_data;

View File

@ -3,41 +3,10 @@ var stream_color = (function () {
var exports = {}; var exports = {};
exports.default_color = "#c2c2c2"; exports.default_color = "#c2c2c2";
// Auto-assigned colors should be from the default palette so it's easy to undo
// changes, so if that palette changes, change these colors.
var stream_assignment_colors = [
"#76ce90", "#fae589", "#a6c7e5", "#e79ab5",
"#bfd56f", "#f4ae55", "#b0a5fd", "#addfe5",
"#f5ce6e", "#c2726a", "#94c849", "#bd86e5",
"#ee7e4a", "#a6dcbf", "#95a5fd", "#53a063",
"#9987e1", "#e4523d", "#c2c2c2", "#4f8de4",
"#c6a8ad", "#e7cc4d", "#c8bebf", "#a47462",
];
// 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 (used_colors) {
var colors = _.shuffle(stream_assignment_colors);
var used_color_hash = {};
_.each(used_colors, function (color) {
used_color_hash[color] = true;
});
var color = _.find(colors, function (color) {
return !_.has(used_color_hash, color);
});
if (color) {
return color;
}
// All available colors were used.
return colors[0];
};
function update_table_stream_color(table, stream_name, color) { function update_table_stream_color(table, stream_name, color) {
// This is ugly, but temporary, as the new design will make it // This is ugly, but temporary, as the new design will make it
// so that we only have color in the headers. // so that we only have color in the headers.

View File

@ -465,7 +465,7 @@ exports.create_sub_from_server_data = function (stream_name, attrs) {
if (!sub.color) { if (!sub.color) {
var used_colors = exports.get_colors(); var used_colors = exports.get_colors();
sub.color = stream_color.pick_color(used_colors); sub.color = color_data.pick_color(used_colors);
} }
exports.update_calculated_fields(sub); exports.update_calculated_fields(sub);

View File

@ -4,7 +4,7 @@ var exports = {};
function get_color() { function get_color() {
var used_colors = stream_data.get_colors(); var used_colors = stream_data.get_colors();
var color = stream_color.pick_color(used_colors); var color = color_data.pick_color(used_colors);
return color; return color;
} }