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 ,
2018-03-29 23:22:08 +00:00
clipboardJS : undefined ,
2018-03-30 02:39:53 +00:00
selectedFiles : [ ] ,
2018-04-04 18:23:45 +00:00
selectAlbumContainer : undefined ,
checkboxes : undefined ,
lastSelected : 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 ( {
2018-03-30 02:39:53 +00:00
title : 'An error occurred!' ,
2018-03-19 16:51:39 +00:00
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-29 23:22:08 +00:00
. catch ( error => {
console . log ( error )
2018-03-30 02:39:53 +00:00
return swal ( 'An error occurred!' , 'There was an error with the request, please check the console for more information.' , '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
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 {
2018-03-30 02:39:53 +00:00
return swal ( 'An error occurred!' , response . data . description , 'error' )
2018-03-28 17:40:50 +00:00
}
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-29 23:22:08 +00:00
const controls = `
2018-03-19 16:51:39 +00:00
< div class = "columns" >
2018-03-29 23:47:31 +00:00
< div class = "column is-hidden-mobile" > < / d i v >
2018-03-29 23:22:08 +00:00
< div class = "column" style = "text-align: center" >
< a class = "button is-small is-danger" title = "List view" onclick = "panel.setFilesView('list', ${album}, ${page}, this)" >
< span class = "icon" >
2018-04-03 18:54:42 +00:00
< i class = "icon-th-list" > < / i >
2018-03-19 16:51:39 +00:00
< / s p a n >
< / a >
2018-03-29 23:22:08 +00:00
< a class = "button is-small is-danger" title = "Thumbs view" onclick = "panel.setFilesView('thumbs', ${album}, ${page}, this)" >
< span class = "icon" >
2018-04-03 18:54:42 +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-03-29 23:22:08 +00:00
< div class = "column" style = "text-align: right" >
2018-04-03 15:59:39 +00:00
< a class = "button is-small is-info" title = "Clear selection" onclick = "panel.clearSelection()" >
< span class = "icon" >
< i class = "icon-cancel" > < / i >
< / s p a n >
< / a >
< a class = "button is-small is-warning" title = "Add selected files to album" onclick = "panel.addSelectedFilesToAlbum(${album})" >
2018-03-30 02:39:53 +00:00
< span class = "icon" >
< i class = "icon-plus" > < / i >
< / s p a n >
< / a >
< a class = "button is-small is-danger" title = "Bulk delete" onclick = "panel.deleteSelectedFiles(${album})" >
2018-03-29 23:22:08 +00:00
< span class = "icon" >
< i class = "icon-trash" > < / i >
< / s p a n >
2018-03-30 02:39:53 +00:00
< span > Bulk delete < / s p a n >
2018-03-29 23:22:08 +00:00
< / 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
2018-03-29 23:47:31 +00:00
let allFilesSelected = true
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 >
2018-03-29 23:22:08 +00:00
$ { controls }
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' )
2018-03-30 02:39:53 +00:00
for ( const file of response . data . files ) {
const selected = panel . selectedFiles . includes ( file . id )
2018-03-29 23:47:31 +00:00
if ( ! selected && allFilesSelected ) { allFilesSelected = false }
2018-03-28 11:36:28 +00:00
const div = document . createElement ( 'div' )
2018-03-30 02:39:53 +00:00
let displayAlbumOrUser = file . album
2018-03-28 11:36:28 +00:00
if ( panel . username === 'root' ) {
displayAlbumOrUser = ''
2018-03-30 02:39:53 +00:00
if ( file . username !== undefined ) { displayAlbumOrUser = file . username }
2018-03-28 11:36:28 +00:00
}
2018-01-23 20:06:30 +00:00
2018-03-24 16:45:51 +00:00
div . className = 'image-container column is-narrow'
2018-03-30 02:39:53 +00:00
if ( file . thumb !== undefined ) {
div . innerHTML = ` <a class="image" href=" ${ file . file } " target="_blank"><img src=" ${ file . thumb } "/></a> `
2018-01-23 20:06:30 +00:00
} else {
2018-03-30 02:39:53 +00:00
div . innerHTML = ` <a class="image" href=" ${ file . file } " target="_blank"><h1 class="title">. ${ file . file . split ( '.' ) . pop ( ) } </h1></a> `
2018-01-23 20:06:30 +00:00
}
2018-03-24 16:45:51 +00:00
div . innerHTML += `
2018-04-04 18:23:45 +00:00
< input type = "checkbox" class = "file-checkbox" title = "Select this file" data - id = "${file.id}" onclick = "panel.selectFile(this, event)" $ { selected ? ' checked' : '' } >
2018-03-29 23:22:08 +00:00
< div class = "controls" >
2018-03-30 02:39:53 +00:00
< a class = "button is-small is-info clipboard-js" title = "Copy link to clipboard" data - clipboard - text = "${file.file}" >
2018-03-29 23:22:08 +00:00
< span class = "icon" >
2018-04-03 18:54:42 +00:00
< i class = "icon-clipboard-1" > < / i >
2018-03-29 23:22:08 +00:00
< / s p a n >
< / a >
2018-03-30 02:39:53 +00:00
< a class = "button is-small is-warning" title = "Add to album" onclick = "panel.addToAlbum([${file.id}], ${album})" >
< span class = "icon" >
< i class = "icon-plus" > < / i >
< / s p a n >
< / a >
< a class = "button is-small is-danger" title = "Delete file" onclick = "panel.deleteFile(${file.id}, ${album}, ${page})" >
2018-03-29 23:22:08 +00:00
< span class = "icon" >
< i class = "icon-trash" > < / i >
< / s p a n >
< / a >
< / d i v >
2018-03-31 14:44:06 +00:00
< div class = "details" >
2018-04-04 17:38:15 +00:00
< p > < span class = "name" title = "${file.file}" > $ { file . name } < / s p a n > < / p >
2018-03-30 02:39:53 +00:00
< p > $ { displayAlbumOrUser ? ` <span> ${ displayAlbumOrUser } </span> – ` : '' } $ { file . size } < / d i v >
2018-03-24 16:45:51 +00:00
`
2018-01-23 20:06:30 +00:00
table . appendChild ( div )
2018-04-04 18:23:45 +00:00
panel . checkboxes = Array . from ( table . getElementsByClassName ( 'file-checkbox' ) )
2018-01-23 20:06:30 +00:00
}
} 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 >
2018-03-29 23:22:08 +00:00
$ { controls }
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 >
2018-03-29 23:22:08 +00:00
< th > < input id = "selectAll" type = "checkbox" title = "Select all files" onclick = "panel.selectAllFiles(this)" > < / t h >
2018-04-03 19:05:15 +00:00
< th style = "width: 25%" > File < / t h >
2018-01-24 15:31:23 +00:00
< 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-30 02:39:53 +00:00
for ( const file of response . data . files ) {
const selected = panel . selectedFiles . includes ( file . id )
2018-03-29 23:22:08 +00:00
if ( ! selected && allFilesSelected ) { allFilesSelected = false }
2018-03-28 11:36:28 +00:00
const tr = document . createElement ( 'tr' )
2018-01-23 20:06:30 +00:00
2018-03-30 02:39:53 +00:00
let displayAlbumOrUser = file . album
2018-01-23 20:06:30 +00:00
if ( panel . username === 'root' ) {
displayAlbumOrUser = ''
2018-03-30 02:39:53 +00:00
if ( file . username !== undefined ) { displayAlbumOrUser = file . username }
2018-01-23 20:06:30 +00:00
}
tr . innerHTML = `
< tr >
2018-04-04 18:23:45 +00:00
< th > < input type = "checkbox" class = "file-checkbox" title = "Select this file" data - id = "${file.id}" onclick = "panel.selectFile(this, event)" $ { selected ? ' checked' : '' } > < / t h >
2018-04-04 16:47:20 +00:00
< th > < a href = "${file.file}" target = "_blank" title = "${file.file}" > $ { file . name } < / a > < / t h >
2018-01-23 20:06:30 +00:00
< th > $ { displayAlbumOrUser } < / t h >
2018-03-30 02:39:53 +00:00
< td > $ { file . size } < / t d >
< td > $ { file . date } < / t d >
2018-03-28 20:05:01 +00:00
< td style = "text-align: right" >
2018-03-31 14:26:53 +00:00
< a class = "button is-small is-primary" title = "View thumbnail" onclick = "panel.displayThumbnailModal(${file.thumb ? `'${file.thumb}'` : null})" $ { file . thumb ? '' : ' disabled' } >
2018-03-29 23:22:08 +00:00
< span class = "icon" >
< i class = "icon-picture-1" > < / i >
2018-03-28 20:05:01 +00:00
< / s p a n >
< / a >
2018-03-30 02:39:53 +00:00
< a class = "button is-small is-info clipboard-js" title = "Copy link to clipboard" data - clipboard - text = "${file.file}" >
2018-03-29 23:22:08 +00:00
< span class = "icon" >
2018-04-03 18:54:42 +00:00
< i class = "icon-clipboard-1" > < / i >
2018-03-29 23:22:08 +00:00
< / s p a n >
< / a >
2018-03-30 02:39:53 +00:00
< a class = "button is-small is-warning" title = "Add to album" onclick = "panel.addToAlbum([${file.id}])" >
< span class = "icon" >
< i class = "icon-plus" > < / i >
< / s p a n >
< / a >
< a class = "button is-small is-danger" title = "Delete file" onclick = "panel.deleteFile(${file.id}, ${album}, ${page})" >
2018-03-29 23:22:08 +00:00
< span class = "icon" >
< i class = "icon-trash" > < / 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-04-04 18:23:45 +00:00
panel . checkboxes = Array . from ( table . getElementsByClassName ( 'file-checkbox' ) )
2018-03-29 23:22:08 +00:00
}
2018-03-29 23:47:31 +00:00
}
2018-03-28 12:28:17 +00:00
2018-03-29 23:47:31 +00:00
if ( allFilesSelected && response . data . files . length ) {
const selectAll = document . getElementById ( 'selectAll' )
if ( selectAll ) { selectAll . checked = true }
2018-01-23 20:06:30 +00:00
}
2018-03-29 23:22:08 +00:00
} ) . catch ( error => {
console . log ( error )
2018-03-30 02:39:53 +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-29 23:22:08 +00:00
panel . displayThumbnailModal = thumb => {
2018-03-30 02:39:53 +00:00
if ( ! thumb ) { return }
2018-03-29 23:22:08 +00:00
document . getElementById ( 'modalImage' ) . src = thumb
document . getElementById ( 'modal' ) . className += ' is-active'
}
panel . selectAllFiles = element => {
const table = document . getElementById ( 'table' )
const checkboxes = table . getElementsByClassName ( 'file-checkbox' )
2018-04-03 15:59:39 +00:00
2018-03-29 23:22:08 +00:00
for ( const checkbox of checkboxes ) {
2018-04-03 15:59:39 +00:00
const id = parseInt ( checkbox . dataset . id )
if ( isNaN ( id ) ) { continue }
2018-03-29 23:22:08 +00:00
if ( checkbox . checked !== element . checked ) {
2018-04-03 15:59:39 +00:00
checkbox . checked = element . checked
if ( checkbox . checked ) {
panel . selectedFiles . push ( id )
} else {
panel . selectedFiles . splice ( panel . selectedFiles . indexOf ( id ) , 1 )
}
2018-03-29 23:22:08 +00:00
}
}
2018-04-03 15:59:39 +00:00
if ( panel . selectedFiles . length ) {
localStorage . selectedFiles = JSON . stringify ( panel . selectedFiles )
} else {
localStorage . removeItem ( 'selectedFiles' )
}
2018-03-29 23:22:08 +00:00
element . title = element . checked ? 'Unselect all files' : 'Select all files'
}
2018-04-04 18:23:45 +00:00
panel . selectInBetween = ( element , lastElement ) => {
if ( ! element || ! lastElement ) { return }
if ( element === lastElement ) { return }
if ( ! panel . checkboxes || ! panel . checkboxes . length ) { return }
let thisIndex = panel . checkboxes . indexOf ( element )
let lastIndex = panel . checkboxes . indexOf ( lastElement )
const distance = thisIndex - lastIndex
if ( distance >= - 1 && distance <= 1 ) { return }
for ( let i = 0 ; i < panel . checkboxes . length ; i ++ ) {
if ( ( thisIndex > lastIndex && i > lastIndex && i < thisIndex ) ||
( thisIndex < lastIndex && i > thisIndex && i < lastIndex ) ) {
panel . checkboxes [ i ] . checked = true
panel . selectedFiles . push ( parseInt ( panel . checkboxes [ i ] . dataset . id ) )
}
}
localStorage . selectedFiles = JSON . stringify ( panel . selectedFiles )
}
panel . selectFile = ( element , event ) => {
if ( event . shiftKey && panel . lastSelected ) {
panel . selectInBetween ( element , panel . lastSelected )
} else {
panel . lastSelected = element
}
2018-04-03 15:59:39 +00:00
const id = parseInt ( element . dataset . id )
if ( isNaN ( id ) ) { return }
2018-03-29 23:22:08 +00:00
if ( ! panel . selectedFiles . includes ( id ) && element . checked ) {
panel . selectedFiles . push ( id )
} else if ( panel . selectedFiles . includes ( id ) && ! element . checked ) {
panel . selectedFiles . splice ( panel . selectedFiles . indexOf ( id ) , 1 )
2018-04-03 15:59:39 +00:00
}
if ( panel . selectedFiles . length ) {
2018-03-29 23:22:08 +00:00
localStorage . selectedFiles = JSON . stringify ( panel . selectedFiles )
2018-04-03 15:59:39 +00:00
} else {
localStorage . removeItem ( 'selectedFiles' )
2018-03-29 23:22:08 +00:00
}
}
2018-04-03 15:59:39 +00:00
panel . clearSelection = async ( ) => {
const count = panel . selectedFiles . length
if ( ! count ) {
return swal ( 'An error occurred!' , 'You have not selected any files.' , 'error' )
}
const suffix = ` file ${ count === 1 ? '' : 's' } `
const proceed = await swal ( {
title : 'Are you sure?' ,
text : ` You are going to unselect ${ count } ${ suffix } . ` ,
buttons : true
} )
if ( ! proceed ) { return }
const table = document . getElementById ( 'table' )
const checkboxes = table . getElementsByClassName ( 'file-checkbox' )
for ( const checkbox of checkboxes ) {
if ( checkbox . checked ) {
checkbox . checked = false
}
}
panel . selectedFiles = [ ]
localStorage . removeItem ( 'selectedFiles' )
const selectAll = document . getElementById ( 'selectAll' )
if ( selectAll ) { selectAll . checked = false }
return swal ( 'Cleared selection!' , ` Unselected ${ count } ${ suffix } . ` , 'success' )
}
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 {
2018-03-30 02:39:53 +00:00
return swal ( 'An error occurred!' , response . data . description , 'error' )
2018-03-28 17:40:50 +00:00
}
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-29 23:22:08 +00:00
. catch ( error => {
console . log ( error )
2018-03-30 02:39:53 +00:00
return swal ( 'An error occurred!' , 'There was an error with the request, please check the console for more information.' , 'error' )
2018-03-29 23:22:08 +00:00
} )
} )
}
2018-03-31 16:34:16 +00:00
panel . deleteSelectedFiles = async album => {
2018-03-29 23:22:08 +00:00
const count = panel . selectedFiles . length
if ( ! count ) {
2018-03-30 02:39:53 +00:00
return swal ( 'An error occurred!' , 'You have not selected any files.' , 'error' )
2018-03-29 23:22:08 +00:00
}
const suffix = ` file ${ count === 1 ? '' : 's' } `
2018-03-31 14:26:53 +00:00
const proceed = await swal ( {
2018-03-29 23:22:08 +00:00
title : 'Are you sure?' ,
text : ` You won't be able to recover ${ count } ${ suffix } ! ` ,
icon : 'warning' ,
dangerMode : true ,
buttons : {
cancel : true ,
confirm : {
text : ` Yes, nuke the ${ suffix } ! ` ,
closeModal : false
}
}
2018-03-31 14:26:53 +00:00
} )
if ( ! proceed ) { return }
const bulkdelete = await axios . post ( 'api/upload/bulkdelete' , {
ids : panel . selectedFiles
} )
. catch ( error => {
console . log ( error )
swal ( 'An error occurred!' , 'There was an error with the request, please check the console for more information.' , 'error' )
2018-03-29 23:22:08 +00:00
} )
2018-03-31 14:26:53 +00:00
if ( ! bulkdelete ) { return }
2018-03-29 23:22:08 +00:00
2018-03-31 14:26:53 +00:00
if ( bulkdelete . data . success === false ) {
if ( bulkdelete . data . description === 'No token provided' ) {
return panel . verifyToken ( panel . token )
} else {
return swal ( 'An error occurred!' , bulkdelete . data . description , 'error' )
}
}
2018-03-29 23:22:08 +00:00
2018-03-31 14:26:53 +00:00
let deleted = count
if ( bulkdelete . data . failedIds && bulkdelete . data . failedIds . length ) {
deleted -= bulkdelete . data . failedIds . length
panel . selectedFiles = panel . selectedFiles . filter ( id => bulkdelete . data . failedIds . includes ( id ) )
} else {
panel . selectedFiles = [ ]
}
2018-03-29 23:22:08 +00:00
2018-03-31 14:26:53 +00:00
localStorage . selectedFiles = JSON . stringify ( panel . selectedFiles )
swal ( 'Deleted!' , ` ${ deleted } file ${ deleted === 1 ? ' has' : 's have' } been deleted. ` , 'success' )
return panel . getUploads ( album )
2018-01-23 20:06:30 +00:00
}
2018-03-31 14:26:53 +00:00
panel . addSelectedFilesToAlbum = async album => {
2018-03-30 02:39:53 +00:00
const count = panel . selectedFiles . length
if ( ! count ) {
return swal ( 'An error occurred!' , 'You have not selected any files.' , 'error' )
}
2018-03-31 14:26:53 +00:00
const failedIds = await panel . addToAlbum ( panel . selectedFiles , album )
2018-03-31 16:34:16 +00:00
if ( ! ( failedIds instanceof Array ) ) { return } // TODO: Make it so that I don't have to do this
if ( failedIds . length ) {
2018-03-31 14:26:53 +00:00
panel . selectedFiles = panel . selectedFiles . filter ( id => failedIds . includes ( id ) )
} else {
panel . selectedFiles = [ ]
}
localStorage . selectedFiles = JSON . stringify ( panel . selectedFiles )
2018-03-30 02:39:53 +00:00
}
panel . addToAlbum = async ( ids , album ) => {
2018-03-31 16:34:16 +00:00
const count = ids . length
2018-03-30 02:39:53 +00:00
const proceed = await swal ( {
title : 'Are you sure?' ,
2018-03-31 16:34:16 +00:00
text : ` You are about to move ${ count } file ${ count === 1 ? '' : 's' } to an album. ` ,
2018-03-30 02:39:53 +00:00
buttons : {
cancel : true ,
confirm : {
text : 'Yes' ,
closeModal : false
}
}
} )
if ( ! proceed ) { return }
const list = await axios . get ( 'api/albums' )
. catch ( error => {
console . log ( error )
swal ( 'An error occurred!' , 'There was an error with the request, please check the console for more information.' , 'error' )
} )
if ( ! list ) { return }
if ( list . data . success === false ) {
if ( list . data . description === 'No token provided' ) {
return panel . verifyToken ( panel . token )
} else {
return swal ( 'An error occurred!' , list . data . description , 'error' )
}
}
if ( ! panel . selectAlbumContainer ) {
// We want to this to be re-usable
panel . selectAlbumContainer = document . createElement ( 'div' )
panel . selectAlbumContainer . id = 'selectAlbum'
panel . selectAlbumContainer . className = 'select is-fullwidth'
}
const options = list . data . albums
. map ( album => ` <option value=" ${ album . id } "> ${ album . name } </option> ` )
. join ( '\n' )
panel . selectAlbumContainer . innerHTML = `
< select >
< option value = "" > Choose an album < / o p t i o n >
< option value = "-1" > Remove from album < / o p t i o n >
$ { options }
< / s e l e c t >
< p class = "help is-danger" > If a file is already in an album , it will be moved . < / p >
`
const choose = await swal ( {
content : panel . selectAlbumContainer ,
buttons : {
cancel : true ,
confirm : {
text : 'OK' ,
closeModal : false
}
}
} )
if ( ! choose ) { return }
let albumid = parseInt ( panel . selectAlbumContainer . getElementsByTagName ( 'select' ) [ 0 ] . value )
if ( isNaN ( albumid ) ) {
return swal ( 'An error occurred!' , 'You did not choose an album.' , 'error' )
}
const add = await axios . post ( 'api/albums/addfiles' , { ids , albumid } )
. catch ( error => {
console . log ( error )
swal ( 'An error occurred!' , 'There was an error with the request, please check the console for more information.' , 'error' )
} )
if ( ! add ) { return }
if ( add . data . success === false ) {
if ( add . data . description === 'No token provided' ) {
return panel . verifyToken ( panel . token )
} else {
return swal ( 'An error occurred!' , add . data . description , 'error' )
}
}
let added = ids . length
if ( add . data . failedIds && add . data . failedIds . length ) {
added -= add . data . failedIds . length
}
const suffix = ` file ${ ids . length === 1 ? '' : 's' } `
if ( ! added ) {
return swal ( 'An error occurred!' , ` Could not add the ${ suffix } to the album. ` , 'error' )
}
swal ( 'Woohoo!' , ` Successfully ${ albumid < 0 ? 'removed' : 'added' } ${ added } ${ suffix } ${ albumid < 0 ? 'from' : 'to' } the album. ` , 'success' )
2018-03-31 14:26:53 +00:00
panel . getUploads ( album )
return add . data . failedIds
2018-03-30 02:39:53 +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 {
2018-03-30 02:39:53 +00:00
return swal ( 'An error occurred!' , response . data . description , 'error' )
2018-03-28 17:40:50 +00:00
}
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" >
2018-03-29 23:22:08 +00:00
< a id = "submitAlbum" class = "button is-breeze" >
< span class = "icon" >
< i class = "icon-paper-plane-empty" > < / i >
< / s p a n >
< span > Submit < / s p a n >
< / a >
2018-03-19 16:51:39 +00:00
< / 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" >
2018-03-29 23:22:08 +00:00
< table class = "table is-fullwidth is-hoverable" >
2018-03-19 16:51:39 +00:00
< thead >
< tr >
< th > Name < / t h >
< th > Files < / t h >
2018-04-04 03:18:53 +00:00
< th > Created at < / t h >
2018-03-19 16:51:39 +00:00
< 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-30 02:39:53 +00:00
for ( const album of response . data . albums ) {
2018-03-28 11:36:28 +00:00
const tr = document . createElement ( 'tr' )
2018-01-23 20:06:30 +00:00
tr . innerHTML = `
< tr >
2018-03-30 02:39:53 +00:00
< th > $ { album . name } < / t h >
< th > $ { album . files } < / t h >
< td > $ { album . date } < / t d >
< td > < a href = "${album.identifier}" target = "_blank" > $ { album . identifier } < / a > < / t d >
2018-03-28 20:05:01 +00:00
< td style = "text-align: right" >
2018-03-30 02:39:53 +00:00
< a class = "button is-small is-primary" title = "Edit name" onclick = "panel.renameAlbum(${album.id})" >
2018-01-23 20:06:30 +00:00
< span class = "icon is-small" >
2018-03-29 23:22:08 +00:00
< i class = "icon-pencil-1" > < / i >
2018-03-28 20:05:01 +00:00
< / s p a n >
< / a >
2018-03-30 02:39:53 +00:00
< a class = "button is-small is-info clipboard-js" title = "Copy link to clipboard" data - clipboard - text = "${album.identifier}" >
2018-03-28 20:05:01 +00:00
< span class = "icon is-small" >
2018-04-03 18:54:42 +00:00
< i class = "icon-clipboard-1" > < / i >
2018-01-23 20:06:30 +00:00
< / s p a n >
< / a >
2018-03-30 02:39:53 +00:00
< a class = "button is-small is-danger" title = "Delete album" onclick = "panel.deleteAlbum(${album.id})" >
2018-01-23 20:06:30 +00:00
< span class = "icon is-small" >
2018-03-29 23:22:08 +00:00
< i class = "icon-trash" > < / 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-29 23:22:08 +00:00
. catch ( error => {
console . log ( error )
2018-03-30 02:39:53 +00:00
return swal ( 'An error occurred!' , 'There was an error with the request, please check the console for more information.' , '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
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-30 02:39:53 +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-29 23:22:08 +00:00
. catch ( error => {
console . log ( error )
2018-03-30 02:39:53 +00:00
return swal ( 'An error occurred!' , 'There was an error with the request, please check the console for more information.' , '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
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
2018-03-30 02:39:53 +00:00
} ,
purge : {
text : 'Umm, delete the files too please?' ,
value : 'purge' ,
className : 'swal-button--danger' ,
closeModal : false
2018-03-19 16:51:39 +00:00
}
}
} ) . 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' , {
2018-03-30 02:39:53 +00:00
id : id ,
purge : value === 'purge'
2018-03-19 16:51:39 +00:00
} )
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 {
2018-03-30 02:39:53 +00:00
return swal ( 'An error occurred!' , response . data . description , 'error' )
2018-03-28 17:40:50 +00:00
}
2018-01-23 20:06:30 +00:00
}
swal ( 'Deleted!' , 'Your album has been deleted.' , 'success' )
panel . getAlbumsSidebar ( )
panel . getAlbums ( )
} )
2018-03-29 23:22:08 +00:00
. catch ( error => {
console . log ( error )
2018-03-30 02:39:53 +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 => {
2018-03-30 02:39:53 +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 {
2018-03-30 02:39:53 +00:00
return swal ( 'An error occurred!' , response . data . description , 'error' )
2018-03-28 17:40:50 +00:00
}
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-29 23:22:08 +00:00
. catch ( error => {
console . log ( error )
2018-03-30 02:39:53 +00:00
panel . isLoading ( element , false )
return swal ( 'An error occurred!' , 'There was an error with the request, please check the console for more information.' , '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
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 {
2018-03-30 02:39:53 +00:00
return swal ( 'An error occurred!' , response . data . description , 'error' )
2018-03-28 17:40:50 +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
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-29 23:22:08 +00:00
. catch ( error => {
console . log ( error )
2018-03-30 02:39:53 +00:00
return swal ( 'An error occurred!' , 'There was an error with the request, please check the console for more information.' , 'error' )
2018-03-19 16:51:39 +00:00
} )
2018-01-23 20:06:30 +00:00
}
2018-03-30 02:39:53 +00:00
panel . getAlbum = album => {
panel . setActiveMenu ( album )
panel . getUploads ( album . id )
2018-01-23 20:06:30 +00:00
}
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 {
2018-03-30 02:39:53 +00:00
return swal ( 'An error occurred!' , response . data . description , 'error' )
2018-03-28 17:40:50 +00:00
}
2018-03-24 13:52:47 +00:00
}
panel . page . innerHTML = `
2018-04-05 09:36:48 +00:00
< h2 class = "subtitle" > 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-29 23:22:08 +00:00
< a id = "setFileLength" class = "button is-breeze" >
< span class = "icon" >
< i class = "icon-paper-plane-empty" > < / i >
< / s p a n >
< span > Set file name length < / s p a n >
< / 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-29 23:22:08 +00:00
. catch ( error => {
console . log ( error )
2018-03-30 02:39:53 +00:00
return swal ( 'An error occurred!' , 'There was an error with the request, please check the console for more information.' , 'error' )
2018-03-24 13:52:47 +00:00
} )
}
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 {
2018-03-30 02:39:53 +00:00
return swal ( 'An error occurred!' , response . data . description , 'error' )
2018-03-28 17:40:50 +00:00
}
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-29 23:22:08 +00:00
. catch ( error => {
console . log ( error )
2018-03-28 17:40:50 +00:00
panel . isLoading ( element , false )
2018-03-30 02:39:53 +00:00
return swal ( 'An error occurred!' , 'There was an error with the request, please check the console for more information.' , 'error' )
2018-03-24 13:52:47 +00:00
} )
}
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 {
2018-03-30 02:39:53 +00:00
return swal ( 'An error occurred!' , response . data . description , 'error' )
2018-03-28 17:40:50 +00:00
}
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" >
2018-03-29 23:22:08 +00:00
< a id = "getNewToken" class = "button is-breeze" >
< span class = "icon" >
< i class = "icon-arrows-cw" > < / i >
< / s p a n >
< span > Request new token < / s p a n >
< / a >
2018-03-19 16:51:39 +00:00
< / 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-29 23:22:08 +00:00
. catch ( error => {
console . log ( error )
2018-03-30 02:39:53 +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 {
2018-03-30 02:39:53 +00:00
return swal ( 'An error occurred!' , response . data . description , 'error' )
2018-03-28 17:40:50 +00:00
}
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-29 23:22:08 +00:00
. catch ( error => {
console . log ( error )
2018-03-28 17:40:50 +00:00
panel . isLoading ( element , false )
2018-03-30 02:39:53 +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" >
2018-03-29 23:22:08 +00:00
< a id = "sendChangePassword" class = "button is-breeze" >
< span class = "icon" >
< i class = "icon-paper-plane-empty" > < / i >
< / s p a n >
< span > Set new password < / s p a n >
< / a >
2018-03-19 16:51:39 +00:00
< / 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 {
2018-03-30 02:39:53 +00:00
return swal ( 'An error occurred!' , response . data . description , 'error' )
2018-03-28 17:40:50 +00:00
}
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-29 23:22:08 +00:00
. catch ( error => {
console . log ( error )
2018-03-28 17:40:50 +00:00
panel . isLoading ( element , false )
2018-03-30 02:39:53 +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-03-29 23:22:08 +00:00
const selectedFiles = localStorage . selectedFiles
if ( selectedFiles ) {
panel . selectedFiles = JSON . parse ( selectedFiles )
}
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 )
2018-03-30 02:39:53 +00:00
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-03-28 20:05:01 +00:00
} )
2018-01-23 20:06:30 +00:00
}