nopaque/web/app/static/js/nopaque.lists.js

141 lines
5.5 KiB
JavaScript
Raw Normal View History

2020-12-07 15:10:40 +00:00
class RessourceList {
constructor(idOrElement, options = {}) {
this.list = new List(idOrElement, {...RessourceList.options, ...options});
2020-01-29 09:50:31 +00:00
}
2020-12-07 15:10:40 +00:00
init(ressources) {
this.list.clear();
this.add(Object.values(ressources));
this.list.sort('id', {order: 'desc'});
2020-01-29 09:50:31 +00:00
}
2020-12-07 15:10:40 +00:00
update(patch) {
2020-01-31 13:14:08 +00:00
let item, pathArray;
2020-01-29 09:50:31 +00:00
2020-01-31 13:14:08 +00:00
for (let operation of patch) {
2020-12-07 15:10:40 +00:00
/*
* '/{ressourceName}/{ressourceId}/{valueName}' -> ['{ressourceId}', {valueName}]
* Example: '/jobs/1/status' -> ['1', 'status']
*/
let [id, valueName] = operation.path.split("/").slice(2);
2020-01-29 09:50:31 +00:00
switch(operation.op) {
2020-12-07 15:10:40 +00:00
case 'add':
this.add(operation.value);
break;
case 'remove':
this.remove(id);
2020-01-29 09:50:31 +00:00
break;
2020-12-07 15:10:40 +00:00
case 'replace':
this.replace(id, valueName, operation.value);
2020-01-29 09:50:31 +00:00
break;
default:
break;
}
}
}
2020-12-07 15:10:40 +00:00
add(values) {
/* WORKAROUND: Set a callback function ('() => {return;}') to force List.js
perform the add method asynchronous.
* https://listjs.com/api/#add
*/
this.list.add(values, () => {return;});
}
remove(id) {
this.list.remove('id', id);
}
replace(id, valueName, newValue) {
if (!this.list.valuesNames.includes(valueName)) {return;}
let item = this.list.get('id', id);
item.values({[valueName]: newValue});
}
}
RessourceList.options = {page: 5, pagination: [{innerWindow: 4, outerWindow: 1}]};
class CorpusList extends RessourceList {
constructor(listElementId, options = {}) {
let listElement = document.querySelector(`#${listElementId}`);
super(listElement, {...CorpusList.options, ...options});
listElement.addEventListener('click', (event) => {
let actionButtonElement = event.target.closest('.action-button');
if (actionButtonElement === null) {return;}
let corpusId = event.target.closest('tr').dataset.id;
let action = actionButtonElement.dataset.action;
switch (action) {
case 'analyse':
window.location.href = nopaque.user.corpora[corpusId].analysis_url;
}
});
nopaque.corporaSubscribers.push(this);
2020-07-10 09:39:16 +00:00
}
2020-01-29 09:50:31 +00:00
}
2020-12-07 15:10:40 +00:00
CorpusList.options = {
item: `<tr>
<td><a class="btn-floating disabled"><i class="material-icons">book</i></a></td>
<td><b class="title"></b><br><i class="description"></i></td>
<td><span class="badge new status" data-badge-caption=""></span></td>
<td class="right-align">
<a class="action-button btn-floating red tooltipped waves-effect waves-light" data-action="delete" data-position="top" data-tooltip="Delete"><i class="material-icons">delete</i></a>
<a class="action-button btn-floating tooltipped waves-effect waves-light" data-action="edit" data-position="top" data-tooltip="Edit"><i class="material-icons">edit</i></a>
<a class="action-button btn-floating tooltipped waves-effect waves-light" data-action="analyse" data-position="top" data-tooltip="Analyse"><i class="material-icons">search</i></a>
</td>
</tr>`,
valueNames: [{data: ['id']}, {name: "status", attr: "data-status"}, 'description', 'title']
};
2020-07-10 09:39:16 +00:00
2020-12-07 15:10:40 +00:00
class JobList extends RessourceList {
constructor(listElementId, options = {}) {
let listElement = document.querySelector(`#${listElementId}`);
super(listElement, {...JobList.options, ...options});
nopaque.jobsSubscribers.push(this);
}
}
2020-07-08 09:35:47 +00:00
2020-12-07 15:10:40 +00:00
JobList.options = {
item: `<tr>
<td><a class="btn-floating disabled"><i class="material-icons service"></i></a></td>
<td><b class="title"></b><br><i class="description"></i></td>
<td><span class="badge new status" data-badge-caption=""></span></td>
<td class="right-align">
<a class="action-button btn-floating red tooltipped waves-effect waves-light" data-action="delete" data-position="top" data-tooltip="Delete"><i class="material-icons">delete</i></a>
<a class="action-button btn-floating tooltipped waves-effect waves-light" data-action="view" data-position="top" data-tooltip="View"><i class="material-icons">send</i></a>
</td>
</tr>`,
valueNames: [{data: ['id']}, {name: 'service', attr: 'data-service'}, {name: "status", attr: "data-status"}, 'description', 'title']
2020-01-31 13:14:08 +00:00
};
2020-12-07 15:10:40 +00:00
class QueryResultList extends RessourceList {
constructor(listElementId, options = {}) {
let listElement = document.querySelector(`#${listElementId}`);
super(listElement, {...QueryResultList.options, ...options});
nopaque.queryResultsSubscribers.push(this);
}
}
QueryResultList.options = {
item: `<tr>
<td><b class="title"></b><br><i class="description"></i><br></td>
<td><span class="corpus_title"></span><br><span class="query"></span></td>
<td class="right-align">
<a class="action-button btn-floating red tooltipped waves-effect waves-light" data-action="delete" data-position="top" data-tooltip="Delete"><i class="material-icons">delete</i></a>
<a class="action-button btn-floating tooltipped waves-effect waves-light" data-action="view" data-position="top" data-tooltip="View"><i class="material-icons">send</i></a>
<a class="action-button btn-floating tooltipped waves-effect waves-light" data-action="analyse" data-position="top" data-tooltip="Analyse"><i class="material-icons">search</i></a>
</td>
</tr>`,
valueNames: [{data: ['id']}, 'corpus_title', 'description', 'query', 'title']
2020-02-12 11:19:54 +00:00
};
2020-01-29 15:12:57 +00:00
2020-12-07 15:10:40 +00:00
export { CorpusList, JobList, QueryResultList };