diff options
| author | kanadeko <[email protected]> | 2017-01-17 17:10:56 -0300 |
|---|---|---|
| committer | kanadeko <[email protected]> | 2017-01-17 17:10:56 -0300 |
| commit | 15fbca242093d3f8b5fa8351ef12804680aae9a0 (patch) | |
| tree | ffe342dff3eac81a9cef1b20ec81b7b5b5a11f18 | |
| parent | Rewrote token handling and upload.js (diff) | |
| download | host.fuwn.me-15fbca242093d3f8b5fa8351ef12804680aae9a0.tar.xz host.fuwn.me-15fbca242093d3f8b5fa8351ef12804680aae9a0.zip | |
Rewrote panel js
| -rw-r--r-- | public/js/panel.js | 112 |
1 files changed, 111 insertions, 1 deletions
diff --git a/public/js/panel.js b/public/js/panel.js index 7e3e383..df977cb 100644 --- a/public/js/panel.js +++ b/public/js/panel.js @@ -1,5 +1,115 @@ +let panel = {} + +panel.page; +panel.token = localStorage.admintoken; + +panel.preparePage = function(){ + if(!panel.token){ + document.getElementById('tokenSubmit').addEventListener('click', function(){ + panel.verifyToken(document.getElementById('token').value); + }); + return; + } + panel.verifyToken(panel.token, true); +} + +panel.verifyToken = function(token, reloadOnError = false){ + var xhr = new XMLHttpRequest(); + + xhr.onreadystatechange = function() { + if (xhr.readyState == XMLHttpRequest.DONE) { + + var json = JSON.parse(xhr.responseText); + if(json.success === false){ + alert(json.description); + if(reloadOnError){ + localStorage.removeItem("admintoken"); + location.reload(); + } + return; + } + + localStorage.admintoken = token; + panel.token = token; + return panel.prepareDashboard(); + + } + } + xhr.open('GET', '/api/token/verify', true); + xhr.setRequestHeader('type', 'admin'); + xhr.setRequestHeader('token', token); + xhr.send(null); +} + +panel.prepareDashboard = function(){ + panel.page = document.getElementById('page'); + document.getElementById('auth').style.display = 'none'; + document.getElementById('dashboard').style.display = 'block'; + + document.getElementById('itemUploads').addEventListener('click', function(){ + panel.getUploads(); + }); + + document.getElementById('itemManageGallery').addEventListener('click', function(){ + panel.getGalleries(); + }); +} + +panel.getUploads = function(){ + page.innerHTML = ''; + var xhr = new XMLHttpRequest(); + + xhr.onreadystatechange = function() { + if(xhr.readyState == XMLHttpRequest.DONE){ + + if(xhr.responseText === 'not-authorized') + return notAuthorized(); + + var json = JSON.parse(xhr.responseText); + + var container = document.createElement('div'); + container.innerHTML = ` + <table class="table"> + <thead> + <tr> + <th>File</th> + <th>Gallery</th> + <th>Date</th> + </tr> + </thead> + <tbody id="table"> + </tbody> + </table>`; + page.appendChild(container); + + var table = document.getElementById('table'); + + for(var item of json){ + + var tr = document.createElement('tr'); + tr.innerHTML = ` + <tr> + <th><a href="${item.file}" target="_blank">${item.file}</a></th> + <th>${item.gallery}</th> + <td>${item.date}</td> + </tr> + `; + + table.appendChild(tr); + } + + } + } + xhr.open('GET', '/api/uploads', true); + xhr.setRequestHeader('auth', panel.token); + xhr.send(null); +} + window.onload = function () { + panel.preparePage(); +} +/* var page; if(!localStorage.admintoken) @@ -117,4 +227,4 @@ window.onload = function () { location.reload(); } -} +*/ |