class SpacyNLPModelList { constructor () { this.elements = { spacyNLPModelList: document.querySelector('#spacy-nlp-model-list'), deleteButtons: document.querySelectorAll('.delete-spacy-model-button'), editButtons: document.querySelectorAll('.edit-spacy-model-button'), } } init () { let userId = this.elements.spacyNLPModelList.dataset.userId; for (let deleteButton of this.elements.deleteButtons) { deleteButton.addEventListener('click', () => {this.deleteModel(deleteButton, userId);}); } for (let editButton of this.elements.editButtons) { editButton.addEventListener('click', () => {this.editModel(editButton);}); } } deleteModel(deleteButton, userId) { return new Promise((resolve, reject) => { let modelId = deleteButton.dataset.modelId; let model = app.data.users[userId].spacy_nlp_pipeline_models[modelId]; let modalElement = Utils.elementFromString( ` ` ); 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) => { let modelTitle = model.title; fetch(`/contributions/edit-spacy-model/${modelId}`, {method: 'DELETE'}) .then( (response) => { app.flash(`Model "${modelTitle}" marked for deletion`, 'corpus'); resolve(response); }, (response) => { if (response.status === 403) {app.flash('Forbidden', 'error');} if (response.status === 404) {app.flash('Not Found', 'error');} reject(response); } ); }); modal.open(); }); } editModel(editButton) { window.location.href = `/contributions/edit-spacy-model/${editButton.dataset.modelId}`; } }