panel.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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(album = undefined){
  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. if(album !== undefined)
  95. xhr.setRequestHeader('albumid', album);
  96. xhr.setRequestHeader('auth', panel.token);
  97. xhr.send(null);
  98. }
  99. panel.getAlbums = function(){
  100. panel.page.innerHTML = '';
  101. var xhr = new XMLHttpRequest();
  102. var container = document.createElement('div');
  103. container.className = "container";
  104. container.innerHTML = `
  105. <h2 class="subtitle">Create new album</h2>
  106. <p class="control has-addons has-addons-centered">
  107. <input id="albumName" class="input" type="text" placeholder="Name">
  108. <a id="submitAlbum" class="button is-primary">Submit</a>
  109. </p>
  110. <h2 class="subtitle">List of albums</h2>
  111. <table class="table is-striped is-narrow">
  112. <thead>
  113. <tr>
  114. <th>Name</th>
  115. <th>Files</th>
  116. <th>Created At</th>
  117. </tr>
  118. </thead>
  119. <tbody id="table">
  120. </tbody>
  121. </table>`;
  122. xhr.onreadystatechange = function() {
  123. if (xhr.readyState == XMLHttpRequest.DONE) {
  124. if(xhr.responseText === 'not-authorized')
  125. return panel.verifyToken(panel.token);
  126. var json = JSON.parse(xhr.responseText);
  127. console.log(json);
  128. if(json.success === false)
  129. return swal("An error ocurred", json.description, "error");
  130. panel.page.appendChild(container);
  131. var table = document.getElementById('table');
  132. for(var item of json.albums){
  133. var tr = document.createElement('tr');
  134. tr.innerHTML = `
  135. <tr>
  136. <th>${item.name}</th>
  137. <th>${item.files}</th>
  138. <td>${item.date}</td>
  139. </tr>
  140. `;
  141. table.appendChild(tr);
  142. }
  143. document.getElementById('submitAlbum').addEventListener('click', function(){
  144. panel.submitAlbum();
  145. });
  146. }
  147. }
  148. xhr.open('GET', '/api/albums', true);
  149. xhr.setRequestHeader('auth', panel.token);
  150. xhr.setRequestHeader('extended', '');
  151. xhr.send(null);
  152. }
  153. panel.submitAlbum = function(){
  154. var xhr = new XMLHttpRequest();
  155. xhr.onreadystatechange = function() {
  156. if (xhr.readyState == XMLHttpRequest.DONE) {
  157. if(xhr.responseText === 'not-authorized')
  158. return panel.verifyToken(panel.token);
  159. var json = JSON.parse(xhr.responseText);
  160. if(json.success === false)
  161. return swal("An error ocurred", json.description, "error");
  162. swal("Woohoo!", "Album was added successfully", "success");
  163. panel.getAlbumsSidebar();
  164. panel.getAlbums();
  165. return;
  166. }
  167. }
  168. xhr.open('POST', '/api/albums', true);
  169. xhr.setRequestHeader('auth', panel.token);
  170. xhr.setRequestHeader('name', document.getElementById('albumName').value);
  171. xhr.send(null);
  172. }
  173. panel.getAlbumsSidebar = function(){
  174. var xhr = new XMLHttpRequest();
  175. xhr.onreadystatechange = function() {
  176. if (xhr.readyState == XMLHttpRequest.DONE) {
  177. if(xhr.responseText === 'not-authorized')
  178. return panel.verifyToken(panel.token);
  179. var json = JSON.parse(xhr.responseText);
  180. if(json.success === false)
  181. return swal("An error ocurred", json.description, "error");
  182. var albumsContainer = document.getElementById('albumsContainer');
  183. albumsContainer.innerHTML = '';
  184. if(json.albums === undefined) return;
  185. for(var album of json.albums){
  186. li = document.createElement('li');
  187. a = document.createElement('a');
  188. a.id = album.id;
  189. a.innerHTML = album.name;
  190. a.addEventListener('click', function(){
  191. panel.getAlbum(this);
  192. });
  193. li.appendChild(a);
  194. albumsContainer.appendChild(li);
  195. }
  196. }
  197. }
  198. xhr.open('GET', '/api/albums', true);
  199. xhr.setRequestHeader('auth', panel.token);
  200. xhr.send(null);
  201. }
  202. panel.getAlbum = function(item){
  203. panel.getUploads(item.id);
  204. }
  205. window.onload = function () {
  206. panel.preparePage();
  207. }