class AdminUserList extends ResourceList {
static autoInit() {
for (let adminUserListElement of document.querySelectorAll('.admin-user-list:not(.no-autoinit)')) {
new AdminUserList(adminUserListElement);
}
}
constructor(listContainerElement, options = {}) {
super(listContainerElement, options);
this.listjs.list.addEventListener('click', (event) => {this.onClick(event)});
}
get item() {
return `
|
|
|
|
|
delete
edit
send
|
`.trim();
}
get valueNames() {
return [
{data: ['id']},
{data: ['member-since']},
'email',
'id-1',
'last-seen',
'role',
'username'
];
}
initListContainerElement() {
if (!this.listContainerElement.hasAttribute('id')) {
this.listContainerElement.id = Utils.generateElementId('user-list-');
}
let listSearchElementId = Utils.generateElementId(`${this.listContainerElement.id}-search-`);
this.listContainerElement.innerHTML = `
search
Id |
Username |
Email |
Last seen |
Role |
|
`.trim();
}
mapResourceToValue(user) {
return {
'id': user.id,
'id-1': user.id,
'username': user.username,
'email': user.email,
'last-seen': new Date(user.last_seen).toLocaleString('en-US'),
'member-since': user.member_since,
'role': user.role.name
};
}
sort() {
this.listjs.sort('member-since', {order: 'desc'});
}
onClick(event) {
let listItemElement = event.target.closest('.list-item');
if (listItemElement === null) {return;}
if (!('id' in listItemElement.dataset)) {return;}
let itemId = listItemElement.dataset.id;
if (itemId === undefined) {return;}
let listActionElement = event.target.closest('.list-item .list-action-trigger');
let listAction =
listActionElement === null || !('listAction' in listActionElement.dataset)
? 'view' : listActionElement.dataset.listAction;
switch (listAction) {
case 'delete': {
console.log('delete', itemId);
Utils.deleteUserRequest(itemId);
if (itemId === currentUserId) {window.location.href = '/';}
break;
}
case 'edit': {
window.location.href = `/admin/users/${itemId}/edit`;
break;
}
case 'view': {
window.location.href = `/admin/users/${itemId}`;
break;
}
default: {
break;
}
}
}
}