2012-10-03 22:07:04 +02:00
|
|
|
// Miscellaneous early setup.
|
|
|
|
// This is the first of our Javascript files to be included.
|
|
|
|
|
|
|
|
var templates = {};
|
2012-11-14 17:31:29 +01:00
|
|
|
var csrf_token;
|
2012-10-03 22:07:04 +02:00
|
|
|
$(function () {
|
|
|
|
// Display loading indicator. This disappears after the first
|
|
|
|
// get_updates completes.
|
2012-10-03 23:22:51 +02:00
|
|
|
if (have_initial_messages) {
|
2013-01-16 19:50:18 +01:00
|
|
|
util.make_loading_indicator($('#page_loading_indicator'), 'Loading...');
|
2013-02-12 19:28:21 +01:00
|
|
|
} else {
|
2013-02-13 22:15:13 +01:00
|
|
|
try {
|
|
|
|
tutorial.run_when_ready();
|
|
|
|
} catch (e) {
|
|
|
|
util.show_first_run_message();
|
|
|
|
}
|
2012-10-03 23:22:51 +02:00
|
|
|
}
|
2012-10-03 22:07:04 +02:00
|
|
|
|
|
|
|
// Compile Handlebars templates.
|
2012-12-12 20:22:18 +01:00
|
|
|
$.each(['message', 'subscription',
|
2013-02-08 23:03:33 +01:00
|
|
|
'actions_popover_title', 'actions_popover_content',
|
2013-02-09 03:29:40 +01:00
|
|
|
'invite_subscription', 'new_stream_users'],
|
2012-10-26 22:04:01 +02:00
|
|
|
function (index, name) {
|
|
|
|
templates[name] = Handlebars.compile($('#template_'+name).html());
|
|
|
|
}
|
|
|
|
);
|
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
|
|
|
}
|
|
|
|
}
|
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.
|
|
|
|
$('body').ajaxError(function (event, xhr) {
|
|
|
|
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.
|
|
|
|
window.location.replace('/accounts/login');
|
|
|
|
}
|
|
|
|
});
|
2012-10-03 22:07:04 +02:00
|
|
|
});
|