2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
2021-02-28 01:22:43 +01:00
|
|
|
import _ from "lodash";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-06-21 10:02:26 +02:00
|
|
|
import render_confirm_delete_user from "../templates/confirm_dialog/confirm_delete_user.hbs";
|
2021-04-24 20:53:39 +02:00
|
|
|
import render_admin_user_group_list from "../templates/settings/admin_user_group_list.hbs";
|
2020-07-25 02:02:35 +02:00
|
|
|
|
2021-02-28 01:22:43 +01:00
|
|
|
import * as channel from "./channel";
|
|
|
|
import * as confirm_dialog from "./confirm_dialog";
|
2021-04-13 06:51:54 +02:00
|
|
|
import {$t, $t_html} from "./i18n";
|
2021-03-25 22:35:45 +01:00
|
|
|
import {page_params} from "./page_params";
|
2021-02-28 01:22:43 +01:00
|
|
|
import * as people from "./people";
|
|
|
|
import * as pill_typeahead from "./pill_typeahead";
|
2021-06-03 15:47:03 +02:00
|
|
|
import * as settings_data from "./settings_data";
|
2021-02-28 01:22:43 +01:00
|
|
|
import * as ui_report from "./ui_report";
|
|
|
|
import * as user_groups from "./user_groups";
|
|
|
|
import * as user_pill from "./user_pill";
|
2020-08-20 21:24:06 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const meta = {
|
2017-11-13 05:20:42 +01:00
|
|
|
loaded: false,
|
|
|
|
};
|
|
|
|
|
2021-02-28 01:22:43 +01:00
|
|
|
export function reset() {
|
2017-11-13 05:20:42 +01:00
|
|
|
meta.loaded = false;
|
2021-02-28 01:22:43 +01:00
|
|
|
}
|
2017-11-13 05:20:42 +01:00
|
|
|
|
2021-02-28 01:22:43 +01:00
|
|
|
export function reload() {
|
2018-03-04 17:31:22 +01:00
|
|
|
if (!meta.loaded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const user_groups_section = $("#user-groups").expectOne();
|
|
|
|
user_groups_section.html("");
|
2021-02-28 01:22:43 +01:00
|
|
|
populate_user_groups();
|
|
|
|
}
|
2017-11-17 06:15:19 +01:00
|
|
|
|
2021-02-28 01:22:43 +01:00
|
|
|
export function can_edit(group_id) {
|
2021-06-03 15:47:03 +02:00
|
|
|
if (!settings_data.user_can_edit_user_groups()) {
|
2018-06-14 08:35:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-06-03 15:47:03 +02:00
|
|
|
// Admins and moderators are allowed to edit user groups even if they
|
|
|
|
// are not a member of that user group. Members can edit user groups
|
|
|
|
// only if they belong to that group.
|
|
|
|
if (page_params.is_admin || page_params.is_moderator) {
|
|
|
|
return true;
|
2019-11-02 17:58:55 +01:00
|
|
|
}
|
|
|
|
|
2018-06-14 09:44:07 +02:00
|
|
|
return user_groups.is_member_of(group_id, people.my_current_user_id());
|
2021-02-28 01:22:43 +01:00
|
|
|
}
|
2018-06-14 08:35:05 +02:00
|
|
|
|
2021-02-28 01:22:43 +01:00
|
|
|
export function populate_user_groups() {
|
2020-07-15 01:29:15 +02:00
|
|
|
const user_groups_section = $("#user-groups").expectOne();
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_groups_array = user_groups.get_realm_user_groups();
|
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 data of user_groups_array) {
|
2020-07-15 00:34:28 +02:00
|
|
|
user_groups_section.append(
|
|
|
|
render_admin_user_group_list({
|
|
|
|
user_group: {
|
|
|
|
name: data.name,
|
|
|
|
id: data.id,
|
|
|
|
description: data.description,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
2021-02-03 23:23:32 +01:00
|
|
|
const pill_container = $(`.pill-container[data-group-pills="${CSS.escape(data.id)}"]`);
|
2019-11-02 00:06:25 +01:00
|
|
|
const pills = user_pill.create_pills(pill_container);
|
2018-03-06 15:03:20 +01:00
|
|
|
|
|
|
|
function get_pill_user_ids() {
|
|
|
|
return user_pill.get_user_ids(pills);
|
|
|
|
}
|
|
|
|
|
2021-02-03 23:23:32 +01:00
|
|
|
const userg = $(`div.user-group[id="${CSS.escape(data.id)}"]`);
|
2021-01-22 22:29:08 +01:00
|
|
|
for (const user_id of data.members) {
|
2020-02-05 14:30:59 +01:00
|
|
|
const user = people.get_by_user_id(user_id);
|
2018-05-09 13:42:10 +02:00
|
|
|
user_pill.append_user(user, pills);
|
2021-01-22 22:29:08 +01:00
|
|
|
}
|
2017-11-17 04:31:53 +01:00
|
|
|
|
2018-03-13 10:46:17 +01:00
|
|
|
function update_membership(group_id) {
|
2021-02-28 01:22:43 +01:00
|
|
|
if (can_edit(group_id)) {
|
2018-03-13 10:46:17 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
userg.find(".name").attr("contenteditable", "false");
|
|
|
|
userg.find(".description").attr("contenteditable", "false");
|
|
|
|
userg.addClass("ntm");
|
|
|
|
pill_container.find(".input").attr("contenteditable", "false");
|
|
|
|
pill_container.find(".input").css("display", "none");
|
|
|
|
pill_container.addClass("not-editable");
|
|
|
|
pill_container.off("keydown", ".pill");
|
|
|
|
pill_container.off("keydown", ".input");
|
|
|
|
pill_container.off("click");
|
|
|
|
pill_container.on("click", (e) => {
|
2018-03-13 10:46:17 +01:00
|
|
|
e.stopPropagation();
|
|
|
|
});
|
2020-07-20 21:55:26 +02:00
|
|
|
pill_container.find(".pill").on("mouseenter", () => {
|
|
|
|
pill_container.find(".pill").find(".exit").css("opacity", "0.5");
|
|
|
|
});
|
2018-03-13 10:46:17 +01:00
|
|
|
}
|
|
|
|
update_membership(data.id);
|
|
|
|
|
2018-03-15 12:10:07 +01:00
|
|
|
function is_user_group_changed() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const draft_group = get_pill_user_ids();
|
|
|
|
const group_data = user_groups.get_user_group_from_id(data.id);
|
2020-02-04 23:46:56 +01:00
|
|
|
const original_group = Array.from(group_data.members);
|
2019-11-02 00:06:25 +01:00
|
|
|
const same_groups = _.isEqual(_.sortBy(draft_group), _.sortBy(original_group));
|
2021-02-03 23:23:32 +01:00
|
|
|
const description = $(`#user-groups #${CSS.escape(data.id)} .description`)
|
2020-07-15 00:34:28 +02:00
|
|
|
.text()
|
|
|
|
.trim();
|
2021-02-03 23:23:32 +01:00
|
|
|
const name = $(`#user-groups #${CSS.escape(data.id)} .name`)
|
2020-07-15 00:34:28 +02:00
|
|
|
.text()
|
|
|
|
.trim();
|
2021-02-03 23:23:32 +01:00
|
|
|
const user_group_status = $(`#user-groups #${CSS.escape(data.id)} .user-group-status`);
|
2018-08-25 00:32:13 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (user_group_status.is(":visible")) {
|
2018-08-25 00:32:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
2018-03-15 12:10:07 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
if (
|
|
|
|
group_data.description === description &&
|
|
|
|
group_data.name === name &&
|
|
|
|
(!draft_group.length || same_groups)
|
|
|
|
) {
|
2018-03-15 12:10:07 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function update_cancel_button() {
|
2021-02-28 01:22:43 +01:00
|
|
|
if (!can_edit(data.id)) {
|
2018-03-13 10:46:17 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-02-03 23:23:32 +01:00
|
|
|
const cancel_button = $(`#user-groups #${CSS.escape(data.id)} .save-status.btn-danger`);
|
|
|
|
const saved_button = $(`#user-groups #${CSS.escape(data.id)} .save-status.sea-green`);
|
|
|
|
const save_instructions = $(`#user-groups #${CSS.escape(data.id)} .save-instructions`);
|
2018-03-15 12:56:31 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
if (is_user_group_changed() && !cancel_button.is(":visible")) {
|
2018-03-15 12:10:07 +01:00
|
|
|
saved_button.fadeOut(0);
|
2020-07-15 01:29:15 +02:00
|
|
|
cancel_button.css({display: "inline-block", opacity: "0"}).fadeTo(400, 1);
|
|
|
|
save_instructions.css({display: "block", opacity: "0"}).fadeTo(400, 1);
|
2020-07-15 00:34:28 +02:00
|
|
|
} else if (!is_user_group_changed() && cancel_button.is(":visible")) {
|
2018-05-06 21:43:17 +02:00
|
|
|
cancel_button.fadeOut();
|
|
|
|
save_instructions.fadeOut();
|
|
|
|
}
|
2018-03-15 12:10:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function show_saved_button() {
|
2021-02-03 23:23:32 +01:00
|
|
|
const cancel_button = $(`#user-groups #${CSS.escape(data.id)} .save-status.btn-danger`);
|
|
|
|
const saved_button = $(`#user-groups #${CSS.escape(data.id)} .save-status.sea-green`);
|
|
|
|
const save_instructions = $(`#user-groups #${CSS.escape(data.id)} .save-instructions`);
|
2020-07-15 01:29:15 +02:00
|
|
|
if (!saved_button.is(":visible")) {
|
2018-03-15 12:10:07 +01:00
|
|
|
cancel_button.fadeOut(0);
|
2018-03-15 12:56:31 +01:00
|
|
|
save_instructions.fadeOut(0);
|
2020-07-15 00:34:28 +02:00
|
|
|
saved_button
|
|
|
|
.css({display: "inline-block", opacity: "0"})
|
|
|
|
.fadeTo(400, 1)
|
|
|
|
.delay(2000)
|
|
|
|
.fadeTo(400, 0);
|
2018-03-15 12:10:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 16:45:37 +01:00
|
|
|
function save_members() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const draft_group = get_pill_user_ids();
|
|
|
|
const group_data = user_groups.get_user_group_from_id(data.id);
|
2020-02-04 23:46:56 +01:00
|
|
|
const original_group = Array.from(group_data.members);
|
2019-11-02 00:06:25 +01:00
|
|
|
const same_groups = _.isEqual(_.sortBy(draft_group), _.sortBy(original_group));
|
2018-03-13 23:51:53 +01:00
|
|
|
if (!draft_group.length || same_groups) {
|
|
|
|
return;
|
2017-12-18 21:38:02 +01:00
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const added = _.difference(draft_group, original_group);
|
|
|
|
const removed = _.difference(original_group, draft_group);
|
2018-03-13 23:51:53 +01:00
|
|
|
channel.post({
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/user_groups/" + data.id + "/members",
|
2018-03-13 23:51:53 +01:00
|
|
|
data: {
|
|
|
|
add: JSON.stringify(added),
|
|
|
|
delete: JSON.stringify(removed),
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
success() {
|
2018-08-22 00:31:48 +02:00
|
|
|
setTimeout(show_saved_button, 200);
|
2018-03-15 12:10:07 +01:00
|
|
|
},
|
2018-03-13 23:51:53 +01:00
|
|
|
});
|
2018-03-14 16:45:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function save_name_desc() {
|
2021-02-03 23:23:32 +01:00
|
|
|
const user_group_status = $(`#user-groups #${CSS.escape(data.id)} .user-group-status`);
|
2019-11-02 00:06:25 +01:00
|
|
|
const group_data = user_groups.get_user_group_from_id(data.id);
|
2021-02-03 23:23:32 +01:00
|
|
|
const description = $(`#user-groups #${CSS.escape(data.id)} .description`)
|
2020-07-15 00:34:28 +02:00
|
|
|
.text()
|
|
|
|
.trim();
|
2021-02-03 23:23:32 +01:00
|
|
|
const name = $(`#user-groups #${CSS.escape(data.id)} .name`)
|
2020-07-15 00:34:28 +02:00
|
|
|
.text()
|
|
|
|
.trim();
|
2018-03-14 16:45:37 +01:00
|
|
|
|
|
|
|
if (group_data.description === description && group_data.name === name) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
channel.patch({
|
|
|
|
url: "/json/user_groups/" + data.id,
|
|
|
|
data: {
|
2020-07-20 22:18:43 +02:00
|
|
|
name,
|
|
|
|
description,
|
2018-03-14 16:45:37 +01:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
success() {
|
2018-08-25 00:32:13 +02:00
|
|
|
user_group_status.hide();
|
2018-08-22 00:31:48 +02:00
|
|
|
setTimeout(show_saved_button, 200);
|
2018-03-14 16:45:37 +01:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
error(xhr) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const errors = JSON.parse(xhr.responseText).msg;
|
2018-08-25 00:32:13 +02:00
|
|
|
xhr.responseText = JSON.stringify({msg: errors});
|
2021-04-13 05:18:25 +02:00
|
|
|
ui_report.error($t_html({defaultMessage: "Failed"}), xhr, user_group_status);
|
2018-08-25 00:32:13 +02:00
|
|
|
update_cancel_button();
|
2021-02-03 23:23:32 +01:00
|
|
|
$(`#user-groups #${CSS.escape(data.id)} .name`).text(group_data.name);
|
|
|
|
$(`#user-groups #${CSS.escape(data.id)} .description`).text(
|
|
|
|
group_data.description,
|
|
|
|
);
|
2018-08-25 00:32:13 +02:00
|
|
|
},
|
2018-03-14 16:45:37 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function do_not_blur(except_class, event) {
|
|
|
|
// Event generated from or inside the typeahead.
|
|
|
|
if ($(event.relatedTarget).closest(".typeahead").length) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-03 23:23:32 +01:00
|
|
|
if ($(event.relatedTarget).closest(`#user-groups #${CSS.escape(data.id)}`).length) {
|
2020-02-26 09:27:28 +01:00
|
|
|
return [".pill-container", ".name", ".description", ".input", ".delete"].some(
|
|
|
|
(class_name) =>
|
|
|
|
class_name !== except_class &&
|
|
|
|
$(event.relatedTarget).closest(class_name).length,
|
2020-02-08 04:08:04 +01:00
|
|
|
);
|
2018-03-14 16:45:37 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-15 12:10:07 +01:00
|
|
|
function auto_save(class_name, event) {
|
2021-02-28 01:22:43 +01:00
|
|
|
if (!can_edit(data.id)) {
|
2018-03-13 10:46:17 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-15 12:10:07 +01:00
|
|
|
if (do_not_blur(class_name, event)) {
|
2018-03-14 16:45:37 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-07-15 00:34:28 +02:00
|
|
|
if (
|
2021-02-03 23:23:32 +01:00
|
|
|
$(event.relatedTarget).closest(`#user-groups #${CSS.escape(data.id)}`) &&
|
2020-07-15 00:34:28 +02:00
|
|
|
$(event.relatedTarget).closest(".save-status.btn-danger").length
|
|
|
|
) {
|
2021-02-28 01:22:43 +01:00
|
|
|
reload();
|
2018-05-06 21:43:17 +02:00
|
|
|
return;
|
2018-03-15 12:10:07 +01:00
|
|
|
}
|
2018-03-14 16:45:37 +01:00
|
|
|
save_name_desc();
|
|
|
|
save_members();
|
2018-03-15 12:10:07 +01:00
|
|
|
}
|
|
|
|
|
2021-02-03 23:23:32 +01:00
|
|
|
$(`#user-groups #${CSS.escape(data.id)}`).on("blur", ".input", (event) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
auto_save(".input", event);
|
2018-03-14 16:45:37 +01:00
|
|
|
});
|
|
|
|
|
2021-02-03 23:23:32 +01:00
|
|
|
$(`#user-groups #${CSS.escape(data.id)}`).on("blur", ".name", (event) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
auto_save(".name", event);
|
2018-03-15 12:10:07 +01:00
|
|
|
});
|
2021-02-03 23:23:32 +01:00
|
|
|
$(`#user-groups #${CSS.escape(data.id)}`).on("input", ".name", () => {
|
2018-03-15 12:10:07 +01:00
|
|
|
update_cancel_button();
|
2018-03-14 16:45:37 +01:00
|
|
|
});
|
|
|
|
|
2021-02-03 23:23:32 +01:00
|
|
|
$(`#user-groups #${CSS.escape(data.id)}`).on("blur", ".description", (event) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
auto_save(".description", event);
|
2018-03-15 12:10:07 +01:00
|
|
|
});
|
2021-02-03 23:23:32 +01:00
|
|
|
$(`#user-groups #${CSS.escape(data.id)}`).on("input", ".description", () => {
|
2018-03-15 12:10:07 +01:00
|
|
|
update_cancel_button();
|
2018-03-13 23:51:53 +01:00
|
|
|
});
|
2017-12-18 21:38:02 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const input = pill_container.children(".input");
|
2021-02-28 01:22:43 +01:00
|
|
|
if (can_edit(data.id)) {
|
2021-05-25 10:11:21 +02:00
|
|
|
const opts = {update_func: update_cancel_button, user: true};
|
2020-07-24 18:04:15 +02:00
|
|
|
pill_typeahead.set_up(input, pills, opts);
|
2018-05-09 13:42:10 +02:00
|
|
|
}
|
2017-12-19 07:43:42 +01:00
|
|
|
|
2021-05-06 21:49:45 +02:00
|
|
|
if (can_edit(data.id)) {
|
2020-07-02 01:45:54 +02:00
|
|
|
pills.onPillRemove(() => {
|
2018-03-13 09:58:00 +01:00
|
|
|
// onPillRemove is fired before the pill is removed from
|
|
|
|
// the DOM.
|
2018-03-15 12:10:07 +01:00
|
|
|
update_cancel_button();
|
2020-07-02 01:45:54 +02:00
|
|
|
setTimeout(() => {
|
2020-07-20 21:24:26 +02:00
|
|
|
input.trigger("focus");
|
2018-03-13 09:58:00 +01:00
|
|
|
}, 100);
|
|
|
|
});
|
2021-05-06 21:49:45 +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
|
|
|
}
|
2021-02-28 01:22:43 +01:00
|
|
|
}
|
2017-11-17 04:31:53 +01:00
|
|
|
|
2021-02-28 01:22:43 +01:00
|
|
|
export function set_up() {
|
2017-11-13 05:20:42 +01:00
|
|
|
meta.loaded = true;
|
2021-02-28 01:22:43 +01:00
|
|
|
populate_user_groups();
|
2017-11-17 06:15:19 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
$(".organization form.admin-user-group-form")
|
|
|
|
.off("submit")
|
|
|
|
.on("submit", function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2017-11-17 06:15:19 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
const user_group_status = $("#admin-user-group-status");
|
2017-11-17 06:15:19 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
const group = {
|
|
|
|
members: JSON.stringify([people.my_current_user_id()]),
|
|
|
|
};
|
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
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
for (const obj of $(this).serializeArray()) {
|
|
|
|
if (obj.value.trim() === "") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
group[obj.name] = obj.value;
|
2017-11-17 06:15:19 +01:00
|
|
|
}
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
channel.post({
|
|
|
|
url: "/json/user_groups/create",
|
|
|
|
data: group,
|
2020-07-20 22:18:43 +02:00
|
|
|
success() {
|
2020-07-15 00:34:28 +02:00
|
|
|
user_group_status.hide();
|
2021-04-13 05:18:25 +02:00
|
|
|
ui_report.success(
|
|
|
|
$t_html({defaultMessage: "User group added!"}),
|
|
|
|
user_group_status,
|
|
|
|
);
|
2020-07-15 00:34:28 +02:00
|
|
|
$("form.admin-user-group-form input[type='text']").val("");
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
error(xhr) {
|
2020-07-15 00:34:28 +02:00
|
|
|
user_group_status.hide();
|
|
|
|
const errors = JSON.parse(xhr.responseText).msg;
|
|
|
|
xhr.responseText = JSON.stringify({msg: errors});
|
2021-04-13 05:18:25 +02:00
|
|
|
ui_report.error($t_html({defaultMessage: "Failed"}), xhr, user_group_status);
|
2020-07-15 00:34:28 +02:00
|
|
|
},
|
|
|
|
});
|
2017-11-17 06:15:19 +01:00
|
|
|
});
|
2017-12-06 20:00:23 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#user-groups").on("click", ".delete", function () {
|
2020-10-07 09:17:30 +02:00
|
|
|
const group_id = Number.parseInt($(this).parents(".user-group").attr("id"), 10);
|
2021-02-28 01:22:43 +01:00
|
|
|
if (!can_edit(group_id)) {
|
2018-03-13 10:46:17 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_group = user_groups.get_user_group_from_id(group_id);
|
|
|
|
const btn = $(this);
|
2018-09-12 20:45:41 +02:00
|
|
|
|
|
|
|
function delete_user_group() {
|
|
|
|
channel.del({
|
|
|
|
url: "/json/user_groups/" + group_id,
|
|
|
|
data: {
|
|
|
|
id: group_id,
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
error() {
|
2021-04-13 06:51:54 +02:00
|
|
|
btn.text($t({defaultMessage: "Failed!"}));
|
2018-09-12 20:45:41 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const html_body = render_confirm_delete_user({
|
2018-09-12 17:43:09 +02:00
|
|
|
group_name: user_group.name,
|
|
|
|
});
|
|
|
|
|
|
|
|
confirm_dialog.launch({
|
2021-04-13 05:24:31 +02:00
|
|
|
html_heading: $t_html({defaultMessage: "Delete user group"}),
|
2020-07-20 22:18:43 +02:00
|
|
|
html_body,
|
2018-09-12 17:43:09 +02:00
|
|
|
on_click: delete_user_group,
|
|
|
|
});
|
2017-12-06 20:00:23 +01:00
|
|
|
});
|
2017-12-19 04:02:31 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#user-groups").on("keypress", ".user-group h4 > span", (e) => {
|
2021-05-31 19:15:01 +02:00
|
|
|
if (e.key === "Enter") {
|
2017-12-19 04:02:31 +01:00
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
2021-02-28 01:22:43 +01:00
|
|
|
}
|