2012-10-29 21:02:46 +01:00
|
|
|
var reload = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
var reload_in_progress = false;
|
|
|
|
var reload_pending = false;
|
|
|
|
|
|
|
|
exports.is_pending = function () {
|
|
|
|
return reload_pending;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.is_in_progress = function () {
|
|
|
|
return reload_in_progress;
|
|
|
|
};
|
|
|
|
|
|
|
|
function preserve_compose(send_after_reload) {
|
|
|
|
if (send_after_reload === undefined) {
|
|
|
|
send_after_reload = 0;
|
|
|
|
}
|
|
|
|
var url = "#reload:send_after_reload=" + Number(send_after_reload);
|
2012-11-14 19:24:01 +01:00
|
|
|
url += "+csrf_token=" + encodeURIComponent(csrf_token);
|
2012-10-29 21:02:46 +01:00
|
|
|
if (compose.composing() === 'stream') {
|
|
|
|
url += "+msg_type=stream";
|
|
|
|
url += "+stream=" + encodeURIComponent(compose.stream_name());
|
|
|
|
url += "+subject=" + encodeURIComponent(compose.subject());
|
|
|
|
} else {
|
2012-11-08 00:38:21 +01:00
|
|
|
url += "+msg_type=private";
|
2012-10-29 21:02:46 +01:00
|
|
|
url += "+recipient=" + encodeURIComponent(compose.recipient());
|
|
|
|
}
|
|
|
|
url += "+msg="+ encodeURIComponent(compose.message_content());
|
|
|
|
|
|
|
|
window.location.replace(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we're doing a compose-preserving reload. This must be
|
|
|
|
// done before the first call to get_updates
|
|
|
|
$(function () {
|
|
|
|
var location = window.location.toString();
|
|
|
|
var fragment = location.substring(location.indexOf('#') + 1);
|
|
|
|
if (fragment.search("reload:") !== 0) {
|
|
|
|
return;
|
|
|
|
}
|
2012-12-18 00:31:21 +01:00
|
|
|
window.location.hash = '';
|
|
|
|
util.reset_favicon();
|
2012-10-29 21:02:46 +01:00
|
|
|
|
|
|
|
fragment = fragment.replace(/^reload:/, "");
|
|
|
|
var keyvals = fragment.split("+");
|
|
|
|
var vars = {};
|
|
|
|
$.each(keyvals, function (idx, str) {
|
|
|
|
var pair = str.split("=");
|
|
|
|
vars[pair[0]] = decodeURIComponent(pair[1]);
|
|
|
|
});
|
|
|
|
|
2012-11-14 19:24:01 +01:00
|
|
|
// Prevent random people on the Internet from constructing links
|
|
|
|
// that make you send a message.
|
|
|
|
if (vars.csrf_token !== csrf_token) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-29 21:02:46 +01:00
|
|
|
var send_now = parseInt(vars.send_after_reload, 10);
|
|
|
|
|
|
|
|
// TODO: preserve focus
|
|
|
|
compose.start(vars.msg_type, {stream: vars.stream,
|
2012-11-16 20:58:05 +01:00
|
|
|
subject: vars.subject,
|
|
|
|
private_message_recipient: vars.recipient,
|
2013-04-17 19:43:02 +02:00
|
|
|
content: vars.msg});
|
2012-10-29 21:02:46 +01:00
|
|
|
if (send_now) {
|
|
|
|
compose.finish();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function do_reload_app(send_after_reload) {
|
|
|
|
// TODO: we should completely disable the UI here
|
|
|
|
if (compose.composing()) {
|
|
|
|
preserve_compose(send_after_reload);
|
|
|
|
}
|
|
|
|
// TODO: We need a better API for showing messages.
|
2012-11-16 16:45:39 +01:00
|
|
|
ui.report_message("The application has been updated; reloading!", $("#reloading-application"));
|
2012-10-29 21:02:46 +01:00
|
|
|
reload_in_progress = true;
|
|
|
|
window.location.reload(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.initiate = function (options) {
|
|
|
|
var defaults = {immediate: false,
|
|
|
|
send_after_reload: false};
|
|
|
|
options = $.extend(defaults, options);
|
|
|
|
|
|
|
|
if (options.immediate) {
|
|
|
|
do_reload_app(options.send_after_reload);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reload_pending) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
reload_pending = true;
|
|
|
|
|
|
|
|
// If the user is composing a message, reload if they become idle
|
|
|
|
// while composing. If they finish or cancel the compose, wait
|
|
|
|
// until they're idle again
|
|
|
|
var idle_control;
|
2012-11-30 18:40:31 +01:00
|
|
|
var unconditional_timeout = 1000*60*30 + util.random_int(0, 1000*60*5);
|
|
|
|
var composing_timeout = 1000*60*5 + util.random_int(0, 1000*60);
|
|
|
|
var home_timeout = 1000*60 + util.random_int(0, 1000*60);
|
2012-10-29 21:02:46 +01:00
|
|
|
var compose_done_handler, compose_started_handler;
|
|
|
|
|
2012-11-30 17:35:19 +01:00
|
|
|
// Make sure we always do a reload eventually
|
|
|
|
setTimeout(function () { do_reload_app(false); }, unconditional_timeout);
|
|
|
|
|
2012-10-29 21:02:46 +01:00
|
|
|
compose_done_handler = function () {
|
|
|
|
idle_control.cancel();
|
|
|
|
idle_control = $(document).idle({'idle': home_timeout,
|
|
|
|
'onIdle': do_reload_app});
|
|
|
|
$(document).off('compose_canceled.zephyr compose_finished.zephyr',
|
|
|
|
compose_done_handler);
|
|
|
|
$(document).on('compose_started.zephyr', compose_started_handler);
|
|
|
|
};
|
|
|
|
compose_started_handler = function () {
|
|
|
|
idle_control.cancel();
|
|
|
|
idle_control = $(document).idle({'idle': composing_timeout,
|
|
|
|
'onIdle': do_reload_app});
|
|
|
|
$(document).off('compose_started.zephyr', compose_started_handler);
|
|
|
|
$(document).on('compose_canceled.zephyr compose_finished.zephyr',
|
|
|
|
compose_done_handler);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (compose.composing()) {
|
|
|
|
idle_control = $(document).idle({'idle': composing_timeout,
|
|
|
|
'onIdle': do_reload_app});
|
|
|
|
$(document).on('compose_canceled.zephyr compose_finished.zephyr',
|
|
|
|
compose_done_handler);
|
|
|
|
} else {
|
|
|
|
idle_control = $(document).idle({'idle': home_timeout,
|
|
|
|
'onIdle': do_reload_app});
|
|
|
|
$(document).on('compose_started.zephyr', compose_started_handler);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return exports;
|
2012-11-01 19:38:01 +01:00
|
|
|
}());
|