2012-12-07 20:52:39 +01:00
|
|
|
var hashchange = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2013-04-09 19:17:42 +02:00
|
|
|
var expected_hash;
|
2013-04-03 20:44:26 +02:00
|
|
|
var changing_hash = false;
|
2012-12-07 20:52:39 +01:00
|
|
|
|
2013-03-20 00:22:51 +01:00
|
|
|
// Some browsers zealously URI-decode the contents of
|
|
|
|
// window.location.hash. So we hide our URI-encoding
|
|
|
|
// by replacing % with . (like MediaWiki).
|
|
|
|
|
2013-07-05 17:43:56 +02:00
|
|
|
exports.encodeHashComponent = function (str) {
|
2013-03-20 00:22:51 +01:00
|
|
|
return encodeURIComponent(str)
|
|
|
|
.replace(/\./g, '%2E')
|
|
|
|
.replace(/%/g, '.');
|
2013-05-02 16:45:38 +02:00
|
|
|
};
|
2013-03-20 00:22:51 +01:00
|
|
|
|
|
|
|
function decodeHashComponent(str) {
|
|
|
|
return decodeURIComponent(str.replace(/\./g, '%'));
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2013-07-25 22:48:55 +02:00
|
|
|
$(document).trigger($.Event('hashchange.zulip'));
|
2014-01-29 22:14:00 +01:00
|
|
|
set_hash(newhash);
|
2012-12-19 21:19:29 +01:00
|
|
|
util.reset_favicon();
|
2012-12-07 20:52:39 +01:00
|
|
|
};
|
|
|
|
|
2013-05-09 21:12:53 +02:00
|
|
|
// Encodes an operator list into the
|
|
|
|
// corresponding hash: the # component
|
|
|
|
// of the narrow URL
|
|
|
|
exports.operators_to_hash = function (operators) {
|
|
|
|
var hash = '#';
|
|
|
|
|
|
|
|
if (operators !== undefined) {
|
|
|
|
hash = '#narrow';
|
2013-07-30 00:35:44 +02:00
|
|
|
_.each(operators, function (elem) {
|
2014-02-03 19:48:10 +01:00
|
|
|
// Support legacy tuples.
|
2014-02-10 23:32:44 +01:00
|
|
|
var operator = elem.operator;
|
|
|
|
var operand = elem.operand;
|
|
|
|
|
2014-02-11 21:36:59 +01:00
|
|
|
var sign = elem.negated ? '-' : '';
|
|
|
|
hash += '/' + sign + hashchange.encodeHashComponent(operator)
|
2014-02-03 19:48:10 +01:00
|
|
|
+ '/' + hashchange.encodeHashComponent(operand);
|
2013-05-09 21:12:53 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
}
|
2013-05-09 21:12:53 +02:00
|
|
|
var new_hash = exports.operators_to_hash(operators);
|
|
|
|
exports.changehash(new_hash);
|
2012-12-12 19:00:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
function parse_narrow(hash) {
|
|
|
|
var i, operators = [];
|
|
|
|
for (i=1; i<hash.length; i+=2) {
|
|
|
|
// 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 {
|
|
|
|
var operator = decodeHashComponent(hash[i]);
|
|
|
|
var operand = decodeHashComponent(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) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2012-12-12 19:00:50 +01:00
|
|
|
}
|
2013-05-02 17:38:29 +02:00
|
|
|
return operators;
|
|
|
|
}
|
|
|
|
|
|
|
|
function activate_home_tab() {
|
|
|
|
ui.change_tab_to("#home");
|
|
|
|
narrow.deactivate();
|
|
|
|
ui.update_floating_recipient_bar();
|
2012-12-12 19:00:50 +01:00
|
|
|
}
|
|
|
|
|
2012-12-21 01:04:27 +01:00
|
|
|
// Returns true if this function performed a narrow
|
2014-02-12 20:03:05 +01:00
|
|
|
function do_hashchange(from_reload) {
|
2012-12-07 20:52:39 +01:00
|
|
|
// If window.location.hash changed because our app explicitly
|
|
|
|
// changed it, then we don't need to do anything.
|
|
|
|
// (This function only neds to jump into action if it changed
|
|
|
|
// because e.g. the back button was pressed by the user)
|
2013-04-09 19:17:42 +02:00
|
|
|
//
|
|
|
|
// The second case is for handling the fact that some browsers
|
|
|
|
// automatically convert '#' to '' when you change the hash to '#'.
|
|
|
|
if (window.location.hash === expected_hash ||
|
|
|
|
(expected_hash !== undefined &&
|
|
|
|
window.location.hash.replace(/^#/, '') === '' &&
|
|
|
|
expected_hash.replace(/^#/, '') === '')) {
|
2012-12-21 01:04:27 +01:00
|
|
|
return false;
|
2012-12-07 20:52:39 +01:00
|
|
|
}
|
|
|
|
|
2013-07-25 22:48:55 +02:00
|
|
|
$(document).trigger($.Event('hashchange.zulip'));
|
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":
|
|
|
|
ui.change_tab_to("#home");
|
|
|
|
var operators = parse_narrow(hash);
|
|
|
|
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
|
|
|
select_first_unread: true,
|
|
|
|
change_hash: false, // already set
|
|
|
|
trigger: 'hash change'
|
2014-02-12 20:03:05 +01:00
|
|
|
};
|
|
|
|
if (from_reload !== undefined && page_params.initial_narrow_pointer !== undefined) {
|
|
|
|
narrow_opts.from_reload = true;
|
2014-03-03 23:37:22 +01:00
|
|
|
narrow_opts.first_unread_from_server = true;
|
2014-02-12 20:03:05 +01:00
|
|
|
}
|
|
|
|
narrow.activate(operators, narrow_opts);
|
2014-02-12 20:03:53 +01:00
|
|
|
ui.update_floating_recipient_bar();
|
|
|
|
return true;
|
|
|
|
case "":
|
|
|
|
case "#":
|
|
|
|
activate_home_tab();
|
|
|
|
break;
|
|
|
|
case "#subscriptions":
|
|
|
|
ui.change_tab_to("#subscriptions");
|
|
|
|
break;
|
|
|
|
case "#administration":
|
|
|
|
ui.change_tab_to("#administration");
|
|
|
|
break;
|
|
|
|
case "#settings":
|
|
|
|
ui.change_tab_to("#settings");
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-02-12 20:03:05 +01:00
|
|
|
function hashchanged(from_reload) {
|
2013-04-03 20:44:26 +02:00
|
|
|
changing_hash = true;
|
2014-02-12 20:03:05 +01:00
|
|
|
var ret = do_hashchange(from_reload);
|
2013-04-03 20:44:26 +02:00
|
|
|
changing_hash = false;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
window.onhashchange = blueslip.wrap_function(hashchanged);
|
2014-02-12 20:03:05 +01:00
|
|
|
hashchanged(true);
|
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;
|
|
|
|
}
|