meancoach_project/static/js/helpers.js
// Buffers tasks to only execute ONCE after delay has ended
window.delay = (function() {
var timer = 0;
return function(callback, ms) {
clearTimeout (timer);
timer = setTimeout(callback, ms);
};
})();
window.set_status_bar = function(message, css_class, fade_out_time) {
fade_out_time = fade_out_time || 3000;
var result = $('#bottom_status_bar').html(message).addClass(css_class).fadeIn().fadeOut(fade_out_time);
};
// Django CSRF ajax helpers
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
var csrftoken = getCookie('csrftoken');
function csrfSafeMethod(method) {
// these HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
}
});
// Selenium test helper
window.onerror=function(msg){
$("body").attr("JSError",msg);
};