dashboard.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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></th>
  225. </tr>
  226. </thead>
  227. <tbody id="table">
  228. </tbody>
  229. </table>`;
  230. panel.page.appendChild(container);
  231. var table = document.getElementById('table');
  232. for(var item of response.data.albums){
  233. var tr = document.createElement('tr');
  234. tr.innerHTML = `
  235. <tr>
  236. <th>${item.name}</th>
  237. <th>${item.files}</th>
  238. <td>${item.date}</td>
  239. <td>
  240. <a class="button is-small is-primary is-outlined" title="Edit name" onclick="panel.renameAlbum(${item.id})">
  241. <span class="icon is-small">
  242. <i class="fa fa-pencil"></i>
  243. </span>
  244. </a>
  245. <a class="button is-small is-danger is-outlined" title="Delete album" onclick="panel.deleteAlbum(${item.id})">
  246. <span class="icon is-small">
  247. <i class="fa fa-trash-o"></i>
  248. </span>
  249. </a>
  250. </td>
  251. </tr>
  252. `;
  253. table.appendChild(tr);
  254. }
  255. document.getElementById('submitAlbum').addEventListener('click', function(){
  256. panel.submitAlbum();
  257. });
  258. })
  259. .catch(function (error) {
  260. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  261. console.log(error);
  262. });
  263. }
  264. panel.renameAlbum = function(id){
  265. swal({
  266. title: "Rename album",
  267. text: "New name you want to give the album:",
  268. type: "input",
  269. showCancelButton: true,
  270. closeOnConfirm: false,
  271. animation: "slide-from-top",
  272. inputPlaceholder: "My super album"
  273. },function(inputValue){
  274. if (inputValue === false) return false;
  275. if (inputValue === "") {
  276. swal.showInputError("You need to write something!");
  277. return false
  278. }
  279. axios.post('/api/albums/rename', {
  280. id: id,
  281. name: inputValue
  282. })
  283. .then(function (response) {
  284. if(response.data.success === false){
  285. if(response.data.description === 'No token provided') return panel.verifyToken(panel.token);
  286. else if(response.data.description === 'Name already in use') swal.showInputError("That name is already in use!");
  287. else swal("An error ocurred", response.data.description, "error");
  288. return;
  289. }
  290. swal("Success!", "Your album was renamed to: " + inputValue, "success");
  291. panel.getAlbumsSidebar();
  292. panel.getAlbums();
  293. return;
  294. })
  295. .catch(function (error) {
  296. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  297. console.log(error);
  298. });
  299. });
  300. }
  301. panel.deleteAlbum = function(id){
  302. swal({
  303. title: "Are you sure?",
  304. text: "This won't delete your files, only the album!",
  305. type: "warning",
  306. showCancelButton: true,
  307. confirmButtonColor: "#ff3860",
  308. confirmButtonText: "Yes, delete it!",
  309. closeOnConfirm: false
  310. },
  311. function(){
  312. axios.post('/api/albums/delete', {
  313. id: id
  314. })
  315. .then(function (response) {
  316. if(response.data.success === false){
  317. if(response.data.description === 'No token provided') return panel.verifyToken(panel.token);
  318. else return swal("An error ocurred", response.data.description, "error");
  319. }
  320. swal("Deleted!", "Your album has been deleted.", "success");
  321. panel.getAlbumsSidebar();
  322. panel.getAlbums();
  323. return;
  324. })
  325. .catch(function (error) {
  326. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  327. console.log(error);
  328. });
  329. }
  330. );
  331. }
  332. panel.submitAlbum = function(){
  333. axios.post('/api/albums', {
  334. name: document.getElementById('albumName').value
  335. })
  336. .then(function (response) {
  337. if(response.data.success === false){
  338. if(response.data.description === 'No token provided') return panel.verifyToken(panel.token);
  339. else return swal("An error ocurred", response.data.description, "error");
  340. }
  341. swal("Woohoo!", "Album was added successfully", "success");
  342. panel.getAlbumsSidebar();
  343. panel.getAlbums();
  344. return;
  345. })
  346. .catch(function (error) {
  347. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  348. console.log(error);
  349. });
  350. }
  351. panel.getAlbumsSidebar = function(){
  352. axios.get('/api/albums/sidebar')
  353. .then(function (response) {
  354. if(response.data.success === false){
  355. if(response.data.description === 'No token provided') return panel.verifyToken(panel.token);
  356. else return swal("An error ocurred", response.data.description, "error");
  357. }
  358. var albumsContainer = document.getElementById('albumsContainer');
  359. albumsContainer.innerHTML = '';
  360. if(response.data.albums === undefined) return;
  361. for(var album of response.data.albums){
  362. li = document.createElement('li');
  363. a = document.createElement('a');
  364. a.id = album.id;
  365. a.innerHTML = album.name;
  366. a.addEventListener('click', function(){
  367. panel.getAlbum(this);
  368. });
  369. li.appendChild(a);
  370. albumsContainer.appendChild(li);
  371. }
  372. })
  373. .catch(function (error) {
  374. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  375. console.log(error);
  376. });
  377. }
  378. panel.getAlbum = function(item){
  379. panel.setActiveMenu(item);
  380. panel.getUploads(item.id);
  381. }
  382. panel.changeToken = function(){
  383. axios.get('/api/tokens')
  384. .then(function (response) {
  385. if(response.data.success === false){
  386. if(response.data.description === 'No token provided') return panel.verifyToken(panel.token);
  387. else return swal("An error ocurred", response.data.description, "error");
  388. }
  389. panel.page.innerHTML = '';
  390. var container = document.createElement('div');
  391. container.className = "container";
  392. container.innerHTML = `
  393. <h2 class="subtitle">Manage your token</h2>
  394. <label class="label">Your current token:</label>
  395. <p class="control has-addons">
  396. <input id="token" readonly class="input is-expanded" type="text" placeholder="Your token" value="${response.data.token}">
  397. <a id="getNewToken" class="button is-primary">Request new token</a>
  398. </p>
  399. `;
  400. panel.page.appendChild(container);
  401. document.getElementById('getNewToken').addEventListener('click', function(){
  402. panel.getNewToken();
  403. });
  404. })
  405. .catch(function (error) {
  406. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  407. console.log(error);
  408. });
  409. }
  410. panel.getNewToken = function(){
  411. axios.post('/api/tokens/change')
  412. .then(function (response) {
  413. if(response.data.success === false){
  414. if(response.data.description === 'No token provided') return panel.verifyToken(panel.token);
  415. else return swal("An error ocurred", response.data.description, "error");
  416. }
  417. swal({
  418. title: "Woohoo!",
  419. text: 'Your token was changed successfully.',
  420. type: "success"
  421. }, function(){
  422. localStorage.token = response.data.token;
  423. location.reload();
  424. })
  425. })
  426. .catch(function (error) {
  427. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  428. console.log(error);
  429. });
  430. }
  431. panel.changePassword = function(){
  432. panel.page.innerHTML = '';
  433. var container = document.createElement('div');
  434. container.className = "container";
  435. container.innerHTML = `
  436. <h2 class="subtitle">Change your password</h2>
  437. <label class="label">New password:</label>
  438. <p class="control has-addons">
  439. <input id="password" class="input is-expanded" type="password" placeholder="Your new password">
  440. <a id="sendChangePassword" class="button is-primary">Set new password</a>
  441. </p>
  442. `;
  443. panel.page.appendChild(container);
  444. document.getElementById('sendChangePassword').addEventListener('click', function(){
  445. panel.sendNewPassword(document.getElementById('password').value);
  446. });
  447. }
  448. panel.sendNewPassword = function(pass){
  449. axios.post('/api/password/change', {password: pass})
  450. .then(function (response) {
  451. if(response.data.success === false){
  452. if(response.data.description === 'No token provided') return panel.verifyToken(panel.token);
  453. else return swal("An error ocurred", response.data.description, "error");
  454. }
  455. swal({
  456. title: "Woohoo!",
  457. text: 'Your password was changed successfully.',
  458. type: "success"
  459. }, function(){
  460. location.reload();
  461. })
  462. })
  463. .catch(function (error) {
  464. return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
  465. console.log(error);
  466. });
  467. }
  468. panel.setActiveMenu = function(item){
  469. var menu = document.getElementById('menu');
  470. var items = menu.getElementsByTagName('a');
  471. for(var i = 0; i < items.length; i++)
  472. items[i].className = "";
  473. item.className = 'is-active';
  474. }
  475. window.onload = function () {
  476. panel.preparePage();
  477. }