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

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2021-02-27 15:38:33 -08:00 committed by Tim Abbott
parent 977e7dfe07
commit 000865ceb1
16 changed files with 36 additions and 38 deletions

View File

@ -263,7 +263,6 @@
"upload_widget": false,
"user_events": false,
"user_groups": false,
"user_pill": false,
"poll_widget": false,
"widgetize": false,
"zxcvbn": false

View File

@ -88,7 +88,6 @@ const stream_data = zrequire("stream_data");
const compose_state = zrequire("compose_state");
const people = zrequire("people");
zrequire("input_pill");
zrequire("user_pill");
const compose_pm_pill = zrequire("compose_pm_pill");
const echo = zrequire("echo");
const compose = zrequire("compose");

View File

@ -10,7 +10,6 @@ const people = zrequire("people");
const compose_pm_pill = zrequire("compose_pm_pill");
const input_pill = zrequire("input_pill");
zrequire("user_pill");
const compose_actions = set_global("compose_actions", {});
let pills = {

View File

@ -657,7 +657,7 @@ run_test("initialize", () => {
let pm_recipient_typeahead_called = false;
$("#private_message_recipient").typeahead = (options) => {
let inserted_users = [];
user_pill.get_user_ids = () => inserted_users;
user_pill.__Rewire__("get_user_ids", () => inserted_users);
// This should match the users added at the beginning of this test file.
let actual_value = options.source("");

View File

@ -363,7 +363,7 @@ test_ui("with_external_user", () => {
people.get_by_user_id = () => noop;
user_pill.append_person = () => noop;
user_pill.__Rewire__("append_person", () => noop);
let can_edit_called = 0;
settings_user_groups.can_edit = () => {

View File

@ -246,7 +246,7 @@ test_ui("subscriber_pills", () => {
// Only Denmark stream pill is created and a
// request is sent to add all it's subscribers.
user_pill.get_user_ids = () => [];
user_pill.__Rewire__("get_user_ids", () => []);
expected_user_ids = potential_denmark_stream_subscribers;
add_subscribers_handler(event);
@ -258,14 +258,14 @@ test_ui("subscriber_pills", () => {
// No request is sent if we try to subscribe ourselves
// only and are already subscribed to the stream.
user_pill.get_user_ids = () => [me.user_id];
user_pill.__Rewire__("get_user_ids", () => [me.user_id]);
add_subscribers_handler(event);
assert(!add_subscribers_request);
// Denmark stream pill and fred and mark user pills are created.
// But only one request for mark is sent even though a mark user
// pill is created and mark is also a subscriber of Denmark stream.
user_pill.get_user_ids = () => [mark.user_id, fred.user_id];
user_pill.__Rewire__("get_user_ids", () => [mark.user_id, fred.user_id]);
stream_pill.get_user_ids = () => peer_data.get_subscribers(denmark.stream_id);
expected_user_ids = potential_denmark_stream_subscribers.concat(fred.user_id);
add_subscribers_handler(event);

View File

@ -16,7 +16,6 @@ import "flatpickr/dist/plugins/confirmDate/confirmDate";
import "../i18n";
import "../fold_dict";
import "../input_pill";
import "../user_pill";
import "../compose_pm_pill";
import "../channel";
import "../setup";

View File

@ -1,6 +1,7 @@
"use strict";
const people = require("./people");
const user_pill = require("./user_pill");
const util = require("./util");
exports.initialize_pill = function () {

View File

@ -11,6 +11,7 @@ const typeahead = require("../shared/js/typeahead");
const people = require("./people");
const settings_data = require("./settings_data");
const user_pill = require("./user_pill");
//************************************
// AN IMPORTANT NOTE ABOUT TYPEAHEADS

View File

@ -134,7 +134,6 @@ declare let upload: any;
declare let upload_widget: any;
declare let user_events: any;
declare let user_groups: any;
declare let user_pill: any;
declare let widgetize: any;
interface JQuery {

View File

@ -1,6 +1,7 @@
import * as people from "./people";
import * as settings_data from "./settings_data";
import * as stream_pill from "./stream_pill";
import * as user_pill from "./user_pill";
export function set_up(input, pills, opts) {
let source = opts.source;

View File

@ -11,6 +11,7 @@ const people = require("./people");
const pill_typeahead = require("./pill_typeahead");
const settings_ui = require("./settings_ui");
const setup = require("./setup");
const user_pill = require("./user_pill");
exports.update_email = function (new_email) {
const email_input = $("#email_value");

View File

@ -7,6 +7,7 @@ const render_confirm_delete_user = require("../templates/confirm_delete_user.hbs
const people = require("./people");
const pill_typeahead = require("./pill_typeahead");
const user_pill = require("./user_pill");
const meta = {
loaded: false,

View File

@ -9,6 +9,7 @@ const people = require("./people");
const settings_config = require("./settings_config");
const settings_data = require("./settings_data");
const settings_ui = require("./settings_ui");
const user_pill = require("./user_pill");
const section = {
active: {},

View File

@ -14,6 +14,7 @@ const settings_data = require("./settings_data");
const settings_ui = require("./settings_ui");
const stream_pill = require("./stream_pill");
const stream_ui_updates = require("./stream_ui_updates");
const user_pill = require("./user_pill");
const util = require("./util");
function setup_subscriptions_stream_hash(sub) {

View File

@ -1,11 +1,9 @@
"use strict";
const people = require("./people");
import * as people from "./people";
// This will be used for pills for things like composing PMs
// or adding users to a stream/group.
exports.create_item_from_email = function (email, current_items) {
export function create_item_from_email(email, current_items) {
// For normal Zulip use, we need to validate the email for our realm.
const user = people.get_by_email(email);
@ -48,13 +46,13 @@ exports.create_item_from_email = function (email, current_items) {
};
return item;
};
}
exports.get_email_from_item = function (item) {
export function get_email_from_item(item) {
return item.email;
};
}
exports.append_person = function (opts) {
export function append_person(opts) {
const person = opts.person;
const pill_widget = opts.pill_widget;
const avatar_url = people.small_avatar_url_for_person(person);
@ -66,17 +64,17 @@ exports.append_person = function (opts) {
img_src: avatar_url,
});
pill_widget.clear_text();
};
}
exports.get_user_ids = function (pill_widget) {
export function get_user_ids(pill_widget) {
const items = pill_widget.items();
let user_ids = items.map((item) => item.user_id);
user_ids = user_ids.filter(Boolean); // be defensive about undefined users
return user_ids;
};
}
exports.has_unconverted_data = function (pill_widget) {
export function has_unconverted_data(pill_widget) {
// This returns true if we either have text that hasn't been
// turned into pills or email-only pills (for Zephyr).
if (pill_widget.is_pending()) {
@ -87,37 +85,35 @@ exports.has_unconverted_data = function (pill_widget) {
const has_unknown_items = items.some((item) => item.user_id === undefined);
return has_unknown_items;
};
}
exports.typeahead_source = function (pill_widget) {
export function typeahead_source(pill_widget) {
const persons = people.get_realm_users();
return user_pill.filter_taken_users(persons, pill_widget);
};
return filter_taken_users(persons, pill_widget);
}
exports.filter_taken_users = function (items, pill_widget) {
const taken_user_ids = exports.get_user_ids(pill_widget);
export function filter_taken_users(items, pill_widget) {
const taken_user_ids = get_user_ids(pill_widget);
items = items.filter((item) => !taken_user_ids.includes(item.user_id));
return items;
};
}
exports.append_user = function (user, pills) {
export function append_user(user, pills) {
if (user) {
exports.append_person({
append_person({
pill_widget: pills,
person: user,
});
} else {
blueslip.warn("Undefined user in function append_user");
}
};
}
exports.create_pills = function (pill_container) {
export function create_pills(pill_container) {
const pills = input_pill.create({
container: pill_container,
create_item_from_text: exports.create_item_from_email,
get_text_from_item: exports.get_email_from_item,
create_item_from_text: create_item_from_email,
get_text_from_item: get_email_from_item,
});
return pills;
};
window.user_pill = exports;
}