2019-11-02 00:06:25 +01:00
|
|
|
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");
|
2019-07-09 21:24:00 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const meta = {
|
2017-12-14 05:51:45 +01:00
|
|
|
loaded: false,
|
|
|
|
};
|
|
|
|
|
2018-12-08 17:37:57 +01:00
|
|
|
exports.maybe_disable_widgets = function () {
|
|
|
|
if (page_params.is_admin) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$(".organization-box [data-name='profile-field-settings']")
|
|
|
|
.find("input, button, select").attr("disabled", true);
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let order = [];
|
|
|
|
const field_types = page_params.custom_profile_field_types;
|
2018-04-08 18:13:37 +02:00
|
|
|
|
2018-05-01 19:33:33 +02:00
|
|
|
exports.field_type_id_to_string = function (type_id) {
|
2020-02-08 05:09:09 +01:00
|
|
|
for (const field_type of Object.values(field_types)) {
|
2018-08-15 11:35:18 +02:00
|
|
|
if (field_type.id === type_id) {
|
2018-08-15 13:28:51 +02:00
|
|
|
// Few necessary modifications in field-type-name for
|
|
|
|
// table-list view of custom fields UI in org settings
|
|
|
|
if (field_type.name === "Date picker") {
|
2020-02-08 05:09:09 +01:00
|
|
|
return "Date";
|
2018-08-15 13:28:51 +02:00
|
|
|
} else if (field_type.name === "Person picker") {
|
2020-02-08 05:09:09 +01:00
|
|
|
return "Person";
|
2018-08-15 13:28:51 +02:00
|
|
|
}
|
2020-02-08 05:09:09 +01:00
|
|
|
return field_type.name;
|
2018-08-15 11:35:18 +02:00
|
|
|
}
|
2020-02-08 05:09:09 +01:00
|
|
|
}
|
2018-05-01 19:33:33 +02:00
|
|
|
};
|
2017-12-14 05:51:45 +01:00
|
|
|
|
2018-08-15 13:37:40 +02:00
|
|
|
function update_profile_fields_table_element() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const profile_fields_table = $("#admin_profile_fields_table").expectOne();
|
2018-08-15 13:37:40 +02:00
|
|
|
|
|
|
|
// If there are no custom fields, hide the table headers at the top
|
|
|
|
if (page_params.custom_profile_fields.length < 1) {
|
|
|
|
profile_fields_table.hide();
|
|
|
|
} else {
|
|
|
|
profile_fields_table.show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 05:51:45 +01:00
|
|
|
function delete_profile_field(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2018-04-10 20:47:15 +02:00
|
|
|
|
|
|
|
settings_ui.do_settings_change(
|
|
|
|
channel.del,
|
|
|
|
"/json/realm/profile_fields/" + encodeURIComponent($(this).attr('data-profile-field-id')),
|
|
|
|
{}, $('#admin-profile-field-status').expectOne());
|
2018-08-15 13:37:40 +02:00
|
|
|
update_profile_fields_table_element();
|
2017-12-14 05:51:45 +01:00
|
|
|
}
|
|
|
|
|
2019-06-07 08:19:00 +02:00
|
|
|
function read_choice_field_data_from_form(field_elem) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const field_data = {};
|
|
|
|
let field_order = 1;
|
2019-06-07 08:19:00 +02:00
|
|
|
$(field_elem).find('div.choice-row').each(function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const text = $(this).find("input")[0].value;
|
2018-08-12 14:57:26 +02:00
|
|
|
if (text) {
|
|
|
|
field_data[field_order - 1] = {text: text, order: field_order.toString()};
|
|
|
|
field_order += 1;
|
|
|
|
}
|
2018-04-12 11:17:52 +02:00
|
|
|
});
|
2018-08-02 19:31:17 +02:00
|
|
|
|
2018-04-12 11:17:52 +02:00
|
|
|
return field_data;
|
|
|
|
}
|
|
|
|
|
2019-05-27 10:59:55 +02:00
|
|
|
function read_external_account_field_data(field_elem) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const field_data = {};
|
2019-05-27 10:59:55 +02:00
|
|
|
field_data.subtype = $(field_elem).find('select[name=external_acc_field_type]').val();
|
|
|
|
if (field_data.subtype === "custom") {
|
|
|
|
field_data.url_pattern = $(field_elem).find('input[name=url_pattern]').val();
|
|
|
|
}
|
|
|
|
return field_data;
|
|
|
|
}
|
|
|
|
|
2018-08-11 19:47:58 +02:00
|
|
|
function update_choice_delete_btn(container, display_flag) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const no_of_choice_row = container.find(".choice-row").length;
|
2018-08-11 19:47:58 +02:00
|
|
|
|
|
|
|
// Disable delete button if there only one choice row
|
|
|
|
// Enable choice delete button more one than once choice
|
|
|
|
if (no_of_choice_row === 1) {
|
|
|
|
if (display_flag === true) {
|
|
|
|
container.find(".choice-row .delete-choice").show();
|
|
|
|
} else {
|
|
|
|
container.find(".choice-row .delete-choice").hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-11 17:02:46 +02:00
|
|
|
function create_choice_row(container) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const context = {};
|
|
|
|
const row = render_settings_profile_field_choice(context);
|
2018-05-10 13:36:09 +02:00
|
|
|
$(container).append(row);
|
|
|
|
}
|
|
|
|
|
|
|
|
function clear_form_data() {
|
2019-07-30 00:10:23 +02:00
|
|
|
$("#profile_field_name").val("").closest(".control-group").show();
|
|
|
|
$("#profile_field_hint").val("").closest(".control-group").show();
|
2018-08-15 13:17:15 +02:00
|
|
|
// Set default type "Short Text" in field type dropdown
|
|
|
|
$("#profile_field_type").val(field_types.SHORT_TEXT.id);
|
2018-05-10 13:36:09 +02:00
|
|
|
// Clear data from choice field form
|
|
|
|
$("#profile_field_choices").html("");
|
2018-08-11 17:02:46 +02:00
|
|
|
create_choice_row($("#profile_field_choices"));
|
2018-08-11 19:47:58 +02:00
|
|
|
update_choice_delete_btn($("#profile_field_choices"), false);
|
2018-05-10 13:36:09 +02:00
|
|
|
$("#profile_field_choices_row").hide();
|
2019-05-27 10:59:55 +02:00
|
|
|
// Clear external account field form
|
|
|
|
$("#custom_field_url_pattern").val("");
|
|
|
|
$("#custom_external_account_url_pattern").hide();
|
|
|
|
$("#profile_field_external_accounts").hide();
|
2019-07-30 00:10:23 +02:00
|
|
|
$("#profile_field_external_accounts_type").val(
|
|
|
|
$("#profile_field_external_accounts_type option:first").val());
|
|
|
|
}
|
|
|
|
|
|
|
|
function set_up_create_field_form() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const field_elem = $("#profile_field_external_accounts");
|
|
|
|
const field_url_pattern_elem = $("#custom_external_account_url_pattern");
|
2019-07-30 00:10:23 +02:00
|
|
|
|
|
|
|
if (parseInt($("#profile_field_type").val(), 10) === field_types.EXTERNAL_ACCOUNT.id) {
|
|
|
|
field_elem.show();
|
|
|
|
if ($("#profile_field_external_accounts_type").val() === 'custom') {
|
|
|
|
field_url_pattern_elem.show();
|
|
|
|
$("#profile_field_name").val("").closest(".control-group").show();
|
|
|
|
$("#profile_field_hint").val("").closest(".control-group").show();
|
|
|
|
} else {
|
|
|
|
field_url_pattern_elem.hide();
|
|
|
|
$("#profile_field_name").closest(".control-group").hide();
|
|
|
|
$("#profile_field_hint").closest(".control-group").hide();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$("#profile_field_name").closest(".control-group").show();
|
|
|
|
$("#profile_field_hint").closest(".control-group").show();
|
|
|
|
field_url_pattern_elem.hide();
|
|
|
|
field_elem.hide();
|
|
|
|
}
|
2018-05-10 13:36:09 +02:00
|
|
|
}
|
|
|
|
|
2019-06-07 08:19:00 +02:00
|
|
|
function read_field_data_from_form(field_type_id, field_elem) {
|
|
|
|
// Only read field data if we are creating a choice field
|
2019-05-27 10:59:55 +02:00
|
|
|
// or external account field.
|
2019-06-07 08:19:00 +02:00
|
|
|
if (field_type_id === field_types.CHOICE.id) {
|
|
|
|
return read_choice_field_data_from_form(field_elem);
|
2019-05-27 10:59:55 +02:00
|
|
|
} else if (field_type_id === field_types.EXTERNAL_ACCOUNT.id) {
|
|
|
|
return read_external_account_field_data(field_elem);
|
2019-06-07 08:19:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 05:51:45 +01:00
|
|
|
function create_profile_field(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let field_data = {};
|
|
|
|
const field_type = $('#profile_field_type').val();
|
|
|
|
const opts = {
|
2018-05-10 13:36:09 +02:00
|
|
|
success_continuation: clear_form_data,
|
|
|
|
};
|
2019-06-07 08:19:00 +02:00
|
|
|
field_data = read_field_data_from_form(parseInt(field_type, 10), $('.new-profile-field-form'));
|
2019-07-30 00:10:23 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const form_data = {
|
2018-08-02 18:35:33 +02:00
|
|
|
name: $("#profile_field_name").val(),
|
2018-08-15 13:17:15 +02:00
|
|
|
field_type: field_type,
|
2018-08-02 18:35:33 +02:00
|
|
|
hint: $("#profile_field_hint").val(),
|
|
|
|
field_data: JSON.stringify(field_data),
|
|
|
|
};
|
2018-04-12 11:17:52 +02:00
|
|
|
|
2018-08-02 18:35:33 +02:00
|
|
|
settings_ui.do_settings_change(channel.post, "/json/realm/profile_fields", form_data,
|
2019-07-10 14:32:27 +02:00
|
|
|
$('#admin-add-profile-field-status').expectOne(), opts);
|
2018-08-15 13:37:40 +02:00
|
|
|
update_profile_fields_table_element();
|
2018-04-12 11:17:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function add_choice_row(e) {
|
2018-08-12 14:50:16 +02:00
|
|
|
if ($(e.target).parent().next().hasClass("choice-row")) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const choices_div = e.delegateTarget;
|
2018-08-12 14:50:16 +02:00
|
|
|
update_choice_delete_btn($(choices_div), true);
|
2018-08-11 17:02:46 +02:00
|
|
|
create_choice_row(choices_div);
|
2018-04-12 11:17:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function delete_choice_row(e) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const row = $(e.currentTarget).parent();
|
|
|
|
const container = row.parent();
|
2018-04-12 11:17:52 +02:00
|
|
|
row.remove();
|
2018-08-11 19:47:58 +02:00
|
|
|
update_choice_delete_btn(container, false);
|
2018-04-12 11:17:52 +02:00
|
|
|
}
|
|
|
|
|
2017-12-14 05:51:45 +01:00
|
|
|
function get_profile_field_info(id) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const info = {};
|
2017-12-14 05:51:45 +01:00
|
|
|
info.row = $("tr.profile-field-row[data-profile-field-id='" + id + "']");
|
|
|
|
info.form = $("tr.profile-field-form[data-profile-field-id='" + id + "']");
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2018-05-10 13:36:09 +02:00
|
|
|
function get_profile_field(id) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const all_custom_fields = page_params.custom_profile_fields;
|
|
|
|
let field;
|
|
|
|
for (let i = 0; i < all_custom_fields.length; i += 1) {
|
2018-05-10 13:36:09 +02:00
|
|
|
if (all_custom_fields[i].id === id) {
|
|
|
|
field = all_custom_fields[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return field;
|
|
|
|
}
|
|
|
|
|
2018-08-06 20:35:50 +02:00
|
|
|
exports.parse_field_choices_from_field_data = function (field_data) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const choices = [];
|
2020-02-06 06:34:47 +01:00
|
|
|
for (const [value, choice] of Object.entries(field_data)) {
|
2018-08-06 20:35:50 +02:00
|
|
|
choices.push({
|
|
|
|
value: value,
|
|
|
|
text: choice.text,
|
|
|
|
order: choice.order,
|
|
|
|
});
|
2020-02-06 06:34:47 +01:00
|
|
|
}
|
2018-08-06 20:35:50 +02:00
|
|
|
|
|
|
|
return choices;
|
|
|
|
};
|
|
|
|
|
2019-05-27 10:59:55 +02:00
|
|
|
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') {
|
|
|
|
field_elem.form.find('input[name=url_pattern]').val(url_pattern_val);
|
|
|
|
field_elem.form.find('.custom_external_account_detail').show();
|
2019-07-30 00:10:23 +02:00
|
|
|
field_elem.form.find('input[name=name]').val("").closest(".control-group").show();
|
|
|
|
field_elem.form.find('input[name=hint]').val("").closest(".control-group").show();
|
2019-05-27 10:59:55 +02:00
|
|
|
} else {
|
2019-07-30 00:10:23 +02:00
|
|
|
field_elem.form.find('input[name=name]').closest(".control-group").hide();
|
|
|
|
field_elem.form.find('input[name=hint]').closest(".control-group").hide();
|
2019-05-27 10:59:55 +02:00
|
|
|
field_elem.form.find('.custom_external_account_detail').hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-06 13:14:34 +02:00
|
|
|
function set_up_choices_field_edit_form(profile_field, field_data) {
|
|
|
|
// Re-render field choices in edit form to load initial choice data
|
2019-11-02 00:06:25 +01:00
|
|
|
const choice_list = profile_field.form.find('.edit_profile_field_choices_container');
|
2019-06-06 13:14:34 +02:00
|
|
|
choice_list.off();
|
|
|
|
choice_list.html("");
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const choices_data = exports.parse_field_choices_from_field_data(field_data);
|
2019-06-06 13:14:34 +02:00
|
|
|
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
for (const choice of choices_data) {
|
2019-06-06 13:14:34 +02:00
|
|
|
choice_list.append(
|
2019-07-09 21:24:00 +02:00
|
|
|
render_settings_profile_field_choice({
|
2019-06-06 13:14:34 +02:00
|
|
|
text: choice.text,
|
2020-07-02 02:16:03 +02:00
|
|
|
}),
|
2019-06-06 13:14:34 +02:00
|
|
|
);
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
}
|
2019-06-06 13:14:34 +02:00
|
|
|
|
|
|
|
// Add blank choice at last
|
|
|
|
create_choice_row(choice_list);
|
|
|
|
update_choice_delete_btn(choice_list, false);
|
|
|
|
Sortable.create(choice_list[0], {
|
|
|
|
onUpdate: function () {},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-14 05:51:45 +01:00
|
|
|
function open_edit_form(e) {
|
2020-01-16 20:40:59 +01:00
|
|
|
const field_id = parseInt($(e.currentTarget).attr("data-profile-field-id"), 10);
|
2019-11-02 00:06:25 +01:00
|
|
|
const profile_field = get_profile_field_info(field_id);
|
2017-12-14 05:51:45 +01:00
|
|
|
|
|
|
|
profile_field.row.hide();
|
|
|
|
profile_field.form.show();
|
2020-01-16 20:40:59 +01:00
|
|
|
const field = get_profile_field(field_id);
|
2018-05-10 13:36:09 +02:00
|
|
|
// Set initial value in edit form
|
|
|
|
profile_field.form.find('input[name=name]').val(field.name);
|
|
|
|
profile_field.form.find('input[name=hint]').val(field.hint);
|
2019-11-02 00:06:25 +01:00
|
|
|
let field_data = {};
|
2019-06-06 13:14:34 +02:00
|
|
|
if (field.field_data) {
|
|
|
|
field_data = JSON.parse(field.field_data);
|
|
|
|
}
|
2017-12-14 05:51:45 +01:00
|
|
|
|
2018-08-15 13:17:15 +02:00
|
|
|
if (parseInt(field.type, 10) === field_types.CHOICE.id) {
|
2019-06-06 13:14:34 +02:00
|
|
|
set_up_choices_field_edit_form(profile_field, field_data);
|
2018-08-02 19:31:17 +02:00
|
|
|
}
|
|
|
|
|
2019-05-27 10:59:55 +02:00
|
|
|
if (parseInt(field.type, 10) === field_types.EXTERNAL_ACCOUNT.id) {
|
|
|
|
profile_field.form.find('select[name=external_acc_field_type]').val(field_data.subtype);
|
|
|
|
set_up_external_account_field_edit_form(profile_field, field_data.url_pattern);
|
|
|
|
}
|
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
profile_field.form.find('.reset').on("click", () => {
|
2017-12-14 05:51:45 +01:00
|
|
|
profile_field.form.hide();
|
|
|
|
profile_field.row.show();
|
|
|
|
});
|
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
profile_field.form.find('.submit').on("click", () => {
|
2017-12-14 05:51:45 +01:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const profile_field_status = $('#admin-profile-field-status').expectOne();
|
2017-12-14 05:51:45 +01:00
|
|
|
|
|
|
|
// For some reason jQuery's serialize() is not working with
|
|
|
|
// channel.patch even though it is supported by $.ajax.
|
2019-11-02 00:06:25 +01:00
|
|
|
const data = {};
|
2019-07-30 00:10:23 +02:00
|
|
|
|
2017-12-14 05:51:45 +01:00
|
|
|
data.name = profile_field.form.find('input[name=name]').val();
|
2018-03-31 09:23:57 +02:00
|
|
|
data.hint = profile_field.form.find('input[name=hint]').val();
|
2019-06-07 08:19:00 +02:00
|
|
|
data.field_data = JSON.stringify(read_field_data_from_form(parseInt(field.type, 10),
|
|
|
|
profile_field.form));
|
2017-12-14 05:51:45 +01:00
|
|
|
|
2018-04-10 20:47:15 +02:00
|
|
|
settings_ui.do_settings_change(channel.patch, "/json/realm/profile_fields/" + field_id,
|
|
|
|
data, profile_field_status);
|
2017-12-14 05:51:45 +01:00
|
|
|
});
|
2018-04-12 11:17:52 +02:00
|
|
|
|
2018-08-12 14:50:16 +02:00
|
|
|
profile_field.form.find(".edit_profile_field_choices_container").on("input", ".choice-row input", add_choice_row);
|
2018-08-02 19:31:17 +02:00
|
|
|
profile_field.form.find(".edit_profile_field_choices_container").on("click", "button.delete-choice", delete_choice_row);
|
2020-07-02 01:45:54 +02:00
|
|
|
$(".profile_field_external_accounts_edit select").on('change', (e) => {
|
2020-01-16 20:40:59 +01:00
|
|
|
const field_id = parseInt($(e.target).closest('.profile-field-form').attr('data-profile-field-id'), 10);
|
2019-11-02 00:06:25 +01:00
|
|
|
const field_form = get_profile_field_info(field_id);
|
2019-05-27 10:59:55 +02:00
|
|
|
set_up_external_account_field_edit_form(field_form, "");
|
|
|
|
});
|
2017-12-14 05:51:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.reset = function () {
|
|
|
|
meta.loaded = false;
|
|
|
|
};
|
|
|
|
|
2018-06-09 09:37:59 +02:00
|
|
|
function update_field_order() {
|
|
|
|
order = [];
|
|
|
|
$('.profile-field-row').each(function () {
|
|
|
|
order.push(parseInt($(this).attr('data-profile-field-id'), 10));
|
|
|
|
});
|
|
|
|
settings_ui.do_settings_change(channel.patch, "/json/realm/profile_fields",
|
|
|
|
{order: JSON.stringify(order)},
|
|
|
|
$('#admin-profile-field-status').expectOne());
|
|
|
|
}
|
|
|
|
|
2017-12-14 05:51:45 +01:00
|
|
|
exports.populate_profile_fields = function (profile_fields_data) {
|
|
|
|
if (!meta.loaded) {
|
2018-06-11 21:02:00 +02:00
|
|
|
// If outside callers call us when we're not loaded, just
|
|
|
|
// exit and we'll draw the widgets again during set_up().
|
2017-12-14 05:51:45 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-06-11 21:02:00 +02:00
|
|
|
exports.do_populate_profile_fields(profile_fields_data);
|
|
|
|
};
|
2017-12-14 05:51:45 +01:00
|
|
|
|
2018-06-11 21:02:00 +02:00
|
|
|
exports.do_populate_profile_fields = function (profile_fields_data) {
|
|
|
|
// We should only call this internally or from tests.
|
2019-11-02 00:06:25 +01:00
|
|
|
const profile_fields_table = $("#admin_profile_fields_table").expectOne();
|
2018-06-09 09:37:59 +02:00
|
|
|
|
2017-12-14 05:51:45 +01:00
|
|
|
profile_fields_table.find("tr.profile-field-row").remove(); // Clear all rows.
|
|
|
|
profile_fields_table.find("tr.profile-field-form").remove(); // Clear all rows.
|
2018-04-08 18:13:37 +02:00
|
|
|
order = [];
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
|
|
|
|
for (const profile_field of profile_fields_data) {
|
2018-04-08 18:13:37 +02:00
|
|
|
order.push(profile_field.id);
|
2019-11-02 00:06:25 +01:00
|
|
|
let field_data = {};
|
2019-06-07 08:19:00 +02:00
|
|
|
if (profile_field.field_data) {
|
2018-04-12 11:17:52 +02:00
|
|
|
field_data = JSON.parse(profile_field.field_data);
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
let choices = [];
|
2018-08-15 13:17:15 +02:00
|
|
|
if (profile_field.type === field_types.CHOICE.id) {
|
2019-06-07 08:19:00 +02:00
|
|
|
choices = exports.parse_field_choices_from_field_data(field_data);
|
2018-04-12 11:17:52 +02:00
|
|
|
}
|
|
|
|
|
2017-12-14 05:51:45 +01:00
|
|
|
profile_fields_table.append(
|
2019-07-09 21:24:00 +02:00
|
|
|
render_admin_profile_field_list({
|
|
|
|
profile_field: {
|
|
|
|
id: profile_field.id,
|
|
|
|
name: profile_field.name,
|
|
|
|
hint: profile_field.hint,
|
|
|
|
type: exports.field_type_id_to_string(profile_field.type),
|
|
|
|
choices: choices,
|
|
|
|
is_choice_field: profile_field.type === field_types.CHOICE.id,
|
|
|
|
is_external_account_field: profile_field.type ===
|
|
|
|
field_types.EXTERNAL_ACCOUNT.id,
|
|
|
|
},
|
|
|
|
can_modify: page_params.is_admin,
|
|
|
|
realm_default_external_accounts: page_params.realm_default_external_accounts,
|
2020-07-02 02:16:03 +02:00
|
|
|
}),
|
2017-12-14 05:51:45 +01:00
|
|
|
);
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
}
|
|
|
|
|
2018-06-09 09:37:59 +02:00
|
|
|
if (page_params.is_admin) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const field_list = $("#admin_profile_fields_table")[0];
|
2018-06-09 09:37:59 +02:00
|
|
|
Sortable.create(field_list, {
|
|
|
|
onUpdate: update_field_order,
|
|
|
|
});
|
|
|
|
}
|
2018-08-15 13:37:40 +02:00
|
|
|
|
|
|
|
update_profile_fields_table_element();
|
2017-12-14 05:51:45 +01:00
|
|
|
loading.destroy_indicator($('#admin_page_profile_fields_loading_indicator'));
|
|
|
|
};
|
|
|
|
|
2018-04-12 11:17:52 +02:00
|
|
|
function set_up_choices_field() {
|
2018-08-11 17:02:46 +02:00
|
|
|
create_choice_row('#profile_field_choices');
|
2018-08-11 19:47:58 +02:00
|
|
|
update_choice_delete_btn($("#profile_field_choices"), false);
|
2018-04-12 11:17:52 +02:00
|
|
|
|
2018-08-24 06:40:40 +02:00
|
|
|
if (page_params.is_admin) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const choice_list = $("#profile_field_choices")[0];
|
2018-08-24 06:40:40 +02:00
|
|
|
Sortable.create(choice_list, {
|
|
|
|
onUpdate: function () {},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const field_type = $('#profile_field_type').val();
|
2018-08-02 19:31:17 +02:00
|
|
|
|
2018-08-15 13:17:15 +02:00
|
|
|
if (parseInt(field_type, 10) !== field_types.CHOICE.id) {
|
2018-04-12 11:17:52 +02:00
|
|
|
// If 'Choice' type is already selected, show choice row.
|
|
|
|
$("#profile_field_choices_row").hide();
|
|
|
|
}
|
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
$('#profile_field_type').on('change', (e) => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const selected_field_id = parseInt($(e.target).val(), 10);
|
2019-06-07 08:19:00 +02:00
|
|
|
if (selected_field_id === field_types.CHOICE.id) {
|
2018-04-12 11:17:52 +02:00
|
|
|
$("#profile_field_choices_row").show();
|
|
|
|
} else {
|
|
|
|
$("#profile_field_choices_row").hide();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-08-12 14:50:16 +02:00
|
|
|
$("#profile_field_choices").on("input", ".choice-row input", add_choice_row);
|
2018-04-12 11:17:52 +02:00
|
|
|
$("#profile_field_choices").on("click", "button.delete-choice", delete_choice_row);
|
|
|
|
}
|
|
|
|
|
2019-05-27 10:59:55 +02:00
|
|
|
function set_up_external_account_field() {
|
2020-07-02 01:45:54 +02:00
|
|
|
$('#profile_field_type').on('change', () => {
|
2019-07-30 00:10:23 +02:00
|
|
|
set_up_create_field_form();
|
2019-05-27 10:59:55 +02:00
|
|
|
});
|
2019-07-30 00:10:23 +02:00
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
$("#profile_field_external_accounts_type").on("change", () => {
|
2019-07-30 00:10:23 +02:00
|
|
|
set_up_create_field_form();
|
2019-05-27 10:59:55 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.get_external_account_link = function (field) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const field_subtype = field.field_data.subtype;
|
|
|
|
let field_url_pattern;
|
2019-05-27 10:59:55 +02:00
|
|
|
|
|
|
|
if (field_subtype === 'custom') {
|
|
|
|
field_url_pattern = field.field_data.url_pattern;
|
|
|
|
} else {
|
|
|
|
field_url_pattern =
|
|
|
|
page_params.realm_default_external_accounts[field_subtype].url_pattern;
|
|
|
|
}
|
|
|
|
return field_url_pattern.replace('%(username)s', field.value);
|
|
|
|
};
|
|
|
|
|
2017-12-14 05:51:45 +01:00
|
|
|
exports.set_up = function () {
|
2018-12-08 18:16:37 +01:00
|
|
|
exports.build_page();
|
|
|
|
exports.maybe_disable_widgets();
|
|
|
|
};
|
2017-12-14 05:51:45 +01:00
|
|
|
|
2018-12-08 18:16:37 +01:00
|
|
|
exports.build_page = function () {
|
2017-12-14 05:51:45 +01:00
|
|
|
// create loading indicators
|
|
|
|
loading.make_indicator($('#admin_page_profile_fields_loading_indicator'));
|
|
|
|
// Populate profile_fields table
|
2018-06-11 21:02:00 +02:00
|
|
|
exports.do_populate_profile_fields(page_params.custom_profile_fields);
|
|
|
|
meta.loaded = true;
|
2017-12-14 05:51:45 +01:00
|
|
|
|
|
|
|
$('#admin_profile_fields_table').on('click', '.delete', delete_profile_field);
|
2018-08-02 18:35:33 +02:00
|
|
|
$("#profile-field-settings").on("click", "#add-custom-profile-field-btn", create_profile_field);
|
2017-12-14 05:51:45 +01:00
|
|
|
$("#admin_profile_fields_table").on("click", ".open-edit-form", open_edit_form);
|
2018-04-12 11:17:52 +02:00
|
|
|
set_up_choices_field();
|
2019-05-27 10:59:55 +02:00
|
|
|
set_up_external_account_field();
|
2018-08-15 13:38:18 +02:00
|
|
|
clear_form_data();
|
2017-12-14 05:51:45 +01:00
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.settings_profile_fields = exports;
|