Buckler/static/buckler.js
2020-11-20 21:13:35 -05:00

56 lines
1.6 KiB
JavaScript

function load() {
let headers = document.querySelectorAll('h2');
headers = Array.prototype.slice.call(headers);
let headers3 = document.querySelectorAll('h3');
headers3 = Array.prototype.slice.call(headers3);
headers = headers.concat(headers3);
headers.forEach(function(header) {
header.addEventListener('click', function() {
let article = this.nextElementSibling;
if (article.style.display === 'none') {
article.style.display = '';
} else {
article.style.display = 'none';
}
});
});
if (user_perms) {
document.querySelector('#user_perm_form').addEventListener('submit', submit_user_perms);
}
}
function submit_user_perms(event) {
event.preventDefault();
let perms_diff = {};
for (let input of event.target.querySelectorAll('input[type=checkbox]')) {
let user_id = input.dataset['userId'];
let app_id = input.dataset['appId'];
if (input.checked != user_perms[user_id][app_id]) {
let perms = perms_diff[user_id] || {};
perms[app_id] = input.checked;
perms_diff[user_id] = perms;
}
}
let form_body = {
'form_name': 'change_user_perms',
'perms': JSON.stringify(perms_diff),
}
let payload = [];
for (let property in form_body) {
let encoded_key = encodeURIComponent(property);
let encoded_value = encodeURIComponent(form_body[property]);
payload.push(encoded_key + '=' + encoded_value);
}
payload = payload.join('&');
fetch(window.location.pathname, {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: payload,
}).then(function(response) {
window.location.reload();
});
}