class JobList extends ResourceList {
static autoInit() {
for (let jobListElement of document.querySelectorAll('.job-list:not(.no-autoinit)')) {
new JobList(jobListElement);
}
}
constructor(listContainerElement, options = {}) {
super(listContainerElement, options);
this.listjs.list.addEventListener('click', (event) => {this.onClick(event)});
this.isInitialized = false;
this.userId = listContainerElement.dataset.userId;
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 `
|
|
|
delete
send
|
`.trim();
}
get valueNames() {
return [
{data: ['id']},
{data: ['creation-date']},
{data: ['service']},
{name: 'status', attr: 'data-status'},
'description',
'title'
];
}
initListContainerElement() {
if (!this.listContainerElement.hasAttribute('id')) {
this.listContainerElement.id = Utils.generateElementId('job-list-');
}
let listSearchElementId = Utils.generateElementId(`${this.listContainerElement.id}-search-`);
this.listContainerElement.innerHTML = `
search
Service |
Title and Description |
Status |
|
`.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');
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-request': {
Utils.deleteJobRequest(this.userId, itemId);
break;
}
case 'view': {
window.location.href = `/jobs/${itemId}`;
break;
}
default: {
break;
}
}
}
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;
}
}
}
}
}