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;
|
|
|
|
};
|
|
|
|
|
2013-07-17 18:07:06 +02:00
|
|
|
function preserve_state(send_after_reload) {
|
2012-10-29 21:02:46 +01:00
|
|
|
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);
|
2013-07-17 18:07:06 +02:00
|
|
|
|
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());
|
2013-07-17 18:07:06 +02:00
|
|
|
} else if (compose.composing() === 'private') {
|
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());
|
|
|
|
}
|
2013-07-17 18:07:06 +02:00
|
|
|
|
|
|
|
if (compose.composing()){
|
|
|
|
url += "+msg=" + encodeURIComponent(compose.message_content());
|
|
|
|
}
|
|
|
|
|
2013-07-18 23:36:04 +02:00
|
|
|
var pointer = current_msg_list.selected_id();
|
|
|
|
if (pointer !== -1) {
|
|
|
|
url += "+pointer=" + pointer;
|
|
|
|
}
|
2013-06-18 23:04:39 +02:00
|
|
|
|
|
|
|
var oldhash = window.location.hash;
|
|
|
|
if (oldhash.length !== 0 && oldhash[0] === '#') {
|
|
|
|
oldhash = oldhash.slice(1);
|
|
|
|
}
|
|
|
|
url += "+oldhash=" + encodeURIComponent(oldhash);
|
2012-10-29 21:02:46 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
fragment = fragment.replace(/^reload:/, "");
|
|
|
|
var keyvals = fragment.split("+");
|
|
|
|
var vars = {};
|
2013-07-30 00:35:44 +02:00
|
|
|
_.each(keyvals, function (str) {
|
2012-10-29 21:02:46 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-07-17 18:07:06 +02:00
|
|
|
if (vars.msg !== undefined){
|
|
|
|
var send_now = parseInt(vars.send_after_reload, 10);
|
|
|
|
|
|
|
|
// TODO: preserve focus
|
2013-07-30 19:10:50 +02:00
|
|
|
compose.start(vars.msg_type, {stream: vars.stream || '',
|
|
|
|
subject: vars.subject || '',
|
|
|
|
private_message_recipient: vars.recipient || '',
|
|
|
|
content: vars.msg || ''});
|
2013-07-17 18:07:06 +02:00
|
|
|
if (send_now) {
|
|
|
|
compose.finish();
|
|
|
|
}
|
|
|
|
}
|
2012-10-29 21:02:46 +01:00
|
|
|
|
2013-07-17 18:07:06 +02:00
|
|
|
var pointer = parseInt(vars.pointer, 10);
|
2013-06-18 23:04:39 +02:00
|
|
|
|
2013-07-17 18:07:06 +02:00
|
|
|
if (pointer) {
|
|
|
|
page_params.initial_pointer = pointer;
|
2012-10-29 21:02:46 +01:00
|
|
|
}
|
2013-07-17 18:07:06 +02:00
|
|
|
|
|
|
|
hashchange.changehash(vars.oldhash);
|
2012-10-29 21:02:46 +01:00
|
|
|
});
|
|
|
|
|
2013-07-22 23:27:32 +02:00
|
|
|
function do_reload_app(send_after_reload, save_state, message) {
|
2012-10-29 21:02:46 +01:00
|
|
|
// TODO: we should completely disable the UI here
|
2013-07-22 23:27:32 +02:00
|
|
|
if (save_state) {
|
|
|
|
preserve_state(send_after_reload);
|
|
|
|
}
|
2013-07-17 18:07:06 +02:00
|
|
|
|
2013-06-18 21:03:20 +02:00
|
|
|
if (message === undefined) {
|
|
|
|
message = "Reloading";
|
|
|
|
}
|
2012-10-29 21:02:46 +01:00
|
|
|
// TODO: We need a better API for showing messages.
|
2013-06-18 21:03:20 +02:00
|
|
|
ui.report_message(message, $("#reloading-application"));
|
2012-10-29 21:02:46 +01:00
|
|
|
reload_in_progress = true;
|
|
|
|
window.location.reload(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.initiate = function (options) {
|
2013-07-30 05:11:50 +02:00
|
|
|
options = _.defaults({}, options, {
|
|
|
|
immediate: false,
|
|
|
|
save_state: true,
|
|
|
|
send_after_reload: false
|
|
|
|
});
|
2012-10-29 21:02:46 +01:00
|
|
|
|
|
|
|
if (options.immediate) {
|
2013-07-22 23:27:32 +02:00
|
|
|
do_reload_app(options.send_after_reload, options.save_state, options.message);
|
2012-10-29 21:02:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2013-06-18 21:03:20 +02:00
|
|
|
function reload_from_idle () {
|
2013-07-22 23:27:32 +02:00
|
|
|
do_reload_app(false, options.save_state, options.message);
|
2013-06-18 21:03:20 +02:00
|
|
|
}
|
2012-11-30 17:35:19 +01:00
|
|
|
|
2013-07-22 23:27:32 +02:00
|
|
|
// Make sure we always do a reload eventually
|
|
|
|
setTimeout(reload_from_idle, unconditional_timeout);
|
|
|
|
|
2012-10-29 21:02:46 +01:00
|
|
|
compose_done_handler = function () {
|
|
|
|
idle_control.cancel();
|
|
|
|
idle_control = $(document).idle({'idle': home_timeout,
|
2013-06-18 21:03:20 +02:00
|
|
|
'onIdle': reload_from_idle});
|
2013-07-25 22:48:55 +02:00
|
|
|
$(document).off('compose_canceled.zulip compose_finished.zulip',
|
2012-10-29 21:02:46 +01:00
|
|
|
compose_done_handler);
|
2013-07-25 22:48:55 +02:00
|
|
|
$(document).on('compose_started.zulip', compose_started_handler);
|
2012-10-29 21:02:46 +01:00
|
|
|
};
|
|
|
|
compose_started_handler = function () {
|
|
|
|
idle_control.cancel();
|
|
|
|
idle_control = $(document).idle({'idle': composing_timeout,
|
2013-06-18 21:03:20 +02:00
|
|
|
'onIdle': reload_from_idle});
|
2013-07-25 22:48:55 +02:00
|
|
|
$(document).off('compose_started.zulip', compose_started_handler);
|
|
|
|
$(document).on('compose_canceled.zulip compose_finished.zulip',
|
2012-10-29 21:02:46 +01:00
|
|
|
compose_done_handler);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (compose.composing()) {
|
|
|
|
idle_control = $(document).idle({'idle': composing_timeout,
|
2013-06-18 21:03:20 +02:00
|
|
|
'onIdle': reload_from_idle});
|
2013-07-25 22:48:55 +02:00
|
|
|
$(document).on('compose_canceled.zulip compose_finished.zulip',
|
2012-10-29 21:02:46 +01:00
|
|
|
compose_done_handler);
|
|
|
|
} else {
|
|
|
|
idle_control = $(document).idle({'idle': home_timeout,
|
2013-06-18 21:03:20 +02:00
|
|
|
'onIdle': reload_from_idle});
|
2013-07-25 22:48:55 +02:00
|
|
|
$(document).on('compose_started.zulip', compose_started_handler);
|
2012-10-29 21:02:46 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return exports;
|
2012-11-01 19:38:01 +01:00
|
|
|
}());
|