dashboard.js 16 KB

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