nopaque/app/static/js/RessourceLists/SpacyNLPPipelineModelList.js

147 lines
5.3 KiB
JavaScript
Raw Normal View History

class SpaCyNLPPipelineModelList extends RessourceList {
2022-11-15 14:11:16 +00:00
static autoInit() {
for (let spaCyNLPPipelineModelListElement of document.querySelectorAll('.spacy-nlp-pipeline-model-list:not(.no-autoinit)')) {
new SpaCyNLPPipelineModelList(spaCyNLPPipelineModelListElement);
2022-11-15 14:11:16 +00:00
}
}
static options = {
initialHtmlGenerator: (id) => {
return `
<div class="input-field">
<i class="material-icons prefix">search</i>
<input id="${id}-search" class="search" type="search"></input>
<label for="${id}-search">Search SpaCy NLP Pipeline Model</label>
</div>
<table>
<thead>
<tr>
2022-11-17 11:11:21 +00:00
<th>Title and Description</th>
<th>Publisher</th>
2022-11-15 14:11:16 +00:00
<th></th>
</tr>
</thead>
<tbody class="list"></tbody>
</table>
<ul class="pagination"></ul>
`.trim();
},
item: `
<tr class="clickable hoverable">
2022-11-17 11:11:21 +00:00
<td><b><span class="title"></span> <span class="version"></span></b><br><i><span class="description"></span></i></td>
<td><a class="publisher-url"><span class="publisher"></span></a> (<span class="publishing-year"></span>)<br><a class="publishing-url"><span class="publishing-url-2"></span></a></td>
<td>
2022-11-21 15:35:11 +00:00
<div class="switch action-switch center-align" data-action="share-request">
2022-11-17 11:11:21 +00:00
<span class="share"></span>
<label>
<input type="checkbox" class="shared">
<span class="lever"></span>
shared
</label>
</div>
</td>
2022-11-15 14:11:16 +00:00
<td class="right-align">
<a class="action-button btn-floating red waves-effect waves-light" data-action="delete-request"><i class="material-icons">delete</i></a>
<a class="action-button btn-floating service-color darken waves-effect waves-light service-2" data-action="view"><i class="material-icons">send</i></a>
</td>
</tr>
`.trim(),
ressourceMapper: (spaCyNLPPipelineModel) => {
return {
'id': spaCyNLPPipelineModel.id,
'creation-date': spaCyNLPPipelineModel.creation_date,
'description': spaCyNLPPipelineModel.description,
'publisher': spaCyNLPPipelineModel.publisher,
'publisher-url': spaCyNLPPipelineModel.publisher_url,
'publishing-url': spaCyNLPPipelineModel.publishing_url,
'publishing-url-2': spaCyNLPPipelineModel.publishing_url,
'publishing-year': spaCyNLPPipelineModel.publishing_year,
'title': spaCyNLPPipelineModel.title,
'title-2': spaCyNLPPipelineModel.title,
2022-11-17 11:11:21 +00:00
'version': spaCyNLPPipelineModel.version,
'shared': spaCyNLPPipelineModel.shared ? 'True' : 'False'
2022-11-15 14:11:16 +00:00
};
},
sortArgs: ['creation-date', {order: 'desc'}],
valueNames: [
{data: ['id']},
{data: ['creation-date']},
{name: 'publisher-url', attr: 'href'},
{name: 'publishing-url', attr: 'href'},
'description',
'publisher',
'publishing-url-2',
'publishing-year',
'title',
'title-2',
2022-11-17 11:11:21 +00:00
'version',
{name: 'shared', attr: 'data-checked'}
2022-11-15 14:11:16 +00:00
]
};
constructor(listElement, options = {}) {
super(listElement, {...SpaCyNLPPipelineModelList.options, ...options});
2022-11-17 11:11:21 +00:00
this.listjs.list.addEventListener('change', (event) => {this.onChange(event)});
2022-11-15 14:11:16 +00:00
}
2022-11-21 14:41:07 +00:00
init(user) {
2022-11-15 14:11:16 +00:00
this._init(user.spacy_nlp_pipeline_models);
2022-11-21 15:35:11 +00:00
if (user.role.name !== ('Administrator' || 'Contributor')) {
for (let switchElement of this.listjs.list.querySelectorAll('.shared')) {
2022-11-21 13:08:49 +00:00
switchElement.setAttribute('disabled', '');
}
}
2022-11-15 14:11:16 +00:00
}
2022-11-17 11:11:21 +00:00
_init(ressources) {
super._init(ressources);
for (let uncheckedCheckbox of this.listjs.list.querySelectorAll('input[data-checked="True"]')) {
uncheckedCheckbox.setAttribute('checked', '');
}
}
2022-11-15 14:11:16 +00:00
onClick(event) {
2022-11-21 15:35:11 +00:00
if (event.target.closest('.action-switch')) {
let userRole = app.data.users[this.userId].role.name;
if (userRole !== ('Administrator' || 'Contributor')) {
app.flash('You need the "Contributor" or "Administrator" role to perform this action.', 'error');
}
return;
}
2022-11-15 14:11:16 +00:00
let actionButtonElement = event.target.closest('.action-button');
let action = actionButtonElement === null ? 'view' : actionButtonElement.dataset.action;
let spaCyNLPPipelineModelElement = event.target.closest('tr');
let spaCyNLPPipelineModelId = spaCyNLPPipelineModelElement.dataset.id;
switch (action) {
case 'delete-request': {
Utils.deleteSpaCyNLPPipelineModelRequest(this.userId, spaCyNLPPipelineModelId);
break;
}
case 'view': {
window.location.href = `/contributions/spacy-nlp-pipeline-models/${spaCyNLPPipelineModelId}`;
break;
}
default: {
break;
}
}
}
2022-11-17 11:11:21 +00:00
onChange(event) {
let actionSwitchElement = event.target.closest('.action-switch');
let action = actionSwitchElement.dataset.action;
let spaCyNLPPipelineModelElement = event.target.closest('tr');
let spaCyNLPPipelineModelId = spaCyNLPPipelineModelElement.dataset.id;
switch (action) {
case 'share-request': {
2022-11-18 10:51:41 +00:00
let shared = actionSwitchElement.querySelector('input').checked;
2022-11-17 11:11:21 +00:00
Utils.shareSpaCyNLPPipelineModelRequest(this.userId, spaCyNLPPipelineModelId, shared);
break;
}
default: {
break;
}
}
}
2022-11-15 14:11:16 +00:00
}