2020-09-20 13:48:17 +00:00
|
|
|
const checkCheckboxes = (): void => {
|
|
|
|
const defaultsButton = document.getElementById('accountsTabSetDefaults');
|
|
|
|
const deleteButton = document.getElementById('accountsTabDelete');
|
|
|
|
const checkboxes: NodeListOf<HTMLInputElement> = document.getElementById('accountsList').querySelectorAll('input[type=checkbox]:checked');
|
|
|
|
let checked = checkboxes.length;
|
|
|
|
if (checked == 0) {
|
|
|
|
Unfocus(defaultsButton);
|
|
|
|
Unfocus(deleteButton);
|
|
|
|
} else {
|
|
|
|
Focus(defaultsButton);
|
|
|
|
Focus(deleteButton);
|
|
|
|
if (checked == 1) {
|
|
|
|
deleteButton.textContent = 'Delete User';
|
|
|
|
} else {
|
|
|
|
deleteButton.textContent = 'Delete Users';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-21 21:03:20 +00:00
|
|
|
const validateEmail = (email: string): boolean => /\S+@\S+\.\S+/.test(email);
|
2020-09-20 13:48:17 +00:00
|
|
|
|
2020-09-21 21:03:20 +00:00
|
|
|
function changeEmail(icon: HTMLElement, id: string): void {
|
2020-09-20 13:48:17 +00:00
|
|
|
const iconContent = icon.outerHTML;
|
|
|
|
icon.setAttribute('class', '');
|
2020-09-21 21:03:20 +00:00
|
|
|
const entry = icon.nextElementSibling as HTMLInputElement;
|
2020-09-20 13:48:17 +00:00
|
|
|
const ogEmail = entry.value;
|
|
|
|
entry.readOnly = false;
|
|
|
|
entry.classList.remove('form-control-plaintext');
|
|
|
|
entry.classList.add('form-control');
|
|
|
|
if (ogEmail == "") {
|
|
|
|
entry.placeholder = 'Address';
|
|
|
|
}
|
2020-09-21 21:03:20 +00:00
|
|
|
const tick = createEl(`
|
2020-09-20 13:48:17 +00:00
|
|
|
<i class="fa fa-check d-inline-block icon-button text-success" style="margin-left: 0.5rem; margin-right: 0.5rem;"></i>
|
2020-09-21 21:03:20 +00:00
|
|
|
`);
|
2020-09-20 13:48:17 +00:00
|
|
|
tick.onclick = (): void => {
|
|
|
|
const newEmail = entry.value;
|
|
|
|
if (!validateEmail(newEmail) || newEmail == ogEmail) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cross.remove();
|
2020-09-21 21:03:20 +00:00
|
|
|
const spinner = createEl(`
|
2020-09-20 13:48:17 +00:00
|
|
|
<div class="spinner-border spinner-border-sm" role="status" style="width: 1rem; height: 1rem; margin-left: 0.5rem;">
|
|
|
|
<span class="sr-only">Saving...</span>
|
|
|
|
</div>
|
2020-09-21 21:03:20 +00:00
|
|
|
`);
|
|
|
|
tick.replaceWith(spinner);
|
2020-09-20 13:48:17 +00:00
|
|
|
let send = {};
|
|
|
|
send[id] = newEmail;
|
|
|
|
_post("/modifyEmails", send, function (): void {
|
|
|
|
if (this.readyState == 4) {
|
2020-09-21 21:03:20 +00:00
|
|
|
if (this.status == 200 || this.status == 204) {
|
2020-09-20 13:48:17 +00:00
|
|
|
entry.nextElementSibling.remove();
|
|
|
|
} else {
|
|
|
|
entry.value = ogEmail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
icon.outerHTML = iconContent;
|
|
|
|
entry.readOnly = true;
|
|
|
|
entry.classList.remove('form-control');
|
|
|
|
entry.classList.add('form-control-plaintext');
|
|
|
|
entry.placeholder = '';
|
|
|
|
};
|
2020-09-21 21:03:20 +00:00
|
|
|
const cross = createEl(`
|
2020-09-20 13:48:17 +00:00
|
|
|
<i class="fa fa-close d-inline-block icon-button text-danger"></i>
|
2020-09-21 21:03:20 +00:00
|
|
|
`);
|
2020-09-20 13:48:17 +00:00
|
|
|
cross.onclick = (): void => {
|
|
|
|
tick.remove();
|
|
|
|
cross.remove();
|
|
|
|
icon.outerHTML = iconContent;
|
|
|
|
entry.readOnly = true;
|
|
|
|
entry.classList.remove('form-control');
|
|
|
|
entry.classList.add('form-control-plaintext');
|
|
|
|
entry.placeholder = '';
|
|
|
|
entry.value = ogEmail;
|
|
|
|
};
|
|
|
|
icon.parentNode.appendChild(tick);
|
|
|
|
icon.parentNode.appendChild(cross);
|
|
|
|
};
|
|
|
|
|
|
|
|
var jfUsers: Array<Object>;
|
|
|
|
|
2020-09-21 21:03:20 +00:00
|
|
|
function populateUsers(): void {
|
2020-09-20 13:48:17 +00:00
|
|
|
const acList = document.getElementById('accountsList');
|
|
|
|
acList.innerHTML = `
|
|
|
|
<div class="d-flex align-items-center">
|
|
|
|
<strong>Getting Users...</strong>
|
|
|
|
<div class="spinner-border ml-auto" role="status" aria-hidden="true"></div>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
Unfocus(acList.parentNode.querySelector('thead'));
|
|
|
|
const accountsList = document.createElement('tbody');
|
|
|
|
accountsList.id = 'accountsList';
|
|
|
|
const generateEmail = (id: string, name: string, email: string): string => {
|
|
|
|
let entry: HTMLDivElement = document.createElement('div');
|
|
|
|
entry.id = 'email_' + id;
|
|
|
|
let emailValue: string = email;
|
|
|
|
if (emailValue == undefined) {
|
|
|
|
emailValue = "";
|
|
|
|
}
|
|
|
|
entry.innerHTML = `
|
|
|
|
<i class="fa fa-edit d-inline-block icon-button" style="margin-right: 2%;" onclick="changeEmail(this, '${id}')"></i>
|
|
|
|
<input type="email" class="form-control-plaintext form-control-sm text-muted d-inline-block addressText" id="address_${id}" style="width: auto;" value="${emailValue}" readonly>
|
|
|
|
`;
|
|
|
|
return entry.outerHTML;
|
|
|
|
};
|
|
|
|
const template = (id: string, username: string, email: string, lastActive: string, admin: boolean): string => {
|
|
|
|
let isAdmin = "No";
|
|
|
|
if (admin) {
|
|
|
|
isAdmin = "Yes";
|
|
|
|
}
|
|
|
|
let fci = "form-check-input";
|
|
|
|
if (bsVersion != 5) {
|
|
|
|
fci = "";
|
|
|
|
}
|
|
|
|
return `
|
|
|
|
<td nowrap="nowrap" class="align-middle" scope="row"><input class="${fci}" type="checkbox" value="" id="select_${id}" onclick="checkCheckboxes();"></td>
|
|
|
|
<td nowrap="nowrap" class="align-middle">${username}</td>
|
|
|
|
<td nowrap="nowrap" class="align-middle">${generateEmail(id, name, email)}</td>
|
|
|
|
<td nowrap="nowrap" class="align-middle">${lastActive}</td>
|
|
|
|
<td nowrap="nowrap" class="align-middle">${isAdmin}</td>
|
|
|
|
`;
|
|
|
|
};
|
|
|
|
|
|
|
|
_get("/getUsers", null, function (): void {
|
2020-09-21 21:03:20 +00:00
|
|
|
if (this.readyState == 4 && this.status == 200) {
|
2020-09-20 13:48:17 +00:00
|
|
|
jfUsers = this.response['users'];
|
|
|
|
for (const user of jfUsers) {
|
|
|
|
let tr = document.createElement('tr');
|
|
|
|
tr.innerHTML = template(user['id'], user['name'], user['email'], user['last_active'], user['admin']);
|
|
|
|
accountsList.appendChild(tr);
|
|
|
|
}
|
|
|
|
Focus(acList.parentNode.querySelector('thead'));
|
|
|
|
acList.replaceWith(accountsList);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-21 21:03:20 +00:00
|
|
|
function populateRadios(): void {
|
|
|
|
const radioList = document.getElementById('defaultUserRadios');
|
|
|
|
radioList.textContent = '';
|
|
|
|
let first = true;
|
|
|
|
for (const i in jfUsers) {
|
|
|
|
const user = jfUsers[i];
|
|
|
|
const radio = document.createElement('div');
|
|
|
|
radio.classList.add('form-check');
|
|
|
|
let checked = '';
|
|
|
|
if (first) {
|
|
|
|
checked = 'checked';
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
radio.innerHTML = `
|
|
|
|
<input class="form-check-input" type="radio" name="defaultRadios" id="default_${user['id']}" ${checked}>
|
|
|
|
<label class="form-check-label" for="default_${user['id']}">${user['name']}</label>`;
|
|
|
|
radioList.appendChild(radio);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-20 13:48:17 +00:00
|
|
|
(<HTMLInputElement>document.getElementById('selectAll')).onclick = function (): void {
|
|
|
|
const checkboxes: NodeListOf<HTMLInputElement> = document.getElementById('accountsList').querySelectorAll('input[type=checkbox]');
|
2020-09-21 21:03:20 +00:00
|
|
|
for (let i = 0; i < checkboxes.length; i++) {
|
2020-09-20 13:48:17 +00:00
|
|
|
checkboxes[i].checked = (<HTMLInputElement>this).checked;
|
|
|
|
}
|
2020-09-21 21:03:20 +00:00
|
|
|
checkCheckboxes();
|
2020-09-20 13:48:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
(<HTMLInputElement>document.getElementById('deleteModalNotify')).onclick = function (): void {
|
|
|
|
const textbox: HTMLElement = document.getElementById('deleteModalReasonBox');
|
|
|
|
if ((<HTMLInputElement>this).checked) {
|
|
|
|
Focus(textbox);
|
|
|
|
} else {
|
|
|
|
Unfocus(textbox);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-21 21:03:20 +00:00
|
|
|
(<HTMLButtonElement>document.getElementById('accountsTabDelete')).onclick = function (): void {
|
|
|
|
const deleteButton = this as HTMLButtonElement;
|
2020-09-20 13:48:17 +00:00
|
|
|
const checkboxes: NodeListOf<HTMLInputElement> = document.getElementById('accountsList').querySelectorAll('input[type=checkbox]:checked');
|
|
|
|
let selected: Array<string> = new Array(checkboxes.length);
|
2020-09-21 21:03:20 +00:00
|
|
|
for (let i = 0; i < checkboxes.length; i++) {
|
2020-09-20 13:48:17 +00:00
|
|
|
selected[i] = checkboxes[i].id.replace("select_", "");
|
|
|
|
}
|
|
|
|
let title = " user";
|
|
|
|
let msg = "Notify user";
|
|
|
|
if (selected.length > 1) {
|
|
|
|
title += "s";
|
|
|
|
msg += "s";
|
|
|
|
}
|
|
|
|
title = `Delete ${selected.length} ${title}`;
|
|
|
|
msg += " of account deletion";
|
|
|
|
|
|
|
|
document.getElementById('deleteModalTitle').textContent = title;
|
2020-09-21 21:03:20 +00:00
|
|
|
const dmNotify = document.getElementById('deleteModalNotify') as HTMLInputElement;
|
2020-09-20 13:48:17 +00:00
|
|
|
dmNotify.checked = false;
|
|
|
|
document.getElementById('deleteModalNotifyLabel').textContent = msg;
|
2020-09-21 21:03:20 +00:00
|
|
|
const dmReason = document.getElementById('deleteModalReason') as HTMLTextAreaElement;
|
2020-09-20 13:48:17 +00:00
|
|
|
dmReason.value = '';
|
|
|
|
Unfocus(document.getElementById('deleteModalReasonBox'));
|
2020-09-21 21:03:20 +00:00
|
|
|
const dmSend = document.getElementById('deleteModalSend') as HTMLButtonElement;
|
2020-09-20 13:48:17 +00:00
|
|
|
dmSend.textContent = 'Delete';
|
|
|
|
dmSend.onclick = function (): void {
|
2020-09-21 21:03:20 +00:00
|
|
|
const button = this as HTMLButtonElement;
|
2020-09-20 13:48:17 +00:00
|
|
|
const send = {
|
|
|
|
'users': selected,
|
|
|
|
'notify': dmNotify.checked,
|
|
|
|
'reason': dmReason.value
|
|
|
|
};
|
|
|
|
_post("/deleteUser", send, function (): void {
|
|
|
|
if (this.readyState == 4) {
|
2020-09-21 21:03:20 +00:00
|
|
|
if (this.status == 500) {
|
|
|
|
if ("error" in this.reponse) {
|
2020-09-20 13:48:17 +00:00
|
|
|
button.textContent = 'Failed';
|
|
|
|
} else {
|
|
|
|
button.textContent = 'Partial fail (check console)';
|
2020-09-21 21:03:20 +00:00
|
|
|
console.log(this.response);
|
2020-09-20 13:48:17 +00:00
|
|
|
}
|
|
|
|
setTimeout((): void => {
|
|
|
|
Unfocus(deleteButton);
|
|
|
|
deleteModal.hide();
|
|
|
|
}, 4000);
|
|
|
|
} else {
|
|
|
|
Unfocus(deleteButton);
|
|
|
|
deleteModal.hide()
|
|
|
|
}
|
|
|
|
populateUsers();
|
|
|
|
checkCheckboxes();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
deleteModal.show();
|
|
|
|
};
|
|
|
|
|
|
|
|
(<HTMLInputElement>document.getElementById('selectAll')).checked = false;
|
|
|
|
|
|
|
|
(<HTMLButtonElement>document.getElementById('accountsTabSetDefaults')).onclick = function (): void {
|
|
|
|
const checkboxes: NodeListOf<HTMLInputElement> = document.getElementById('accountsList').querySelectorAll('input[type=checkbox]:checked');
|
|
|
|
let userIDs: Array<string> = new Array(checkboxes.length);
|
2020-09-21 21:03:20 +00:00
|
|
|
for (let i = 0; i < checkboxes.length; i++){
|
2020-09-20 13:48:17 +00:00
|
|
|
userIDs[i] = checkboxes[i].id.replace("select_", "");
|
|
|
|
}
|
|
|
|
if (userIDs.length == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
populateRadios();
|
|
|
|
let userString = 'user';
|
|
|
|
if (userIDs.length > 1) {
|
|
|
|
userString += "s";
|
|
|
|
}
|
|
|
|
document.getElementById('defaultsTitle').textContent = `Apply settings to ${userIDs.length} ${userString}`;
|
|
|
|
document.getElementById('userDefaultsDescription').textContent = `
|
|
|
|
Create an account and configure it to your liking, then choose it from below to apply to your selected users.
|
|
|
|
`;
|
|
|
|
document.getElementById('storeHomescreenLabel').textContent = `Apply homescreen layout`;
|
|
|
|
Focus(document.getElementById('defaultsSourceSection'));
|
|
|
|
(<HTMLSelectElement>document.getElementById('defaultsSource')).value = 'userTemplate';
|
|
|
|
Unfocus(document.getElementById('defaultUserRadios'));
|
|
|
|
document.getElementById('storeDefaults').onclick = (): void => storeDefaults(userIDs);
|
|
|
|
userDefaultsModal.show();
|
|
|
|
};
|
|
|
|
|
|
|
|
(<HTMLSelectElement>document.getElementById('defaultsSource')).addEventListener('change', function (): void {
|
|
|
|
const radios = document.getElementById('defaultUserRadios');
|
|
|
|
if (this.value == 'userTemplate') {
|
|
|
|
Unfocus(radios);
|
|
|
|
} else {
|
|
|
|
Focus(radios);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
(<HTMLButtonElement>document.getElementById('newUserCreate')).onclick = function (): void {
|
2020-09-21 21:03:20 +00:00
|
|
|
const button = this as HTMLButtonElement;
|
|
|
|
const ogText = button.textContent;
|
|
|
|
button.innerHTML = `
|
2020-09-20 13:48:17 +00:00
|
|
|
<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> Creating...
|
|
|
|
`;
|
|
|
|
const email: string = (<HTMLInputElement>document.getElementById('newUserEmail')).value;
|
|
|
|
var username: string = email;
|
|
|
|
if (document.getElementById('newUserName') != null) {
|
|
|
|
username = (<HTMLInputElement>document.getElementById('newUserName')).value;
|
|
|
|
}
|
|
|
|
const password: string = (<HTMLInputElement>document.getElementById('newUserPassword')).value;
|
|
|
|
if (!validateEmail(email) && email != "") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const send = {
|
|
|
|
'username': username,
|
|
|
|
'password': password,
|
|
|
|
'email': email
|
|
|
|
};
|
|
|
|
_post("/newUserAdmin", send, function (): void {
|
|
|
|
if (this.readyState == 4) {
|
|
|
|
rmAttr(button, 'btn-primary');
|
2020-09-21 21:03:20 +00:00
|
|
|
if (this.status == 200) {
|
2020-09-20 13:48:17 +00:00
|
|
|
addAttr(button, 'btn-success');
|
|
|
|
button.textContent = 'Success';
|
|
|
|
setTimeout((): void => {
|
|
|
|
rmAttr(button, 'btn-success');
|
2020-09-21 21:03:20 +00:00
|
|
|
addAttr(button, 'btn-primary');
|
2020-09-20 13:48:17 +00:00
|
|
|
button.textContent = ogText;
|
|
|
|
newUserModal.hide();
|
|
|
|
}, 1000);
|
2020-09-21 21:03:20 +00:00
|
|
|
populateUsers();
|
2020-09-20 13:48:17 +00:00
|
|
|
} else {
|
|
|
|
addAttr(button, 'btn-danger');
|
2020-09-21 21:03:20 +00:00
|
|
|
if ("error" in this.response) {
|
|
|
|
button.textContent = this.response["error"];
|
2020-09-20 13:48:17 +00:00
|
|
|
} else {
|
|
|
|
button.textContent = 'Failed';
|
|
|
|
}
|
|
|
|
setTimeout((): void => {
|
|
|
|
rmAttr(button, 'btn-danger');
|
|
|
|
addAttr(button, 'btn-primary');
|
|
|
|
button.textContent = ogText;
|
|
|
|
}, 2000);
|
2020-09-21 21:03:20 +00:00
|
|
|
populateUsers();
|
2020-09-20 13:48:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
(<HTMLButtonElement>document.getElementById('accountsTabAddUser')).onclick = function (): void {
|
|
|
|
(<HTMLInputElement>document.getElementById('newUserEmail')).value = '';
|
|
|
|
(<HTMLInputElement>document.getElementById('newUserPassword')).value = '';
|
|
|
|
if (document.getElementById('newUserName') != null) {
|
|
|
|
(<HTMLInputElement>document.getElementById('newUserName')).value = '';
|
|
|
|
}
|
|
|
|
newUserModal.show();
|
|
|
|
};
|
2020-09-21 21:03:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|