2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
2021-06-06 22:22:22 +02:00
|
|
|
import type {ReferenceElement} from "tippy.js";
|
2021-04-20 16:49:39 +02:00
|
|
|
import tippy from "tippy.js";
|
2021-03-11 05:43:45 +01:00
|
|
|
|
2021-04-13 06:51:54 +02:00
|
|
|
import {$t} from "./i18n";
|
2021-03-25 21:38:40 +01:00
|
|
|
|
2021-02-28 00:35:35 +01:00
|
|
|
export const status_classes = "alert-error alert-success alert-info alert-warning";
|
2017-06-22 22:08:43 +02:00
|
|
|
|
2021-03-13 15:49:01 +01:00
|
|
|
// TODO: Move this to the portico codebase.
|
2021-06-06 22:22:22 +02:00
|
|
|
export function autofocus(selector: string): void {
|
2020-07-02 01:45:54 +02:00
|
|
|
$(() => {
|
2020-07-20 21:24:26 +02:00
|
|
|
$(selector).trigger("focus");
|
2012-08-29 17:45:15 +02:00
|
|
|
});
|
2021-02-28 00:35:35 +01:00
|
|
|
}
|
2013-04-03 22:30:36 +02:00
|
|
|
|
2021-06-06 22:22:22 +02:00
|
|
|
export function phrase_match(query: string, phrase: string): boolean {
|
2018-06-25 17:14:45 +02:00
|
|
|
// match "tes" to "test" and "stream test" but not "hostess"
|
2019-11-02 00:06:25 +01:00
|
|
|
let i;
|
2018-06-25 17:14:45 +02:00
|
|
|
query = query.toLowerCase();
|
|
|
|
|
|
|
|
phrase = phrase.toLowerCase();
|
2020-01-28 15:26:02 +01:00
|
|
|
if (phrase.startsWith(query)) {
|
2018-06-25 17:14:45 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const parts = phrase.split(" ");
|
2018-06-25 17:14:45 +02:00
|
|
|
for (i = 0; i < parts.length; i += 1) {
|
2020-01-28 15:26:02 +01:00
|
|
|
if (parts[i].startsWith(query)) {
|
2018-06-25 17:14:45 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2021-02-28 00:35:35 +01:00
|
|
|
}
|
2018-06-25 17:14:45 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
export function copy_data_attribute_value($elem: JQuery, key: string): void {
|
2019-06-12 16:09:24 +02:00
|
|
|
// function to copy the value of data-key
|
|
|
|
// attribute of the element to clipboard
|
2022-01-25 11:36:19 +01:00
|
|
|
const $temp = $(document.createElement("input"));
|
|
|
|
$("body").append($temp);
|
|
|
|
$temp.val($elem.data(key)).trigger("select");
|
2019-06-12 16:09:24 +02:00
|
|
|
document.execCommand("copy");
|
2022-01-25 11:36:19 +01:00
|
|
|
$temp.remove();
|
|
|
|
$elem.fadeOut(250);
|
|
|
|
$elem.fadeIn(1000);
|
2021-02-28 00:35:35 +01:00
|
|
|
}
|
2019-06-12 16:09:24 +02:00
|
|
|
|
2021-06-06 22:22:22 +02:00
|
|
|
export function has_mac_keyboard(): boolean {
|
2020-10-07 12:37:15 +02:00
|
|
|
return /mac/i.test(navigator.platform);
|
2021-02-28 00:35:35 +01:00
|
|
|
}
|
2019-06-10 09:09:04 +02:00
|
|
|
|
2022-08-29 17:29:27 +02:00
|
|
|
export function adjust_mac_shortcuts(kbd_elem_class: string): void {
|
2021-02-28 00:35:35 +01:00
|
|
|
if (!has_mac_keyboard()) {
|
2019-06-07 11:03:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const keys_map = new Map([
|
2020-07-15 01:29:15 +02:00
|
|
|
["Backspace", "Delete"],
|
|
|
|
["Enter", "Return"],
|
2022-06-21 17:58:02 +02:00
|
|
|
["Home", "←"],
|
|
|
|
["End", "→"],
|
|
|
|
["PgUp", "↑"],
|
|
|
|
["PgDn", "↓"],
|
2020-07-15 01:29:15 +02:00
|
|
|
["Ctrl", "⌘"],
|
2022-07-13 12:28:12 +02:00
|
|
|
["Alt", "⌘"],
|
2019-06-07 11:03:13 +02:00
|
|
|
]);
|
|
|
|
|
2022-06-21 17:58:02 +02:00
|
|
|
const fn_shortcuts = new Set(["Home", "End", "PgUp", "PgDn"]);
|
|
|
|
|
2022-08-29 17:29:27 +02:00
|
|
|
$(kbd_elem_class).each(function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
let key_text = $(this).text();
|
2019-06-07 11:03:13 +02:00
|
|
|
|
2022-06-21 17:58:02 +02:00
|
|
|
if (fn_shortcuts.has(key_text)) {
|
2022-08-29 17:29:27 +02:00
|
|
|
$(this).before("<kbd>Fn</kbd> + ");
|
|
|
|
$(this).addClass("arrow-key");
|
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
|
|
|
}
|
|
|
|
|
2022-06-21 17:58:02 +02:00
|
|
|
const replace_key = keys_map.get(key_text);
|
|
|
|
if (replace_key !== undefined) {
|
|
|
|
key_text = replace_key;
|
|
|
|
}
|
|
|
|
|
2019-06-07 11:03:13 +02:00
|
|
|
$(this).text(key_text);
|
|
|
|
});
|
2021-02-28 00:35:35 +01:00
|
|
|
}
|
2021-04-05 09:42:29 +02:00
|
|
|
|
|
|
|
// See https://zulip.readthedocs.io/en/latest/development/authentication.html#password-form-implementation
|
|
|
|
// for design details on this feature.
|
2021-06-06 22:22:22 +02:00
|
|
|
function set_password_toggle_label(
|
|
|
|
password_selector: string,
|
|
|
|
label: string,
|
|
|
|
tippy_tooltips: boolean,
|
|
|
|
): void {
|
2021-04-20 16:49:39 +02:00
|
|
|
$(password_selector).attr("aria-label", label);
|
|
|
|
if (tippy_tooltips) {
|
2021-06-06 22:22:22 +02:00
|
|
|
const element: ReferenceElement = $(password_selector)[0];
|
2021-06-14 17:43:40 +02:00
|
|
|
const tippy_instance = element._tippy ?? tippy(element);
|
|
|
|
tippy_instance.setContent(label);
|
2021-04-20 16:49:39 +02:00
|
|
|
} else {
|
|
|
|
$(password_selector).attr("title", label);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:22:22 +02:00
|
|
|
function toggle_password_visibility(
|
|
|
|
password_field_id: string,
|
|
|
|
password_selector: string,
|
|
|
|
tippy_tooltips: boolean,
|
|
|
|
): void {
|
2021-04-20 16:49:39 +02:00
|
|
|
let label;
|
2022-01-25 11:36:19 +01:00
|
|
|
const $password_field = $(password_field_id);
|
2021-04-05 09:42:29 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
if ($password_field.attr("type") === "password") {
|
|
|
|
$password_field.attr("type", "text");
|
2021-04-05 09:42:29 +02:00
|
|
|
$(password_selector).removeClass("fa-eye-slash").addClass("fa-eye");
|
2021-04-20 16:49:39 +02:00
|
|
|
label = $t({defaultMessage: "Hide password"});
|
2021-04-05 09:42:29 +02:00
|
|
|
} else {
|
2022-01-25 11:36:19 +01:00
|
|
|
$password_field.attr("type", "password");
|
2021-04-05 09:42:29 +02:00
|
|
|
$(password_selector).removeClass("fa-eye").addClass("fa-eye-slash");
|
2021-04-20 16:49:39 +02:00
|
|
|
label = $t({defaultMessage: "Show password"});
|
2021-04-05 09:42:29 +02:00
|
|
|
}
|
2021-04-20 16:49:39 +02:00
|
|
|
set_password_toggle_label(password_selector, label, tippy_tooltips);
|
2021-04-05 09:42:29 +02:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:22:22 +02:00
|
|
|
export function reset_password_toggle_icons(
|
|
|
|
password_field: string,
|
|
|
|
password_selector: string,
|
|
|
|
): void {
|
2021-04-05 09:53:15 +02:00
|
|
|
$(password_field).attr("type", "password");
|
|
|
|
$(password_selector).removeClass("fa-eye").addClass("fa-eye-slash");
|
2021-04-20 16:49:39 +02:00
|
|
|
const label = $t({defaultMessage: "Show password"});
|
|
|
|
set_password_toggle_label(password_selector, label, true);
|
2021-04-05 09:53:15 +02:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:20:23 +02:00
|
|
|
export function setup_password_visibility_toggle(
|
2021-06-06 22:22:22 +02:00
|
|
|
password_field_id: string,
|
|
|
|
password_selector: string,
|
2021-06-06 22:20:23 +02:00
|
|
|
{tippy_tooltips = false} = {},
|
2021-06-06 22:22:22 +02:00
|
|
|
): void {
|
2021-04-20 16:49:39 +02:00
|
|
|
const label = $t({defaultMessage: "Show password"});
|
2021-06-06 22:20:23 +02:00
|
|
|
set_password_toggle_label(password_selector, label, tippy_tooltips);
|
2021-04-05 09:42:29 +02:00
|
|
|
$(password_selector).on("click", (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2021-06-06 22:20:23 +02:00
|
|
|
toggle_password_visibility(password_field_id, password_selector, tippy_tooltips);
|
2021-04-05 09:42:29 +02:00
|
|
|
});
|
|
|
|
}
|