nopaque/app/static/js/resource-lists/job-list.js

323 lines
12 KiB
JavaScript
Raw Normal View History

nopaque.resource_lists.JobList = class JobList extends nopaque.resource_lists.ResourceList {
2023-10-11 14:20:17 +00:00
static htmlClass = 'job-list';
2023-01-04 19:00:37 +00:00
constructor(listContainerElement, options = {}) {
super(listContainerElement, options);
this.documentJobArea = document.querySelector('#jobs');
2023-01-04 19:00:37 +00:00
this.listjs.list.addEventListener('click', (event) => {this.onClick(event)});
document.querySelectorAll('.job-list-selection-action-trigger[data-selection-action]').forEach((element) => {
element.addEventListener('click', (event) => {this.onSelectionAction(event)});
});
2023-01-04 19:00:37 +00:00
this.isInitialized = false;
this.selectedItemIds = new Set();
2023-01-04 19:00:37 +00:00
this.userId = listContainerElement.dataset.userId;
2023-02-10 13:51:47 +00:00
if (this.userId === undefined) {return;}
2023-01-04 19:00:37 +00:00
app.subscribeUser(this.userId).then((response) => {
app.socket.on('PATCH', (patch) => {
if (this.isInitialized) {this.onPatch(patch);}
});
});
app.getUser(this.userId).then((user) => {
this.add(Object.values(user.jobs));
this.isInitialized = true;
});
}
get item() {
return `
2024-05-21 08:29:12 +00:00
<tr class="list-item clickable hoverable">
<td>
<label class="list-action-trigger" data-list-action="select">
<input class="select-checkbox" type="checkbox">
<span class="disable-on-click"></span>
</label>
</td>
2024-05-21 08:29:12 +00:00
<td><a class="btn-floating service-color darken" data-service="inherit"><i class="nopaque-icons service-icons" data-service="inherit"></i></a></td>
2023-01-04 19:00:37 +00:00
<td><b class="title"></b><br><i class="description"></i></td>
2024-05-21 08:29:12 +00:00
<td><span class="status badge new job-status-color job-status-text" data-badge-caption=""></span></td>
2023-01-04 19:00:37 +00:00
<td class="right-align">
2023-01-05 14:03:59 +00:00
<a class="list-action-trigger btn-floating red waves-effect waves-light" data-list-action="delete-request"><i class="material-icons">delete</i></a>
<a class="list-action-trigger btn-floating darken waves-effect waves-light" data-list-action="view"><i class="material-icons">send</i></a>
2023-01-04 19:00:37 +00:00
</td>
</tr>
`.trim();
}
get valueNames() {
return [
{data: ['id']},
{data: ['creation-date']},
{data: ['service']},
2024-05-21 08:29:12 +00:00
{name: 'status', attr: 'data-job-status'},
2023-01-04 19:00:37 +00:00
'description',
'title'
];
}
initListContainerElement() {
if (!this.listContainerElement.hasAttribute('id')) {
this.listContainerElement.id = nopaque.Utils.generateElementId('job-list-');
2023-01-04 19:00:37 +00:00
}
let listSearchElementId = nopaque.Utils.generateElementId(`${this.listContainerElement.id}-search-`);
2023-01-04 19:00:37 +00:00
this.listContainerElement.innerHTML = `
<div class="input-field">
<i class="material-icons prefix">search</i>
<input id="${listSearchElementId}" class="search" type="text"></input>
2023-03-21 08:14:16 +00:00
<label for="${listSearchElementId}">Search Job</label>
2023-01-04 19:00:37 +00:00
</div>
<table>
<thead>
<tr>
<th>
<label class="job-list-selection-action-trigger" data-selection-action="select-all">
<input class="job-list-select-all-checkbox" type="checkbox">
<span class="disable-on-click"></span>
</label>
</th>
2023-01-04 19:00:37 +00:00
<th>Service</th>
<th>Title and Description</th>
<th>Status</th>
<th class="right-align">
<a class="job-list-selection-action-trigger btn-floating red waves-effect waves-light hide" data-selection-action="delete"><i class="material-icons">delete</i></a>
</th>
2023-01-04 19:00:37 +00:00
</tr>
</thead>
<tbody class="list"></tbody>
</table>
<ul class="pagination"></ul>
`.trim();
}
2023-01-05 07:11:27 +00:00
2023-01-04 19:00:37 +00:00
mapResourceToValue(job) {
return {
'id': job.id,
'creation-date': job.creation_date,
'description': job.description,
'service': job.service,
'status': job.status,
'title': job.title
};
}
sort() {
this.listjs.sort('creation-date', {order: 'desc'});
}
onClick(event) {
let listItemElement = event.target.closest('.list-item[data-id]');
2023-01-05 14:03:59 +00:00
if (listItemElement === null) {return;}
let itemId = listItemElement.dataset.id;
let listActionElement = event.target.closest('.list-action-trigger[data-list-action]');
2023-10-05 12:19:46 +00:00
let listAction = listActionElement === null ? 'view' : listActionElement.dataset.listAction;
2023-01-05 14:03:59 +00:00
switch (listAction) {
2023-01-04 19:00:37 +00:00
case 'delete-request': {
2023-03-21 08:14:16 +00:00
let values = this.listjs.get('id', itemId)[0].values();
let modalElement = nopaque.Utils.HTMLToElement(
2023-03-21 08:14:16 +00:00
`
<div class="modal">
<div class="modal-content">
<h4>Confirm Job deletion</h4>
<p>Do you really want to delete the Job <b>${values.title}</b>? All files will be permanently deleted!</p>
</div>
<div class="modal-footer">
<a class="btn modal-close waves-effect waves-light">Cancel</a>
<a class="action-button btn modal-close red waves-effect waves-light" data-action="confirm">Delete</a>
</div>
</div>
`
);
document.querySelector('#modals').appendChild(modalElement);
let modal = M.Modal.init(
modalElement,
{
dismissible: false,
onCloseEnd: () => {
modal.destroy();
modalElement.remove();
}
}
);
let confirmElement = modalElement.querySelector('.action-button[data-action="confirm"]');
confirmElement.addEventListener('click', (event) => {
nopaque.requests.jobs.entity.delete(itemId);
2023-03-21 08:14:16 +00:00
});
modal.open();
2023-01-04 19:00:37 +00:00
break;
}
case 'view': {
2023-01-05 14:03:59 +00:00
window.location.href = `/jobs/${itemId}`;
2023-01-04 19:00:37 +00:00
break;
}
case 'select': {
if (event.target.checked) {
this.selectedItemIds.add(itemId);
} else {
this.selectedItemIds.delete(itemId);
}
this.renderingItemSelection();
break;
}
2023-01-04 19:00:37 +00:00
default: {
break;
}
}
}
onSelectionAction(event) {
let selectionActionElement = event.target.closest('.job-list-selection-action-trigger[data-selection-action]');
let selectionAction = selectionActionElement.dataset.selectionAction;
let items = this.listjs.items;
let selectableItems = Array.from(items)
.filter(item => item.elm)
.map(item => item.elm.querySelector('.select-checkbox[type="checkbox"]'));
switch (selectionAction) {
case 'select-all': {
let selectedIds = new Set(Array.from(items)
.map(item => item.values().id))
if (event.target.checked !== undefined) {
if (event.target.checked) {
selectableItems.forEach(selectableItem => selectableItem.checked = true);
this.selectedItemIds = selectedIds;
} else {
selectableItems.forEach(checkbox => checkbox.checked = false);
this.selectedItemIds = new Set([...this.selectedItemIds].filter(id => !selectedIds.has(id)));
}
this.renderingItemSelection();
}
break;
}
case 'delete': {
let modalElement = nopaque.Utils.HTMLToElement(
`
<div class="modal">
<div class="modal-content">
<h4>Confirm Corpus File deletion</h4>
<p>Do you really want to delete the Jobs?</p>
<ul id="selected-items-list"></ul>
<p>All files will be permanently deleted!</p>
</div>
<div class="modal-footer">
<a class="btn modal-close waves-effect waves-light">Cancel</a>
<a class="action-button btn modal-close red waves-effect waves-light" data-action="confirm">Delete</a>
</div>
</div>
`
);
document.querySelector('#modals').appendChild(modalElement);
let itemList = document.querySelector('#selected-items-list');
this.selectedItemIds.forEach(selectedItemId => {
let listItem = this.listjs.get('id', selectedItemId)[0].elm;
let values = this.listjs.get('id', listItem.dataset.id)[0].values();
let itemElement = nopaque.Utils.HTMLToElement(`<li> - ${values.title}</li>`);
itemList.appendChild(itemElement);
});
let modal = M.Modal.init(
modalElement,
{
dismissible: false,
onCloseEnd: () => {
modal.destroy();
modalElement.remove();
}
}
);
let confirmElement = modalElement.querySelector('.action-button[data-action="confirm"]');
confirmElement.addEventListener('click', (event) => {
this.selectedItemIds.forEach(selectedItemId => {
nopaque.requests.jobs.entity.delete(selectedItemId);
});
this.selectedItemIds.clear();
this.renderingItemSelection();
});
modal.open();
break;
}
default: {
break;
}
}
}
renderingItemSelection() {
let selectionActionButtons = document.querySelectorAll('.job-list-selection-action-trigger:not([data-selection-action="select-all"])');
let selectableItems = this.listjs.items;
let actionButtons = [];
Object.values(selectableItems).forEach(selectableItem => {
if (selectableItem.elm) {
let checkbox = selectableItem.elm.querySelector('.select-checkbox[type="checkbox"]');
if (checkbox.checked) {
selectableItem.elm.classList.add('grey', 'lighten-3');
} else {
selectableItem.elm.classList.remove('grey', 'lighten-3');
}
let itemActionButtons = selectableItem.elm.querySelectorAll('.list-action-trigger:not([data-list-action="select"])');
itemActionButtons.forEach(itemActionButton => {
actionButtons.push(itemActionButton);
});
}
});
// Hide item action buttons if > 0 item is selected and show selection action buttons
if (this.selectedItemIds.size > 0) {
selectionActionButtons.forEach(selectionActionButton => {
selectionActionButton.classList.remove('hide');
});
actionButtons.forEach(actionButton => {
actionButton.classList.add('hide');
});
} else {
selectionActionButtons.forEach(selectionActionButton => {
selectionActionButton.classList.add('hide');
});
actionButtons.forEach(actionButton => {
actionButton.classList.remove('hide');
});
}
// Check select all checkbox if all items are selected
let selectAllCheckbox = document.querySelector('.job-list-select-all-checkbox[type="checkbox"]');
if (selectableItems.length === this.selectedItemIds.size && selectAllCheckbox.checked === false) {
selectAllCheckbox.checked = true;
} else if (selectableItems.length !== this.selectedItemIds.size && selectAllCheckbox.checked === true) {
selectAllCheckbox.checked = false;
}
}
2023-01-04 19:00:37 +00:00
onPatch(patch) {
let re = new RegExp(`^/users/${this.userId}/jobs/([A-Za-z0-9]*)`);
let filteredPatch = patch.filter(operation => re.test(operation.path));
for (let operation of filteredPatch) {
switch(operation.op) {
case 'add': {
let re = new RegExp(`^/users/${this.userId}/jobs/([A-Za-z0-9]*)$`);
if (re.test(operation.path)) {this.add(operation.value);}
break;
}
case 'remove': {
let re = new RegExp(`^/users/${this.userId}/jobs/([A-Za-z0-9]*)$`);
if (re.test(operation.path)) {
let [match, jobId] = operation.path.match(re);
this.remove(jobId);
}
break;
}
case 'replace': {
let re = new RegExp(`^/users/${this.userId}/jobs/([A-Za-z0-9]*)/(service|status|description|title)$`);
if (re.test(operation.path)) {
let [match, jobId, valueName] = operation.path.match(re);
this.replace(jobId, valueName, operation.value);
}
break;
}
default: {
break;
}
}
}
}
2023-10-11 14:20:17 +00:00
};