class SaveAndShare { constructor() { this.dataType = 'unknown'; this.dataFct = function () {return ''; }; this.currentAction = null; this.url = ''; this.shareId = null; this.initFct = null; } setInitFct(initFct) { this.initFct = initFct; } setURL(url) { this.url = url; } setDataFct(dataFct) { this.dataFct = dataFct; } setDataType(dataType) { this.dataType = dataType; const pathname = window.location.pathname; if (window.location.pathname.indexOf('/' + this.dataType + '/') !== -1) { this.shareId = window.location.pathname.substring(window.location.pathname.indexOf('/' + this.dataType + '/') + ('/' + this.dataType + '/').length); } } setShareId(shareId) { this.shareId = shareId; } deleteDataPwd() { document.getElementById('message-save').style.display = 'none'; document.getElementById('message-update').style.display = 'none'; document.getElementById('message-fork').style.display = 'none'; document.getElementById('message-delete').style.display = ''; this.currentAction = this.deleteData.bind(this); this.dialogPwd.show(); } saveDataPwd() { document.getElementById('message-save').style.display = ''; document.getElementById('message-update').style.display = 'none'; document.getElementById('message-fork').style.display = 'none'; document.getElementById('message-delete').style.display = 'none'; this.currentAction = this.saveData.bind(this); this.dialogPwd.show(); } updateDataPwd() { document.getElementById('message-save').style.display = 'none'; document.getElementById('message-update').style.display = ''; document.getElementById('message-fork').style.display = 'none'; document.getElementById('message-delete').style.display = 'none'; this.currentAction = this.updateData.bind(this); this.dialogPwd.show(); } forkDataPwd() { document.getElementById('message-save').style.display = 'none'; document.getElementById('message-update').style.display = 'none'; document.getElementById('message-fork').style.display = ''; document.getElementById('message-delete').style.display = 'none'; this.currentAction = this.forkData.bind(this); this.dialogPwd.show(); } doIt() { const pwd = document.getElementById('pwd').value; if (pwd == '') { alert('You must fill password input'); return; } this.dialogPwd.hide(); this.currentAction(); } forkData() { const self = this; const pwd = document.getElementById('pwd').value; const xhr= new XMLHttpRequest(); xhr.open('POST', myDomain + '/share/' + this.dataType, true); xhr.setRequestHeader("Content-type", "application/json"); xhr.setRequestHeader("Security-key", pwd); xhr.onreadystatechange= function() { if (this.readyState!==4) return; const responseObj = JSON.parse(this.responseText); if (this.status!==201) { if (responseObj && responseObj.message) { alert(responseObj.message); } else { alert('An error has occured. Please, try later.'); } } else { history.pushState({}, "", "/"+self.dataType+"/"+responseObj.id); document.getElementById('link-id').value = myDomain + "/"+self.dataType+"/" + responseObj.id; document.getElementById('link-id').select(); document.getElementById('data-save').style.display = 'none'; document.getElementById('message-save').style.display = 'none'; document.getElementById('data-update').style.display = ''; document.getElementById('data-delete').style.display = ''; document.getElementById('data-fork').style.display = ''; document.getElementById('message-update').style.display = ''; document.getElementById('message-delete').style.display = ''; self.setShareId(responseObj.id); self.dialogAdded.show(); } }; xhr.send(JSON.stringify(this.dataFct())); } saveData() { const self = this; const pwd = document.getElementById('pwd').value; const xhr= new XMLHttpRequest(); xhr.open('POST', myDomain + '/share/' + this.dataType, true); xhr.setRequestHeader("Content-type", "application/json"); xhr.setRequestHeader("Security-key", pwd); xhr.onreadystatechange= function() { if (this.readyState!==4) return; const responseObj = JSON.parse(this.responseText); if (this.status!==201) { if (responseObj && responseObj.message) { alert(responseObj.message); } else { alert('An error has occured. Please, try later.'); } } else { history.pushState({}, "", "/"+self.dataType+"/"+responseObj.id); document.getElementById('link-id').value = myDomain + "/"+self.dataType+"/" + responseObj.id; document.getElementById('link-id').select(); document.getElementById('data-save').style.display = 'none'; document.getElementById('message-save').style.display = 'none'; document.getElementById('data-update').style.display = ''; document.getElementById('data-delete').style.display = ''; document.getElementById('data-fork').style.display = ''; document.getElementById('message-update').style.display = ''; document.getElementById('message-delete').style.display = ''; self.setShareId(responseObj.id); self.dialogAdded.show(); } }; xhr.send(JSON.stringify(this.dataFct())); } updateData() { const pwd = document.getElementById('pwd').value; const data = this.dataFct(); const xhr= new XMLHttpRequest(); xhr.open('PUT', myDomain + '/share/' + this.dataType + '/' + this.shareId, true); xhr.setRequestHeader("Content-type", "application/json"); xhr.setRequestHeader("Security-key", pwd); xhr.onreadystatechange= function() { if (this.readyState!==4) return; if (this.status!==200) { var responseObj = JSON.parse(this.responseText); if (responseObj.message) { alert(responseObj.message); } else { alert('An error has occured. Please, try later.'); } } else { alert('Your data has been updated.'); } }; xhr.send(JSON.stringify(data)); } deleteData() { const self = this; const pwd = document.getElementById('pwd').value; const xhr= new XMLHttpRequest(); const data = this.dataFct(); xhr.open('DELETE', myDomain + '/share/' + this.dataType + '/' + this.shareId, true); xhr.setRequestHeader("Content-type", "application/json"); xhr.setRequestHeader("Security-key", pwd); xhr.onreadystatechange= function() { if (this.readyState!==4) return; if (this.status!==200) { const responseObj = JSON.parse(this.responseText); if (responseObj.message) { alert(responseObj.message); } else { alert('An error has occured. Please, try later.'); } } else { alert('Your data has been deleted.'); document.location.href = myDomain + "/" + self.url; } }; xhr.send(); } init() { const self = this; this.dialogAdded = new A11yDialog(document.getElementById('dialog-added')); this.dialogPwd = new A11yDialog(document.getElementById('dialog-pwd')); if (this.shareId !== null && this.shareId !== '' && this.shareId !== undefined) { document.getElementById('data-save').style.display = 'none'; document.getElementById('data-save').style.display = 'none'; const updateView = function (data, error) { if (error) { alert(error); document.getElementById('data-save').style.display = ''; document.getElementById('message-save').style.display = ''; document.getElementById('data-update').style.display = 'none'; document.getElementById('data-delete').style.display = 'none'; document.getElementById('data-fork').style.display = 'none'; document.getElementById('message-update').style.display = 'none'; document.getElementById('message-delete').style.display = 'none'; return; } else { ['editor-error', 'editor-valid'].forEach(function (id) { if (document.getElementById(id)) { services.billboard.emptyAndHide(id); } }); self.initFct(data); } }; const xhr= new XMLHttpRequest(); xhr.open('GET', myDomain + '/share/' + this.dataType + '/' + this.shareId, true); xhr.setRequestHeader("Content-type", "application/json"); xhr.onreadystatechange= function() { if (this.readyState!==4) return; let error = null; const responseObj = JSON.parse(this.responseText); if (this.status!==200) { if (responseObj.message) { error = responseObj.message; } else { error = 'An error has occured. Please, try later.'; } } updateView(responseObj, error); }; xhr.send(); } else { document.getElementById('data-update').style.display = 'none'; document.getElementById('data-delete').style.display = 'none'; document.getElementById('message-update').style.display = 'none'; document.getElementById('message-delete').style.display = 'none'; document.getElementById('data-fork').style.display = 'none'; } } }