2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2021-02-28 00:37:57 +01:00
|
|
|
const drafts = require("./drafts");
|
2021-02-10 17:00:58 +01:00
|
|
|
const invite = require("./invite");
|
|
|
|
|
2017-11-16 19:51:44 +01:00
|
|
|
// Read https://zulip.readthedocs.io/en/latest/subsystems/hashchange-system.html
|
2018-12-04 23:01:41 +01:00
|
|
|
// or locally: docs/subsystems/hashchange-system.md
|
2019-11-02 00:06:25 +01:00
|
|
|
let changing_hash = false;
|
2012-12-07 20:52:39 +01:00
|
|
|
|
2018-12-06 22:49:26 +01:00
|
|
|
function get_full_url(hash) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const location = window.location;
|
2014-02-13 05:38:02 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (hash === "" || hash.charAt(0) !== "#") {
|
|
|
|
hash = "#" + hash;
|
2018-12-06 22:49:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// IE returns pathname as undefined and missing the leading /
|
2019-11-02 00:06:25 +01:00
|
|
|
let pathname = location.pathname;
|
2018-12-06 22:49:26 +01:00
|
|
|
if (pathname === undefined) {
|
2020-07-15 01:29:15 +02:00
|
|
|
pathname = "/";
|
|
|
|
} else if (pathname === "" || pathname.charAt(0) !== "/") {
|
|
|
|
pathname = "/" + pathname;
|
2018-12-06 22:49:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build a full URL to not have same origin problems
|
2020-07-16 23:29:01 +02:00
|
|
|
const url = location.protocol + "//" + location.host + pathname + hash;
|
2018-12-06 22:49:26 +01:00
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
function set_hash(hash) {
|
2014-01-29 22:14:00 +01:00
|
|
|
if (history.pushState) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const url = get_full_url(hash);
|
2014-01-29 22:14:00 +01:00
|
|
|
history.pushState(null, null, url);
|
|
|
|
} else {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.warn("browser does not support pushState");
|
2018-12-06 22:49:26 +01:00
|
|
|
window.location.hash = hash;
|
2014-01-29 22:14:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-05 12:19:09 +02:00
|
|
|
function maybe_hide_recent_topics() {
|
|
|
|
if (recent_topics.is_visible()) {
|
|
|
|
recent_topics.hide();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-17 13:32:58 +01:00
|
|
|
exports.in_recent_topics_hash = function () {
|
|
|
|
return ["recent_topics", "#", ""].includes(window.location.hash);
|
|
|
|
};
|
|
|
|
|
2012-12-07 20:52:39 +01:00
|
|
|
exports.changehash = function (newhash) {
|
2013-04-03 20:44:26 +02:00
|
|
|
if (changing_hash) {
|
|
|
|
return;
|
|
|
|
}
|
2020-07-05 12:19:09 +02:00
|
|
|
maybe_hide_recent_topics();
|
2018-12-04 19:08:26 +01:00
|
|
|
message_viewport.stop_auto_scrolling();
|
2014-01-29 22:14:00 +01:00
|
|
|
set_hash(newhash);
|
2012-12-07 20:52:39 +01:00
|
|
|
};
|
|
|
|
|
2012-12-12 19:00:50 +01:00
|
|
|
exports.save_narrow = function (operators) {
|
2013-04-03 20:44:26 +02:00
|
|
|
if (changing_hash) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const new_hash = hash_util.operators_to_hash(operators);
|
2013-05-09 21:12:53 +02:00
|
|
|
exports.changehash(new_hash);
|
2012-12-12 19:00:50 +01:00
|
|
|
};
|
|
|
|
|
2013-05-02 17:38:29 +02:00
|
|
|
function activate_home_tab() {
|
2020-07-05 12:19:09 +02:00
|
|
|
const coming_from_recent_topics = maybe_hide_recent_topics();
|
2020-07-04 16:25:41 +02:00
|
|
|
ui_util.change_tab_to("#message_feed_container");
|
2020-07-05 12:19:09 +02:00
|
|
|
narrow.deactivate(coming_from_recent_topics);
|
2020-09-20 10:14:24 +02:00
|
|
|
top_left_corner.handle_narrow_deactivated();
|
2014-03-13 21:14:33 +01:00
|
|
|
floating_recipient_bar.update();
|
2020-07-03 10:38:24 +02:00
|
|
|
search.update_button_visibility();
|
|
|
|
// We need to maybe scroll to the selected message
|
|
|
|
// once we have the proper viewport set up
|
|
|
|
setTimeout(navigate.maybe_scroll_to_selected, 0);
|
2012-12-12 19:00:50 +01:00
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const state = {
|
2018-12-04 23:06:30 +01:00
|
|
|
is_internal_change: false,
|
|
|
|
hash_before_overlay: null,
|
|
|
|
old_hash: typeof window !== "undefined" ? window.location.hash : "#",
|
|
|
|
};
|
|
|
|
|
|
|
|
function is_overlay_hash(hash) {
|
|
|
|
// Hash changes within this list are overlays and should not unnarrow (etc.)
|
2020-07-15 00:34:28 +02:00
|
|
|
const overlay_list = [
|
|
|
|
"streams",
|
|
|
|
"drafts",
|
|
|
|
"settings",
|
|
|
|
"organization",
|
|
|
|
"invite",
|
2020-07-02 12:07:30 +02:00
|
|
|
"keyboard-shortcuts",
|
|
|
|
"message-formatting",
|
|
|
|
"search-operators",
|
2020-07-15 00:34:28 +02:00
|
|
|
];
|
2019-11-02 00:06:25 +01:00
|
|
|
const main_hash = hash_util.get_hash_category(hash);
|
2018-12-04 23:06:30 +01:00
|
|
|
|
js: Convert a.indexOf(…) !== -1 to a.includes(…).
Babel polyfills this for us for Internet Explorer.
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, {
visitBinaryExpression(path) {
const { operator, left, right } = path.node;
if (
n.CallExpression.check(left) &&
n.MemberExpression.check(left.callee) &&
!left.callee.computed &&
n.Identifier.check(left.callee.property) &&
left.callee.property.name === "indexOf" &&
left.arguments.length === 1 &&
checkExpression(left.arguments[0]) &&
((["===", "!==", "==", "!=", ">", "<="].includes(operator) &&
n.UnaryExpression.check(right) &&
right.operator == "-" &&
n.Literal.check(right.argument) &&
right.argument.value === 1) ||
([">=", "<"].includes(operator) &&
n.Literal.check(right) &&
right.value === 0))
) {
const test = b.callExpression(
b.memberExpression(left.callee.object, b.identifier("includes")),
[left.arguments[0]]
);
path.replace(
["!==", "!=", ">", ">="].includes(operator)
? test
: b.unaryExpression("!", test)
);
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 04:55:06 +01:00
|
|
|
return overlay_list.includes(main_hash);
|
2018-12-04 23:06:30 +01:00
|
|
|
}
|
|
|
|
|
2012-12-21 01:04:27 +01:00
|
|
|
// Returns true if this function performed a narrow
|
2018-12-04 19:00:01 +01:00
|
|
|
function do_hashchange_normal(from_reload) {
|
2018-12-04 19:08:26 +01:00
|
|
|
message_viewport.stop_auto_scrolling();
|
2013-04-23 21:59:49 +02:00
|
|
|
|
2013-03-20 00:22:51 +01:00
|
|
|
// NB: In Firefox, window.location.hash is URI-decoded.
|
|
|
|
// Even if the URL bar says #%41%42%43%44, the value here will
|
|
|
|
// be #ABCD.
|
2019-11-02 00:06:25 +01:00
|
|
|
const hash = window.location.hash.split("/");
|
2012-12-19 21:19:29 +01:00
|
|
|
switch (hash[0]) {
|
2020-07-15 02:14:03 +02:00
|
|
|
case "#narrow": {
|
2020-07-05 12:19:09 +02:00
|
|
|
maybe_hide_recent_topics();
|
2020-07-04 16:25:41 +02:00
|
|
|
ui_util.change_tab_to("#message_feed_container");
|
2020-07-15 02:14:03 +02:00
|
|
|
const operators = hash_util.parse_narrow(hash);
|
|
|
|
if (operators === undefined) {
|
2020-07-15 00:34:28 +02:00
|
|
|
// If the narrow URL didn't parse, clear
|
|
|
|
// window.location.hash and send them to the home tab
|
2020-09-20 10:14:24 +02:00
|
|
|
set_hash("#all_messages");
|
2020-07-15 02:14:03 +02:00
|
|
|
activate_home_tab();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const narrow_opts = {
|
2020-07-16 23:29:01 +02:00
|
|
|
change_hash: false, // already set
|
2020-07-15 02:14:03 +02:00
|
|
|
trigger: "hash change",
|
|
|
|
};
|
|
|
|
if (from_reload) {
|
|
|
|
blueslip.debug("We are narrowing as part of a reload.");
|
|
|
|
if (page_params.initial_narrow_pointer !== undefined) {
|
|
|
|
home_msg_list.pre_narrow_offset = page_params.initial_offset;
|
|
|
|
narrow_opts.then_select_id = page_params.initial_narrow_pointer;
|
|
|
|
narrow_opts.then_select_offset = page_params.initial_narrow_offset;
|
|
|
|
}
|
2017-06-15 18:30:40 +02:00
|
|
|
}
|
2020-07-15 02:14:03 +02:00
|
|
|
narrow.activate(operators, narrow_opts);
|
|
|
|
floating_recipient_bar.update();
|
|
|
|
return true;
|
2014-02-12 20:03:05 +01:00
|
|
|
}
|
2020-07-15 02:14:03 +02:00
|
|
|
case "":
|
|
|
|
case "#":
|
2020-07-05 12:19:09 +02:00
|
|
|
case "#recent_topics":
|
|
|
|
recent_topics.show();
|
|
|
|
break;
|
2020-09-20 10:14:24 +02:00
|
|
|
case "#all_messages":
|
|
|
|
activate_home_tab();
|
|
|
|
break;
|
2020-07-15 02:14:03 +02:00
|
|
|
case "#keyboard-shortcuts":
|
|
|
|
case "#message-formatting":
|
|
|
|
case "#search-operators":
|
|
|
|
case "#drafts":
|
|
|
|
case "#invite":
|
|
|
|
case "#streams":
|
|
|
|
case "#organization":
|
|
|
|
case "#settings":
|
|
|
|
blueslip.error("overlay logic skipped for: " + hash);
|
|
|
|
break;
|
2012-12-07 20:52:39 +01:00
|
|
|
}
|
2012-12-21 01:04:27 +01:00
|
|
|
return false;
|
2012-12-07 20:52:39 +01:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:00:01 +01:00
|
|
|
function do_hashchange_overlay(old_hash) {
|
2020-11-04 09:10:18 +01:00
|
|
|
if (old_hash === undefined) {
|
|
|
|
// User directly requested to open an overlay.
|
|
|
|
// We need to show recent topics in the background.
|
|
|
|
recent_topics.show();
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const base = hash_util.get_hash_category(window.location.hash);
|
|
|
|
const old_base = hash_util.get_hash_category(old_hash);
|
2020-06-25 00:51:20 +02:00
|
|
|
const section = hash_util.get_hash_section(window.location.hash);
|
2018-12-01 01:08:48 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const coming_from_overlay = is_overlay_hash(old_hash || "#");
|
2018-12-01 01:08:48 +01:00
|
|
|
|
2018-12-01 18:54:41 +01:00
|
|
|
// Start by handling the specific case of going
|
|
|
|
// from something like streams/all to streams_subscribed.
|
|
|
|
//
|
|
|
|
// In most situations we skip by this logic and load
|
|
|
|
// the new overlay.
|
2020-12-22 11:26:39 +01:00
|
|
|
if (coming_from_overlay && base === old_base) {
|
|
|
|
if (base === "streams") {
|
|
|
|
subs.change_state(section);
|
|
|
|
return;
|
|
|
|
}
|
2018-12-06 22:28:52 +01:00
|
|
|
|
2020-12-22 11:26:39 +01:00
|
|
|
if (base === "settings") {
|
|
|
|
if (!section) {
|
|
|
|
// We may be on a really old browser or somebody
|
|
|
|
// hand-typed a hash.
|
|
|
|
blueslip.warn("missing section for settings");
|
2018-12-06 22:28:52 +01:00
|
|
|
}
|
2020-12-22 11:26:39 +01:00
|
|
|
settings_panel_menu.normal_settings.activate_section_or_default(section);
|
|
|
|
return;
|
|
|
|
}
|
2018-12-06 22:28:52 +01:00
|
|
|
|
2020-12-22 11:26:39 +01:00
|
|
|
if (base === "organization") {
|
|
|
|
if (!section) {
|
|
|
|
// We may be on a really old browser or somebody
|
|
|
|
// hand-typed a hash.
|
|
|
|
blueslip.warn("missing section for organization");
|
2018-12-01 18:54:41 +01:00
|
|
|
}
|
2020-12-22 11:26:39 +01:00
|
|
|
settings_panel_menu.org_settings.activate_section_or_default(section);
|
2018-12-01 18:54:41 +01:00
|
|
|
return;
|
2018-12-01 01:08:48 +01:00
|
|
|
}
|
2020-12-22 11:26:39 +01:00
|
|
|
|
|
|
|
// TODO: handle other cases like internal settings
|
|
|
|
// changes.
|
|
|
|
return;
|
2018-12-01 18:54:41 +01:00
|
|
|
}
|
2018-12-01 01:08:48 +01:00
|
|
|
|
2018-12-01 18:54:41 +01:00
|
|
|
// It's not super likely that an overlay is already open,
|
|
|
|
// but you can jump from /settings to /streams by using
|
|
|
|
// the browser's history menu or hand-editing the URL or
|
|
|
|
// whatever. If so, just close the overlays.
|
2018-12-03 18:11:14 +01:00
|
|
|
if (base !== old_base) {
|
2018-12-01 18:54:41 +01:00
|
|
|
overlays.close_for_hash_change();
|
|
|
|
}
|
2018-12-01 01:08:48 +01:00
|
|
|
|
2018-12-01 18:54:41 +01:00
|
|
|
// NORMAL FLOW: basically, launch the overlay:
|
|
|
|
|
|
|
|
if (!coming_from_overlay) {
|
|
|
|
state.hash_before_overlay = old_hash;
|
2018-12-01 01:08:48 +01:00
|
|
|
}
|
2018-12-01 18:54:41 +01:00
|
|
|
|
|
|
|
if (base === "streams") {
|
2018-12-05 20:41:20 +01:00
|
|
|
subs.launch(section);
|
2018-12-06 20:03:18 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (base === "drafts") {
|
2018-12-01 18:54:41 +01:00
|
|
|
drafts.launch();
|
2018-12-06 20:03:18 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (base === "settings") {
|
2018-12-06 20:18:56 +01:00
|
|
|
settings.launch(section);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (base === "organization") {
|
2018-12-06 20:18:56 +01:00
|
|
|
admin.launch(section);
|
2018-12-06 20:03:18 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (base === "invite") {
|
2018-12-01 18:54:41 +01:00
|
|
|
invite.launch();
|
2018-12-06 20:03:18 +01:00
|
|
|
return;
|
2018-12-01 18:54:41 +01:00
|
|
|
}
|
2020-04-08 13:59:56 +02:00
|
|
|
|
2020-07-02 12:07:30 +02:00
|
|
|
if (base === "keyboard-shortcuts") {
|
|
|
|
info_overlay.show("keyboard-shortcuts");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (base === "message-formatting") {
|
|
|
|
info_overlay.show("message-formatting");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (base === "search-operators") {
|
|
|
|
info_overlay.show("search-operators");
|
|
|
|
return;
|
|
|
|
}
|
2018-12-01 01:08:48 +01:00
|
|
|
}
|
|
|
|
|
2018-12-04 23:06:30 +01:00
|
|
|
function hashchanged(from_reload, e) {
|
2020-02-27 04:46:33 +01:00
|
|
|
const old_hash = e && (e.oldURL ? new URL(e.oldURL).hash : state.old_hash);
|
|
|
|
state.old_hash = window.location.hash;
|
|
|
|
|
2018-12-04 23:06:30 +01:00
|
|
|
if (state.is_internal_change) {
|
|
|
|
state.is_internal_change = false;
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2018-12-04 23:06:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_overlay_hash(window.location.hash)) {
|
|
|
|
do_hashchange_overlay(old_hash);
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2018-12-04 23:06:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// We are changing to a "main screen" view.
|
|
|
|
overlays.close_for_hash_change();
|
|
|
|
changing_hash = true;
|
2019-11-02 00:06:25 +01:00
|
|
|
const ret = do_hashchange_normal(from_reload);
|
2018-12-04 23:06:30 +01:00
|
|
|
changing_hash = false;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-12-01 20:15:50 +01:00
|
|
|
exports.update_browser_history = function (new_hash) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const old_hash = window.location.hash;
|
2018-12-01 20:15:50 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (!new_hash.startsWith("#")) {
|
|
|
|
blueslip.error("programming error: prefix hashes with #: " + new_hash);
|
2018-12-03 17:57:48 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-01 20:15:50 +01:00
|
|
|
if (old_hash === new_hash) {
|
|
|
|
// If somebody is calling us with the same hash we already have, it's
|
|
|
|
// probably harmless, and we just ignore it. But it could be a symptom
|
|
|
|
// of disorganized code that's prone to an infinite loop of repeatedly
|
|
|
|
// assigning the same hash.
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.info("ignoring probably-harmless call to update_browser_history: " + new_hash);
|
2018-12-01 20:15:50 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
state.old_hash = old_hash;
|
|
|
|
state.is_internal_change = true;
|
|
|
|
window.location.hash = new_hash;
|
|
|
|
};
|
|
|
|
|
2018-12-06 22:40:43 +01:00
|
|
|
exports.replace_hash = function (hash) {
|
|
|
|
if (!window.history.replaceState) {
|
|
|
|
// We may have strange behavior with the back button.
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.warn("browser does not support replaceState");
|
2018-12-06 22:40:43 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const url = get_full_url(hash);
|
2018-12-06 22:40:43 +01:00
|
|
|
window.history.replaceState(null, null, url);
|
|
|
|
};
|
|
|
|
|
2018-12-02 17:12:47 +01:00
|
|
|
exports.go_to_location = function (hash) {
|
|
|
|
// Call this function when you WANT the hashchanged
|
|
|
|
// function to run.
|
|
|
|
window.location.hash = hash;
|
|
|
|
};
|
|
|
|
|
2012-12-07 20:52:39 +01:00
|
|
|
exports.initialize = function () {
|
2020-07-15 01:29:15 +02:00
|
|
|
$(window).on("hashchange", (e) => {
|
2019-07-09 04:30:51 +02:00
|
|
|
hashchanged(false, e.originalEvent);
|
2016-10-23 07:07:09 +02:00
|
|
|
});
|
2014-02-12 20:03:05 +01:00
|
|
|
hashchanged(true);
|
2012-12-07 20:52:39 +01:00
|
|
|
};
|
|
|
|
|
2017-05-27 18:55:41 +02:00
|
|
|
exports.exit_overlay = function (callback) {
|
2018-12-01 00:34:05 +01:00
|
|
|
if (is_overlay_hash(window.location.hash)) {
|
2017-03-18 21:35:35 +01:00
|
|
|
ui_util.blur_active_element();
|
2019-11-02 00:06:25 +01:00
|
|
|
const new_hash = state.hash_before_overlay || "#";
|
2018-12-03 16:53:02 +01:00
|
|
|
exports.update_browser_history(new_hash);
|
2016-11-07 22:39:25 +01:00
|
|
|
if (typeof callback === "function") {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.hashchange = exports;
|