panel.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. window.onload = function () {
  2. var page;
  3. if(!localStorage.admintoken)
  4. return askForToken();
  5. prepareDashboard();
  6. function askForToken(){
  7. document.getElementById('tokenSubmit').addEventListener('click', function(){
  8. checkToken();
  9. });
  10. function checkToken(){
  11. var xhr = new XMLHttpRequest();
  12. xhr.onreadystatechange = function() {
  13. if (xhr.readyState == XMLHttpRequest.DONE) {
  14. try{
  15. var json = JSON.parse(xhr.responseText);
  16. if(json.success === false)
  17. return alert(json.description);
  18. localStorage.admintoken = document.getElementById('token').value;
  19. prepareDashboard();
  20. }catch(e){
  21. console.log(e);
  22. }
  23. console.log(xhr.responseText);
  24. // xhr.responseText
  25. }
  26. }
  27. xhr.open('GET', '/api/token/verify', true);
  28. xhr.setRequestHeader('type', 'admin');
  29. xhr.setRequestHeader('token', document.getElementById('token').value);
  30. xhr.send(null);
  31. }
  32. }
  33. function prepareDashboard(){
  34. page = document.getElementById('page');
  35. document.getElementById('auth').style.display = 'none';
  36. document.getElementById('dashboard').style.display = 'block';
  37. document.getElementById('itemUploads').addEventListener('click', function(){
  38. getUploads();
  39. });
  40. document.getElementById('itemManageGallery').addEventListener('click', function(){
  41. getGalleries();
  42. });
  43. }
  44. function getUploads(){
  45. page.innerHTML = '';
  46. var xhr = new XMLHttpRequest();
  47. xhr.onreadystatechange = function() {
  48. if(xhr.readyState == XMLHttpRequest.DONE){
  49. if(xhr.responseText === 'not-authorized')
  50. return notAuthorized();
  51. var json = JSON.parse(xhr.responseText);
  52. var container = document.createElement('div');
  53. container.innerHTML = `
  54. <table class="table">
  55. <thead>
  56. <tr>
  57. <th>File</th>
  58. <th>Gallery</th>
  59. <th>Date</th>
  60. </tr>
  61. </thead>
  62. <tbody id="table">
  63. </tbody>
  64. </table>`;
  65. page.appendChild(container);
  66. var table = document.getElementById('table');
  67. for(var item of json){
  68. var tr = document.createElement('tr');
  69. tr.innerHTML = `
  70. <tr>
  71. <th><a href="${item.file}" target="_blank">${item.file}</a></th>
  72. <th>${item.gallery}</th>
  73. <td>${item.date}</td>
  74. </tr>
  75. `;
  76. table.appendChild(tr);
  77. }
  78. }
  79. }
  80. xhr.open('GET', '/api/uploads', true);
  81. xhr.setRequestHeader('auth', localStorage.admintoken);
  82. xhr.send(null);
  83. }
  84. function getContent(item, value){
  85. let endpoint;
  86. if(item === 'uploads') endpoint = '/api/uploads'
  87. if(item === 'galleries') endpoint = '/api/uploads'
  88. }
  89. function notAuthorized() {
  90. localStorage.removeItem("admintoken");
  91. location.reload();
  92. }
  93. }