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
|
2012-12-07 20:52:39 +01:00
|
|
|
var hashchange = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2013-04-03 20:44:26 +02:00
|
|
|
var changing_hash = false;
|
2012-12-07 20:52:39 +01:00
|
|
|
|
2014-01-29 22:14:00 +01:00
|
|
|
function set_hash(hash) {
|
2014-02-13 05:38:02 +01:00
|
|
|
var location = window.location;
|
|
|
|
|
2014-01-29 22:14:00 +01:00
|
|
|
if (history.pushState) {
|
2014-02-12 21:52:57 +01:00
|
|
|
if (hash === '' || hash.charAt(0) !== '#') {
|
|
|
|
hash = '#' + hash;
|
|
|
|
}
|
2014-02-13 05:38:02 +01:00
|
|
|
|
|
|
|
// IE returns pathname as undefined and missing the leading /
|
|
|
|
var pathname = location.pathname;
|
|
|
|
if (pathname === undefined) {
|
|
|
|
pathname = '/';
|
|
|
|
} else if (pathname === '' || pathname.charAt(0) !== '/') {
|
|
|
|
pathname = '/' + pathname;
|
|
|
|
}
|
|
|
|
|
2014-01-29 22:14:00 +01:00
|
|
|
// Build a full URL to not have same origin problems
|
2014-02-13 05:38:02 +01:00
|
|
|
var url = location.protocol + '//' + location.host + pathname + hash;
|
2014-01-29 22:14:00 +01:00
|
|
|
history.pushState(null, null, url);
|
|
|
|
} else {
|
2014-02-13 05:38:02 +01:00
|
|
|
location.hash = hash;
|
2014-01-29 22:14:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-07 20:52:39 +01:00
|
|
|
exports.changehash = function (newhash) {
|
2013-04-03 20:44:26 +02:00
|
|
|
if (changing_hash) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-04 19:08:26 +01:00
|
|
|
message_viewport.stop_auto_scrolling();
|
2014-01-29 22:14:00 +01:00
|
|
|
set_hash(newhash);
|
2014-03-13 17:44:43 +01:00
|
|
|
favicon.reset();
|
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;
|
|
|
|
}
|
2018-08-04 16:33:28 +02:00
|
|
|
var 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
|
|
|
};
|
|
|
|
|
2017-01-05 22:04:33 +01:00
|
|
|
exports.parse_narrow = function (hash) {
|
2016-12-02 17:09:31 +01:00
|
|
|
var i;
|
|
|
|
var operators = [];
|
2018-06-04 21:13:07 +02:00
|
|
|
for (i = 1; i < hash.length; i += 2) {
|
2012-12-12 19:00:50 +01:00
|
|
|
// We don't construct URLs with an odd number of components,
|
|
|
|
// but the user might write one.
|
2013-05-02 17:38:29 +02:00
|
|
|
try {
|
2017-03-19 00:43:14 +01:00
|
|
|
var operator = hash_util.decodeHashComponent(hash[i]);
|
2018-05-15 15:59:50 +02:00
|
|
|
// Do not parse further if empty operator encountered.
|
|
|
|
if (operator === '') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-06-04 21:13:07 +02:00
|
|
|
var operand = hash_util.decode_operand(operator, hash[i + 1] || '');
|
2014-02-11 21:36:59 +01:00
|
|
|
var negated = false;
|
|
|
|
if (operator[0] === '-') {
|
|
|
|
negated = true;
|
|
|
|
operator = operator.slice(1);
|
|
|
|
}
|
|
|
|
operators.push({negated: negated, operator: operator, operand: operand});
|
2013-05-02 17:38:29 +02:00
|
|
|
} catch (err) {
|
2018-03-13 13:04:16 +01:00
|
|
|
return;
|
2013-05-02 17:38:29 +02:00
|
|
|
}
|
2012-12-12 19:00:50 +01:00
|
|
|
}
|
2013-05-02 17:38:29 +02:00
|
|
|
return operators;
|
2017-01-05 22:04:33 +01:00
|
|
|
};
|
2013-05-02 17:38:29 +02:00
|
|
|
|
|
|
|
function activate_home_tab() {
|
2017-03-18 21:35:35 +01:00
|
|
|
ui_util.change_tab_to("#home");
|
2013-05-02 17:38:29 +02:00
|
|
|
narrow.deactivate();
|
2014-03-13 21:14:33 +01:00
|
|
|
floating_recipient_bar.update();
|
2012-12-12 19:00:50 +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.
|
2012-12-07 20:52:39 +01:00
|
|
|
var hash = window.location.hash.split("/");
|
2012-12-19 21:19:29 +01:00
|
|
|
switch (hash[0]) {
|
2014-02-12 20:03:53 +01:00
|
|
|
case "#narrow":
|
2017-03-18 21:35:35 +01:00
|
|
|
ui_util.change_tab_to("#home");
|
2017-01-05 22:04:33 +01:00
|
|
|
var operators = exports.parse_narrow(hash);
|
2014-02-12 20:03:53 +01:00
|
|
|
if (operators === undefined) {
|
|
|
|
// If the narrow URL didn't parse, clear
|
|
|
|
// window.location.hash and send them to the home tab
|
2014-01-29 22:14:00 +01:00
|
|
|
set_hash('');
|
2013-05-02 17:38:29 +02:00
|
|
|
activate_home_tab();
|
2014-02-12 20:03:53 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-02-12 20:03:05 +01:00
|
|
|
var narrow_opts = {
|
2014-02-12 20:03:53 +01:00
|
|
|
change_hash: false, // already set
|
2017-01-12 00:17:43 +01:00
|
|
|
trigger: 'hash change',
|
2014-02-12 20:03:05 +01:00
|
|
|
};
|
2017-06-15 18:30:40 +02:00
|
|
|
if (from_reload) {
|
|
|
|
blueslip.debug('We are narrowing as part of a reload.');
|
|
|
|
if (page_params.initial_narrow_pointer !== undefined) {
|
2018-04-23 06:17:56 +02:00
|
|
|
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
|
|
|
}
|
2014-02-12 20:03:05 +01:00
|
|
|
}
|
|
|
|
narrow.activate(operators, narrow_opts);
|
2014-03-13 21:14:33 +01:00
|
|
|
floating_recipient_bar.update();
|
2014-02-12 20:03:53 +01:00
|
|
|
return true;
|
|
|
|
case "":
|
|
|
|
case "#":
|
|
|
|
activate_home_tab();
|
|
|
|
break;
|
2017-06-04 03:39:47 +02:00
|
|
|
case "#keyboard-shortcuts":
|
2018-03-30 14:09:11 +02:00
|
|
|
info_overlay.show("keyboard-shortcuts");
|
2017-06-04 03:39:47 +02:00
|
|
|
break;
|
2018-08-29 19:15:25 +02:00
|
|
|
case "#message-formatting":
|
|
|
|
info_overlay.show("message-formatting");
|
2017-06-04 03:39:47 +02:00
|
|
|
break;
|
|
|
|
case "#search-operators":
|
2018-03-30 14:09:11 +02:00
|
|
|
info_overlay.show("search-operators");
|
2017-06-04 03:39:47 +02:00
|
|
|
break;
|
2017-02-22 02:34:05 +01:00
|
|
|
case "#drafts":
|
2017-03-18 21:35:35 +01:00
|
|
|
ui_util.change_tab_to("#drafts");
|
2017-02-22 02:34:05 +01:00
|
|
|
break;
|
2018-12-01 19:44:05 +01:00
|
|
|
case "#invite":
|
|
|
|
case "#streams":
|
2017-04-07 21:39:58 +02:00
|
|
|
case "#organization":
|
2014-02-12 20:03:53 +01:00
|
|
|
case "#settings":
|
2018-12-01 19:44:05 +01:00
|
|
|
blueslip.error('overlay logic skipped for: ' + hash);
|
2014-02-12 20:03:53 +01:00
|
|
|
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-01 01:55:36 +01:00
|
|
|
var state = {
|
2018-12-01 20:15:50 +01:00
|
|
|
is_internal_change: false,
|
2018-12-01 00:49:01 +01:00
|
|
|
hash_before_overlay: null,
|
2017-02-02 01:51:48 +01:00
|
|
|
old_hash: typeof window !== "undefined" ? window.location.hash : "#",
|
2016-11-07 22:39:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
function get_main_hash(hash) {
|
2016-12-03 01:12:52 +01:00
|
|
|
return hash ? hash.replace(/^#/, "").split(/\//)[0] : "";
|
2016-11-07 22:39:25 +01:00
|
|
|
}
|
|
|
|
|
2017-02-21 18:13:32 +01:00
|
|
|
function get_hash_components() {
|
|
|
|
var hash = window.location.hash.split(/\//);
|
|
|
|
|
|
|
|
return {
|
|
|
|
base: hash.shift(),
|
|
|
|
arguments: hash,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-12-01 00:34:05 +01:00
|
|
|
function is_overlay_hash(hash) {
|
2017-03-23 20:28:03 +01:00
|
|
|
// Hash changes within this list are overlays and should not unnarrow (etc.)
|
2018-12-01 00:34:05 +01:00
|
|
|
var overlay_list = ["streams", "drafts", "settings", "organization", "invite"];
|
2016-11-07 22:39:25 +01:00
|
|
|
var main_hash = get_main_hash(hash);
|
|
|
|
|
2018-12-01 00:34:05 +01:00
|
|
|
return overlay_list.indexOf(main_hash) > -1;
|
2016-11-07 22:39:25 +01:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:00:01 +01:00
|
|
|
function do_hashchange_overlay(old_hash) {
|
2018-12-01 01:08:48 +01:00
|
|
|
var base = get_main_hash(window.location.hash);
|
2018-12-03 18:11:14 +01:00
|
|
|
var old_base = get_main_hash(old_hash);
|
2018-12-01 01:08:48 +01:00
|
|
|
|
2018-12-01 18:54:41 +01:00
|
|
|
var 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.
|
|
|
|
if (coming_from_overlay) {
|
2018-12-03 18:11:14 +01:00
|
|
|
if (base === old_base) {
|
2018-12-01 18:54:41 +01:00
|
|
|
if (base === 'streams') {
|
|
|
|
subs.change_state(get_hash_components());
|
|
|
|
}
|
2018-12-01 01:08:48 +01:00
|
|
|
|
2018-12-01 18:54:41 +01:00
|
|
|
// TODO: handle other cases like internal settings
|
|
|
|
// changes.
|
|
|
|
return;
|
2018-12-01 01:08:48 +01:00
|
|
|
}
|
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") {
|
|
|
|
subs.launch(get_hash_components());
|
|
|
|
} else if (base === "drafts") {
|
|
|
|
drafts.launch();
|
|
|
|
} else if (/settings|organization/.test(base)) {
|
|
|
|
settings.setup_page();
|
|
|
|
admin.setup_page();
|
|
|
|
} else if (base === "invite") {
|
|
|
|
invite.launch();
|
|
|
|
}
|
2018-12-01 01:08:48 +01:00
|
|
|
}
|
|
|
|
|
2018-12-01 20:15:50 +01:00
|
|
|
exports.update_browser_history = function (new_hash) {
|
|
|
|
var old_hash = window.location.hash;
|
|
|
|
|
2018-12-03 17:57:48 +01:00
|
|
|
if (!new_hash.startsWith('#')) {
|
|
|
|
blueslip.error('programming error: prefix hashes with #: ' + new_hash);
|
|
|
|
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.
|
|
|
|
blueslip.info('ignoring probably-harmless call to update_browser_history: ' + new_hash);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
state.old_hash = old_hash;
|
|
|
|
state.is_internal_change = true;
|
|
|
|
window.location.hash = new_hash;
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2016-11-07 22:39:25 +01:00
|
|
|
function hashchanged(from_reload, e) {
|
2018-12-01 20:15:50 +01:00
|
|
|
if (state.is_internal_change) {
|
|
|
|
state.is_internal_change = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-07 22:39:25 +01:00
|
|
|
var old_hash;
|
|
|
|
if (e) {
|
2018-12-01 01:55:36 +01:00
|
|
|
old_hash = "#" + (e.oldURL || state.old_hash).split(/#/).slice(1).join("");
|
|
|
|
state.old_hash = window.location.hash;
|
2016-11-07 22:39:25 +01:00
|
|
|
}
|
|
|
|
|
2018-12-01 00:34:05 +01:00
|
|
|
if (is_overlay_hash(window.location.hash)) {
|
2018-12-04 19:00:01 +01:00
|
|
|
do_hashchange_overlay(old_hash);
|
2018-12-01 01:17:20 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We are changing to a "main screen" view.
|
|
|
|
overlays.close_for_hash_change();
|
|
|
|
changing_hash = true;
|
2018-12-04 19:00:01 +01:00
|
|
|
var ret = do_hashchange_normal(from_reload);
|
2018-12-01 01:17:20 +01:00
|
|
|
changing_hash = false;
|
|
|
|
return ret;
|
2013-04-03 20:44:26 +02:00
|
|
|
}
|
|
|
|
|
2012-12-07 20:52:39 +01:00
|
|
|
exports.initialize = function () {
|
2013-04-03 18:55:28 +02:00
|
|
|
// jQuery doesn't have a hashchange event, so we manually wrap
|
|
|
|
// our event handler
|
2016-11-07 22:39:25 +01:00
|
|
|
window.onhashchange = blueslip.wrap_function(function (e) {
|
|
|
|
hashchanged(false, e);
|
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();
|
2018-12-03 16:53:02 +01:00
|
|
|
var new_hash = state.hash_before_overlay || "#";
|
|
|
|
exports.update_browser_history(new_hash);
|
2016-11-07 22:39:25 +01:00
|
|
|
if (typeof callback === "function") {
|
|
|
|
callback();
|
|
|
|
}
|
2018-12-03 16:53:02 +01:00
|
|
|
|
2016-11-07 22:39:25 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-12-07 20:52:39 +01:00
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|
2013-11-26 16:39:58 +01:00
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = hashchange;
|
|
|
|
}
|
2018-05-28 08:04:36 +02:00
|
|
|
window.hashchange = hashchange;
|