2012-12-07 20:52:39 +01:00
|
|
|
var hashchange = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
var expected_hash = false;
|
|
|
|
|
|
|
|
exports.changehash = function (newhash) {
|
|
|
|
expected_hash = newhash;
|
2013-01-11 16:57:17 +01:00
|
|
|
// Some browsers reset scrollTop when changing the hash to "",
|
2012-12-19 21:19:29 +01:00
|
|
|
// so we save and restore it.
|
|
|
|
// http://stackoverflow.com/questions/4715073/window-location-hash-prevent-scrolling-to-the-top
|
2013-02-15 22:41:36 +01:00
|
|
|
var scrolltop = viewport.scrollTop();
|
2012-12-07 20:52:39 +01:00
|
|
|
window.location.hash = newhash;
|
2012-12-19 21:19:29 +01:00
|
|
|
util.reset_favicon();
|
2013-02-15 22:41:36 +01:00
|
|
|
if (newhash === "" || newhash === "#") {
|
2012-12-19 21:19:29 +01:00
|
|
|
viewport.scrollTop(scrolltop);
|
|
|
|
}
|
2012-12-07 20:52:39 +01:00
|
|
|
};
|
|
|
|
|
2012-12-12 19:00:50 +01:00
|
|
|
exports.save_narrow = function (operators) {
|
|
|
|
if (operators === undefined) {
|
|
|
|
exports.changehash('#');
|
|
|
|
} else {
|
|
|
|
var new_hash = '#narrow';
|
|
|
|
$.each(operators, function (idx, elem) {
|
|
|
|
new_hash += '/' + encodeURIComponent(elem[0])
|
|
|
|
+ '/' + encodeURIComponent(elem[1]);
|
|
|
|
});
|
|
|
|
exports.changehash(new_hash);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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.
|
|
|
|
var operator = decodeURIComponent(hash[i]);
|
|
|
|
var operand = decodeURIComponent(hash[i+1] || '');
|
|
|
|
operators.push([operator, operand]);
|
|
|
|
}
|
2013-02-20 22:59:56 +01:00
|
|
|
var new_selection;
|
|
|
|
if (current_msg_list.selected_id() !== -1) {
|
|
|
|
new_selection = current_msg_list.selected_id();
|
|
|
|
} else {
|
|
|
|
new_selection = initial_pointer;
|
|
|
|
}
|
|
|
|
narrow.activate(operators, {then_select_id: new_selection});
|
2012-12-12 19:00:50 +01:00
|
|
|
}
|
|
|
|
|
2012-12-21 01:04:27 +01:00
|
|
|
// Returns true if this function performed a narrow
|
2012-12-07 20:52:39 +01:00
|
|
|
function hashchanged() {
|
|
|
|
// 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)
|
|
|
|
if (window.location.hash === expected_hash) {
|
2012-12-21 01:04:27 +01:00
|
|
|
return false;
|
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]) {
|
|
|
|
case "#narrow":
|
|
|
|
ui.change_tab_to("#home");
|
2012-12-12 19:00:50 +01:00
|
|
|
parse_narrow(hash);
|
2012-12-19 21:19:29 +01:00
|
|
|
ui.update_floating_recipient_bar();
|
2012-12-21 01:04:27 +01:00
|
|
|
return true;
|
2012-12-19 21:19:29 +01:00
|
|
|
case "":
|
|
|
|
case "#":
|
|
|
|
ui.change_tab_to("#home");
|
2012-12-12 20:36:05 +01:00
|
|
|
narrow.deactivate();
|
2012-12-19 21:19:29 +01:00
|
|
|
ui.update_floating_recipient_bar();
|
|
|
|
break;
|
|
|
|
case "#subscriptions":
|
|
|
|
ui.change_tab_to("#subscriptions");
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
exports.initialize = function () {
|
|
|
|
window.onhashchange = hashchanged;
|
2012-12-21 01:04:27 +01:00
|
|
|
if (hashchanged()) {
|
2013-02-12 20:01:24 +01:00
|
|
|
load_more_messages(current_msg_list);
|
2012-12-21 01:04:27 +01:00
|
|
|
}
|
2012-12-07 20:52:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|