js: Convert static/js/color_data.js to ES6 module.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2021-02-27 15:52:39 -08:00 committed by Tim Abbott
parent 6f660793e7
commit 374dfc2fda
11 changed files with 33 additions and 29 deletions

View File

@ -135,7 +135,6 @@
"buddy_data": false, "buddy_data": false,
"buddy_list": false, "buddy_list": false,
"click_handlers": false, "click_handlers": false,
"color_data": false,
"compose_actions": false, "compose_actions": false,
"compose_ui": false, "compose_ui": false,
"composebox_typeahead": false, "composebox_typeahead": false,

View File

@ -8,7 +8,7 @@ const {run_test} = require("../zjsunit/test");
const color_data = zrequire("color_data"); const color_data = zrequire("color_data");
run_test("pick_color", () => { run_test("pick_color", () => {
color_data.colors = ["blue", "orange", "red", "yellow"]; color_data.__Rewire__("colors", ["blue", "orange", "red", "yellow"]);
color_data.reset(); color_data.reset();

View File

@ -653,7 +653,7 @@ run_test("create_sub", () => {
color: "#76ce90", color: "#76ce90",
}; };
color_data.pick_color = () => "#bd86e5"; color_data.__Rewire__("pick_color", () => "#bd86e5");
const india_sub = stream_data.create_sub_from_server_data(india); const india_sub = stream_data.create_sub_from_server_data(india);
assert(india_sub); assert(india_sub);

View File

@ -2,6 +2,8 @@
const {strict: assert} = require("assert"); const {strict: assert} = require("assert");
const rewiremock = require("rewiremock/node");
const {set_global, zrequire} = require("../zjsunit/namespace"); const {set_global, zrequire} = require("../zjsunit/namespace");
const {make_stub} = require("../zjsunit/stub"); const {make_stub} = require("../zjsunit/stub");
const {run_test} = require("../zjsunit/test"); const {run_test} = require("../zjsunit/test");
@ -13,7 +15,8 @@ const _settings_notifications = {
}; };
set_global("settings_notifications", _settings_notifications); set_global("settings_notifications", _settings_notifications);
const color_data = set_global("color_data", {}); const color_data = {__esModule: true};
rewiremock("../../static/js/color_data").with(color_data);
set_global("current_msg_list", {}); set_global("current_msg_list", {});
const message_util = set_global("message_util", {}); const message_util = set_global("message_util", {});
const stream_color = set_global("stream_color", {}); const stream_color = set_global("stream_color", {});
@ -35,6 +38,8 @@ set_global("message_list", {
subs = set_global("subs", {update_settings_for_subscribed: noop}); subs = set_global("subs", {update_settings_for_subscribed: noop});
set_global("overlays", {streams_open: () => true}); set_global("overlays", {streams_open: () => true});
rewiremock.enable();
const peer_data = zrequire("peer_data"); const peer_data = zrequire("peer_data");
const people = zrequire("people"); const people = zrequire("people");
const stream_data = zrequire("stream_data"); const stream_data = zrequire("stream_data");
@ -397,3 +402,4 @@ run_test("remove_deactivated_user_from_all_streams", () => {
// verify that we issue a call to update subscriber count/list UI // verify that we issue a call to update subscriber count/list UI
assert.equal(subs_stub.num_calls, 1); assert.equal(subs_stub.num_calls, 1);
}); });
rewiremock.disable();

View File

@ -110,7 +110,6 @@ const util = zrequire("util");
zrequire("hash_util"); zrequire("hash_util");
zrequire("stream_color"); zrequire("stream_color");
zrequire("stream_edit"); zrequire("stream_edit");
zrequire("color_data");
zrequire("stream_data"); zrequire("stream_data");
zrequire("condense"); zrequire("condense");
zrequire("lightbox"); zrequire("lightbox");

View File

@ -2,6 +2,8 @@ import _ from "lodash";
import render_archive_message_group from "../templates/archive_message_group.hbs"; import render_archive_message_group from "../templates/archive_message_group.hbs";
import * as color_data from "./color_data";
const {format, isSameDay} = require("date-fns"); const {format, isSameDay} = require("date-fns");
function should_separate_into_groups(current_msg_time, next_msg_time) { function should_separate_into_groups(current_msg_time, next_msg_time) {

View File

@ -25,7 +25,6 @@ import "../message_list";
import "../narrow"; import "../narrow";
import "../reload"; import "../reload";
import "../compose_actions"; import "../compose_actions";
import "../color_data";
import "../stream_color"; import "../stream_color";
import "../stream_data"; import "../stream_data";
import "../stream_topic_history"; import "../stream_topic_history";

View File

@ -1,6 +1,6 @@
"use strict"; import _ from "lodash";
const _ = require("lodash"); export let unused_colors;
// These colors are used now for streams. // These colors are used now for streams.
const stream_colors = [ const stream_colors = [
"#76ce90", "#76ce90",
@ -31,41 +31,39 @@ const stream_colors = [
// Shuffle our colors on page load to prevent // Shuffle our colors on page load to prevent
// bias toward "early" colors. // bias toward "early" colors.
exports.colors = _.shuffle(stream_colors); export const colors = _.shuffle(stream_colors);
exports.reset = function () { export function reset() {
exports.unused_colors = exports.colors.slice(); unused_colors = colors.slice();
}; }
exports.reset(); reset();
exports.claim_color = function (color) { export function claim_color(color) {
const i = exports.unused_colors.indexOf(color); const i = unused_colors.indexOf(color);
if (i < 0) { if (i < 0) {
return; return;
} }
exports.unused_colors.splice(i, 1); unused_colors.splice(i, 1);
if (exports.unused_colors.length === 0) { if (unused_colors.length === 0) {
exports.reset(); reset();
} }
}; }
exports.claim_colors = function (subs) { export function claim_colors(subs) {
const colors = new Set(subs.map((sub) => sub.color)); const colors = new Set(subs.map((sub) => sub.color));
for (const color of colors) { for (const color of colors) {
exports.claim_color(color); claim_color(color);
} }
}; }
exports.pick_color = function () { export function pick_color() {
const color = exports.unused_colors[0]; const color = unused_colors[0];
exports.claim_color(color); claim_color(color);
return color; return color;
}; }
window.color_data = exports;

View File

@ -13,7 +13,6 @@ declare let bot_data: any;
declare let buddy_data: any; declare let buddy_data: any;
declare let buddy_list: any; declare let buddy_list: any;
declare let click_handlers: any; declare let click_handlers: any;
declare let color_data: any;
declare let compose_actions: any; declare let compose_actions: any;
declare let composebox_typeahead: any; declare let composebox_typeahead: any;
declare let compose_ui: any; declare let compose_ui: any;

View File

@ -1,5 +1,6 @@
"use strict"; "use strict";
const color_data = require("./color_data");
const {FoldDict} = require("./fold_dict"); const {FoldDict} = require("./fold_dict");
const peer_data = require("./peer_data"); const peer_data = require("./peer_data");
const people = require("./people"); const people = require("./people");

View File

@ -1,5 +1,6 @@
"use strict"; "use strict";
const color_data = require("./color_data");
const narrow_state = require("./narrow_state"); const narrow_state = require("./narrow_state");
const peer_data = require("./peer_data"); const peer_data = require("./peer_data");