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

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2021-02-28 12:34:05 -08:00 committed by Tim Abbott
parent 7ec8b8980f
commit 099e8fb00e
10 changed files with 38 additions and 40 deletions

View File

@ -140,7 +140,6 @@
"page_params": false,
"pointer": false,
"realm_night_logo": false,
"settings_profile_fields": false,
"StripeCheckout": false,
"ui_init": false,
"zxcvbn": false

View File

@ -79,7 +79,8 @@ const settings_notifications = {__esModule: true};
rewiremock("../../static/js/settings_notifications").with(settings_notifications);
const settings_org = {__esModule: true};
rewiremock("../../static/js/settings_org").with(settings_org);
const settings_profile_fields = set_global("settings_profile_fields", {});
const settings_profile_fields = {__esModule: true};
rewiremock("../../static/js/settings_profile_fields").with(settings_profile_fields);
const settings_streams = {__esModule: true};
rewiremock("../../static/js/settings_streams").with(settings_streams);
const settings_user_groups = {__esModule: true};

View File

@ -17,7 +17,7 @@ rewiremock("../../static/js/settings_linkifiers").with({
rewiremock("../../static/js/settings_org").with({
maybe_disable_widgets() {},
});
set_global("settings_profile_fields", {
rewiremock("../../static/js/settings_profile_fields").with({
maybe_disable_widgets() {},
});
rewiremock("../../static/js/settings_streams").with({

View File

@ -23,7 +23,6 @@ import "../notifications";
import "../server_events";
import "../zulip";
import "../templates";
import "../settings_profile_fields";
import "../settings";
import "../ui_init";
import "../desktop_integration";

View File

@ -12,7 +12,6 @@ declare let home_msg_list: any;
declare let i18n: any;
declare let page_params: any;
declare let pointer: any;
declare let settings_profile_fields: any;
declare let zulip_test: any;
interface JQuery {

View File

@ -37,6 +37,7 @@ import * as rows from "./rows";
import * as settings_account from "./settings_account";
import * as settings_config from "./settings_config";
import * as settings_data from "./settings_data";
import * as settings_profile_fields from "./settings_profile_fields";
import * as stream_popover from "./stream_popover";
import * as user_groups from "./user_groups";
import * as user_status from "./user_status";

View File

@ -38,6 +38,7 @@ import * as settings_invites from "./settings_invites";
import * as settings_linkifiers from "./settings_linkifiers";
import * as settings_notifications from "./settings_notifications";
import * as settings_org from "./settings_org";
import * as settings_profile_fields from "./settings_profile_fields";
import * as settings_streams from "./settings_streams";
import * as settings_user_groups from "./settings_user_groups";
import * as settings_users from "./settings_users";

View File

@ -1,19 +1,17 @@
"use strict";
import {Sortable} from "sortablejs";
const {Sortable} = require("sortablejs");
import render_admin_profile_field_list from "../templates/admin_profile_field_list.hbs";
import render_settings_profile_field_choice from "../templates/settings/profile_field_choice.hbs";
const render_admin_profile_field_list = require("../templates/admin_profile_field_list.hbs");
const render_settings_profile_field_choice = require("../templates/settings/profile_field_choice.hbs");
const channel = require("./channel");
const loading = require("./loading");
const settings_ui = require("./settings_ui");
import * as channel from "./channel";
import * as loading from "./loading";
import * as settings_ui from "./settings_ui";
const meta = {
loaded: false,
};
exports.maybe_disable_widgets = function () {
export function maybe_disable_widgets() {
if (page_params.is_admin) {
return;
}
@ -21,12 +19,12 @@ exports.maybe_disable_widgets = function () {
$(".organization-box [data-name='profile-field-settings']")
.find("input, button, select")
.prop("disabled", true);
};
}
let order = [];
const field_types = page_params.custom_profile_field_types;
exports.field_type_id_to_string = function (type_id) {
export function field_type_id_to_string(type_id) {
for (const field_type of Object.values(field_types)) {
if (field_type.id === type_id) {
// Few necessary modifications in field-type-name for
@ -40,7 +38,7 @@ exports.field_type_id_to_string = function (type_id) {
}
}
return undefined;
};
}
function update_profile_fields_table_element() {
const profile_fields_table = $("#admin_profile_fields_table").expectOne();
@ -222,7 +220,7 @@ function get_profile_field(id) {
return page_params.custom_profile_fields.find((field) => field.id === id);
}
exports.parse_field_choices_from_field_data = function (field_data) {
export function parse_field_choices_from_field_data(field_data) {
const choices = [];
for (const [value, choice] of Object.entries(field_data)) {
choices.push({
@ -233,7 +231,7 @@ exports.parse_field_choices_from_field_data = function (field_data) {
}
return choices;
};
}
function set_up_external_account_field_edit_form(field_elem, url_pattern_val) {
if (field_elem.form.find("select[name=external_acc_field_type]").val() === "custom") {
@ -254,7 +252,7 @@ function set_up_choices_field_edit_form(profile_field, field_data) {
choice_list.off();
choice_list.html("");
const choices_data = exports.parse_field_choices_from_field_data(field_data);
const choices_data = parse_field_choices_from_field_data(field_data);
for (const choice of choices_data) {
choice_list.append(
@ -341,9 +339,9 @@ function open_edit_form(e) {
});
}
exports.reset = function () {
export function reset() {
meta.loaded = false;
};
}
function update_field_order() {
order = [];
@ -358,16 +356,16 @@ function update_field_order() {
);
}
exports.populate_profile_fields = function (profile_fields_data) {
export function populate_profile_fields(profile_fields_data) {
if (!meta.loaded) {
// If outside callers call us when we're not loaded, just
// exit and we'll draw the widgets again during set_up().
return;
}
exports.do_populate_profile_fields(profile_fields_data);
};
do_populate_profile_fields(profile_fields_data);
}
exports.do_populate_profile_fields = function (profile_fields_data) {
export function do_populate_profile_fields(profile_fields_data) {
// We should only call this internally or from tests.
const profile_fields_table = $("#admin_profile_fields_table").expectOne();
@ -383,7 +381,7 @@ exports.do_populate_profile_fields = function (profile_fields_data) {
}
let choices = [];
if (profile_field.type === field_types.CHOICE.id) {
choices = exports.parse_field_choices_from_field_data(field_data);
choices = parse_field_choices_from_field_data(field_data);
}
profile_fields_table.append(
@ -392,7 +390,7 @@ exports.do_populate_profile_fields = function (profile_fields_data) {
id: profile_field.id,
name: profile_field.name,
hint: profile_field.hint,
type: exports.field_type_id_to_string(profile_field.type),
type: field_type_id_to_string(profile_field.type),
choices,
is_choice_field: profile_field.type === field_types.CHOICE.id,
is_external_account_field:
@ -413,7 +411,7 @@ exports.do_populate_profile_fields = function (profile_fields_data) {
update_profile_fields_table_element();
loading.destroy_indicator($("#admin_page_profile_fields_loading_indicator"));
};
}
function set_up_choices_field() {
create_choice_row("#profile_field_choices");
@ -456,7 +454,7 @@ function set_up_external_account_field() {
});
}
exports.get_external_account_link = function (field) {
export function get_external_account_link(field) {
const field_subtype = field.field_data.subtype;
let field_url_pattern;
@ -466,18 +464,18 @@ exports.get_external_account_link = function (field) {
field_url_pattern = page_params.realm_default_external_accounts[field_subtype].url_pattern;
}
return field_url_pattern.replace("%(username)s", field.value);
};
}
exports.set_up = function () {
exports.build_page();
exports.maybe_disable_widgets();
};
export function set_up() {
build_page();
maybe_disable_widgets();
}
exports.build_page = function () {
export function build_page() {
// create loading indicators
loading.make_indicator($("#admin_page_profile_fields_loading_indicator"));
// Populate profile_fields table
exports.do_populate_profile_fields(page_params.custom_profile_fields);
do_populate_profile_fields(page_params.custom_profile_fields);
meta.loaded = true;
$("#admin_profile_fields_table").on("click", ".delete", delete_profile_field);
@ -486,6 +484,4 @@ exports.build_page = function () {
set_up_choices_field();
set_up_external_account_field();
clear_form_data();
};
window.settings_profile_fields = exports;
}

View File

@ -10,6 +10,7 @@ import * as settings_linkifiers from "./settings_linkifiers";
import * as settings_muting from "./settings_muting";
import * as settings_notifications from "./settings_notifications";
import * as settings_org from "./settings_org";
import * as settings_profile_fields from "./settings_profile_fields";
import * as settings_streams from "./settings_streams";
import * as settings_user_groups from "./settings_user_groups";
import * as settings_users from "./settings_users";

View File

@ -13,6 +13,7 @@ import * as settings_account from "./settings_account";
import * as settings_config from "./settings_config";
import * as settings_linkifiers from "./settings_linkifiers";
import * as settings_org from "./settings_org";
import * as settings_profile_fields from "./settings_profile_fields";
import * as settings_streams from "./settings_streams";
import * as settings_users from "./settings_users";