2012-10-03 22:07:04 +02:00
|
|
|
// Miscellaneous early setup.
|
|
|
|
|
2012-11-14 17:31:29 +01:00
|
|
|
var csrf_token;
|
2012-10-03 22:07:04 +02:00
|
|
|
$(function () {
|
2017-03-09 02:34:45 +01:00
|
|
|
if (util.is_mobile()) {
|
2017-05-11 20:01:56 +02:00
|
|
|
// if the client is mobile, disable websockets for message sending
|
|
|
|
// (it doesn't work on iOS for some reason).
|
2017-03-09 02:34:45 +01:00
|
|
|
page_params.use_websockets = false;
|
2017-05-11 20:01:56 +02:00
|
|
|
// Also disable the tutorial; it's ugly on mobile.
|
|
|
|
page_params.needs_tutorial = false;
|
2017-03-09 02:34:45 +01:00
|
|
|
}
|
|
|
|
|
2017-03-23 07:05:36 +01:00
|
|
|
page_params.page_load_time = new Date().getTime();
|
|
|
|
|
2012-10-03 22:07:04 +02:00
|
|
|
// Display loading indicator. This disappears after the first
|
2014-01-30 20:29:00 +01:00
|
|
|
// get_events completes.
|
2014-01-14 17:42:46 +01:00
|
|
|
if (page_params.have_initial_messages && !page_params.needs_tutorial) {
|
2017-04-05 03:55:28 +02:00
|
|
|
loading.make_indicator($('#page_loading_indicator'), {text: 'Loading...', abs_positioned: true});
|
2013-06-27 22:18:28 +02:00
|
|
|
} else if (!page_params.needs_tutorial) {
|
2014-03-13 16:47:25 +01:00
|
|
|
$('#first_run_message').show();
|
2012-10-03 23:22:51 +02:00
|
|
|
}
|
2012-10-03 22:07:04 +02:00
|
|
|
|
2012-10-26 21:49:47 +02:00
|
|
|
// This requires that we used Django's {% csrf_token %} somewhere on the page.
|
2012-11-14 17:31:29 +01:00
|
|
|
csrf_token = $('input[name="csrfmiddlewaretoken"]').attr('value');
|
2012-10-26 21:49:47 +02:00
|
|
|
|
|
|
|
$.ajaxSetup({
|
|
|
|
beforeSend: function (xhr, settings) {
|
|
|
|
if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
|
|
|
|
// Only send the token to relative URLs i.e. locally.
|
2012-11-14 17:31:29 +01:00
|
|
|
xhr.setRequestHeader("X-CSRFToken", csrf_token);
|
2012-10-03 22:07:04 +02:00
|
|
|
}
|
2017-01-12 00:17:43 +01:00
|
|
|
},
|
2012-10-26 21:49:47 +02:00
|
|
|
});
|
2012-11-21 00:16:09 +01:00
|
|
|
|
|
|
|
// For some reason, jQuery wants this to be attached to an element.
|
2016-08-24 22:11:37 +02:00
|
|
|
$(document).ajaxError(function (event, xhr) {
|
2012-11-21 00:16:09 +01:00
|
|
|
if (xhr.status === 401) {
|
|
|
|
// We got logged out somehow, perhaps from another window or a session timeout.
|
|
|
|
// We could display an error message, but jumping right to the login page seems
|
|
|
|
// smoother and conveys the same information.
|
2013-11-13 21:31:09 +01:00
|
|
|
window.location.replace(page_params.login_page);
|
2012-11-21 00:16:09 +01:00
|
|
|
}
|
|
|
|
});
|
2013-04-04 00:55:36 +02:00
|
|
|
|
2014-03-13 16:06:14 +01:00
|
|
|
if (typeof $ !== 'undefined') {
|
|
|
|
$.fn.expectOne = function () {
|
|
|
|
if (blueslip && this.length !== 1) {
|
|
|
|
blueslip.error("Expected one element in jQuery set, " + this.length + " found");
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
2017-03-01 01:31:33 +01:00
|
|
|
|
|
|
|
$.fn.within = function (sel) {
|
|
|
|
return ($(this).is(sel) || $(this).closest(sel).length);
|
|
|
|
};
|
2014-03-13 16:06:14 +01:00
|
|
|
}
|
|
|
|
|
2012-10-03 22:07:04 +02:00
|
|
|
});
|