panel.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. let panel = {}
  2. panel.page;
  3. panel.token = localStorage.admintoken;
  4. panel.preparePage = function(){
  5. if(!panel.token){
  6. document.getElementById('auth').style.display = 'flex';
  7. document.getElementById('tokenSubmit').addEventListener('click', function(){
  8. panel.verifyToken(document.getElementById('token').value);
  9. });
  10. return;
  11. }
  12. panel.verifyToken(panel.token, true);
  13. }
  14. panel.verifyToken = function(token, reloadOnError = false){
  15. var xhr = new XMLHttpRequest();
  16. xhr.onreadystatechange = function() {
  17. if (xhr.readyState == XMLHttpRequest.DONE) {
  18. var json = JSON.parse(xhr.responseText);
  19. if(json.success === false){
  20. swal({
  21. title: "An error ocurred",
  22. text: json.description,
  23. type: "error"
  24. }, function(){
  25. if(reloadOnError){
  26. localStorage.removeItem("admintoken");
  27. location.reload();
  28. }
  29. })
  30. return;
  31. }
  32. localStorage.admintoken = token;
  33. panel.token = token;
  34. return panel.prepareDashboard();
  35. }
  36. }
  37. xhr.open('GET', '/api/token/verify', true);
  38. xhr.setRequestHeader('type', 'admin');
  39. xhr.setRequestHeader('token', token);
  40. xhr.send(null);
  41. }
  42. panel.prepareDashboard = function(){
  43. panel.page = document.getElementById('page');
  44. document.getElementById('auth').style.display = 'none';
  45. document.getElementById('dashboard').style.display = 'block';
  46. document.getElementById('itemUploads').addEventListener('click', function(){
  47. panel.getUploads();
  48. });
  49. document.getElementById('itemManageGallery').addEventListener('click', function(){
  50. panel.getAlbums();
  51. });
  52. panel.getAlbumsSidebar();
  53. }
  54. panel.getUploads = function(){
  55. panel.page.innerHTML = '';
  56. var xhr = new XMLHttpRequest();
  57. xhr.onreadystatechange = function() {
  58. if(xhr.readyState == XMLHttpRequest.DONE){
  59. if(xhr.responseText === 'not-authorized')
  60. return panel.verifyToken(panel.token);
  61. var json = JSON.parse(xhr.responseText);
  62. console.log(json);
  63. if(json.success === false)
  64. return swal("An error ocurred", json.description, "error");
  65. var container = document.createElement('div');
  66. container.innerHTML = `
  67. <table class="table is-striped is-narrow">
  68. <thead>
  69. <tr>
  70. <th>File</th>
  71. <th>Album</th>
  72. <th>Date</th>
  73. </tr>
  74. </thead>
  75. <tbody id="table">
  76. </tbody>
  77. </table>`;
  78. panel.page.appendChild(container);
  79. var table = document.getElementById('table');
  80. for(var item of json){
  81. var tr = document.createElement('tr');
  82. tr.innerHTML = `
  83. <tr>
  84. <th><a href="${item.file}" target="_blank">${item.file}</a></th>
  85. <th>${item.album}</th>
  86. <td>${item.date}</td>
  87. </tr>
  88. `;
  89. table.appendChild(tr);
  90. }
  91. }
  92. }
  93. xhr.open('GET', '/api/uploads', true);
  94. xhr.setRequestHeader('auth', panel.token);
  95. xhr.send(null);
  96. }
  97. panel.getAlbums = function(){
  98. panel.page.innerHTML = '';
  99. var xhr = new XMLHttpRequest();
  100. var container = document.createElement('div');
  101. container.className = "container";
  102. container.innerHTML = `
  103. <h2 class="subtitle">Create new album</h2>
  104. <p class="control has-addons has-addons-centered">
  105. <input id="albumName" class="input" type="text" placeholder="Name">
  106. <a id="submitAlbum" class="button is-primary">Submit</a>
  107. </p>
  108. <h2 class="subtitle">List of albums</h2>
  109. <table class="table is-striped is-narrow">
  110. <thead>
  111. <tr>
  112. <th>Name</th>
  113. <th>Files</th>
  114. <th>Created At</th>
  115. </tr>
  116. </thead>
  117. <tbody id="table">
  118. </tbody>
  119. </table>`;
  120. xhr.onreadystatechange = function() {
  121. if (xhr.readyState == XMLHttpRequest.DONE) {
  122. if(xhr.responseText === 'not-authorized')
  123. return panel.verifyToken(panel.token);
  124. var json = JSON.parse(xhr.responseText);
  125. console.log(json);
  126. if(json.success === false)
  127. return swal("An error ocurred", json.description, "error");
  128. panel.page.appendChild(container);
  129. var table = document.getElementById('table');
  130. for(var item of json.albums){
  131. var tr = document.createElement('tr');
  132. tr.innerHTML = `
  133. <tr>
  134. <th>${item.name}</th>
  135. <th>${item.files}</th>
  136. <td>${item.date}</td>
  137. </tr>
  138. `;
  139. table.appendChild(tr);
  140. }
  141. document.getElementById('submitAlbum').addEventListener('click', function(){
  142. panel.submitAlbum();
  143. });
  144. }
  145. }
  146. xhr.open('GET', '/api/albums', true);
  147. xhr.setRequestHeader('auth', panel.token);
  148. xhr.setRequestHeader('extended', '');
  149. xhr.send(null);
  150. }
  151. panel.submitAlbum = function(){
  152. var xhr = new XMLHttpRequest();
  153. xhr.onreadystatechange = function() {
  154. if (xhr.readyState == XMLHttpRequest.DONE) {
  155. if(xhr.responseText === 'not-authorized')
  156. return panel.verifyToken(panel.token);
  157. var json = JSON.parse(xhr.responseText);
  158. if(json.success === false)
  159. return swal("An error ocurred", json.description, "error");
  160. swal("Woohoo!", "Album was added successfully", "success");
  161. panel.getAlbumsSidebar();
  162. panel.getAlbums();
  163. return;
  164. }
  165. }
  166. xhr.open('POST', '/api/albums', true);
  167. xhr.setRequestHeader('auth', panel.token);
  168. xhr.setRequestHeader('name', document.getElementById('albumName').value);
  169. xhr.send(null);
  170. }
  171. panel.getAlbumsSidebar = function(){
  172. var xhr = new XMLHttpRequest();
  173. xhr.onreadystatechange = function() {
  174. if (xhr.readyState == XMLHttpRequest.DONE) {
  175. if(xhr.responseText === 'not-authorized')
  176. return panel.verifyToken(panel.token);
  177. var json = JSON.parse(xhr.responseText);
  178. console.log(json);
  179. if(json.success === false)
  180. return swal("An error ocurred", json.description, "error");
  181. var albumsContainer = document.getElementById('albumsContainer');
  182. albumsContainer.innerHTML = '';
  183. if(json.albums === undefined) return;
  184. for(var album of json.albums){
  185. li = document.createElement('li');
  186. a = document.createElement('a');
  187. a.innerHTML = album.name;
  188. li.appendChild(a);
  189. albumsContainer.appendChild(li);
  190. }
  191. }
  192. }
  193. xhr.open('GET', '/api/albums', true);
  194. xhr.setRequestHeader('auth', panel.token);
  195. xhr.send(null);
  196. }
  197. window.onload = function () {
  198. panel.preparePage();
  199. }