64 lines
1.9 KiB
JavaScript
64 lines
1.9 KiB
JavaScript
function csrfSafeMethod(method) {
|
|
// these HTTP methods do not require CSRF protection
|
|
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
|
|
}
|
|
|
|
function init_password_toggles() {
|
|
jQuery('input[type="password"]').each(function(counter,value){
|
|
jQuery('<button type="button" class="password_toggle password_hidden icon" title="Toggle password" />').text('Toggle password').insertAfter(value);
|
|
});
|
|
|
|
jQuery('button.password_toggle').on('click',function(event){
|
|
event.preventDefault();
|
|
toggle_password(this);
|
|
});
|
|
}
|
|
|
|
function toggle_password(button) {
|
|
button = jQuery(button);
|
|
let password_field = button.prev('input');
|
|
let show = password_field.attr('type') == 'password';
|
|
|
|
password_field.attr('type',( show ? 'text' : 'password' ));
|
|
button.removeClass('password_hidden password_shown').addClass(( show ? 'password_shown' : 'password_hidden' ))
|
|
}
|
|
|
|
function human_sizes(value) {
|
|
const units = ['B','KB','MB','GB','TB','HB'];
|
|
const unit_value = 1000;
|
|
|
|
let counter = 0;
|
|
while (value / unit_value > 1) {
|
|
value /= unit_value;
|
|
counter++;
|
|
}
|
|
return value + '' + units[counter];
|
|
}
|
|
|
|
function label_required_fields() {
|
|
jQuery('input,textarea,select').filter('[required]:visible').each(function(counter,value){
|
|
let field = jQuery(value);
|
|
jQuery('label[for="' + field.attr('id') + '"]').append('<span class="required">*</span>');
|
|
});
|
|
|
|
/*
|
|
jQuery('select').each(function(counter,value){
|
|
let field = jQuery(value);
|
|
console.log(jQuery('label[for="' + field.attr('id') + '"]'));
|
|
jQuery('label[for="' + field.attr('id') + '"]').append('<span class="required">*</span>');
|
|
});
|
|
*/
|
|
}
|
|
|
|
jQuery(function(){
|
|
jQuery.ajaxSetup({
|
|
beforeSend: function(xhr, settings) {
|
|
if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
|
|
xhr.setRequestHeader("X-CSRFToken", Cookies.get('csrftoken'));
|
|
}
|
|
}
|
|
});
|
|
|
|
init_password_toggles();
|
|
label_required_fields();
|
|
}); |