panel.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. let panel = {}
  2. panel.page;
  3. panel.token = localStorage.admintoken;
  4. panel.filesView = localStorage.filesView;
  5. panel.preparePage = function(){
  6. if(!panel.token){
  7. document.getElementById('auth').style.display = 'flex';
  8. document.getElementById('tokenSubmit').addEventListener('click', function(){
  9. panel.verifyToken(document.getElementById('token').value);
  10. });
  11. return;
  12. }
  13. panel.verifyToken(panel.token, true);
  14. }
  15. panel.verifyToken = function(token, reloadOnError){
  16. if(reloadOnError === undefined)
  17. reloadOnError = false;
  18. axios.post('/api/tokens/verify', {
  19. type: 'admin',
  20. token: token
  21. })
  22. .then(function (response) {
  23. if(response.data.success === false){
  24. swal({
  25. title: "An error ocurred",
  26. text: response.data.description,
  27. type: "error"
  28. }, function(){
  29. if(reloadOnError){
  30. localStorage.removeItem("admintoken");
  31. location.reload();
  32. }
  33. })
  34. return;
  35. }
  36. axios.defaults.headers.common['auth'] = token;
  37. localStorage.admintoken = token;
  38. panel.token = token;
  39. return panel.prepareDashboard();
  40. })
  41. .catch(function (error) {
  42. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  43. console.log(error);
  44. });
  45. }
  46. panel.prepareDashboard = function(){
  47. panel.page = document.getElementById('page');
  48. document.getElementById('auth').style.display = 'none';
  49. document.getElementById('dashboard').style.display = 'block';
  50. document.getElementById('itemUploads').addEventListener('click', function(){
  51. panel.setActiveMenu(this);
  52. });
  53. document.getElementById('itemManageGallery').addEventListener('click', function(){
  54. panel.setActiveMenu(this);
  55. });
  56. document.getElementById('itemTokens').addEventListener('click', function(){
  57. panel.setActiveMenu(this);
  58. });
  59. panel.getAlbumsSidebar();
  60. }
  61. panel.logout = function(){
  62. localStorage.removeItem("admintoken");
  63. location.reload('/');
  64. }
  65. panel.getUploads = function(album = undefined, page = undefined){
  66. if(page === undefined) page = 0;
  67. let url = '/api/uploads/' + page
  68. if(album !== undefined)
  69. url = '/api/album/' + album + '/' + page
  70. axios.get(url)
  71. .then(function (response) {
  72. if(response.data.success === false){
  73. if(response.data.description === 'not-authorized') return panel.verifyToken(panel.token);
  74. else return swal("An error ocurred", response.data.description, "error");
  75. }
  76. var prevPage = 0;
  77. var nextPage = page + 1;
  78. if(response.data.files.length < 25)
  79. nextPage = page;
  80. if(page > 0) prevPage = page - 1;
  81. panel.page.innerHTML = '';
  82. var container = document.createElement('div');
  83. var pagination = `<nav class="pagination is-centered">
  84. <a class="pagination-previous" onclick="panel.getUploads(${album}, ${prevPage} )">Previous</a>
  85. <a class="pagination-next" onclick="panel.getUploads(${album}, ${nextPage} )">Next page</a>
  86. </nav>`;
  87. var listType = `
  88. <div class="columns">
  89. <div class="column">
  90. <a class="button is-small is-outlined is-danger" title="List view" onclick="panel.setFilesView('list', ${album}, ${page})">
  91. <span class="icon is-small">
  92. <i class="fa fa-list-ul"></i>
  93. </span>
  94. </a>
  95. <a class="button is-small is-outlined is-danger" title="List view" onclick="panel.setFilesView('thumbs', ${album}, ${page})">
  96. <span class="icon is-small">
  97. <i class="fa fa-th-large"></i>
  98. </span>
  99. </a>
  100. </div>
  101. </div>`
  102. if(panel.filesView === 'thumbs'){
  103. container.innerHTML = `
  104. ${pagination}
  105. <hr>
  106. ${listType}
  107. <div class="columns is-multiline is-mobile" id="table">
  108. </div>
  109. ${pagination}
  110. `;
  111. panel.page.appendChild(container);
  112. var table = document.getElementById('table');
  113. for(var item of response.data.files){
  114. var div = document.createElement('div');
  115. div.className = "column is-2";
  116. if(item.thumb !== undefined)
  117. div.innerHTML = `<a href="${item.file}" target="_blank"><img src="${item.thumb}"/></a>`;
  118. else
  119. div.innerHTML = `<a href="${item.file}" target="_blank"><h1 class="title">.${item.file.split('.').pop()}</h1></a>`;
  120. table.appendChild(div);
  121. }
  122. }else{
  123. container.innerHTML = `
  124. ${pagination}
  125. <hr>
  126. ${listType}
  127. <table class="table is-striped is-narrow is-left">
  128. <thead>
  129. <tr>
  130. <th>File</th>
  131. <th>Album</th>
  132. <th>Date</th>
  133. <th></th>
  134. </tr>
  135. </thead>
  136. <tbody id="table">
  137. </tbody>
  138. </table>
  139. <hr>
  140. ${pagination}
  141. `;
  142. panel.page.appendChild(container);
  143. var table = document.getElementById('table');
  144. for(var item of response.data.files){
  145. var tr = document.createElement('tr');
  146. tr.innerHTML = `
  147. <tr>
  148. <th><a href="${item.file}" target="_blank">${item.file}</a></th>
  149. <th>${item.album}</th>
  150. <td>${item.date}</td>
  151. <td>
  152. <a class="button is-small is-danger is-outlined" title="Delete album" onclick="panel.deleteFile(${item.id})">
  153. <span class="icon is-small">
  154. <i class="fa fa-trash-o"></i>
  155. </span>
  156. </a>
  157. </td>
  158. </tr>
  159. `;
  160. table.appendChild(tr);
  161. }
  162. }
  163. })
  164. .catch(function (error) {
  165. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  166. console.log(error);
  167. });
  168. }
  169. panel.setFilesView = function(view, album, page){
  170. localStorage.filesView = view;
  171. panel.filesView = view;
  172. panel.getUploads(album, page);
  173. }
  174. panel.deleteFile = function(id){
  175. swal({
  176. title: "Are you sure?",
  177. text: "You wont be able to recover the file!",
  178. type: "warning",
  179. showCancelButton: true,
  180. confirmButtonColor: "#ff3860",
  181. confirmButtonText: "Yes, delete it!",
  182. closeOnConfirm: false
  183. },
  184. function(){
  185. axios.post('/api/upload/delete', {
  186. id: id
  187. })
  188. .then(function (response) {
  189. if(response.data.success === false){
  190. if(response.data.description === 'not-authorized') return panel.verifyToken(panel.token);
  191. else return swal("An error ocurred", response.data.description, "error");
  192. }
  193. swal("Deleted!", "The file has been deleted.", "success");
  194. panel.getUploads();
  195. return;
  196. })
  197. .catch(function (error) {
  198. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  199. console.log(error);
  200. });
  201. }
  202. );
  203. }
  204. panel.getAlbums = function(){
  205. axios.get('/api/albums')
  206. .then(function (response) {
  207. if(response.data.success === false){
  208. if(response.data.description === 'not-authorized') return panel.verifyToken(panel.token);
  209. else return swal("An error ocurred", response.data.description, "error");
  210. }
  211. panel.page.innerHTML = '';
  212. var container = document.createElement('div');
  213. container.className = "container";
  214. container.innerHTML = `
  215. <h2 class="subtitle">Create new album</h2>
  216. <p class="control has-addons has-addons-centered">
  217. <input id="albumName" class="input" type="text" placeholder="Name">
  218. <a id="submitAlbum" class="button is-primary">Submit</a>
  219. </p>
  220. <h2 class="subtitle">List of albums</h2>
  221. <table class="table is-striped is-narrow">
  222. <thead>
  223. <tr>
  224. <th>Name</th>
  225. <th>Files</th>
  226. <th>Created At</th>
  227. <th></th>
  228. </tr>
  229. </thead>
  230. <tbody id="table">
  231. </tbody>
  232. </table>`;
  233. panel.page.appendChild(container);
  234. var table = document.getElementById('table');
  235. for(var item of response.data.albums){
  236. var tr = document.createElement('tr');
  237. tr.innerHTML = `
  238. <tr>
  239. <th>${item.name}</th>
  240. <th>${item.files}</th>
  241. <td>${item.date}</td>
  242. <td>
  243. <a class="button is-small is-primary is-outlined" title="Edit name" onclick="panel.renameAlbum(${item.id})">
  244. <span class="icon is-small">
  245. <i class="fa fa-pencil"></i>
  246. </span>
  247. </a>
  248. <a class="button is-small is-danger is-outlined" title="Delete album" onclick="panel.deleteAlbum(${item.id})">
  249. <span class="icon is-small">
  250. <i class="fa fa-trash-o"></i>
  251. </span>
  252. </a>
  253. </td>
  254. </tr>
  255. `;
  256. table.appendChild(tr);
  257. }
  258. document.getElementById('submitAlbum').addEventListener('click', function(){
  259. panel.submitAlbum();
  260. });
  261. })
  262. .catch(function (error) {
  263. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  264. console.log(error);
  265. });
  266. }
  267. panel.renameAlbum = function(id){
  268. swal({
  269. title: "Rename album",
  270. text: "New name you want to give the album:",
  271. type: "input",
  272. showCancelButton: true,
  273. closeOnConfirm: false,
  274. animation: "slide-from-top",
  275. inputPlaceholder: "My super album"
  276. },function(inputValue){
  277. if (inputValue === false) return false;
  278. if (inputValue === "") {
  279. swal.showInputError("You need to write something!");
  280. return false
  281. }
  282. axios.post('/api/albums/rename', {
  283. id: id,
  284. name: inputValue
  285. })
  286. .then(function (response) {
  287. if(response.data.success === false){
  288. if(response.data.description === 'not-authorized') return panel.verifyToken(panel.token);
  289. else if(response.data.description === 'Name already in use') swal.showInputError("That name is already in use!");
  290. else swal("An error ocurred", response.data.description, "error");
  291. return;
  292. }
  293. swal("Success!", "Your album was renamed to: " + inputValue, "success");
  294. panel.getAlbumsSidebar();
  295. panel.getAlbums();
  296. return;
  297. })
  298. .catch(function (error) {
  299. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  300. console.log(error);
  301. });
  302. });
  303. }
  304. panel.deleteAlbum = function(id){
  305. swal({
  306. title: "Are you sure?",
  307. text: "This won't delete your files, only the album!",
  308. type: "warning",
  309. showCancelButton: true,
  310. confirmButtonColor: "#ff3860",
  311. confirmButtonText: "Yes, delete it!",
  312. closeOnConfirm: false
  313. },
  314. function(){
  315. axios.post('/api/albums/delete', {
  316. id: id
  317. })
  318. .then(function (response) {
  319. if(response.data.success === false){
  320. if(response.data.description === 'not-authorized') return panel.verifyToken(panel.token);
  321. else return swal("An error ocurred", response.data.description, "error");
  322. }
  323. swal("Deleted!", "Your album has been deleted.", "success");
  324. panel.getAlbumsSidebar();
  325. panel.getAlbums();
  326. return;
  327. })
  328. .catch(function (error) {
  329. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  330. console.log(error);
  331. });
  332. }
  333. );
  334. }
  335. panel.submitAlbum = function(){
  336. axios.post('/api/albums', {
  337. name: document.getElementById('albumName').value
  338. })
  339. .then(function (response) {
  340. if(response.data.success === false){
  341. if(response.data.description === 'not-authorized') return panel.verifyToken(panel.token);
  342. else return swal("An error ocurred", response.data.description, "error");
  343. }
  344. swal("Woohoo!", "Album was added successfully", "success");
  345. panel.getAlbumsSidebar();
  346. panel.getAlbums();
  347. return;
  348. })
  349. .catch(function (error) {
  350. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  351. console.log(error);
  352. });
  353. }
  354. panel.getAlbumsSidebar = function(){
  355. axios.get('/api/albums/sidebar')
  356. .then(function (response) {
  357. if(response.data.success === false){
  358. if(response.data.description === 'not-authorized') return panel.verifyToken(panel.token);
  359. else return swal("An error ocurred", response.data.description, "error");
  360. }
  361. var albumsContainer = document.getElementById('albumsContainer');
  362. albumsContainer.innerHTML = '';
  363. if(response.data.albums === undefined) return;
  364. for(var album of response.data.albums){
  365. li = document.createElement('li');
  366. a = document.createElement('a');
  367. a.id = album.id;
  368. a.innerHTML = album.name;
  369. a.addEventListener('click', function(){
  370. panel.getAlbum(this);
  371. });
  372. li.appendChild(a);
  373. albumsContainer.appendChild(li);
  374. }
  375. })
  376. .catch(function (error) {
  377. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  378. console.log(error);
  379. });
  380. }
  381. panel.getAlbum = function(item){
  382. panel.setActiveMenu(item);
  383. panel.getUploads(item.id);
  384. }
  385. panel.changeTokens = function(){
  386. axios.get('/api/tokens')
  387. .then(function (response) {
  388. if(response.data.success === false){
  389. if(response.data.description === 'not-authorized') return panel.verifyToken(panel.token);
  390. else return swal("An error ocurred", response.data.description, "error");
  391. }
  392. panel.page.innerHTML = '';
  393. var container = document.createElement('div');
  394. container.className = "container";
  395. container.innerHTML = `
  396. <h2 class="subtitle">Manage your tokens</h2>
  397. <label class="label">Client token:</label>
  398. <p class="control has-addons">
  399. <input id="clientToken" class="input is-expanded" type="text" placeholder="Your client token">
  400. <a id="submitClientToken" class="button is-primary">Save</a>
  401. </p>
  402. <label class="label">Admin token:</label>
  403. <p class="control has-addons">
  404. <input id="adminToken" class="input is-expanded" type="text" placeholder="Your admin token">
  405. <a id="submitAdminToken" class="button is-primary">Save</a>
  406. </p>
  407. `;
  408. panel.page.appendChild(container);
  409. document.getElementById('clientToken').value = response.data.clientToken;
  410. document.getElementById('adminToken').value = response.data.adminToken;
  411. document.getElementById('submitClientToken').addEventListener('click', function(){
  412. panel.submitToken('client', document.getElementById('clientToken').value);
  413. });
  414. document.getElementById('submitAdminToken').addEventListener('click', function(){
  415. panel.submitToken('admin', document.getElementById('adminToken').value);
  416. });
  417. })
  418. .catch(function (error) {
  419. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  420. console.log(error);
  421. });
  422. }
  423. panel.submitToken = function(type, token){
  424. axios.post('/api/tokens/change', {
  425. type: type,
  426. token: token
  427. })
  428. .then(function (response) {
  429. if(response.data.success === false){
  430. if(response.data.description === 'not-authorized') return panel.verifyToken(panel.token);
  431. else return swal("An error ocurred", response.data.description, "error");
  432. }
  433. swal({
  434. title: "Woohoo!",
  435. text: 'Your token was changed successfully.',
  436. type: "success"
  437. }, function(){
  438. if(type === 'client')
  439. localStorage.token = token;
  440. else if(type === 'admin')
  441. localStorage.admintoken = token
  442. location.reload();
  443. })
  444. })
  445. .catch(function (error) {
  446. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  447. console.log(error);
  448. });
  449. }
  450. panel.setActiveMenu = function(item){
  451. var menu = document.getElementById('menu');
  452. var items = menu.getElementsByTagName('a');
  453. for(var i = 0; i < items.length; i++)
  454. items[i].className = "";
  455. item.className = 'is-active';
  456. }
  457. window.onload = function () {
  458. panel.preparePage();
  459. }