nopaque/app/static/js/resource-lists/spacy-nlp-pipeline-model-list.js

220 lines
8.2 KiB
JavaScript
Raw Permalink Normal View History

nopaque.resource_lists.SpaCyNLPPipelineModelList = class SpaCyNLPPipelineModelList extends nopaque.resource_lists.ResourceList {
2023-10-11 14:20:17 +00:00
static htmlClass = 'spacy-nlp-pipeline-model-list';
2023-01-04 19:00:37 +00:00
constructor(listContainerElement, options = {}) {
super(listContainerElement, options);
this.listjs.list.addEventListener('change', (event) => {this.onChange(event)});
this.listjs.list.addEventListener('click', (event) => {this.onClick(event)});
this.isInitialized = false;
this.userId = listContainerElement.dataset.userId;
2023-02-10 13:51:47 +00:00
if (this.userId === undefined) {return;}
2023-01-04 19:00:37 +00:00
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.spacy_nlp_pipeline_models));
this.isInitialized = true;
});
}
get item() {
2023-01-11 12:29:47 +00:00
return (values) => {
return `
<tr class="list-item clickable hoverable">
<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 publishing-url-2"></a></td>
<td>
2023-02-21 10:05:23 +00:00
<span class="disable-on-click">
2023-01-11 12:29:47 +00:00
<label>
2023-02-21 10:05:23 +00:00
<input ${values['is-public'] ? 'checked' : ''} class="is-public list-action-trigger" data-list-action="toggle-is-public" type="checkbox">
<span>Public</span>
2023-01-11 12:29:47 +00:00
</label>
2023-02-21 10:05:23 +00:00
</span>
2023-01-11 12:29:47 +00:00
</td>
<td class="right-align">
<a class="list-action-trigger btn-floating red waves-effect waves-light" data-list-action="delete-request"><i class="material-icons">delete</i></a>
<a class="list-action-trigger btn-floating service-color darken waves-effect waves-light service-2" data-list-action="view"><i class="material-icons">send</i></a>
</td>
</tr>
`.trim();
};
2023-01-04 19:00:37 +00:00
}
get valueNames() {
return [
{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',
2023-01-11 12:29:47 +00:00
'version'
2023-01-04 19:00:37 +00:00
];
}
initListContainerElement() {
if (!this.listContainerElement.hasAttribute('id')) {
this.listContainerElement.id = nopaque.Utils.generateElementId('spacy-nlp-pipeline-model-list-');
2023-01-04 19:00:37 +00:00
}
let listSearchElementId = nopaque.Utils.generateElementId(`${this.listContainerElement.id}-search-`);
2023-01-04 19:00:37 +00:00
this.listContainerElement.innerHTML = `
<div class="input-field">
<i class="material-icons prefix">search</i>
<input id="${listSearchElementId}" class="search" type="text"></input>
<label for="${listSearchElementId}">Search SpaCy NLP Pipeline Model</label>
</div>
<table>
<thead>
<tr>
<th>Title and Description</th>
<th>Publisher</th>
2023-02-21 10:05:23 +00:00
<th>Availability</th>
2023-01-04 19:00:37 +00:00
<th></th>
</tr>
</thead>
<tbody class="list"></tbody>
</table>
<ul class="pagination"></ul>
`.trim();
}
2023-01-05 07:11:27 +00:00
2023-01-04 19:00:37 +00:00
mapResourceToValue(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,
'version': spaCyNLPPipelineModel.version,
2023-01-11 12:29:47 +00:00
'is-public': spaCyNLPPipelineModel.is_public
2023-01-04 19:00:37 +00:00
};
}
sort() {
this.listjs.sort('creation-date', {order: 'desc'});
}
onChange(event) {
2023-02-21 10:05:23 +00:00
if (event.target.tagName !== 'INPUT') {return;}
2023-01-11 12:29:47 +00:00
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]');
if (listActionElement === null) {return;}
let listAction = listActionElement.dataset.listAction;
switch (listAction) {
2023-02-21 10:05:23 +00:00
case 'toggle-is-public': {
2023-03-06 14:04:06 +00:00
let newIsPublicValue = listActionElement.checked;
nopaque.requests.contributions.spacy_nlp_pipeline_models.entity.isPublic.update(itemId, newIsPublicValue)
2023-03-06 14:04:06 +00:00
.catch((response) => {
listActionElement.checked = !newIsPublicValue;
});
2023-01-04 19:00:37 +00:00
break;
}
default: {
break;
}
}
}
onClick(event) {
2023-02-21 10:05:23 +00:00
if (event.target.closest('.disable-on-click') !== null) {return;}
2023-01-11 12:29:47 +00:00
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) {
2023-01-04 19:00:37 +00:00
case 'delete-request': {
2023-03-09 11:07:16 +00:00
let values = this.listjs.get('id', itemId)[0].values();
let modalElement = nopaque.Utils.HTMLToElement(
2023-03-09 11:07:16 +00:00
`
<div class="modal">
<div class="modal-content">
<h4>Confirm SpaCy NLP Pipeline Model deletion</h4>
<p>Do you really want to delete the SpaCy NLP Pipeline Model <b>${values.title}</b>? All files will be permanently deleted!</p>
</div>
<div class="modal-footer">
<a class="btn modal-close waves-effect waves-light">Cancel</a>
<a class="action-button btn modal-close red waves-effect waves-light" data-action="confirm">Delete</a>
</div>
</div>
`
);
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.contributions.spacy_nlp_pipeline_models.entity.delete(itemId);
2023-03-09 11:07:16 +00:00
});
modal.open();
2023-01-04 19:00:37 +00:00
break;
}
case 'view': {
2023-01-11 12:29:47 +00:00
window.location.href = `/contributions/spacy-nlp-pipeline-models/${itemId}`;
2023-01-04 19:00:37 +00:00
break;
}
default: {
break;
}
}
}
2023-01-11 12:29:47 +00:00
onPatch(patch) {
let re = new RegExp(`^/users/${this.userId}/spacy_nlp_pipeline_models/([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}/spacy_nlp_pipeline_models/([A-Za-z0-9]*)$`);
if (re.test(operation.path)) {this.add(operation.value);}
break;
}
case 'remove': {
let re = new RegExp(`^/users/${this.userId}/spacy_nlp_pipeline_models/([A-Za-z0-9]*)$`);
if (re.test(operation.path)) {
let [match, itemId] = operation.path.match(re);
this.remove(itemId);
}
break;
}
case 'replace': {
let re = new RegExp(`^/users/${this.userId}/spacy_nlp_pipeline_models/([A-Za-z0-9]*)/(is_public)$`);
if (re.test(operation.path)) {
let [match, itemId, valueName] = operation.path.match(re);
if (valueName === 'is_public') {
this.listjs.list.querySelector(`.list-item[data-id="${itemId}"] .is-public`).checked = operation.value;
valueName = 'is-public';
}
this.replace(itemId, valueName, operation.value);
}
break;
}
default: {
break;
}
}
}
}
2023-10-11 14:20:17 +00:00
};