2019-11-02 00:06:25 +01:00
|
|
|
const render_tab_bar = require('../templates/tab_bar.hbs');
|
2019-07-09 21:24:00 +02:00
|
|
|
|
2014-01-08 16:12:22 +01:00
|
|
|
function make_tab(title, hash, data, extra_class, home) {
|
2013-05-09 21:12:53 +02:00
|
|
|
return {active: "inactive",
|
|
|
|
cls: extra_class || "",
|
|
|
|
title: title,
|
|
|
|
hash: hash,
|
2014-01-08 16:12:22 +01:00
|
|
|
data: data,
|
|
|
|
home: home || false };
|
2013-05-09 21:12:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function make_tab_data() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const tabs = [];
|
|
|
|
const filter = narrow_state.filter();
|
2013-12-13 22:15:00 +01:00
|
|
|
|
2017-05-13 20:54:53 +02:00
|
|
|
function filtered_to_non_home_view_stream() {
|
|
|
|
if (!filter.has_operator('stream')) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_name = filter.operands('stream')[0];
|
|
|
|
const stream_id = stream_data.get_stream_id(stream_name);
|
2017-05-13 20:54:53 +02:00
|
|
|
if (!stream_id) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-21 09:33:21 +02:00
|
|
|
return stream_data.is_muted(stream_id);
|
2017-05-13 20:54:53 +02:00
|
|
|
}
|
|
|
|
|
2017-08-12 21:30:48 +02:00
|
|
|
function in_all() {
|
2018-06-06 18:19:09 +02:00
|
|
|
return filter !== undefined &&
|
2017-08-12 21:30:48 +02:00
|
|
|
(filtered_to_non_home_view_stream() ||
|
2018-06-06 18:19:09 +02:00
|
|
|
filter.has_operand("in", "all"));
|
2017-08-12 21:30:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (in_all()) {
|
2013-12-13 22:15:00 +01:00
|
|
|
tabs.push(make_tab("All Messages", "#narrow/in/all", undefined, "root"));
|
2013-12-13 23:12:13 +01:00
|
|
|
} else if (page_params.narrow !== undefined) {
|
|
|
|
tabs.push(make_tab("Stream " + page_params.narrow_stream,
|
2018-08-04 16:33:28 +02:00
|
|
|
hash_util.operators_to_hash([page_params.narrow[0]]),
|
2013-12-13 23:12:13 +01:00
|
|
|
page_params.narrow_stream, 'stream'));
|
2014-01-17 00:18:21 +01:00
|
|
|
if (page_params.narrow_topic !== undefined) {
|
|
|
|
tabs.push(make_tab("Topic " + page_params.narrow_topic,
|
2018-08-04 16:33:28 +02:00
|
|
|
hash_util.operators_to_hash(page_params.narrow),
|
2014-01-17 00:18:21 +01:00
|
|
|
null));
|
|
|
|
}
|
2013-12-13 22:15:00 +01:00
|
|
|
}
|
2013-05-09 21:12:53 +02:00
|
|
|
|
2017-04-25 15:25:31 +02:00
|
|
|
if (narrow_state.active() && narrow_state.operators().length > 0) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let stream;
|
|
|
|
const ops = narrow_state.operators();
|
2013-05-09 21:12:53 +02:00
|
|
|
// Second breadcrumb item
|
2019-11-02 00:06:25 +01:00
|
|
|
let hashed = hash_util.operators_to_hash(ops.slice(0, 1));
|
2013-06-13 20:29:58 +02:00
|
|
|
if (filter.has_operator("stream")) {
|
|
|
|
stream = filter.operands("stream")[0];
|
|
|
|
tabs.push(make_tab(stream, hashed, stream, 'stream'));
|
|
|
|
} else if (filter.has_operator("pm-with") ||
|
2013-07-10 03:22:34 +02:00
|
|
|
filter.has_operand("is", "private")) {
|
2013-05-09 21:12:53 +02:00
|
|
|
|
2013-07-10 03:22:34 +02:00
|
|
|
tabs.push(make_tab("Private Messages", '#narrow/is/private',
|
2018-05-06 21:43:17 +02:00
|
|
|
undefined, 'private_message '));
|
2013-05-09 21:12:53 +02:00
|
|
|
|
2013-06-13 20:29:58 +02:00
|
|
|
if (filter.has_operator("pm-with")) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const emails = filter.operands("pm-with")[0].split(',');
|
js: Convert _.map(a, …) to a.map(…).
And convert the corresponding function expressions to arrow style
while we’re here.
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 K from "ast-types/gen/kinds";
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);
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;
recast.visit(ast, {
visitCallExpression(path) {
const { callee, arguments: args } = path.node;
if (
n.MemberExpression.check(callee) &&
!callee.computed &&
n.Identifier.check(callee.object) &&
callee.object.name === "_" &&
n.Identifier.check(callee.property) &&
callee.property.name === "map" &&
args.length === 2 &&
checkExpression(args[0]) &&
checkExpression(args[1])
) {
const [arr, fn] = args;
path.replace(
b.callExpression(b.memberExpression(arr, b.identifier("map")), [
n.FunctionExpression.check(fn) ||
n.ArrowFunctionExpression.check(fn)
? b.arrowFunctionExpression(
fn.params,
n.BlockStatement.check(fn.body) &&
fn.body.body.length === 1 &&
n.ReturnStatement.check(fn.body.body[0])
? fn.body.body[0].argument || b.identifier("undefined")
: fn.body
)
: fn,
])
);
changed = true;
}
this.traverse(path);
},
});
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-08 02:43:49 +01:00
|
|
|
const names = emails.map(email => {
|
2018-06-04 21:09:11 +02:00
|
|
|
if (!people.get_by_email(email)) {
|
2013-05-23 16:15:58 +02:00
|
|
|
return email;
|
|
|
|
}
|
2014-01-30 22:42:19 +01:00
|
|
|
return people.get_by_email(email).full_name;
|
2013-05-09 21:12:53 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
tabs.push(make_tab(names.join(', '), hashed));
|
|
|
|
}
|
|
|
|
|
2017-06-04 01:51:53 +02:00
|
|
|
} else if (filter.has_operator("group-pm-with")) {
|
|
|
|
|
|
|
|
tabs.push(make_tab("Group Private", '#narrow/group-pm-with',
|
2018-05-06 21:43:17 +02:00
|
|
|
undefined, 'private_message '));
|
2017-06-04 01:51:53 +02:00
|
|
|
|
|
|
|
|
2013-06-13 20:29:58 +02:00
|
|
|
} else if (filter.has_operand("is", "starred")) {
|
2013-05-09 21:12:53 +02:00
|
|
|
tabs.push(make_tab("Starred", hashed));
|
2019-08-13 20:20:36 +02:00
|
|
|
} else if (filter.has_operand("streams", "public")) {
|
|
|
|
tabs.push(make_tab("Public Streams", hashed));
|
2013-07-31 20:33:38 +02:00
|
|
|
} else if (filter.has_operator("near")) {
|
|
|
|
tabs.push(make_tab("Near " + filter.operands("near")[0], hashed));
|
2013-07-31 20:54:51 +02:00
|
|
|
} else if (filter.has_operator("id")) {
|
|
|
|
tabs.push(make_tab("ID " + filter.operands("id")[0], hashed));
|
2013-07-10 03:07:01 +02:00
|
|
|
} else if (filter.has_operand("is", "mentioned")) {
|
2013-05-29 00:33:03 +02:00
|
|
|
tabs.push(make_tab("Mentions", hashed));
|
2013-06-13 20:29:58 +02:00
|
|
|
} else if (filter.has_operator("sender")) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let sender = filter.operands("sender")[0];
|
2014-01-30 22:42:19 +01:00
|
|
|
if (people.get_by_email(sender)) {
|
|
|
|
sender = people.get_by_email(sender).full_name;
|
2013-05-30 21:49:26 +02:00
|
|
|
}
|
2013-05-09 21:12:53 +02:00
|
|
|
tabs.push(make_tab("Sent by " + sender, hashed));
|
2013-06-13 20:29:58 +02:00
|
|
|
} else if (filter.has_operator("search")) {
|
2013-05-09 21:12:53 +02:00
|
|
|
// Search is not a clickable link, since we don't have
|
|
|
|
// a search narrow
|
2013-07-04 22:55:26 +02:00
|
|
|
tabs.push(make_tab("Search results", false));
|
2013-05-09 21:12:53 +02:00
|
|
|
}
|
|
|
|
|
2018-11-13 16:27:16 +01:00
|
|
|
// Third breadcrumb item for stream-topic naarrows
|
2013-06-13 20:29:58 +02:00
|
|
|
if (filter.has_operator("stream") &&
|
2013-07-16 22:52:02 +02:00
|
|
|
filter.has_operator("topic")) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const topic = filter.operands("topic")[0];
|
2018-08-04 16:33:28 +02:00
|
|
|
hashed = hash_util.operators_to_hash(ops.slice(0, 2));
|
2013-05-09 21:12:53 +02:00
|
|
|
|
2018-11-13 16:41:18 +01:00
|
|
|
tabs.push(make_tab(topic, hashed, null));
|
2013-05-09 21:12:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-12 22:13:58 +02:00
|
|
|
if (tabs.length === 0) {
|
2017-08-08 07:37:39 +02:00
|
|
|
tabs.push(make_tab('All messages', "#", "home", "root", true));
|
2017-08-12 22:13:58 +02:00
|
|
|
}
|
|
|
|
|
2013-05-09 21:12:53 +02:00
|
|
|
// Last tab is not a link
|
2014-01-08 16:12:22 +01:00
|
|
|
tabs[tabs.length - 1].hash = null;
|
2017-08-12 22:13:58 +02:00
|
|
|
|
2013-05-09 21:12:53 +02:00
|
|
|
return tabs;
|
|
|
|
}
|
|
|
|
|
2013-07-08 23:33:47 +02:00
|
|
|
exports.colorize_tab_bar = function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_tab = $('#tab_list .stream');
|
2013-05-09 21:12:53 +02:00
|
|
|
if (stream_tab.length > 0) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let stream_name = stream_tab.data('name');
|
2013-05-22 21:18:31 +02:00
|
|
|
if (stream_name === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2013-06-06 16:55:43 +02:00
|
|
|
stream_name = stream_name.toString();
|
2013-05-22 21:18:31 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const color_for_stream = stream_data.get_color(stream_name);
|
|
|
|
const stream_dark = stream_color.get_color_class(color_for_stream);
|
|
|
|
const stream_light = colorspace.getHexColor(
|
2018-05-06 21:43:17 +02:00
|
|
|
colorspace.getLighterColor(
|
|
|
|
colorspace.getDecimalColor(color_for_stream), 0.2));
|
2014-01-08 16:12:22 +01:00
|
|
|
|
|
|
|
if (stream_tab.hasClass("stream")) {
|
2017-08-12 22:51:06 +02:00
|
|
|
stream_tab.css('background-color', color_for_stream);
|
2014-01-08 16:12:22 +01:00
|
|
|
if (stream_tab.hasClass("inactive")) {
|
2018-05-06 21:43:17 +02:00
|
|
|
stream_tab.hover (
|
|
|
|
function () {
|
|
|
|
$(this).css('background-color', stream_light);
|
|
|
|
}, function () {
|
|
|
|
$(this).css('background-color', color_for_stream);
|
|
|
|
}
|
|
|
|
);
|
2014-01-08 16:12:22 +01:00
|
|
|
}
|
2014-02-07 22:01:10 +01:00
|
|
|
stream_tab.removeClass(stream_color.color_classes);
|
2014-01-08 16:12:22 +01:00
|
|
|
stream_tab.addClass(stream_dark);
|
2013-05-09 21:12:53 +02:00
|
|
|
}
|
|
|
|
}
|
2013-07-08 23:33:47 +02:00
|
|
|
};
|
2013-05-09 21:12:53 +02:00
|
|
|
|
|
|
|
function build_tab_bar() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const tabs = make_tab_data();
|
2013-05-09 21:12:53 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const tab_bar = $("#tab_bar");
|
2013-05-09 21:12:53 +02:00
|
|
|
tab_bar.empty();
|
|
|
|
|
|
|
|
tabs[tabs.length - 1].active = "active";
|
2019-11-02 00:06:25 +01:00
|
|
|
const rendered = render_tab_bar({tabs: tabs});
|
2013-05-09 21:12:53 +02:00
|
|
|
|
|
|
|
tab_bar.append(rendered);
|
2013-07-08 23:33:47 +02:00
|
|
|
exports.colorize_tab_bar();
|
2013-05-09 21:12:53 +02:00
|
|
|
tab_bar.removeClass('notdisplayed');
|
|
|
|
}
|
|
|
|
|
2018-05-15 22:03:14 +02:00
|
|
|
exports.initialize = function () {
|
2013-05-09 21:12:53 +02:00
|
|
|
build_tab_bar();
|
2018-05-15 22:03:14 +02:00
|
|
|
};
|
2013-05-09 21:12:53 +02:00
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.tab_bar = exports;
|