js: Convert static/js/narrow_state.js to ES6 module.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2021-02-27 15:47:56 -08:00
committed by Tim Abbott
parent a1e9be884d
commit b830f53eb5
47 changed files with 161 additions and 118 deletions

View File

@@ -1,52 +1,50 @@
"use strict";
const {Filter} = require("./filter");
const people = require("./people");
import {Filter} from "./filter";
import * as people from "./people";
let current_filter;
exports.reset_current_filter = function () {
export function reset_current_filter() {
current_filter = undefined;
};
}
exports.set_current_filter = function (filter) {
export function set_current_filter(filter) {
current_filter = filter;
};
}
exports.active = function () {
export function active() {
return current_filter !== undefined;
};
}
exports.filter = function () {
export function filter() {
// Both, `All messages` and
// `Recent topics` have `current_filter=undefined`
return current_filter;
};
}
exports.operators = function () {
export function operators() {
if (current_filter === undefined) {
return new Filter(page_params.narrow).operators();
}
return current_filter.operators();
};
}
exports.update_email = function (user_id, new_email) {
export function update_email(user_id, new_email) {
if (current_filter !== undefined) {
current_filter.update_email(user_id, new_email);
}
};
}
/* Operators we should send to the server. */
exports.public_operators = function () {
export function public_operators() {
if (current_filter === undefined) {
return undefined;
}
return current_filter.public_operators();
};
}
exports.search_string = function () {
return Filter.unparse(exports.operators());
};
export function search_string() {
return Filter.unparse(operators());
}
// Collect operators which appear only once into an object,
// and discard those which appear more than once.
@@ -73,9 +71,9 @@ function collect_single(operators) {
// This logic is here and not in the 'compose' module because
// it will get more complicated as we add things to the narrow
// operator language.
exports.set_compose_defaults = function () {
export function set_compose_defaults() {
const opts = {};
const single = collect_single(exports.operators());
const single = collect_single(operators());
// Set the stream, topic, and/or PM recipient if they are
// uniquely specified in the narrow view.
@@ -95,9 +93,9 @@ exports.set_compose_defaults = function () {
}
}
return opts;
};
}
exports.stream = function () {
export function stream() {
if (current_filter === undefined) {
return undefined;
}
@@ -110,9 +108,9 @@ exports.stream = function () {
return stream_data.get_name(name);
}
return undefined;
};
}
exports.stream_sub = function () {
export function stream_sub() {
if (current_filter === undefined) {
return undefined;
}
@@ -125,9 +123,9 @@ exports.stream_sub = function () {
const sub = stream_data.get_sub_by_name(name);
return sub;
};
}
exports.topic = function () {
export function topic() {
if (current_filter === undefined) {
return undefined;
}
@@ -136,9 +134,9 @@ exports.topic = function () {
return operands[0];
}
return undefined;
};
}
exports.pm_string = function () {
export function pm_string() {
// If you are narrowed to a PM conversation
// with users 4, 5, and 99, this will return "4,5,99"
@@ -160,9 +158,9 @@ exports.pm_string = function () {
const user_ids_string = people.reply_to_to_user_ids_string(emails_string);
return user_ids_string;
};
}
exports.get_first_unread_info = function () {
export function get_first_unread_info() {
if (current_filter === undefined) {
// we don't yet support the all-messages view
blueslip.error("unexpected call to get_first_unread_info");
@@ -181,7 +179,7 @@ exports.get_first_unread_info = function () {
};
}
const unread_ids = exports._possible_unread_message_ids();
const unread_ids = _possible_unread_message_ids();
if (unread_ids === undefined) {
// _possible_unread_message_ids() only works for certain narrows
@@ -202,9 +200,9 @@ exports.get_first_unread_info = function () {
flavor: "found",
msg_id: unread_ids[0],
};
};
}
exports._possible_unread_message_ids = function () {
export function _possible_unread_message_ids() {
// This function currently only returns valid results for
// certain types of narrows, mostly left sidebar narrows.
// For more complicated narrows we may return undefined.
@@ -221,16 +219,16 @@ exports._possible_unread_message_ids = function () {
let current_filter_pm_string;
if (current_filter.can_bucket_by("stream", "topic")) {
sub = exports.stream_sub();
sub = stream_sub();
if (sub === undefined) {
return [];
}
topic_name = exports.topic();
topic_name = topic();
return unread.get_msg_ids_for_topic(sub.stream_id, topic_name);
}
if (current_filter.can_bucket_by("stream")) {
sub = exports.stream_sub();
sub = stream_sub();
if (sub === undefined) {
return [];
}
@@ -238,7 +236,7 @@ exports._possible_unread_message_ids = function () {
}
if (current_filter.can_bucket_by("pm-with")) {
current_filter_pm_string = exports.pm_string();
current_filter_pm_string = pm_string();
if (current_filter_pm_string === undefined) {
return [];
}
@@ -267,25 +265,25 @@ exports._possible_unread_message_ids = function () {
}
return undefined;
};
}
// Are we narrowed to PMs: all PMs or PMs with particular people.
exports.narrowed_to_pms = function () {
export function narrowed_to_pms() {
if (current_filter === undefined) {
return false;
}
return current_filter.has_operator("pm-with") || current_filter.has_operand("is", "private");
};
}
exports.narrowed_by_pm_reply = function () {
export function narrowed_by_pm_reply() {
if (current_filter === undefined) {
return false;
}
const operators = current_filter.operators();
return operators.length === 1 && current_filter.has_operator("pm-with");
};
}
exports.narrowed_by_topic_reply = function () {
export function narrowed_by_topic_reply() {
if (current_filter === undefined) {
return false;
}
@@ -295,60 +293,58 @@ exports.narrowed_by_topic_reply = function () {
current_filter.operands("stream").length === 1 &&
current_filter.operands("topic").length === 1
);
};
}
// We auto-reply under certain conditions, namely when you're narrowed
// to a PM (or huddle), and when you're narrowed to some stream/topic pair
exports.narrowed_by_reply = function () {
return exports.narrowed_by_pm_reply() || exports.narrowed_by_topic_reply();
};
export function narrowed_by_reply() {
return narrowed_by_pm_reply() || narrowed_by_topic_reply();
}
exports.narrowed_by_stream_reply = function () {
export function narrowed_by_stream_reply() {
if (current_filter === undefined) {
return false;
}
const operators = current_filter.operators();
return operators.length === 1 && current_filter.operands("stream").length === 1;
};
}
exports.narrowed_to_topic = function () {
export function narrowed_to_topic() {
if (current_filter === undefined) {
return false;
}
return current_filter.has_operator("stream") && current_filter.has_operator("topic");
};
}
exports.narrowed_to_search = function () {
export function narrowed_to_search() {
return current_filter !== undefined && current_filter.is_search();
};
}
exports.narrowed_to_starred = function () {
export function narrowed_to_starred() {
if (current_filter === undefined) {
return false;
}
return current_filter.has_operand("is", "starred");
};
}
exports.excludes_muted_topics = function () {
export function excludes_muted_topics() {
return (
!exports.narrowed_to_topic() &&
!exports.narrowed_to_search() &&
!exports.narrowed_to_pms() &&
!exports.narrowed_to_starred()
!narrowed_to_topic() &&
!narrowed_to_search() &&
!narrowed_to_pms() &&
!narrowed_to_starred()
);
};
}
exports.is_for_stream_id = function (stream_id) {
export function is_for_stream_id(stream_id) {
// This is not perfect, since we still track narrows by
// name, not id, but at least the interface is good going
// forward.
const narrow_sub = exports.stream_sub();
const narrow_sub = stream_sub();
if (narrow_sub === undefined) {
return false;
}
return stream_id === narrow_sub.stream_id;
};
window.narrow_state = exports;
}