dashboard.js 15 KB

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