class JobResultList extends ResourceList {
static autoInit() {
for (let jobResultListElement of document.querySelectorAll('.job-result-list:not(.no-autoinit)')) {
new JobResultList(jobResultListElement);
}
}
constructor(listContainerElement, options = {}) {
super(listContainerElement, options);
this.listjs.list.addEventListener('click', (event) => {this.onClick(event)});
this.isInitialized = false;
this.userId = listContainerElement.dataset.userId;
this.jobId = listContainerElement.dataset.jobId;
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.jobId].results));
this.isInitialized = true;
});
}
// #region Mandatory getters and methods to implement
get item() {
return `
|
|
file_download
|
`.trim();
}
get valueNames() {
return [
{data: ['id']},
{data: ['creation-date']},
'description',
'filename'
];
}
initListContainerElement() {
if (!this.listContainerElement.hasAttribute('id')) {
this.listContainerElement.id = Utils.generateElementId('job-result-list-');
}
let listSearchElementId = Utils.generateElementId(`${this.listContainerElement.id}-search-`);
this.listContainerElement.innerHTML = `
search
`.trim();
}
// #endregion
// #region Optional methods to implement
mapResourceToValue(jobResult) {
return {
'id': jobResult.id,
'creation-date': jobResult.creation_date,
'description': jobResult.description,
'filename': jobResult.filename
};
}
sort() {
this.listjs.sort('filename', {order: 'asc'});
}
// #endregion
onClick(event) {
let jobResultElement = event.target.closest('tr');
if (jobResultElement === null) {return;}
let jobResultId = jobResultElement.dataset.id;
if (jobResultId === undefined) {return;}
let actionButtonElement = event.target.closest('.action-button');
let action = actionButtonElement === null ? 'download' : actionButtonElement.dataset.action;
switch (action) {
case 'download': {
window.location.href = `/jobs/${this.jobId}/results/${jobResultId}/download`;
break;
}
default: {
break;
}
}
}
onPatch(patch) {
let re = new RegExp(`^/users/${this.userId}/jobs/${this.jobId}/results/([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/${this.jobId}/results/([A-Za-z0-9]*)$`);
if (re.test(operation.path)) {this.add(operation.value);}
break;
}
default: {
break;
}
}
}
}
}