2018-01-23 20:06:30 +00:00
/* eslint-disable no-unused-expressions */
2018-03-28 20:05:01 +00:00
/* global swal, axios, ClipboardJS */
2018-01-23 20:06:30 +00:00
2018-03-28 11:36:28 +00:00
const panel = {
page : undefined ,
username : undefined ,
token : localStorage . token ,
2018-03-28 20:05:01 +00:00
filesView : localStorage . filesView ,
clipboardJS : undefined
2018-03-28 11:36:28 +00:00
}
2018-01-23 20:06:30 +00:00
2018-03-28 11:36:28 +00:00
panel . preparePage = ( ) => {
2018-01-23 20:06:30 +00:00
if ( ! panel . token ) {
2018-01-24 15:31:23 +00:00
window . location = 'auth'
2018-01-23 20:06:30 +00:00
}
panel . verifyToken ( panel . token , true )
}
2018-03-28 11:36:28 +00:00
panel . verifyToken = ( token , reloadOnError ) => {
2018-01-23 20:06:30 +00:00
if ( reloadOnError === undefined ) {
reloadOnError = false
}
2018-01-24 15:31:23 +00:00
axios . post ( 'api/tokens/verify' , {
2018-01-23 20:06:30 +00:00
token : token
} )
2018-03-28 11:36:28 +00:00
. then ( response => {
2018-03-19 16:51:39 +00:00
if ( response . data . success === false ) {
swal ( {
title : 'An error occurred' ,
text : response . data . description ,
icon : 'error'
} ) . then ( ( ) => {
if ( reloadOnError ) {
localStorage . removeItem ( 'token' )
location . location = 'auth'
}
} )
return
}
2018-01-23 20:06:30 +00:00
2018-03-19 16:51:39 +00:00
axios . defaults . headers . common . token = token
localStorage . token = token
panel . token = token
panel . username = response . data . username
return panel . prepareDashboard ( )
} )
2018-03-28 11:36:28 +00:00
. catch ( err => {
console . log ( err )
2018-03-19 16:51:39 +00:00
return swal ( 'An error occurred' , 'There was an error with the request, please check the console for more information.' , 'error' )
} )
2018-01-23 20:06:30 +00:00
}
2018-03-28 11:36:28 +00:00
panel . prepareDashboard = ( ) => {
2018-01-23 20:06:30 +00:00
panel . page = document . getElementById ( 'page' )
document . getElementById ( 'auth' ) . style . display = 'none'
document . getElementById ( 'dashboard' ) . style . display = 'block'
document . getElementById ( 'itemUploads' ) . addEventListener ( 'click' , function ( ) {
panel . setActiveMenu ( this )
} )
document . getElementById ( 'itemManageGallery' ) . addEventListener ( 'click' , function ( ) {
panel . setActiveMenu ( this )
} )
2018-03-24 14:49:44 +00:00
document . getElementById ( 'itemFileLength' ) . addEventListener ( 'click' , function ( ) {
panel . setActiveMenu ( this )
} )
2018-01-23 20:06:30 +00:00
document . getElementById ( 'itemTokens' ) . addEventListener ( 'click' , function ( ) {
panel . setActiveMenu ( this )
} )
document . getElementById ( 'itemPassword' ) . addEventListener ( 'click' , function ( ) {
panel . setActiveMenu ( this )
} )
document . getElementById ( 'itemLogout' ) . innerHTML = ` Logout ( ${ panel . username } ) `
panel . getAlbumsSidebar ( )
}
2018-03-28 11:36:28 +00:00
panel . logout = ( ) => {
2018-01-23 20:06:30 +00:00
localStorage . removeItem ( 'token' )
2018-01-24 15:31:23 +00:00
location . reload ( '.' )
2018-01-23 20:06:30 +00:00
}
2018-03-28 12:28:17 +00:00
panel . closeModal = ( ) => {
document . getElementById ( 'modal' ) . className = 'modal'
}
2018-03-28 17:40:50 +00:00
panel . isLoading = ( element , state ) => {
if ( ! element ) { return }
if ( state && ! element . className . includes ( ' is-loading' ) ) {
element . className += ' is-loading'
} else if ( ! state && element . className . includes ( ' is-loading' ) ) {
element . className = element . className . replace ( ' is-loading' , '' )
}
}
panel . getUploads = ( album , page , element ) => {
if ( element ) { panel . isLoading ( element , true ) }
if ( page === undefined ) { page = 0 }
2018-01-23 20:06:30 +00:00
2018-01-24 15:31:23 +00:00
let url = 'api/uploads/' + page
if ( album !== undefined ) { url = 'api/album/' + album + '/' + page }
2018-01-23 20:06:30 +00:00
2018-03-28 11:36:28 +00:00
axios . get ( url ) . then ( response => {
2018-01-23 20:06:30 +00:00
if ( response . data . success === false ) {
2018-03-28 17:40:50 +00:00
if ( response . data . description === 'No token provided' ) {
return panel . verifyToken ( panel . token )
} else {
return swal ( 'An error occurred' , response . data . description , 'error' )
}
2018-01-23 20:06:30 +00:00
}
2018-03-28 11:36:28 +00:00
let prevPage = 0
let nextPage = page + 1
2018-01-23 20:06:30 +00:00
if ( response . data . files . length < 25 ) { nextPage = page }
2018-03-28 17:40:50 +00:00
if ( page > 0 ) { prevPage = page - 1 }
2018-01-23 20:06:30 +00:00
2018-03-28 11:36:28 +00:00
const pagination = `
2018-03-19 16:51:39 +00:00
< nav class = "pagination is-centered" >
2018-03-28 17:40:50 +00:00
< a class = "button pagination-previous" onclick = "panel.getUploads(${album}, ${prevPage}, this)" > Previous < / a >
< a class = "button pagination-next" onclick = "panel.getUploads(${album}, ${nextPage}, this)" > Next page < / a >
2018-03-19 16:51:39 +00:00
< / n a v >
`
2018-03-28 11:36:28 +00:00
const listType = `
2018-03-19 16:51:39 +00:00
< div class = "columns" >
< div class = "column" >
2018-03-28 17:40:50 +00:00
< a class = "button is-small is-outlined is-danger" title = "List view" onclick = "panel.setFilesView('list', ${album}, ${page}, this)" >
2018-03-19 16:51:39 +00:00
< span class = "icon is-small" >
2018-03-28 20:05:01 +00:00
< i class = "icon-th-list" > < / i >
2018-03-19 16:51:39 +00:00
< / s p a n >
< / a >
2018-03-28 17:40:50 +00:00
< a class = "button is-small is-outlined is-danger" title = "Thumbs view" onclick = "panel.setFilesView('thumbs', ${album}, ${page}, this)" >
2018-03-19 16:51:39 +00:00
< span class = "icon is-small" >
2018-03-28 20:05:01 +00:00
< i class = "icon-th-large" > < / i >
2018-03-19 16:51:39 +00:00
< / s p a n >
< / a >
< / d i v >
2018-01-23 20:06:30 +00:00
< / d i v >
2018-03-19 16:51:39 +00:00
`
2018-01-23 20:06:30 +00:00
if ( panel . filesView === 'thumbs' ) {
2018-03-19 16:51:39 +00:00
panel . page . innerHTML = `
2018-01-23 20:06:30 +00:00
$ { pagination }
< hr >
$ { listType }
2018-03-19 17:18:29 +00:00
< div class = "columns is-multiline is-mobile is-centered" id = "table" >
2018-01-23 20:06:30 +00:00
< / d i v >
$ { pagination }
`
2018-03-28 11:36:28 +00:00
const table = document . getElementById ( 'table' )
for ( const item of response . data . files ) {
const div = document . createElement ( 'div' )
let displayAlbumOrUser = item . album
if ( panel . username === 'root' ) {
displayAlbumOrUser = ''
if ( item . username !== undefined ) { displayAlbumOrUser = item . username }
}
2018-01-23 20:06:30 +00:00
2018-03-24 16:45:51 +00:00
div . className = 'image-container column is-narrow'
2018-01-23 20:06:30 +00:00
if ( item . thumb !== undefined ) {
2018-03-24 16:45:51 +00:00
div . innerHTML = ` <a class="image" href=" ${ item . file } " target="_blank"><img src=" ${ item . thumb } "/></a> `
2018-01-23 20:06:30 +00:00
} else {
2018-03-24 16:45:51 +00:00
div . innerHTML = ` <a class="image" href=" ${ item . file } " target="_blank"><h1 class="title">. ${ item . file . split ( '.' ) . pop ( ) } </h1></a> `
2018-01-23 20:06:30 +00:00
}
2018-03-24 16:45:51 +00:00
div . innerHTML += `
2018-03-24 19:47:41 +00:00
< a class = "button is-small is-danger is-outlined" title = "Delete album" onclick = "panel.deleteFile(${item.id}, ${album}, ${page})" >
2018-03-24 16:45:51 +00:00
< span class = "icon is-small" >
2018-03-28 20:05:01 +00:00
< i class = "icon-trash-1" > < / i >
2018-03-24 16:45:51 +00:00
< / s p a n >
< / a >
2018-03-28 11:36:28 +00:00
< div class = "name" >
< p > < span > $ { item . name } < / s p a n > < / p >
< p > $ { displayAlbumOrUser ? ` <span> ${ displayAlbumOrUser } </span> – ` : '' } $ { item . size } < / d i v >
2018-03-24 16:45:51 +00:00
`
2018-01-23 20:06:30 +00:00
table . appendChild ( div )
}
} else {
2018-03-28 11:36:28 +00:00
let albumOrUser = 'Album'
2018-01-23 20:06:30 +00:00
if ( panel . username === 'root' ) { albumOrUser = 'User' }
2018-03-19 16:51:39 +00:00
panel . page . innerHTML = `
2018-01-23 20:06:30 +00:00
$ { pagination }
< hr >
$ { listType }
2018-01-24 15:31:23 +00:00
< div class = "table-container" >
2018-03-19 16:51:39 +00:00
< table class = "table is-narrow is-fullwidth is-hoverable" >
2018-01-24 15:31:23 +00:00
< thead >
< tr >
< th > File < / t h >
< th > $ { albumOrUser } < / t h >
2018-03-24 16:45:51 +00:00
< th > Size < / t h >
2018-01-24 15:31:23 +00:00
< th > Date < / t h >
< th > < / t h >
< / t r >
< / t h e a d >
< tbody id = "table" >
< / t b o d y >
< / t a b l e >
< / d i v >
2018-01-23 20:06:30 +00:00
< hr >
$ { pagination }
`
2018-03-28 11:36:28 +00:00
const table = document . getElementById ( 'table' )
2018-01-23 20:06:30 +00:00
2018-03-28 11:36:28 +00:00
for ( const item of response . data . files ) {
const tr = document . createElement ( 'tr' )
2018-01-23 20:06:30 +00:00
2018-03-28 11:36:28 +00:00
let displayAlbumOrUser = item . album
2018-01-23 20:06:30 +00:00
if ( panel . username === 'root' ) {
displayAlbumOrUser = ''
if ( item . username !== undefined ) { displayAlbumOrUser = item . username }
}
tr . innerHTML = `
< tr >
2018-03-28 12:28:17 +00:00
< th > < a href = "${item.file}" target = "_blank" onclick = "event.stopPropagation()" > $ { item . file } < / a > < / t h >
2018-01-23 20:06:30 +00:00
< th > $ { displayAlbumOrUser } < / t h >
2018-03-24 16:45:51 +00:00
< td > $ { item . size } < / t d >
2018-01-23 20:06:30 +00:00
< td > $ { item . date } < / t d >
2018-03-28 20:05:01 +00:00
< td style = "text-align: right" >
< a class = "button is-small is-info is-outlined clipboard-js" title = "Copy link to clipboard" data - clipboard - text = "${item.file}" >
2018-01-23 20:06:30 +00:00
< span class = "icon is-small" >
2018-03-28 20:05:01 +00:00
< i class = "icon-attach" > < / i >
< / s p a n >
< / a >
< a class = "button is-small is-danger is-outlined" title = "Delete album" panel . deleteFile ( $ { item . id } , $ { album } , $ { page } ) " >
< span class = "icon is-small" >
< i class = "icon-trash-1" > < / i >
2018-01-23 20:06:30 +00:00
< / s p a n >
< / a >
< / t d >
< / t r >
2018-03-19 16:51:39 +00:00
`
2018-01-23 20:06:30 +00:00
table . appendChild ( tr )
2018-03-28 12:28:17 +00:00
if ( item . thumb ) {
tr . addEventListener ( 'click' , function ( event ) {
2018-03-28 20:05:01 +00:00
if ( event . target . tagName . toLowerCase ( ) === 'a' ) { return }
if ( event . target . className . includes ( 'icon' ) ) { return }
2018-03-28 12:28:17 +00:00
document . getElementById ( 'modalImage' ) . src = item . thumb
document . getElementById ( 'modal' ) . className += ' is-active'
} )
}
2018-01-23 20:06:30 +00:00
}
}
} )
2018-03-28 11:36:28 +00:00
. catch ( err => {
console . log ( err )
2018-03-19 16:51:39 +00:00
return swal ( 'An error occurred' , 'There was an error with the request, please check the console for more information.' , 'error' )
} )
2018-01-23 20:06:30 +00:00
}
2018-03-28 17:40:50 +00:00
panel . setFilesView = ( view , album , page , element ) => {
2018-01-23 20:06:30 +00:00
localStorage . filesView = view
panel . filesView = view
2018-03-28 17:40:50 +00:00
panel . getUploads ( album , page , element )
2018-01-23 20:06:30 +00:00
}
2018-03-28 11:36:28 +00:00
panel . deleteFile = ( id , album , page ) => {
2018-01-23 20:06:30 +00:00
swal ( {
title : 'Are you sure?' ,
2018-03-24 19:47:41 +00:00
text : 'You won\'t be able to recover the file!' ,
2018-03-19 16:51:39 +00:00
icon : 'warning' ,
dangerMode : true ,
buttons : {
cancel : true ,
confirm : {
text : 'Yes, delete it!' ,
closeModal : false
}
}
} ) . then ( value => {
2018-03-28 17:40:50 +00:00
if ( ! value ) { return }
2018-03-19 16:51:39 +00:00
axios . post ( 'api/upload/delete' , {
id : id
} )
2018-03-28 11:36:28 +00:00
. then ( response => {
2018-01-23 20:06:30 +00:00
if ( response . data . success === false ) {
2018-03-28 17:40:50 +00:00
if ( response . data . description === 'No token provided' ) {
return panel . verifyToken ( panel . token )
} else {
return swal ( 'An error occurred' , response . data . description , 'error' )
}
2018-01-23 20:06:30 +00:00
}
swal ( 'Deleted!' , 'The file has been deleted.' , 'success' )
2018-03-24 19:47:41 +00:00
panel . getUploads ( album , page )
2018-01-23 20:06:30 +00:00
} )
2018-03-28 11:36:28 +00:00
. catch ( err => {
console . log ( err )
2018-02-24 19:33:31 +00:00
return swal ( 'An error occurred' , 'There was an error with the request, please check the console for more information.' , 'error' )
2018-01-23 20:06:30 +00:00
} )
2018-03-19 16:51:39 +00:00
} )
2018-01-23 20:06:30 +00:00
}
2018-03-28 11:36:28 +00:00
panel . getAlbums = ( ) => {
axios . get ( 'api/albums' ) . then ( response => {
2018-01-23 20:06:30 +00:00
if ( response . data . success === false ) {
2018-03-28 17:40:50 +00:00
if ( response . data . description === 'No token provided' ) {
return panel . verifyToken ( panel . token )
} else {
return swal ( 'An error occurred' , response . data . description , 'error' )
}
2018-01-23 20:06:30 +00:00
}
2018-03-19 16:51:39 +00:00
panel . page . innerHTML = `
2018-01-23 20:06:30 +00:00
< h2 class = "subtitle" > Create new album < / h 2 >
2018-03-19 16:51:39 +00:00
< div class = "field has-addons has-addons-centered" >
< div class = "control is-expanded" >
< input id = "albumName" class = "input" type = "text" placeholder = "Name" >
< / d i v >
< div class = "control" >
< a id = "submitAlbum" class = "button is-primary" > Submit < / a >
< / d i v >
< / d i v >
2018-01-23 20:06:30 +00:00
< h2 class = "subtitle" > List of albums < / h 2 >
2018-03-19 16:51:39 +00:00
< div class = "table-container" >
< table class = "table is-narrow is-fullwidth is-hoverable" >
< thead >
< tr >
< th > Name < / t h >
< th > Files < / t h >
< th > Created At < / t h >
< th > Public link < / t h >
< th > < / t h >
< / t r >
< / t h e a d >
< tbody id = "table" >
< / t b o d y >
< / t a b l e >
< / d i v >
`
2018-01-23 20:06:30 +00:00
2018-03-28 11:36:28 +00:00
const table = document . getElementById ( 'table' )
2018-01-23 20:06:30 +00:00
2018-03-28 11:36:28 +00:00
for ( const item of response . data . albums ) {
const tr = document . createElement ( 'tr' )
2018-01-23 20:06:30 +00:00
tr . innerHTML = `
< tr >
< th > $ { item . name } < / t h >
< th > $ { item . files } < / t h >
< td > $ { item . date } < / t d >
2018-03-19 16:51:39 +00:00
< td > < a href = "${item.identifier}" target = "_blank" > $ { item . identifier } < / a > < / t d >
2018-03-28 20:05:01 +00:00
< td style = "text-align: right" >
2018-01-23 20:06:30 +00:00
< a class = "button is-small is-primary is-outlined" title = "Edit name" onclick = "panel.renameAlbum(${item.id})" >
< span class = "icon is-small" >
2018-03-28 20:05:01 +00:00
< i class = "icon-edit" > < / i >
< / s p a n >
< / a >
< a class = "button is-small is-info is-outlined clipboard-js" title = "Copy link to clipboard" data - clipboard - text = "${item.identifier}" >
< span class = "icon is-small" >
< i class = "icon-attach" > < / i >
2018-01-23 20:06:30 +00:00
< / s p a n >
< / a >
< a class = "button is-small is-danger is-outlined" title = "Delete album" onclick = "panel.deleteAlbum(${item.id})" >
< span class = "icon is-small" >
2018-03-28 20:05:01 +00:00
< i class = "icon-trash-1" > < / i >
2018-01-23 20:06:30 +00:00
< / s p a n >
< / a >
< / t d >
< / t r >
2018-03-19 16:51:39 +00:00
`
2018-01-23 20:06:30 +00:00
table . appendChild ( tr )
}
document . getElementById ( 'submitAlbum' ) . addEventListener ( 'click' , function ( ) {
2018-03-28 17:40:50 +00:00
panel . submitAlbum ( this )
2018-01-23 20:06:30 +00:00
} )
} )
2018-03-28 11:36:28 +00:00
. catch ( err => {
console . log ( err )
2018-03-19 16:51:39 +00:00
return swal ( 'An error occurred' , 'There was an error with the request, please check the console for more information.' , 'error' )
} )
2018-01-23 20:06:30 +00:00
}
2018-03-28 11:36:28 +00:00
panel . renameAlbum = id => {
2018-01-23 20:06:30 +00:00
swal ( {
title : 'Rename album' ,
text : 'New name you want to give the album:' ,
2018-03-19 16:51:39 +00:00
icon : 'info' ,
content : {
element : 'input' ,
attributes : {
placeholder : 'My super album'
}
} ,
buttons : {
cancel : true ,
confirm : {
closeModal : false
}
2018-01-23 20:06:30 +00:00
}
2018-03-19 16:51:39 +00:00
} ) . then ( value => {
2018-03-28 17:40:50 +00:00
if ( ! value ) { return swal . close ( ) }
2018-01-24 15:31:23 +00:00
axios . post ( 'api/albums/rename' , {
2018-01-23 20:06:30 +00:00
id : id ,
2018-03-19 16:51:39 +00:00
name : value
2018-01-23 20:06:30 +00:00
} )
2018-03-28 11:36:28 +00:00
. then ( response => {
2018-03-19 16:51:39 +00:00
if ( response . data . success === false ) {
2018-03-28 17:40:50 +00:00
if ( response . data . description === 'No token provided' ) { return panel . verifyToken ( panel . token ) } else if ( response . data . description === 'Name already in use' ) { swal . showInputError ( 'That name is already in use!' ) } else { swal ( 'An error occurred' , response . data . description , 'error' ) }
2018-03-19 16:51:39 +00:00
return
}
2018-01-23 20:06:30 +00:00
2018-03-19 16:51:39 +00:00
swal ( 'Success!' , 'Your album was renamed to: ' + value , 'success' )
panel . getAlbumsSidebar ( )
panel . getAlbums ( )
} )
2018-03-28 11:36:28 +00:00
. catch ( err => {
console . log ( err )
2018-03-19 16:51:39 +00:00
return swal ( 'An error occurred' , 'There was an error with the request, please check the console for more information.' , 'error' )
} )
2018-01-23 20:06:30 +00:00
} )
}
2018-03-28 11:36:28 +00:00
panel . deleteAlbum = id => {
2018-01-23 20:06:30 +00:00
swal ( {
title : 'Are you sure?' ,
2018-03-19 16:51:39 +00:00
text : 'This won\'t delete your files, only the album!' ,
icon : 'warning' ,
dangerMode : true ,
buttons : {
cancel : true ,
confirm : {
text : 'Yes, delete it!' ,
closeModal : false
}
}
} ) . then ( value => {
2018-03-28 17:40:50 +00:00
if ( ! value ) { return }
2018-03-19 16:51:39 +00:00
axios . post ( 'api/albums/delete' , {
id : id
} )
2018-03-28 11:36:28 +00:00
. then ( response => {
2018-01-23 20:06:30 +00:00
if ( response . data . success === false ) {
2018-03-28 17:40:50 +00:00
if ( response . data . description === 'No token provided' ) {
return panel . verifyToken ( panel . token )
} else {
return swal ( 'An error occurred' , response . data . description , 'error' )
}
2018-01-23 20:06:30 +00:00
}
swal ( 'Deleted!' , 'Your album has been deleted.' , 'success' )
panel . getAlbumsSidebar ( )
panel . getAlbums ( )
} )
2018-03-28 11:36:28 +00:00
. catch ( err => {
console . log ( err )
2018-02-24 19:33:31 +00:00
return swal ( 'An error occurred' , 'There was an error with the request, please check the console for more information.' , 'error' )
2018-01-23 20:06:30 +00:00
} )
2018-03-19 16:51:39 +00:00
} )
2018-01-23 20:06:30 +00:00
}
2018-03-28 17:40:50 +00:00
panel . submitAlbum = element => {
panel . isLoading ( element , true )
2018-01-24 15:31:23 +00:00
axios . post ( 'api/albums' , {
2018-01-23 20:06:30 +00:00
name : document . getElementById ( 'albumName' ) . value
} )
2018-03-28 17:40:50 +00:00
. then ( async response => {
panel . setLoading ( element , false )
2018-03-19 16:51:39 +00:00
if ( response . data . success === false ) {
2018-03-28 17:40:50 +00:00
if ( response . data . description === 'No token provided' ) {
return panel . verifyToken ( panel . token )
} else {
return swal ( 'An error occurred' , response . data . description , 'error' )
}
2018-03-19 16:51:39 +00:00
}
2018-01-23 20:06:30 +00:00
2018-03-19 16:51:39 +00:00
swal ( 'Woohoo!' , 'Album was added successfully' , 'success' )
panel . getAlbumsSidebar ( )
panel . getAlbums ( )
} )
2018-03-28 11:36:28 +00:00
. catch ( err => {
console . log ( err )
2018-03-28 17:40:50 +00:00
panel . setLoading ( element , false )
2018-03-19 16:51:39 +00:00
return swal ( 'An error occurred' , 'There was an error with the request, please check the console for more information.' , 'error' )
} )
2018-01-23 20:06:30 +00:00
}
2018-03-28 11:36:28 +00:00
panel . getAlbumsSidebar = ( ) => {
2018-01-24 15:31:23 +00:00
axios . get ( 'api/albums/sidebar' )
2018-03-28 11:36:28 +00:00
. then ( response => {
2018-03-19 16:51:39 +00:00
if ( response . data . success === false ) {
2018-03-28 17:40:50 +00:00
if ( response . data . description === 'No token provided' ) {
return panel . verifyToken ( panel . token )
} else {
return swal ( 'An error occurred' , response . data . description , 'error' )
}
2018-03-19 16:51:39 +00:00
}
2018-01-23 20:06:30 +00:00
2018-03-28 11:36:28 +00:00
const albumsContainer = document . getElementById ( 'albumsContainer' )
2018-03-19 16:51:39 +00:00
albumsContainer . innerHTML = ''
2018-01-23 20:06:30 +00:00
2018-03-28 17:40:50 +00:00
if ( response . data . albums === undefined ) { return }
2018-01-23 20:06:30 +00:00
2018-03-28 11:36:28 +00:00
for ( const album of response . data . albums ) {
const li = document . createElement ( 'li' )
const a = document . createElement ( 'a' )
2018-03-19 16:51:39 +00:00
a . id = album . id
a . innerHTML = album . name
2018-01-23 20:06:30 +00:00
2018-03-19 16:51:39 +00:00
a . addEventListener ( 'click' , function ( ) {
panel . getAlbum ( this )
} )
2018-01-23 20:06:30 +00:00
2018-03-19 16:51:39 +00:00
li . appendChild ( a )
albumsContainer . appendChild ( li )
}
} )
2018-03-28 11:36:28 +00:00
. catch ( err => {
console . log ( err )
2018-03-19 16:51:39 +00:00
return swal ( 'An error occurred' , 'There was an error with the request, please check the console for more information.' , 'error' )
} )
2018-01-23 20:06:30 +00:00
}
2018-03-28 11:36:28 +00:00
panel . getAlbum = item => {
2018-01-23 20:06:30 +00:00
panel . setActiveMenu ( item )
panel . getUploads ( item . id )
}
2018-03-28 11:36:28 +00:00
panel . changeFileLength = ( ) => {
axios . get ( 'api/filelength/config' )
. then ( response => {
2018-03-24 13:52:47 +00:00
if ( response . data . success === false ) {
2018-03-28 17:40:50 +00:00
if ( response . data . description === 'No token provided' ) {
return panel . verifyToken ( panel . token )
} else {
return swal ( 'An error occurred' , response . data . description , 'error' )
}
2018-03-24 13:52:47 +00:00
}
panel . page . innerHTML = `
2018-03-24 16:45:51 +00:00
< h2 class = "subtitle" > Preferred file name length < / h 2 >
2018-03-24 13:52:47 +00:00
< div class = "field" >
2018-03-24 16:45:51 +00:00
< label class = "label" > Your current file name length : < / l a b e l >
2018-03-24 13:52:47 +00:00
< div class = "field has-addons" >
< div class = "control is-expanded" >
< input id = "fileLength" class = "input" type = "text" placeholder = "Your file length" value = "${response.data.fileLength ? Math.min(Math.max(response.data.fileLength, response.data.config.min), response.data.config.max) : response.data.config.default}" >
< / d i v >
< div class = "control" >
2018-03-24 16:45:51 +00:00
< a id = "setFileLength" class = "button is-primary" > Set file name length < / a >
2018-03-24 13:52:47 +00:00
< / d i v >
< / d i v >
2018-03-24 16:45:51 +00:00
< p class = "help" > Default file name length is < b > $ { response . data . config . default } < / b > c h a r a c t e r s . $ { r e s p o n s e . d a t a . c o n f i g . u s e r C h a n g e a b l e ? ` R a n g e a l l o w e d f o r u s e r i s < b > $ { r e s p o n s e . d a t a . c o n f i g . m i n } < / b > t o < b > $ { r e s p o n s e . d a t a . c o n f i g . m a x } < / b > c h a r a c t e r s . ` : ' C h a n g i n g f i l e n a m e l e n g t h i s d i s a b l e d a t t h e m o m e n t . ' } < / p >
2018-03-24 13:52:47 +00:00
< / d i v >
`
document . getElementById ( 'setFileLength' ) . addEventListener ( 'click' , function ( ) {
2018-03-28 17:40:50 +00:00
panel . setFileLength ( document . getElementById ( 'fileLength' ) . value , this )
2018-03-24 13:52:47 +00:00
} )
} )
2018-03-28 11:36:28 +00:00
. catch ( err => {
console . log ( err )
2018-03-24 13:52:47 +00:00
return swal ( 'An error occurred' , 'There was an error with the request, please check the console for more information.' , 'error' )
} )
}
2018-03-28 17:40:50 +00:00
panel . setFileLength = ( fileLength , element ) => {
panel . isLoading ( element , true )
2018-03-28 11:36:28 +00:00
axios . post ( 'api/filelength/change' , { fileLength } )
. then ( response => {
2018-03-28 17:40:50 +00:00
panel . isLoading ( element , false )
2018-03-24 13:52:47 +00:00
if ( response . data . success === false ) {
2018-03-28 17:40:50 +00:00
if ( response . data . description === 'No token provided' ) {
return panel . verifyToken ( panel . token )
} else {
return swal ( 'An error occurred' , response . data . description , 'error' )
}
2018-03-24 13:52:47 +00:00
}
swal ( {
title : 'Woohoo!' ,
text : 'Your file length was successfully changed.' ,
icon : 'success'
} ) . then ( ( ) => {
location . reload ( )
} )
} )
2018-03-28 11:36:28 +00:00
. catch ( err => {
console . log ( err )
2018-03-28 17:40:50 +00:00
panel . isLoading ( element , false )
2018-03-24 13:52:47 +00:00
return swal ( 'An error occurred' , 'There was an error with the request, please check the console for more information.' , 'error' )
} )
}
2018-03-28 11:36:28 +00:00
panel . changeToken = ( ) => {
2018-01-24 15:31:23 +00:00
axios . get ( 'api/tokens' )
2018-03-28 11:36:28 +00:00
. then ( response => {
2018-03-19 16:51:39 +00:00
if ( response . data . success === false ) {
2018-03-28 17:40:50 +00:00
if ( response . data . description === 'No token provided' ) {
return panel . verifyToken ( panel . token )
} else {
return swal ( 'An error occurred' , response . data . description , 'error' )
}
2018-03-19 16:51:39 +00:00
}
2018-01-23 20:06:30 +00:00
2018-03-19 16:51:39 +00:00
panel . page . innerHTML = `
< h2 class = "subtitle" > Manage your token < / h 2 >
< div class = "field" >
< label class = "label" > Your current token : < / l a b e l >
< div class = "field has-addons" >
< div class = "control is-expanded" >
< input id = "token" readonly class = "input" type = "text" placeholder = "Your token" value = "${response.data.token}" >
< / d i v >
< div class = "control" >
< a id = "getNewToken" class = "button is-primary" > Request new token < / a >
< / d i v >
< / d i v >
< / d i v >
`
2018-01-23 20:06:30 +00:00
2018-03-19 16:51:39 +00:00
document . getElementById ( 'getNewToken' ) . addEventListener ( 'click' , function ( ) {
2018-03-28 17:40:50 +00:00
panel . getNewToken ( this )
2018-03-19 16:51:39 +00:00
} )
} )
2018-03-28 11:36:28 +00:00
. catch ( err => {
console . log ( err )
2018-03-19 16:51:39 +00:00
return swal ( 'An error occurred' , 'There was an error with the request, please check the console for more information.' , 'error' )
2018-01-23 20:06:30 +00:00
} )
}
2018-03-28 17:40:50 +00:00
panel . getNewToken = element => {
panel . isLoading ( element , true )
2018-01-24 15:31:23 +00:00
axios . post ( 'api/tokens/change' )
2018-03-28 11:36:28 +00:00
. then ( response => {
2018-03-28 17:40:50 +00:00
panel . isLoading ( element , false )
2018-03-19 16:51:39 +00:00
if ( response . data . success === false ) {
2018-03-28 17:40:50 +00:00
if ( response . data . description === 'No token provided' ) {
return panel . verifyToken ( panel . token )
} else {
return swal ( 'An error occurred' , response . data . description , 'error' )
}
2018-03-19 16:51:39 +00:00
}
2018-01-23 20:06:30 +00:00
2018-03-19 16:51:39 +00:00
swal ( {
title : 'Woohoo!' ,
2018-03-24 13:52:47 +00:00
text : 'Your token was successfully changed.' ,
2018-03-19 16:51:39 +00:00
icon : 'success'
} ) . then ( ( ) => {
localStorage . token = response . data . token
location . reload ( )
} )
} )
2018-03-28 11:36:28 +00:00
. catch ( err => {
console . log ( err )
2018-03-28 17:40:50 +00:00
panel . isLoading ( element , false )
2018-03-19 16:51:39 +00:00
return swal ( 'An error occurred' , 'There was an error with the request, please check the console for more information.' , 'error' )
2018-01-23 20:06:30 +00:00
} )
}
2018-03-28 11:36:28 +00:00
panel . changePassword = ( ) => {
2018-03-19 16:51:39 +00:00
panel . page . innerHTML = `
2018-01-23 20:06:30 +00:00
< h2 class = "subtitle" > Change your password < / h 2 >
2018-03-19 16:51:39 +00:00
< div class = "field" >
< label class = "label" > New password : < / l a b e l >
< div class = "control" >
< input id = "password" class = "input" type = "password" placeholder = "Your new password" >
< / d i v >
< / d i v >
< div class = "field" >
< label class = "label" > Confirm password : < / l a b e l >
< div class = "field has-addons" >
< div class = "control is-expanded" >
< input id = "passwordConfirm" class = "input is-expanded" type = "password" placeholder = "Verify your new password" >
< / d i v >
< div class = "control" >
< a id = "sendChangePassword" class = "button is-primary" > Set new password < / a >
< / d i v >
< / d i v >
< / d i v >
2018-01-23 20:06:30 +00:00
`
document . getElementById ( 'sendChangePassword' ) . addEventListener ( 'click' , function ( ) {
if ( document . getElementById ( 'password' ) . value === document . getElementById ( 'passwordConfirm' ) . value ) {
2018-03-28 17:40:50 +00:00
panel . sendNewPassword ( document . getElementById ( 'password' ) . value , this )
2018-01-23 20:06:30 +00:00
} else {
swal ( {
title : 'Password mismatch!' ,
text : 'Your passwords do not match, please try again.' ,
2018-03-19 16:51:39 +00:00
icon : 'error'
} ) . then ( ( ) => {
2018-01-23 20:06:30 +00:00
panel . changePassword ( )
} )
}
} )
}
2018-03-28 17:40:50 +00:00
panel . sendNewPassword = ( pass , element ) => {
panel . isLoading ( element , true )
axios . post ( 'api/password/change' , { password : pass } )
2018-03-28 11:36:28 +00:00
. then ( response => {
2018-03-28 17:40:50 +00:00
panel . isLoading ( element , false )
2018-03-19 16:51:39 +00:00
if ( response . data . success === false ) {
2018-03-28 17:40:50 +00:00
if ( response . data . description === 'No token provided' ) {
return panel . verifyToken ( panel . token )
} else {
return swal ( 'An error occurred' , response . data . description , 'error' )
}
2018-03-19 16:51:39 +00:00
}
2018-01-23 20:06:30 +00:00
2018-03-19 16:51:39 +00:00
swal ( {
title : 'Woohoo!' ,
2018-03-24 13:52:47 +00:00
text : 'Your password was successfully changed.' ,
2018-03-19 16:51:39 +00:00
icon : 'success'
} ) . then ( ( ) => {
location . reload ( )
} )
} )
2018-03-28 11:36:28 +00:00
. catch ( err => {
console . log ( err )
2018-03-28 17:40:50 +00:00
panel . isLoading ( element , false )
2018-03-19 16:51:39 +00:00
return swal ( 'An error occurred' , 'There was an error with the request, please check the console for more information.' , 'error' )
2018-01-23 20:06:30 +00:00
} )
}
2018-03-28 11:36:28 +00:00
panel . setActiveMenu = item => {
const menu = document . getElementById ( 'menu' )
const items = menu . getElementsByTagName ( 'a' )
for ( let i = 0 ; i < items . length ; i ++ ) {
items [ i ] . className = ''
}
2018-01-23 20:06:30 +00:00
item . className = 'is-active'
}
2018-03-28 11:36:28 +00:00
window . onload = ( ) => {
// Add 'no-touch' class to non-touch devices
if ( ! ( 'ontouchstart' in document . documentElement ) ) {
document . documentElement . className += ' no-touch'
}
2018-03-28 20:05:01 +00:00
2018-01-23 20:06:30 +00:00
panel . preparePage ( )
2018-03-28 20:05:01 +00:00
panel . clipboardJS = new ClipboardJS ( '.clipboard-js' )
panel . clipboardJS . on ( 'success' , ( ) => {
return swal ( 'Copied!' , 'The link has been copied to clipboard.' , 'success' )
} )
panel . clipboardJS . on ( 'error' , event => {
console . error ( event )
return swal ( 'An error occurred' , 'There was an error when trying to copy the link to clipboard, please check the console for more information.' , 'error' )
} )
2018-01-23 20:06:30 +00:00
}