2021-03-11 05:43:45 +01:00
|
|
|
|
import $ from "jquery";
|
2021-04-23 20:58:44 +02:00
|
|
|
|
import SimpleBar from "simplebar";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
|
2021-04-13 06:51:54 +02:00
|
|
|
|
import {$t} from "./i18n";
|
2021-03-30 02:21:21 +02:00
|
|
|
|
import * as message_lists from "./message_lists";
|
2021-02-24 05:00:36 +01:00
|
|
|
|
|
2012-12-03 20:54:29 +01:00
|
|
|
|
// What, if anything, obscures the home tab?
|
2012-11-30 18:28:23 +01:00
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
|
export function replace_emoji_with_text($element) {
|
|
|
|
|
$element.find(".emoji").replaceWith(function () {
|
2017-10-14 23:14:45 +02:00
|
|
|
|
if ($(this).is("img")) {
|
|
|
|
|
return $(this).attr("alt");
|
|
|
|
|
}
|
2017-09-27 19:39:42 +02:00
|
|
|
|
return $(this).text();
|
2013-07-23 00:25:25 +02:00
|
|
|
|
});
|
2021-02-28 21:33:10 +01:00
|
|
|
|
}
|
2013-07-23 00:25:25 +02:00
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
|
export function get_content_element($element) {
|
|
|
|
|
const element = $element.expectOne()[0];
|
2019-11-13 21:04:29 +01:00
|
|
|
|
const sb = SimpleBar.instances.get(element);
|
|
|
|
|
if (sb) {
|
|
|
|
|
return $(sb.getContentElement());
|
2019-03-01 01:40:05 +01:00
|
|
|
|
}
|
2022-01-25 11:36:19 +01:00
|
|
|
|
return $element;
|
2021-02-28 21:33:10 +01:00
|
|
|
|
}
|
2017-08-02 16:06:38 +02:00
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
|
export function get_scroll_element($element) {
|
|
|
|
|
const element = $element.expectOne()[0];
|
2019-11-13 21:04:29 +01:00
|
|
|
|
const sb = SimpleBar.instances.get(element);
|
|
|
|
|
if (sb) {
|
|
|
|
|
return $(sb.getScrollElement());
|
2020-07-15 01:29:15 +02:00
|
|
|
|
} else if ("simplebar" in element.dataset) {
|
2019-07-26 00:22:43 +02:00
|
|
|
|
// The SimpleBar mutation observer hasn’t processed this element yet.
|
|
|
|
|
// Create the SimpleBar early in case we need to add event listeners.
|
|
|
|
|
return $(new SimpleBar(element).getScrollElement());
|
2019-01-09 14:30:35 +01:00
|
|
|
|
}
|
2022-01-25 11:36:19 +01:00
|
|
|
|
return $element;
|
2021-02-28 21:33:10 +01:00
|
|
|
|
}
|
2019-01-09 14:30:35 +01:00
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
|
export function reset_scrollbar($element) {
|
|
|
|
|
const element = $element.expectOne()[0];
|
2019-11-13 21:04:29 +01:00
|
|
|
|
const sb = SimpleBar.instances.get(element);
|
|
|
|
|
if (sb) {
|
|
|
|
|
sb.getScrollElement().scrollTop = 0;
|
2019-03-01 01:40:05 +01:00
|
|
|
|
} else {
|
|
|
|
|
element.scrollTop = 0;
|
2018-03-20 04:10:20 +01:00
|
|
|
|
}
|
2021-02-28 21:33:10 +01:00
|
|
|
|
}
|
2017-08-02 16:06:38 +02:00
|
|
|
|
|
2013-12-19 17:03:08 +01:00
|
|
|
|
function update_message_in_all_views(message_id, callback) {
|
2022-08-17 07:10:19 +02:00
|
|
|
|
for (const msg_list of message_lists.all_rendered_message_lists()) {
|
|
|
|
|
const $row = msg_list.get_row(message_id);
|
2022-01-25 11:36:19 +01:00
|
|
|
|
if ($row === undefined) {
|
2013-12-19 17:03:08 +01:00
|
|
|
|
// The row may not exist, e.g. if you do an action on a message in
|
|
|
|
|
// a narrowed view
|
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
|
|
|
|
continue;
|
2013-03-27 18:30:02 +01:00
|
|
|
|
}
|
2022-01-25 11:36:19 +01:00
|
|
|
|
callback($row);
|
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
|
|
|
|
}
|
2013-12-19 17:03:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-28 21:33:10 +01:00
|
|
|
|
export function update_starred_view(message_id, new_value) {
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const starred = new_value;
|
2013-12-19 17:03:08 +01:00
|
|
|
|
|
|
|
|
|
// Avoid a full re-render, but update the star in each message
|
|
|
|
|
// table in which it is visible.
|
2022-01-25 11:36:19 +01:00
|
|
|
|
update_message_in_all_views(message_id, ($row) => {
|
|
|
|
|
const $elt = $row.find(".star");
|
|
|
|
|
const $star_container = $row.find(".star_container");
|
2014-01-23 17:52:28 +01:00
|
|
|
|
if (starred) {
|
2022-01-25 11:36:19 +01:00
|
|
|
|
$elt.addClass("fa-star").removeClass("fa-star-o");
|
|
|
|
|
$star_container.removeClass("empty-star");
|
2014-01-23 17:52:28 +01:00
|
|
|
|
} else {
|
2022-01-25 11:36:19 +01:00
|
|
|
|
$elt.removeClass("fa-star").addClass("fa-star-o");
|
|
|
|
|
$star_container.addClass("empty-star");
|
2014-01-23 17:52:28 +01:00
|
|
|
|
}
|
2021-04-13 06:51:54 +02:00
|
|
|
|
const title_state = starred ? $t({defaultMessage: "Unstar"}) : $t({defaultMessage: "Star"});
|
2022-01-25 11:36:19 +01:00
|
|
|
|
$star_container.attr(
|
2021-05-13 19:16:36 +02:00
|
|
|
|
"data-tippy-content",
|
2021-04-13 06:51:54 +02:00
|
|
|
|
$t(
|
|
|
|
|
{defaultMessage: "{starred_status} this message (Ctrl + s)"},
|
|
|
|
|
{starred_status: title_state},
|
|
|
|
|
),
|
2021-02-07 22:58:41 +01:00
|
|
|
|
);
|
2013-03-27 18:30:02 +01:00
|
|
|
|
});
|
2021-02-28 21:33:10 +01:00
|
|
|
|
}
|
2013-03-27 18:30:02 +01:00
|
|
|
|
|
2021-02-28 21:33:10 +01:00
|
|
|
|
export function show_message_failed(message_id, failed_msg) {
|
2013-12-19 17:03:08 +01:00
|
|
|
|
// Failed to send message, so display inline retry/cancel
|
2022-01-25 11:36:19 +01:00
|
|
|
|
update_message_in_all_views(message_id, ($row) => {
|
|
|
|
|
const $failed_div = $row.find(".message_failed");
|
2023-01-19 08:34:22 +01:00
|
|
|
|
$failed_div.toggleClass("hide", false);
|
2022-01-25 11:36:19 +01:00
|
|
|
|
$failed_div.find(".failed_text").attr("title", failed_msg);
|
2013-12-19 17:03:08 +01:00
|
|
|
|
});
|
2021-02-28 21:33:10 +01:00
|
|
|
|
}
|
2013-12-19 17:03:08 +01:00
|
|
|
|
|
2021-02-28 21:33:10 +01:00
|
|
|
|
export function show_failed_message_success(message_id) {
|
2013-12-19 17:03:08 +01:00
|
|
|
|
// Previously failed message succeeded
|
2022-01-25 11:36:19 +01:00
|
|
|
|
update_message_in_all_views(message_id, ($row) => {
|
2023-01-19 08:34:22 +01:00
|
|
|
|
$row.find(".message_failed").toggleClass("hide", true);
|
2013-12-19 17:03:08 +01:00
|
|
|
|
});
|
2021-02-28 21:33:10 +01:00
|
|
|
|
}
|
2013-12-19 17:03:08 +01:00
|
|
|
|
|
2013-02-18 08:16:57 +01:00
|
|
|
|
// Save the compose content cursor position and restore when we
|
|
|
|
|
// shift-tab back in (see hotkey.js).
|
2019-11-02 00:06:25 +01:00
|
|
|
|
let saved_compose_cursor = 0;
|
2013-02-18 08:16:57 +01:00
|
|
|
|
|
2021-02-28 21:33:10 +01:00
|
|
|
|
export function set_compose_textarea_handlers() {
|
2020-07-20 21:26:58 +02:00
|
|
|
|
$("#compose-textarea").on("blur", function () {
|
2016-06-14 22:49:41 +02:00
|
|
|
|
saved_compose_cursor = $(this).caret();
|
2013-02-18 08:16:57 +01:00
|
|
|
|
});
|
2017-04-13 22:33:26 +02:00
|
|
|
|
|
|
|
|
|
// on the end of the modified-message fade in, remove the fade-in-message class.
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const animationEnd = "webkitAnimationEnd oanimationend msAnimationEnd animationend";
|
2017-04-13 22:33:26 +02:00
|
|
|
|
$("body").on(animationEnd, ".fade-in-message", function () {
|
|
|
|
|
$(this).removeClass("fade-in-message");
|
|
|
|
|
});
|
2021-02-28 21:33:10 +01:00
|
|
|
|
}
|
2013-02-18 08:16:57 +01:00
|
|
|
|
|
2021-02-28 21:33:10 +01:00
|
|
|
|
export function restore_compose_cursor() {
|
2020-07-20 21:24:26 +02:00
|
|
|
|
$("#compose-textarea").trigger("focus").caret(saved_compose_cursor);
|
2021-02-28 21:33:10 +01:00
|
|
|
|
}
|
2017-08-08 02:16:35 +02:00
|
|
|
|
|
2021-02-28 21:33:10 +01:00
|
|
|
|
export function initialize() {
|
|
|
|
|
set_compose_textarea_handlers();
|
|
|
|
|
}
|