2018-04-29 12:47:24 +00:00
/* global swal, axios, ClipboardJS, LazyLoad */
2018-01-23 20:06:30 +00:00
2018-04-29 12:47:24 +00:00
const page = {
// #page
dom : null ,
// user token
2018-03-28 11:36:28 +00:00
token : localStorage . token ,
2018-04-29 12:47:24 +00:00
username : null , // from api/tokens/verify
// view config (either list or thumbs)
2018-03-28 20:05:01 +00:00
filesView : localStorage . filesView ,
2018-04-29 12:47:24 +00:00
// current view (which album and which page)
currentView : { album : null , pageNum : null } ,
// id of selected files (shared across pages and will be synced with localStorage)
2018-03-30 02:39:53 +00:00
selectedFiles : [ ] ,
2018-04-29 12:47:24 +00:00
checkboxes : [ ] ,
lastSelected : null ,
2018-05-05 19:44:58 +00:00
// select album dom for dialogs/modals
2018-04-29 12:47:24 +00:00
selectAlbumContainer : null ,
2018-05-05 19:44:58 +00:00
// cache of files and albums data for dialogs/modals
files : new Map ( ) ,
albums : new Map ( ) ,
2018-04-29 12:47:24 +00:00
clipboardJS : null ,
lazyLoad : null
2018-03-28 11:36:28 +00:00
}
2018-01-23 20:06:30 +00:00
2018-04-29 12:47:24 +00:00
page . preparePage = ( ) => {
if ( ! page . token ) {
2018-01-24 15:31:23 +00:00
window . location = 'auth'
2018-04-29 12:47:24 +00:00
return
2018-01-23 20:06:30 +00:00
}
2018-04-29 12:47:24 +00:00
page . verifyToken ( page . token , true )
2018-01-23 20:06:30 +00:00
}
2018-05-05 19:44:58 +00:00
page . verifyToken = async ( token , reloadOnError = false ) => {
2018-04-29 12:47:24 +00:00
const response = await axios . post ( 'api/tokens/verify' , { token } )
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-04-29 12:47:24 +00:00
if ( ! response ) { return }
if ( response . data . success === false ) {
2018-05-06 14:14:57 +00:00
await swal ( {
2018-04-29 12:47:24 +00:00
title : 'An error occurred!' ,
text : response . data . description ,
icon : 'error'
} )
2018-05-06 14:14:57 +00:00
if ( reloadOnError ) {
localStorage . removeItem ( 'token' )
location . location = 'auth'
}
return
2018-04-29 12:47:24 +00:00
}
axios . defaults . headers . common . token = token
localStorage . token = token
page . token = token
page . username = response . data . username
page . prepareDashboard ( )
2018-01-23 20:06:30 +00:00
}
2018-04-29 12:47:24 +00:00
page . prepareDashboard = ( ) => {
page . dom = document . getElementById ( 'page' )
2018-01-23 20:06:30 +00:00
document . getElementById ( 'auth' ) . style . display = 'none'
document . getElementById ( 'dashboard' ) . style . display = 'block'
document . getElementById ( 'itemUploads' ) . addEventListener ( 'click' , function ( ) {
2018-04-29 12:47:24 +00:00
page . setActiveMenu ( this )
2018-01-23 20:06:30 +00:00
} )
2018-05-05 19:44:58 +00:00
document . getElementById ( 'itemDeleteByNames' ) . addEventListener ( 'click' , function ( ) {
page . setActiveMenu ( this )
} )
2018-01-23 20:06:30 +00:00
document . getElementById ( 'itemManageGallery' ) . addEventListener ( 'click' , function ( ) {
2018-04-29 12:47:24 +00:00
page . setActiveMenu ( this )
2018-01-23 20:06:30 +00:00
} )
2018-03-24 14:49:44 +00:00
document . getElementById ( 'itemFileLength' ) . addEventListener ( 'click' , function ( ) {
2018-04-29 12:47:24 +00:00
page . setActiveMenu ( this )
2018-03-24 14:49:44 +00:00
} )
2018-01-23 20:06:30 +00:00
document . getElementById ( 'itemTokens' ) . addEventListener ( 'click' , function ( ) {
2018-04-29 12:47:24 +00:00
page . setActiveMenu ( this )
2018-01-23 20:06:30 +00:00
} )
document . getElementById ( 'itemPassword' ) . addEventListener ( 'click' , function ( ) {
2018-04-29 12:47:24 +00:00
page . setActiveMenu ( this )
2018-01-23 20:06:30 +00:00
} )
2018-04-29 12:47:24 +00:00
document . getElementById ( 'itemLogout' ) . innerHTML = ` Logout ( ${ page . username } ) `
2018-01-23 20:06:30 +00:00
2018-04-29 12:47:24 +00:00
page . getAlbumsSidebar ( )
2018-01-23 20:06:30 +00:00
}
2018-04-29 12:47:24 +00:00
page . 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-04-29 12:47:24 +00:00
page . isLoading = ( element , state ) => {
2018-03-28 17:40:50 +00:00
if ( ! element ) { return }
2018-05-01 14:41:25 +00:00
if ( state ) { return element . classList . add ( 'is-loading' ) }
element . classList . remove ( 'is-loading' )
2018-03-28 17:40:50 +00:00
}
2018-05-06 14:14:57 +00:00
page . getUploads = async ( album , pageNum , element ) => {
2018-04-29 12:47:24 +00:00
if ( element ) { page . isLoading ( element , true ) }
if ( pageNum === undefined ) { pageNum = 0 }
2018-01-23 20:06:30 +00:00
2018-04-29 12:47:24 +00:00
let url = 'api/uploads/' + pageNum
if ( album !== undefined ) { url = 'api/album/' + album + '/' + pageNum }
2018-01-23 20:06:30 +00:00
2018-05-06 14:14:57 +00:00
const response = await axios . get ( url )
. catch ( error => {
console . log ( error )
return swal ( 'An error occurred!' , 'There was an error with the request, please check the console for more information.' , 'error' )
} )
if ( ! response ) { return }
if ( response . data . success === false ) {
if ( response . data . description === 'No token provided' ) {
return page . verifyToken ( page . token )
} else {
return swal ( 'An error occurred!' , response . data . description , 'error' )
2018-01-23 20:06:30 +00:00
}
2018-05-06 14:14:57 +00:00
}
2018-01-23 20:06:30 +00:00
2018-05-06 14:14:57 +00:00
page . files . clear ( )
2018-05-05 19:44:58 +00:00
2018-05-06 14:14:57 +00:00
let prevPage = 0
let nextPage = pageNum + 1
2018-01-23 20:06:30 +00:00
2018-05-06 14:14:57 +00:00
if ( response . data . files . length < 25 ) { nextPage = pageNum }
2018-01-23 20:06:30 +00:00
2018-05-06 14:14:57 +00:00
if ( pageNum > 0 ) { prevPage = pageNum - 1 }
2018-01-23 20:06:30 +00:00
2018-05-06 14:14:57 +00:00
const pagination = `
< nav class = "pagination is-centered" >
< a class = "button pagination-previous" onclick = "page.getUploads(${album}, ${prevPage}, this)" > Previous < / a >
< a class = "button pagination-next" onclick = "page.getUploads(${album}, ${nextPage}, this)" > Next page < / a >
< / n a v >
`
const controls = `
< div class = "columns" >
< div class = "column is-hidden-mobile" > < / d i v >
< div class = "column" style = "text-align: center" >
< a class = "button is-small is-danger" title = "List view" onclick = "page.setFilesView('list', this)" >
< span class = "icon" >
< i class = "icon-th-list" > < / i >
< / s p a n >
< / a >
< a class = "button is-small is-danger" title = "Thumbs view" onclick = "page.setFilesView('thumbs', this)" >
< span class = "icon" >
< i class = "icon-th-large" > < / i >
< / s p a n >
< / a >
< / d i v >
< div class = "column" style = "text-align: right" >
< a class = "button is-small is-info" title = "Clear selection" onclick = "page.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 = "page.addSelectedFilesToAlbum()" >
< span class = "icon" >
< i class = "icon-plus" > < / i >
< / s p a n >
< / a >
< a class = "button is-small is-danger" title = "Bulk delete" onclick = "page.deleteSelectedFiles()" >
< span class = "icon" >
< i class = "icon-trash" > < / i >
< / s p a n >
< span > Bulk delete < / s p a n >
< / a >
< / d i v >
< / d i v >
`
let allFilesSelected = true
if ( page . filesView === 'thumbs' ) {
page . dom . innerHTML = `
$ { pagination }
< hr >
$ { controls }
< div id = "table" class = "columns is-multiline is-mobile is-centered" >
< / d i v >
$ { pagination }
2018-03-19 16:51:39 +00:00
`
2018-05-06 14:14:57 +00:00
const table = document . getElementById ( 'table' )
for ( const file of response . data . files ) {
const selected = page . selectedFiles . includes ( file . id )
if ( ! selected && allFilesSelected ) { allFilesSelected = false }
const div = document . createElement ( 'div' )
let displayAlbumOrUser = file . album
if ( page . username === 'root' ) {
displayAlbumOrUser = ''
if ( file . username !== undefined ) { displayAlbumOrUser = file . username }
}
div . className = 'image-container column is-narrow'
if ( file . thumb !== undefined ) {
div . innerHTML = ` <a class="image" href=" ${ file . file } " target="_blank" rel="noopener"><img alt=" ${ file . name } " data-src=" ${ file . thumb } "/></a> `
} else {
div . innerHTML = ` <a class="image" href=" ${ file . file } " target="_blank" rel="noopener"><h1 class="title"> ${ file . extname || 'N/A' } </h1></a> `
}
div . innerHTML += `
< input type = "checkbox" class = "file-checkbox" title = "Select this file" data - id = "${file.id}" onclick = "page.selectFile(this, event)" $ { selected ? ' checked' : '' } >
< div class = "controls" >
< a class = "button is-small is-info clipboard-js" title = "Copy link to clipboard" data - clipboard - text = "${file.file}" >
2018-04-03 15:59:39 +00:00
< span class = "icon" >
2018-05-06 14:14:57 +00:00
< i class = "icon-clipboard-1" > < / i >
2018-04-03 15:59:39 +00:00
< / s p a n >
< / a >
2018-05-06 14:14:57 +00:00
< a class = "button is-small is-warning" title = "Add to album" onclick = "page.addSingleFileToAlbum(${file.id})" >
2018-03-30 02:39:53 +00:00
< span class = "icon" >
< i class = "icon-plus" > < / i >
< / s p a n >
< / a >
2018-05-06 14:14:57 +00:00
< a class = "button is-small is-danger" title = "Delete file" onclick = "page.deleteFile(${file.id})" >
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-05-06 14:14:57 +00:00
< div class = "details" >
< p > < span class = "name" title = "${file.file}" > $ { file . name } < / s p a n > < / p >
< p > $ { displayAlbumOrUser ? ` <span> ${ displayAlbumOrUser } </span> – ` : '' } $ { file . size } < / p >
< / d i v >
`
table . appendChild ( div )
page . checkboxes = Array . from ( table . getElementsByClassName ( 'file-checkbox' ) )
page . lazyLoad . update ( )
}
} else {
let albumOrUser = 'Album'
if ( page . username === 'root' ) { albumOrUser = 'User' }
page . dom . innerHTML = `
$ { pagination }
< hr >
$ { controls }
< div class = "table-container" >
< table class = "table is-narrow is-fullwidth is-hoverable" >
< thead >
< tr >
< th > < input id = "selectAll" type = "checkbox" title = "Select all files" onclick = "page.selectAllFiles(this)" > < / t h >
< th style = "width: 25%" > File < / t h >
< th > $ { albumOrUser } < / t h >
< th > Size < / t h >
< 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 >
2018-01-23 20:06:30 +00:00
< / d i v >
2018-05-06 14:14:57 +00:00
< hr >
$ { pagination }
2018-03-19 16:51:39 +00:00
`
2018-01-23 20:06:30 +00:00
2018-05-06 14:14:57 +00:00
const table = document . getElementById ( 'table' )
2018-01-23 20:06:30 +00:00
2018-05-06 14:14:57 +00:00
for ( const file of response . data . files ) {
const selected = page . selectedFiles . includes ( file . id )
if ( ! selected && allFilesSelected ) { allFilesSelected = false }
page . files . set ( file . id , {
name : file . name ,
thumb : file . thumb
} )
const tr = document . createElement ( 'tr' )
let displayAlbumOrUser = file . album
if ( page . username === 'root' ) {
displayAlbumOrUser = ''
if ( file . username !== undefined ) { displayAlbumOrUser = file . username }
}
2018-01-23 20:06:30 +00:00
2018-05-06 14:14:57 +00:00
tr . innerHTML = `
< tr >
< th > < input type = "checkbox" class = "file-checkbox" title = "Select this file" data - id = "${file.id}" onclick = "page.selectFile(this, event)" $ { selected ? ' checked' : '' } > < / t h >
< th > < a href = "${file.file}" target = "_blank" rel = "noopener" title = "${file.file}" > $ { file . name } < / a > < / t h >
< th > $ { displayAlbumOrUser } < / t h >
< td > $ { file . size } < / t d >
< td > $ { file . date } < / t d >
< td style = "text-align: right" >
< a class = "button is-small is-primary" title = "View thumbnail" onclick = "page.displayThumbnail(${file.id})" $ { file . thumb ? '' : ' disabled' } >
< span class = "icon" >
< i class = "icon-picture-1" > < / i >
< / 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-04-29 12:47:24 +00:00
< a class = "button is-small is-warning" title = "Add to album" onclick = "page.addSingleFileToAlbum(${file.id})" >
2018-03-30 02:39:53 +00:00
< span class = "icon" >
< i class = "icon-plus" > < / i >
< / s p a n >
< / a >
2018-04-29 12:47:24 +00:00
< a class = "button is-small is-danger" title = "Delete file" onclick = "page.deleteFile(${file.id})" >
2018-03-29 23:22:08 +00:00
< span class = "icon" >
< i class = "icon-trash" > < / i >
< / s p a n >
< / a >
2018-05-06 14:14:57 +00:00
< / t d >
< / t r >
2018-01-23 20:06:30 +00:00
`
2018-05-06 14:14:57 +00:00
table . appendChild ( tr )
page . checkboxes = Array . from ( table . getElementsByClassName ( 'file-checkbox' ) )
2018-03-29 23:47:31 +00:00
}
2018-05-06 14:14:57 +00:00
}
2018-03-28 12:28:17 +00:00
2018-05-06 14:14:57 +00:00
if ( allFilesSelected && response . data . files . length ) {
const selectAll = document . getElementById ( 'selectAll' )
if ( selectAll ) { selectAll . checked = true }
}
2018-04-28 23:44:25 +00:00
2018-05-06 14:14:57 +00:00
page . currentView . album = album
page . currentView . pageNum = pageNum
2018-01-23 20:06:30 +00:00
}
2018-04-29 12:47:24 +00:00
page . setFilesView = ( view , element ) => {
2018-01-23 20:06:30 +00:00
localStorage . filesView = view
2018-04-29 12:47:24 +00:00
page . filesView = view
page . getUploads ( page . currentView . album , page . currentView . pageNum , element )
2018-01-23 20:06:30 +00:00
}
2018-05-05 19:44:58 +00:00
page . displayThumbnail = id => {
const file = page . files . get ( id )
if ( ! file . thumb ) { return }
2018-05-01 14:41:25 +00:00
swal ( {
2018-05-05 19:44:58 +00:00
text : file . name ,
2018-05-01 14:41:25 +00:00
content : {
element : 'img' ,
2018-05-05 19:44:58 +00:00
attributes : { src : file . thumb }
2018-05-01 14:41:25 +00:00
} ,
button : true
} )
2018-03-29 23:22:08 +00:00
}
2018-04-29 12:47:24 +00:00
page . selectAllFiles = element => {
2018-03-29 23:22:08 +00:00
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 ) {
2018-04-29 12:47:24 +00:00
page . selectedFiles . push ( id )
2018-04-03 15:59:39 +00:00
} else {
2018-04-29 12:47:24 +00:00
page . selectedFiles . splice ( page . selectedFiles . indexOf ( id ) , 1 )
2018-04-03 15:59:39 +00:00
}
2018-03-29 23:22:08 +00:00
}
}
2018-04-03 15:59:39 +00:00
2018-04-29 12:47:24 +00:00
if ( page . selectedFiles . length ) {
localStorage . selectedFiles = JSON . stringify ( page . selectedFiles )
2018-04-03 15:59:39 +00:00
} else {
localStorage . removeItem ( 'selectedFiles' )
}
2018-03-29 23:22:08 +00:00
element . title = element . checked ? 'Unselect all files' : 'Select all files'
}
2018-04-29 12:47:24 +00:00
page . selectInBetween = ( element , lastElement ) => {
2018-04-04 18:23:45 +00:00
if ( ! element || ! lastElement ) { return }
if ( element === lastElement ) { return }
2018-04-29 12:47:24 +00:00
if ( ! page . checkboxes || ! page . checkboxes . length ) { return }
2018-04-04 18:23:45 +00:00
2018-04-29 12:47:24 +00:00
const thisIndex = page . checkboxes . indexOf ( element )
const lastIndex = page . checkboxes . indexOf ( lastElement )
2018-04-04 18:23:45 +00:00
const distance = thisIndex - lastIndex
if ( distance >= - 1 && distance <= 1 ) { return }
2018-04-29 12:47:24 +00:00
for ( let i = 0 ; i < page . checkboxes . length ; i ++ ) {
2018-04-04 18:23:45 +00:00
if ( ( thisIndex > lastIndex && i > lastIndex && i < thisIndex ) ||
( thisIndex < lastIndex && i > thisIndex && i < lastIndex ) ) {
2018-04-29 12:47:24 +00:00
page . checkboxes [ i ] . checked = true
page . selectedFiles . push ( parseInt ( page . checkboxes [ i ] . dataset . id ) )
2018-04-04 18:23:45 +00:00
}
}
2018-04-29 12:47:24 +00:00
localStorage . selectedFiles = JSON . stringify ( page . selectedFiles )
2018-04-04 18:23:45 +00:00
}
2018-04-29 12:47:24 +00:00
page . selectFile = ( element , event ) => {
if ( event . shiftKey && page . lastSelected ) {
page . selectInBetween ( element , page . lastSelected )
2018-04-04 18:23:45 +00:00
} else {
2018-04-29 12:47:24 +00:00
page . lastSelected = element
2018-04-04 18:23:45 +00:00
}
2018-04-03 15:59:39 +00:00
const id = parseInt ( element . dataset . id )
if ( isNaN ( id ) ) { return }
2018-04-29 12:47:24 +00:00
if ( ! page . selectedFiles . includes ( id ) && element . checked ) {
page . selectedFiles . push ( id )
} else if ( page . selectedFiles . includes ( id ) && ! element . checked ) {
page . selectedFiles . splice ( page . selectedFiles . indexOf ( id ) , 1 )
2018-04-03 15:59:39 +00:00
}
2018-04-29 12:47:24 +00:00
if ( page . selectedFiles . length ) {
localStorage . selectedFiles = JSON . stringify ( page . selectedFiles )
2018-04-03 15:59:39 +00:00
} else {
localStorage . removeItem ( 'selectedFiles' )
2018-03-29 23:22:08 +00:00
}
}
2018-04-29 12:47:24 +00:00
page . clearSelection = async ( ) => {
const count = page . selectedFiles . length
2018-04-03 15:59:39 +00:00
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
}
}
2018-04-29 12:47:24 +00:00
page . selectedFiles = [ ]
2018-04-03 15:59:39 +00:00
localStorage . removeItem ( 'selectedFiles' )
const selectAll = document . getElementById ( 'selectAll' )
if ( selectAll ) { selectAll . checked = false }
return swal ( 'Cleared selection!' , ` Unselected ${ count } ${ suffix } . ` , 'success' )
}
2018-05-06 14:14:57 +00:00
page . deleteFile = async id => {
2018-04-28 23:44:25 +00:00
// TODO: Share function with bulk delete, just like 'add selected files to album' and 'add single file to album'
2018-05-06 14:14:57 +00:00
const proceed = await swal ( {
2018-01-23 20:06:30 +00:00
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
}
}
2018-03-29 23:22:08 +00:00
} )
2018-05-06 14:14:57 +00:00
if ( ! proceed ) { return }
const response = await axios . post ( 'api/upload/delete' , { id } )
. catch ( error => {
console . log ( error )
return swal ( 'An error occurred!' , 'There was an error with the request, please check the console for more information.' , 'error' )
} )
if ( ! response ) { return }
if ( response . data . success === false ) {
if ( response . data . description === 'No token provided' ) {
return page . verifyToken ( page . token )
} else {
return swal ( 'An error occurred!' , response . data . description , 'error' )
}
}
swal ( 'Deleted!' , 'The file has been deleted.' , 'success' )
page . getUploads ( page . currentView . album , page . currentView . pageNum )
2018-03-29 23:22:08 +00:00
}
2018-04-29 12:47:24 +00:00
page . deleteSelectedFiles = async ( ) => {
const count = page . selectedFiles . length
2018-03-29 23:22:08 +00:00
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' , {
2018-05-05 19:44:58 +00:00
field : 'id' ,
values : page . selectedFiles
2018-03-31 14:26:53 +00:00
} )
. 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' ) {
2018-04-29 12:47:24 +00:00
return page . verifyToken ( page . token )
2018-03-31 14:26:53 +00:00
} 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
2018-05-05 19:44:58 +00:00
if ( bulkdelete . data . failed && bulkdelete . data . failed . length ) {
deleted -= bulkdelete . data . failed . length
page . selectedFiles = page . selectedFiles . filter ( id => bulkdelete . data . failed . includes ( id ) )
2018-03-31 14:26:53 +00:00
} else {
2018-04-29 12:47:24 +00:00
page . selectedFiles = [ ]
2018-03-31 14:26:53 +00:00
}
2018-03-29 23:22:08 +00:00
2018-04-29 12:47:24 +00:00
localStorage . selectedFiles = JSON . stringify ( page . selectedFiles )
2018-03-31 14:26:53 +00:00
swal ( 'Deleted!' , ` ${ deleted } file ${ deleted === 1 ? ' has' : 's have' } been deleted. ` , 'success' )
2018-04-29 12:47:24 +00:00
return page . getUploads ( page . currentView . album , page . currentView . pageNum )
2018-01-23 20:06:30 +00:00
}
2018-05-05 19:44:58 +00:00
page . deleteByNames = ( ) => {
page . dom . innerHTML = `
< h2 class = "subtitle" > Delete by names < / h 2 >
< div class = "field" >
< label class = "label" > File names : < / l a b e l >
< div class = "control" >
< textarea id = "names" class = "textarea" > < / t e x t a r e a >
< / d i v >
< p class = "help" > Separate each entry with a new line . < / p >
< / d i v >
< div class = "field" >
< div class = "control" >
< a class = "button is-danger is-fullwidth" onclick = "page.deleteFileByNames()" >
< span class = "icon" >
< i class = "icon-trash" > < / i >
< / s p a n >
< span > Bulk delete < / s p a n >
< / a >
< / d i v >
< / d i v >
`
}
page . deleteFileByNames = async ( ) => {
const names = document . getElementById ( 'names' ) . value . split ( /\r?\n/ ) . filter ( n => n . trim ( ) . length )
const count = names . length
if ( ! count ) {
return swal ( 'An error occurred!' , 'You have not entered any file names.' , 'error' )
}
const suffix = ` file ${ count === 1 ? '' : 's' } `
const proceed = await swal ( {
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
}
}
} )
if ( ! proceed ) { return }
const bulkdelete = await axios . post ( 'api/upload/bulkdelete' , {
field : 'name' ,
values : names
} )
. 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 ( ! bulkdelete ) { return }
if ( bulkdelete . data . success === false ) {
if ( bulkdelete . data . description === 'No token provided' ) {
return page . verifyToken ( page . token )
} else {
return swal ( 'An error occurred!' , bulkdelete . data . description , 'error' )
}
}
let deleted = count
if ( bulkdelete . data . failed && bulkdelete . data . failed . length ) {
deleted -= bulkdelete . data . failed . length
document . getElementById ( 'names' ) . value = bulkdelete . data . failed . join ( '\n' )
}
swal ( 'Deleted!' , ` ${ deleted } file ${ deleted === 1 ? ' has' : 's have' } been deleted. ` , 'success' )
}
2018-04-29 12:47:24 +00:00
page . addSelectedFilesToAlbum = async ( ) => {
const count = page . selectedFiles . length
2018-03-30 02:39:53 +00:00
if ( ! count ) {
return swal ( 'An error occurred!' , 'You have not selected any files.' , 'error' )
}
2018-05-05 19:44:58 +00:00
const failed = await page . addFilesToAlbum ( page . selectedFiles )
if ( ! failed ) { return }
if ( failed . length ) {
page . selectedFiles = page . selectedFiles . filter ( id => failed . includes ( id ) )
2018-03-31 14:26:53 +00:00
} else {
2018-04-29 12:47:24 +00:00
page . selectedFiles = [ ]
2018-03-31 14:26:53 +00:00
}
2018-04-29 12:47:24 +00:00
localStorage . selectedFiles = JSON . stringify ( page . selectedFiles )
page . getUploads ( page . currentView . album , page . currentView . pageNum )
2018-04-28 23:44:25 +00:00
}
2018-04-29 12:47:24 +00:00
page . addSingleFileToAlbum = async id => {
2018-05-05 19:44:58 +00:00
const failed = await page . addFilesToAlbum ( [ id ] )
if ( ! failed ) { return }
2018-04-29 12:47:24 +00:00
page . getUploads ( page . currentView . album , page . currentView . pageNum )
2018-03-30 02:39:53 +00:00
}
2018-04-29 12:47:24 +00:00
page . addFilesToAlbum = async ids => {
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' ) {
2018-04-29 12:47:24 +00:00
page . verifyToken ( page . token )
2018-03-30 02:39:53 +00:00
} else {
2018-04-05 12:58:22 +00:00
swal ( 'An error occurred!' , list . data . description , 'error' )
2018-03-30 02:39:53 +00:00
}
2018-04-05 12:58:22 +00:00
return
2018-03-30 02:39:53 +00:00
}
2018-04-29 12:47:24 +00:00
if ( ! page . selectAlbumContainer ) {
2018-03-30 02:39:53 +00:00
// We want to this to be re-usable
2018-04-29 12:47:24 +00:00
page . selectAlbumContainer = document . createElement ( 'div' )
page . selectAlbumContainer . id = 'selectAlbum'
2018-03-30 02:39:53 +00:00
}
const options = list . data . albums
. map ( album => ` <option value=" ${ album . id } "> ${ album . name } </option> ` )
. join ( '\n' )
2018-04-29 12:47:24 +00:00
page . selectAlbumContainer . innerHTML = `
2018-04-28 17:26:39 +00:00
< div class = "field" >
< label class = "label" > If a file is already in an album , it will be moved . < / l a b e l >
< div class = "control" >
< div class = "select is-fullwidth" >
< select >
< option value = "-1" > Remove from album < / o p t i o n >
< option value = "" selected disabled > Choose an album < / o p t i o n >
$ { options }
< / s e l e c t >
< / d i v >
< / d i v >
2018-03-30 02:39:53 +00:00
`
const choose = await swal ( {
2018-04-29 12:47:24 +00:00
content : page . selectAlbumContainer ,
2018-03-30 02:39:53 +00:00
buttons : {
cancel : true ,
confirm : {
text : 'OK' ,
closeModal : false
}
}
} )
if ( ! choose ) { return }
2018-04-29 12:47:24 +00:00
const albumid = parseInt ( page . selectAlbumContainer . getElementsByTagName ( 'select' ) [ 0 ] . value )
2018-03-30 02:39:53 +00:00
if ( isNaN ( albumid ) ) {
2018-04-05 12:58:22 +00:00
swal ( 'An error occurred!' , 'You did not choose an album.' , 'error' )
return
2018-03-30 02:39:53 +00:00
}
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' ) {
2018-04-29 12:47:24 +00:00
page . verifyToken ( page . token )
2018-03-30 02:39:53 +00:00
} else {
2018-04-05 12:58:22 +00:00
swal ( 'An error occurred!' , add . data . description , 'error' )
2018-03-30 02:39:53 +00:00
}
2018-04-05 12:58:22 +00:00
return
2018-03-30 02:39:53 +00:00
}
let added = ids . length
2018-05-05 19:44:58 +00:00
if ( add . data . failed && add . data . failed . length ) {
added -= add . data . failed . length
2018-03-30 02:39:53 +00:00
}
const suffix = ` file ${ ids . length === 1 ? '' : 's' } `
if ( ! added ) {
2018-04-05 12:58:22 +00:00
swal ( 'An error occurred!' , ` Could not add the ${ suffix } to the album. ` , 'error' )
return
2018-03-30 02:39:53 +00:00
}
swal ( 'Woohoo!' , ` Successfully ${ albumid < 0 ? 'removed' : 'added' } ${ added } ${ suffix } ${ albumid < 0 ? 'from' : 'to' } the album. ` , 'success' )
2018-05-05 19:44:58 +00:00
return add . data . failed
2018-03-30 02:39:53 +00:00
}
2018-05-06 14:14:57 +00:00
page . getAlbums = async ( ) => {
const response = await axios . get ( 'api/albums' )
. catch ( error => {
console . log ( error )
return swal ( 'An error occurred!' , 'There was an error with the request, please check the console for more information.' , 'error' )
} )
if ( ! response ) { return }
if ( response . data . success === false ) {
if ( response . data . description === 'No token provided' ) {
return page . verifyToken ( page . token )
} else {
return swal ( 'An error occurred!' , response . data . description , 'error' )
2018-01-23 20:06:30 +00:00
}
2018-05-06 14:14:57 +00:00
}
2018-01-23 20:06:30 +00:00
2018-05-06 14:14:57 +00:00
page . albums . clear ( )
2018-05-05 19:44:58 +00:00
2018-05-06 14:14:57 +00:00
page . dom . innerHTML = `
< h2 class = "subtitle" > Create new album < / h 2 >
2018-01-23 20:06:30 +00:00
2018-05-06 14:14:57 +00:00
< div class = "field" >
< div class = "control" >
< input id = "albumName" class = "input" type = "text" placeholder = "Name" >
2018-05-05 19:44:58 +00:00
< / d i v >
2018-05-06 14:14:57 +00:00
< / d i v >
2018-05-05 19:44:58 +00:00
2018-05-06 14:14:57 +00:00
< div class = "field" >
< div class = "control" >
< a id = "submitAlbum" class = "button is-breeze is-fullwidth" onclick = "page.submitAlbum(this)" >
< span class = "icon" >
< i class = "icon-paper-plane-empty" > < / i >
< / s p a n >
< span > Create < / s p a n >
< / a >
2018-03-19 16:51:39 +00:00
< / d i v >
2018-05-06 14:14:57 +00:00
< / d i v >
2018-01-23 20:06:30 +00:00
2018-05-06 14:14:57 +00:00
< hr >
2018-01-23 20:06:30 +00:00
2018-05-06 14:14:57 +00:00
< h2 class = "subtitle" > List of albums < / h 2 >
2018-01-23 20:06:30 +00:00
2018-05-06 14:14:57 +00:00
< div class = "table-container" >
< table class = "table is-fullwidth is-hoverable" >
< thead >
< tr >
< th > ID < / t h >
< 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-05-06 14:14:57 +00:00
const homeDomain = response . data . homeDomain
const table = document . getElementById ( 'table' )
2018-05-05 19:44:58 +00:00
2018-05-06 14:14:57 +00:00
for ( const album of response . data . albums ) {
const albumUrl = ` ${ homeDomain } /a/ ${ album . identifier } `
2018-05-05 19:44:58 +00:00
2018-05-06 14:14:57 +00:00
page . albums . set ( album . id , {
name : album . name ,
download : album . download ,
public : album . public
} )
2018-01-23 20:06:30 +00:00
2018-05-06 14:14:57 +00:00
const tr = document . createElement ( 'tr' )
tr . innerHTML = `
< tr >
< th > $ { album . id } < / t h >
< th > $ { album . name } < / t h >
< th > $ { album . files } < / t h >
< td > $ { album . date } < / t d >
< td > < a$ { album . public ? ` href=" ${ albumUrl } " ` : '' } target = "_blank" rel = "noopener" > $ { albumUrl } < / a > < / t d >
< td style = "text-align: right" >
< a class = "button is-small is-primary" title = "Edit album" onclick = "page.editAlbum(${album.id})" >
< span class = "icon is-small" >
< i class = "icon-pencil-1" > < / i >
< / s p a n >
< / a >
< a class = "button is-small is-info clipboard-js" title = "Copy link to clipboard" $ { album . public ? ` data-clipboard-text=" ${ albumUrl } " ` : 'disabled' } >
< span class = "icon is-small" >
< i class = "icon-clipboard-1" > < / i >
< / s p a n >
< / a >
< a class = "button is-small is-warning" title = "Download album" $ { album . download ? ` href="api/album/zip/ ${ album . identifier } ?v= ${ album . editedAt } " ` : 'disabled' } >
< span class = "icon is-small" >
< i class = "icon-download" > < / i >
< / s p a n >
< / a >
< a class = "button is-small is-danger" title = "Delete album" onclick = "page.deleteAlbum(${album.id})" >
< span class = "icon is-small" >
< i class = "icon-trash" > < / i >
< / s p a n >
< / a >
< / t d >
< / t r >
`
2018-01-23 20:06:30 +00:00
2018-05-06 14:14:57 +00:00
table . appendChild ( tr )
}
2018-01-23 20:06:30 +00:00
}
2018-04-29 12:47:24 +00:00
page . editAlbum = async id => {
2018-05-05 19:44:58 +00:00
const album = page . albums . get ( id )
if ( ! album ) { return }
2018-04-28 17:26:39 +00:00
const div = document . createElement ( 'div' )
div . innerHTML = `
< div class = "field" >
< label class = "label" > Album name < / l a b e l >
< div class = "controls" >
2018-05-06 14:14:57 +00:00
< input id = "_name" class = "input" type = "text" value = "${album.name || ''}" >
2018-04-28 17:26:39 +00:00
< / d i v >
< / d i v >
< div class = "field" >
< div class = "control" >
< label class = "checkbox" >
< input id = "_download" type = "checkbox" $ { album . download ? 'checked' : '' } >
Enable download
< / l a b e l >
< / d i v >
< / d i v >
< div class = "field" >
< div class = "control" >
< label class = "checkbox" >
< input id = "_public" type = "checkbox" $ { album . public ? 'checked' : '' } >
Enable public link
< / l a b e l >
< / d i v >
< / d i v >
< div class = "field" >
< div class = "control" >
< label class = "checkbox" >
< input id = "_requestLink" type = "checkbox" >
Request new public link
< / l a b e l >
< / d i v >
< / d i v >
`
const value = await swal ( {
title : 'Edit album' ,
2018-03-19 16:51:39 +00:00
icon : 'info' ,
2018-04-28 17:26:39 +00:00
content : div ,
2018-03-19 16:51:39 +00:00
buttons : {
cancel : true ,
confirm : {
closeModal : false
}
2018-01-23 20:06:30 +00:00
}
2018-04-28 17:26:39 +00:00
} )
if ( ! value ) { return }
const response = await axios . post ( 'api/albums/edit' , {
id ,
name : document . getElementById ( '_name' ) . value ,
download : document . getElementById ( '_download' ) . checked ,
public : document . getElementById ( '_public' ) . checked ,
requestLink : document . getElementById ( '_requestLink' ) . checked
} )
. catch ( error => {
console . log ( error )
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-05-06 14:14:57 +00:00
if ( ! response ) { return }
2018-01-23 20:06:30 +00:00
2018-04-28 17:26:39 +00:00
if ( response . data . success === false ) {
if ( response . data . description === 'No token provided' ) {
2018-04-29 12:47:24 +00:00
return page . verifyToken ( page . token )
2018-04-28 17:26:39 +00:00
} else {
return swal ( 'An error occurred!' , response . data . description , 'error' )
}
}
if ( response . data . identifier ) {
swal ( 'Success!' , ` Your album's new identifier is: ${ response . data . identifier } . ` , 'success' )
} else if ( response . data . name !== album . name ) {
swal ( 'Success!' , ` Your album was renamed to: ${ response . data . name } . ` , 'success' )
} else {
swal ( 'Success!' , 'Your album was edited!' , 'success' )
}
2018-04-29 12:47:24 +00:00
page . getAlbumsSidebar ( )
page . getAlbums ( )
2018-01-23 20:06:30 +00:00
}
2018-04-29 12:47:24 +00:00
page . deleteAlbum = async id => {
2018-04-28 17:26:39 +00:00
const proceed = await swal ( {
2018-01-23 20:06:30 +00:00
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
}
}
2018-04-28 17:26:39 +00:00
} )
if ( ! proceed ) { return }
2018-01-23 20:06:30 +00:00
2018-04-28 17:26:39 +00:00
const response = await axios . post ( 'api/albums/delete' , {
id ,
purge : proceed === 'purge'
2018-03-19 16:51:39 +00:00
} )
2018-04-28 17:26:39 +00:00
. catch ( error => {
console . log ( error )
return swal ( 'An error occurred!' , 'There was an error with the request, please check the console for more information.' , 'error' )
} )
if ( response . data . success === false ) {
if ( response . data . description === 'No token provided' ) {
2018-04-29 12:47:24 +00:00
return page . verifyToken ( page . token )
2018-04-28 17:26:39 +00:00
} else {
return swal ( 'An error occurred!' , response . data . description , 'error' )
}
}
swal ( 'Deleted!' , 'Your album has been deleted.' , 'success' )
2018-04-29 12:47:24 +00:00
page . getAlbumsSidebar ( )
page . getAlbums ( )
2018-01-23 20:06:30 +00:00
}
2018-05-06 14:14:57 +00:00
page . submitAlbum = async element => {
2018-04-29 12:47:24 +00:00
page . isLoading ( element , true )
2018-05-06 14:14:57 +00:00
const response = await axios . post ( 'api/albums' , {
2018-01-23 20:06:30 +00:00
name : document . getElementById ( 'albumName' ) . value
} )
2018-03-29 23:22:08 +00:00
. catch ( error => {
console . log ( error )
2018-04-29 12:47:24 +00:00
page . 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-19 16:51:39 +00:00
} )
2018-05-06 14:14:57 +00:00
if ( ! response ) { return }
2018-01-23 20:06:30 +00:00
2018-05-06 14:14:57 +00:00
page . isLoading ( element , false )
2018-01-23 20:06:30 +00:00
2018-05-06 14:14:57 +00:00
if ( response . data . success === false ) {
if ( response . data . description === 'No token provided' ) {
return page . verifyToken ( page . token )
} else {
return swal ( 'An error occurred!' , response . data . description , 'error' )
}
}
2018-01-23 20:06:30 +00:00
2018-05-06 14:14:57 +00:00
swal ( 'Woohoo!' , 'Album was created successfully' , 'success' )
page . getAlbumsSidebar ( )
page . getAlbums ( )
}
2018-01-23 20:06:30 +00:00
2018-05-06 14:14:57 +00:00
page . getAlbumsSidebar = async ( ) => {
const response = await axios . get ( 'api/albums/sidebar' )
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-05-06 14:14:57 +00:00
if ( ! response ) { return }
if ( response . data . success === false ) {
if ( response . data . description === 'No token provided' ) {
return page . verifyToken ( page . token )
} else {
return swal ( 'An error occurred!' , response . data . description , 'error' )
}
}
const albumsContainer = document . getElementById ( 'albumsContainer' )
albumsContainer . innerHTML = ''
if ( response . data . albums === undefined ) { return }
for ( const album of response . data . albums ) {
const li = document . createElement ( 'li' )
const a = document . createElement ( 'a' )
a . id = album . id
a . innerHTML = album . name
a . addEventListener ( 'click' , function ( ) {
page . getAlbum ( this )
} )
li . appendChild ( a )
albumsContainer . appendChild ( li )
}
2018-01-23 20:06:30 +00:00
}
2018-04-29 12:47:24 +00:00
page . getAlbum = album => {
page . setActiveMenu ( album )
page . getUploads ( album . id )
2018-01-23 20:06:30 +00:00
}
2018-05-06 14:14:57 +00:00
page . changeFileLength = async ( ) => {
const response = await axios . get ( 'api/filelength/config' )
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-05-06 14:14:57 +00:00
if ( ! response ) { return }
if ( response . data . success === false ) {
if ( response . data . description === 'No token provided' ) {
return page . verifyToken ( page . token )
} else {
return swal ( 'An error occurred!' , response . data . description , 'error' )
}
}
page . dom . innerHTML = `
< h2 class = "subtitle" > File name length < / h 2 >
< div class = "field" >
< div class = "field" >
< label class = "label" > Your current file name length : < / l a b e l >
< div class = "control" >
< 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 >
< 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 >
< / d i v >
< div class = "field" >
< div class = "control" >
< a id = "setFileLength" class = "button is-breeze is-fullwidth" >
< span class = "icon" >
< i class = "icon-paper-plane-empty" > < / i >
< / s p a n >
< span > Set file name length < / s p a n >
< / a >
< / d i v >
< div >
< / d i v >
`
document . getElementById ( 'setFileLength' ) . addEventListener ( 'click' , function ( ) {
page . setFileLength ( document . getElementById ( 'fileLength' ) . value , this )
} )
2018-03-24 13:52:47 +00:00
}
2018-05-06 14:14:57 +00:00
page . setFileLength = async ( fileLength , element ) => {
2018-04-29 12:47:24 +00:00
page . isLoading ( element , true )
2018-03-24 13:52:47 +00:00
2018-05-06 14:14:57 +00:00
const response = await axios . post ( 'api/filelength/change' , { fileLength } )
2018-03-29 23:22:08 +00:00
. catch ( error => {
console . log ( error )
2018-04-29 12:47:24 +00:00
page . 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-05-06 14:14:57 +00:00
if ( ! response ) { return }
2018-03-24 13:52:47 +00:00
2018-05-06 14:14:57 +00:00
page . isLoading ( element , false )
2018-01-23 20:06:30 +00:00
2018-05-06 14:14:57 +00:00
if ( response . data . success === false ) {
if ( response . data . description === 'No token provided' ) {
return page . verifyToken ( page . token )
} else {
return swal ( 'An error occurred!' , response . data . description , 'error' )
}
}
2018-03-19 16:51:39 +00:00
2018-05-06 14:14:57 +00:00
await swal ( {
title : 'Woohoo!' ,
text : 'Your file length was successfully changed.' ,
icon : 'success'
} )
2018-05-05 19:44:58 +00:00
2018-05-06 14:14:57 +00:00
page . changeFileLength ( )
}
2018-01-23 20:06:30 +00:00
2018-05-06 14:14:57 +00:00
page . changeToken = async ( ) => {
const response = await axios . get ( 'api/tokens' )
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-05-06 14:14:57 +00:00
if ( ! response ) { return }
if ( response . data . success === false ) {
if ( response . data . description === 'No token provided' ) {
return page . verifyToken ( page . token )
} else {
return swal ( 'An error occurred!' , response . data . description , 'error' )
}
}
page . dom . 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" >
< div class = "control" >
< input id = "token" readonly class = "input" type = "text" placeholder = "Your token" value = "${response.data.token}" >
< / d i v >
< / d i v >
< / d i v >
< div class = "field" >
< div class = "control" >
< a id = "getNewToken" class = "button is-breeze is-fullwidth" onclick = "page.getNewToken(this)" >
< span class = "icon" >
< i class = "icon-arrows-cw" > < / i >
< / s p a n >
< span > Request new token < / s p a n >
< / a >
< / d i v >
< / d i v >
`
2018-01-23 20:06:30 +00:00
}
2018-05-06 14:14:57 +00:00
page . getNewToken = async element => {
2018-04-29 12:47:24 +00:00
page . isLoading ( element , true )
2018-01-23 20:06:30 +00:00
2018-05-06 14:14:57 +00:00
const response = await axios . post ( 'api/tokens/change' )
2018-03-29 23:22:08 +00:00
. catch ( error => {
console . log ( error )
2018-04-29 12:47:24 +00:00
page . 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-05-06 14:14:57 +00:00
if ( ! response ) { return }
page . isLoading ( element , false )
if ( response . data . success === false ) {
if ( response . data . description === 'No token provided' ) {
return page . verifyToken ( page . token )
} else {
return swal ( 'An error occurred!' , response . data . description , 'error' )
}
}
await swal ( {
title : 'Woohoo!' ,
text : 'Your token was successfully changed.' ,
icon : 'success'
} )
axios . defaults . headers . common . token = response . data . token
localStorage . token = response . data . token
page . token = response . data . token
page . changeToken ( )
2018-01-23 20:06:30 +00:00
}
2018-04-29 12:47:24 +00:00
page . changePassword = ( ) => {
page . dom . 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" >
2018-05-05 19:44:58 +00:00
< input id = "password" class = "input" type = "password" >
2018-03-19 16:51:39 +00:00
< / d i v >
< / d i v >
2018-05-05 19:44:58 +00:00
2018-03-19 16:51:39 +00:00
< div class = "field" >
2018-05-05 19:44:58 +00:00
< label class = "label" > Re - type new password : < / l a b e l >
< div class = "control" >
< input id = "passwordConfirm" class = "input" type = "password" >
< / d i v >
< / d i v >
< div class = "field" >
< div class = "control" >
< a id = "sendChangePassword" class = "button is-breeze is-fullwidth" >
< 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 >
2018-01-23 20:06:30 +00:00
`
document . getElementById ( 'sendChangePassword' ) . addEventListener ( 'click' , function ( ) {
if ( document . getElementById ( 'password' ) . value === document . getElementById ( 'passwordConfirm' ) . value ) {
2018-04-29 12:47:24 +00:00
page . 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'
2018-01-23 20:06:30 +00:00
} )
}
} )
}
2018-05-06 14:14:57 +00:00
page . sendNewPassword = async ( pass , element ) => {
2018-04-29 12:47:24 +00:00
page . isLoading ( element , true )
2018-01-23 20:06:30 +00:00
2018-05-06 14:14:57 +00:00
const response = await axios . post ( 'api/password/change' , { password : pass } )
2018-03-29 23:22:08 +00:00
. catch ( error => {
console . log ( error )
2018-04-29 12:47:24 +00:00
page . 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-05-06 14:14:57 +00:00
if ( ! response ) { return }
page . isLoading ( element , false )
if ( response . data . success === false ) {
if ( response . data . description === 'No token provided' ) {
return page . verifyToken ( page . token )
} else {
return swal ( 'An error occurred!' , response . data . description , 'error' )
}
}
await swal ( {
title : 'Woohoo!' ,
text : 'Your password was successfully changed.' ,
icon : 'success'
} )
page . changePassword ( )
2018-01-23 20:06:30 +00:00
}
2018-05-01 14:41:25 +00:00
page . setActiveMenu = activeItem => {
2018-03-28 11:36:28 +00:00
const menu = document . getElementById ( 'menu' )
const items = menu . getElementsByTagName ( 'a' )
2018-05-01 14:41:25 +00:00
for ( const item of items ) { item . classList . remove ( 'is-active' ) }
2018-01-23 20:06:30 +00:00
2018-05-01 14:41:25 +00:00
activeItem . classList . add ( 'is-active' )
2018-01-23 20:06:30 +00:00
}
2018-03-28 11:36:28 +00:00
window . onload = ( ) => {
// Add 'no-touch' class to non-touch devices
if ( ! ( 'ontouchstart' in document . documentElement ) ) {
2018-05-01 14:41:25 +00:00
document . documentElement . classList . add ( 'no-touch' )
2018-03-28 11:36:28 +00:00
}
2018-03-28 20:05:01 +00:00
2018-03-29 23:22:08 +00:00
const selectedFiles = localStorage . selectedFiles
if ( selectedFiles ) {
2018-04-29 12:47:24 +00:00
page . selectedFiles = JSON . parse ( selectedFiles )
2018-03-29 23:22:08 +00:00
}
2018-04-29 12:47:24 +00:00
page . preparePage ( )
2018-03-28 20:05:01 +00:00
2018-04-29 12:47:24 +00:00
page . clipboardJS = new ClipboardJS ( '.clipboard-js' )
2018-03-28 20:05:01 +00:00
2018-04-29 12:47:24 +00:00
page . clipboardJS . on ( 'success' , ( ) => {
2018-03-28 20:05:01 +00:00
return swal ( 'Copied!' , 'The link has been copied to clipboard.' , 'success' )
} )
2018-04-29 12:47:24 +00:00
page . clipboardJS . on ( 'error' , event => {
2018-03-28 20:05:01 +00:00
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-04-29 12:47:24 +00:00
page . lazyLoad = new LazyLoad ( )
2018-01-23 20:06:30 +00:00
}