nopaque.resource_lists.JobList = class JobList extends nopaque.resource_lists.ResourceList { static htmlClass = 'job-list'; constructor(listContainerElement, options = {}) { super(listContainerElement, options); this.documentJobArea = document.querySelector('#jobs'); 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)}); }); this.isInitialized = false; this.selectedItemIds = new Set(); this.userId = listContainerElement.dataset.userId; if (this.userId === undefined) {return;} app.users.subscribe(this.userId).then((response) => { app.socket.on('users.patch', (patch) => { if (this.isInitialized) {this.onPatch(patch);} }); }); app.users.get(this.userId).then((user) => { this.add(Object.values(user.jobs)); this.isInitialized = true; }); } get item() { return `
delete send `.trim(); } get valueNames() { return [ {data: ['id']}, {data: ['creation-date']}, {data: ['service']}, {name: 'status', attr: 'data-job-status'}, 'description', 'title' ]; } initListContainerElement() { if (!this.listContainerElement.hasAttribute('id')) { this.listContainerElement.id = nopaque.Utils.generateElementId('job-list-'); } let listSearchElementId = nopaque.Utils.generateElementId(`${this.listContainerElement.id}-search-`); this.listContainerElement.innerHTML = `
search
Service Title and Description Status delete
`.trim(); } 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]'); if (listItemElement === null) {return;} let itemId = listItemElement.dataset.id; let listActionElement = event.target.closest('.list-action-trigger[data-list-action]'); let listAction = listActionElement === null ? 'view' : listActionElement.dataset.listAction; switch (listAction) { case 'delete-request': { let values = this.listjs.get('id', itemId)[0].values(); let modalElement = nopaque.Utils.HTMLToElement( ` ` ); 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); }); modal.open(); break; } case 'view': { window.location.href = `/jobs/${itemId}`; break; } case 'select': { if (event.target.checked) { this.selectedItemIds.add(itemId); } else { this.selectedItemIds.delete(itemId); } this.renderingItemSelection(); break; } 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( ` ` ); 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(`
  • - ${values.title}
  • `); 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; } } 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; } } } } };