2018-04-29 12:47:24 +00:00
/* global swal, axios, ClipboardJS, LazyLoad */
2018-01-23 20:06:30 +00:00
2018-07-14 03:42:18 +00:00
var page = {
2018-04-29 12:47:24 +00:00
// #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
2018-07-14 03:42:18 +00:00
files : { } ,
albums : { } ,
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-07-14 03:42:18 +00:00
page . preparePage = function ( ) {
2018-04-29 12:47:24 +00:00
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-07-14 03:42:18 +00:00
page . verifyToken = function ( token , reloadOnError ) {
2018-09-04 15:49:37 +00:00
axios . post ( 'api/tokens/verify' , { token : token } )
2018-07-14 03:42:18 +00:00
. then ( function ( response ) {
if ( response . data . success === false ) {
return swal ( {
title : 'An error occurred!' ,
text : response . data . description ,
icon : 'error'
} )
. then ( function ( ) {
if ( ! reloadOnError ) { return }
localStorage . removeItem ( 'token' )
location . location = 'auth'
} )
}
axios . defaults . headers . common . token = token
localStorage . token = token
page . token = token
page . username = response . data . username
page . prepareDashboard ( )
} )
. catch ( function ( error ) {
2018-03-29 23:22:08 +00:00
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-07-14 03:42:18 +00:00
page . prepareDashboard = function ( ) {
2018-04-29 12:47:24 +00:00
page . dom = document . getElementById ( 'page' )
2018-09-04 15:49:37 +00:00
page . dom . addEventListener ( 'click' , page . domClick , true )
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-07-17 03:21:04 +00:00
page . getUploads ( )
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-07-17 03:21:04 +00:00
page . deleteByNames ( )
2018-05-05 19:44:58 +00:00
} )
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-07-17 03:21:04 +00:00
page . getAlbums ( )
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-07-17 03:21:04 +00:00
page . changeFileLength ( )
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-07-17 03:21:04 +00:00
page . changeToken ( )
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-07-17 03:21:04 +00:00
page . changePassword ( )
2018-01-23 20:06:30 +00:00
} )
2018-07-17 03:21:04 +00:00
var logoutBtn = document . getElementById ( 'itemLogout' )
logoutBtn . addEventListener ( 'click' , function ( ) {
page . logout ( )
} )
2018-09-04 15:49:37 +00:00
logoutBtn . innerHTML = 'Logout ( ' + page . username + ' )'
2018-01-23 20:06:30 +00:00
2018-04-29 12:47:24 +00:00
page . getAlbumsSidebar ( )
2018-09-07 15:22:17 +00:00
page . prepareShareX ( )
2018-01-23 20:06:30 +00:00
}
2018-07-14 03:42:18 +00:00
page . logout = function ( ) {
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-09-04 15:49:37 +00:00
page . getItemID = function ( element ) {
// This expects the item's parent to have the item's ID
var parent = element . parentNode
// If the element is part of a set of controls, use the container's parent instead
if ( element . parentNode . classList . contains ( 'controls' ) ) { parent = parent . parentNode }
return parseInt ( parent . dataset . id )
}
page . domClick = function ( event ) {
var element = event . target
if ( ! element ) { return }
// If the clicked element is an icon, delegate event to its A parent; hacky
if ( element . tagName === 'I' && element . parentNode . tagName === 'SPAN' ) { element = element . parentNode }
if ( element . tagName === 'SPAN' && element . parentNode . tagName === 'A' ) { element = element . parentNode }
// Skip elements that have no action data
if ( ! element . dataset || ! element . dataset . action ) { return }
event . stopPropagation ( ) // maybe necessary
var id = page . getItemID ( element )
var action = element . dataset . action
switch ( action ) {
2018-09-07 15:02:04 +00:00
case 'page-prev' :
if ( page . currentView . pageNum === 0 ) {
return swal ( 'Can\'t do that!' , 'This is already the first page!' , 'warning' )
}
return page . getUploads ( page . currentView . album , page . currentView . pageNum - 1 , element )
case 'page-next' :
return page . getUploads ( page . currentView . album , page . currentView . pageNum + 1 , element )
case 'view-list' :
return page . setFilesView ( 'list' , element )
case 'view-thumbs' :
return page . setFilesView ( 'thumbs' , element )
case 'clear-selection' :
return page . clearSelection ( )
case 'add-selected-files-to-album' :
return page . addSelectedFilesToAlbum ( )
case 'bulk-delete' :
return page . deleteSelectedFiles ( )
case 'select-file' :
return page . selectFile ( element , event )
case 'add-to-album' :
return page . addSingleFileToAlbum ( id )
case 'delete-file' :
return page . deleteFile ( id )
case 'select-all-files' :
return page . selectAllFiles ( element )
case 'display-thumbnail' :
return page . displayThumbnail ( id )
case 'delete-file-by-names' :
return page . deleteFileByNames ( )
case 'submit-album' :
return page . submitAlbum ( element )
case 'edit-album' :
return page . editAlbum ( id )
case 'delete-album' :
return page . deleteAlbum ( id )
case 'get-new-token' :
return page . getNewToken ( element )
2018-09-04 15:49:37 +00:00
}
}
2018-07-14 03:42:18 +00:00
page . isLoading = function ( 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-07-14 03:42:18 +00:00
page . getUploads = function ( 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-07-14 03:42:18 +00:00
var url = 'api/uploads/' + pageNum
2018-04-29 12:47:24 +00:00
if ( album !== undefined ) { url = 'api/album/' + album + '/' + pageNum }
2018-01-23 20:06:30 +00:00
2018-07-14 03:42:18 +00:00
axios . get ( url )
. then ( function ( response ) {
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-05-06 14:14:57 +00:00
2018-09-04 15:49:37 +00:00
if ( pageNum && ( response . data . files . length === 0 ) ) {
// Only remove loading class here, since beyond this the entire page will be replaced anyways
if ( element ) { page . isLoading ( element , false ) }
return swal ( 'Can\'t do that!' , 'There are no more files!' , 'warning' )
}
2018-07-14 03:42:18 +00:00
page . files = { }
2018-05-06 14:14:57 +00:00
2018-09-04 15:49:37 +00:00
var pagination =
'<nav class="pagination is-centered">\n' +
' <a class="button pagination-previous" data-action="page-prev">Previous</a>\n' +
' <a class="button pagination-next" data-action="page-next">Next page</a>\n' +
'</nav>'
var controls =
'<div class="columns">\n' +
' <div class="column is-hidden-mobile"></div>\n' +
' <div class="column" style="text-align: center">\n' +
' <a class="button is-small is-danger" title="List view" data-action="view-list">\n' +
' <span class="icon">\n' +
' <i class="icon-th-list"></i>\n' +
' </span>\n' +
' </a>\n' +
' <a class="button is-small is-danger" title="Thumbs view" data-action="view-thumbs">\n' +
' <span class="icon">\n' +
' <i class="icon-th-large"></i>\n' +
' </span>\n' +
' </a>\n' +
' </div>\n' +
' <div class="column" style="text-align: right">\n' +
' <a class="button is-small is-info" title="Clear selection" data-action="clear-selection">\n' +
' <span class="icon">\n' +
' <i class="icon-cancel"></i>\n' +
' </span>\n' +
' </a>\n' +
' <a class="button is-small is-warning" title="Add selected files to album" data-action="add-selected-files-to-album">\n' +
' <span class="icon">\n' +
' <i class="icon-plus"></i>\n' +
' </span>\n' +
' </a>\n' +
' <a class="button is-small is-danger" title="Bulk delete" data-action="bulk-delete">\n' +
' <span class="icon">\n' +
' <i class="icon-trash"></i>\n' +
' </span>\n' +
' <span>Bulk delete</span>\n' +
' </a>\n' +
' </div>\n' +
'</div>'
2018-01-23 20:06:30 +00:00
2018-07-14 03:42:18 +00:00
var allFilesSelected = true
2018-09-04 15:49:37 +00:00
var table , i , file , selected , displayAlbumOrUser
2018-05-06 14:14:57 +00:00
2018-07-14 03:42:18 +00:00
if ( page . filesView === 'thumbs' ) {
2018-09-04 15:49:37 +00:00
page . dom . innerHTML =
pagination + '\n' +
'<hr>\n' +
controls + '\n' +
'<div id="table" class="columns is-multiline is-mobile is-centered">\n' +
'</div>\n' +
pagination
2018-01-23 20:06:30 +00:00
2018-07-14 03:42:18 +00:00
table = document . getElementById ( 'table' )
2018-09-04 15:49:37 +00:00
for ( i = 0 ; i < response . data . files . length ; i ++ ) {
file = response . data . files [ i ]
2018-07-14 03:42:18 +00:00
selected = page . selectedFiles . includes ( file . id )
if ( ! selected && allFilesSelected ) { allFilesSelected = false }
displayAlbumOrUser = file . album
if ( page . username === 'root' ) {
displayAlbumOrUser = ''
if ( file . username !== undefined ) { displayAlbumOrUser = file . username }
}
var div = document . createElement ( 'div' )
div . className = 'image-container column is-narrow'
2018-09-04 15:49:37 +00:00
div . dataset . id = file . id
2018-07-14 03:42:18 +00:00
if ( file . thumb !== undefined ) {
2018-09-04 15:49:37 +00:00
div . innerHTML = '<a class="image" href="' + file . file + '" target="_blank" rel="noopener"><img alt="' + file . name + '" data-src="' + file . thumb + '"/></a>'
2018-07-14 03:42:18 +00:00
} else {
2018-09-04 15:49:37 +00:00
div . innerHTML = '<a class="image" href="' + file . file + '" target="_blank" rel="noopener"><h1 class="title">' + ( file . extname || 'N/A' ) + '</h1></a>'
2018-07-14 03:42:18 +00:00
}
2018-09-04 15:49:37 +00:00
div . innerHTML +=
'<input type="checkbox" class="file-checkbox" title="Select this file" data-action="select-file"' + ( selected ? ' checked' : '' ) + '>\n' +
'<div class="controls">\n' +
' <a class="button is-small is-info clipboard-js" title="Copy link to clipboard" data-clipboard-text="' + file . file + '">\n' +
' <span class="icon">\n' +
' <i class="icon-clipboard-1"></i>\n' +
' </span>\n' +
' </a>\n' +
' <a class="button is-small is-warning" title="Add to album" data-action="add-to-album">\n' +
' <span class="icon">\n' +
' <i class="icon-plus"></i>\n' +
' </span>\n' +
' </a>\n' +
' <a class="button is-small is-danger" title="Delete file" data-action="delete-file">\n' +
' <span class="icon">\n' +
' <i class="icon-trash"></i>\n' +
' </span>\n' +
' </a>\n' +
'</div>\n' +
'<div class="details">\n' +
' <p><span class="name" title="' + file . file + '">' + file . name + '</span></p>\n' +
' <p>' + ( displayAlbumOrUser ? ( '<span>' + displayAlbumOrUser + '</span> – ' ) : '' ) + file . size + '</p>\n' +
'</div>'
2018-07-14 03:42:18 +00:00
table . appendChild ( div )
page . checkboxes = Array . from ( table . getElementsByClassName ( 'file-checkbox' ) )
page . lazyLoad . update ( )
}
} else {
var albumOrUser = 'Album'
if ( page . username === 'root' ) { albumOrUser = 'User' }
2018-09-04 15:49:37 +00:00
page . dom . innerHTML =
pagination + '\n' +
'<hr>\n' +
controls + '\n' +
'<div class="table-container">\n' +
' <table class="table is-narrow is-fullwidth is-hoverable">\n' +
' <thead>\n' +
' <tr>\n' +
' <th><input id="selectAll" type="checkbox" title="Select all files" data-action="select-all-files"></th>\n' +
' <th style="width: 25%">File</th>\n' +
' <th>' + albumOrUser + '</th>\n' +
' <th>Size</th>\n' +
' <th>Date</th>\n' +
' <th></th>\n' +
' </tr>\n' +
' </thead>\n' +
' <tbody id="table">\n' +
' </tbody>\n' +
' </table>\n' +
'</div>\n' +
'<hr>\n' +
pagination
2018-01-23 20:06:30 +00:00
2018-07-14 03:42:18 +00:00
table = document . getElementById ( 'table' )
2018-03-28 12:28:17 +00:00
2018-09-04 15:49:37 +00:00
for ( i = 0 ; i < response . data . files . length ; i ++ ) {
file = response . data . files [ i ]
2018-07-14 03:42:18 +00:00
selected = page . selectedFiles . includes ( file . id )
if ( ! selected && allFilesSelected ) { allFilesSelected = false }
page . files [ file . id ] = {
name : file . name ,
thumb : file . thumb
}
displayAlbumOrUser = file . album
if ( page . username === 'root' ) {
displayAlbumOrUser = ''
if ( file . username !== undefined ) { displayAlbumOrUser = file . username }
}
var tr = document . createElement ( 'tr' )
2018-09-04 15:49:37 +00:00
tr . dataset . id = file . id
tr . innerHTML =
'<th class="controls"><input type="checkbox" class="file-checkbox" title="Select this file" data-action="select-file"' + ( selected ? ' checked' : '' ) + '></th>\n' +
'<th><a href="' + file . file + '" target="_blank" rel="noopener" title="' + file . file + '">' + file . name + '</a></th>\n' +
'<th>' + displayAlbumOrUser + '</th>\n' +
'<td>' + file . size + '</td>\n' +
'<td>' + file . date + '</td>\n' +
'<td class="controls" style="text-align: right" >\n' +
' <a class="button is-small is-primary" title="View thumbnail" data-action="display-thumbnail"' + ( file . thumb ? '' : ' disabled' ) + '>\n' +
' <span class="icon">\n' +
' <i class="icon-picture-1"></i>\n' +
' </span>\n' +
' </a>\n' +
' <a class="button is-small is-info clipboard-js" title="Copy link to clipboard" data-clipboard-text="' + file . file + '">\n' +
' <span class="icon">\n' +
' <i class="icon-clipboard-1"></i>\n' +
' </span>\n' +
' </a>\n' +
' <a class="button is-small is-warning" title="Add to album" data-action="add-to-album">\n' +
' <span class="icon">\n' +
' <i class="icon-plus"></i>\n' +
' </span>\n' +
' </a>\n' +
' <a class="button is-small is-danger" title="Delete file" data-action="delete-file">\n' +
' <span class="icon">\n' +
' <i class="icon-trash"></i>\n' +
' </span>\n' +
' </a>\n' +
'</td>'
2018-07-14 03:42:18 +00:00
table . appendChild ( tr )
page . checkboxes = Array . from ( table . getElementsByClassName ( 'file-checkbox' ) )
}
}
if ( allFilesSelected && response . data . files . length ) {
var selectAll = document . getElementById ( 'selectAll' )
if ( selectAll ) { selectAll . checked = true }
}
2018-04-28 23:44:25 +00:00
2018-07-14 03:42:18 +00:00
page . currentView . album = album
2018-09-04 15:49:37 +00:00
page . currentView . pageNum = response . data . files . length ? pageNum : 0
2018-07-14 03:42:18 +00:00
} )
. catch ( function ( 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-07-14 03:42:18 +00:00
page . setFilesView = function ( 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-07-14 03:42:18 +00:00
page . displayThumbnail = function ( id ) {
var file = page . files [ id ]
2018-05-05 19:44:58 +00:00
if ( ! file . thumb ) { return }
2018-07-14 03:42:18 +00:00
return 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-07-14 03:42:18 +00:00
page . selectAllFiles = function ( element ) {
var table = document . getElementById ( 'table' )
var checkboxes = table . getElementsByClassName ( 'file-checkbox' )
2018-04-03 15:59:39 +00:00
2018-07-14 04:37:36 +00:00
for ( var i = 0 ; i < checkboxes . length ; i ++ ) {
2018-09-04 15:49:37 +00:00
var id = page . getItemID ( checkboxes [ i ] )
2018-04-03 15:59:39 +00:00
if ( isNaN ( id ) ) { continue }
2018-07-14 04:37:36 +00:00
if ( checkboxes [ i ] . checked !== element . checked ) {
checkboxes [ i ] . checked = element . checked
if ( checkboxes [ i ] . 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-07-14 03:42:18 +00:00
page . selectInBetween = function ( 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-07-14 03:42:18 +00:00
var thisIndex = page . checkboxes . indexOf ( element )
var lastIndex = page . checkboxes . indexOf ( lastElement )
2018-04-04 18:23:45 +00:00
2018-07-14 03:42:18 +00:00
var distance = thisIndex - lastIndex
2018-04-04 18:23:45 +00:00
if ( distance >= - 1 && distance <= 1 ) { return }
2018-07-14 03:42:18 +00:00
for ( var 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
2018-09-04 15:49:37 +00:00
page . selectedFiles . push ( page . getItemID ( page . checkboxes [ i ] ) )
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-07-14 03:42:18 +00:00
page . selectFile = function ( element , event ) {
2018-04-29 12:47:24 +00:00
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-09-04 15:49:37 +00:00
var id = page . getItemID ( element )
2018-04-03 15:59:39 +00:00
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-07-14 03:42:18 +00:00
page . clearSelection = function ( ) {
var 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' )
}
2018-09-04 15:49:37 +00:00
var suffix = 'file' + ( count === 1 ? '' : 's' )
2018-07-14 03:42:18 +00:00
return swal ( {
2018-04-03 15:59:39 +00:00
title : 'Are you sure?' ,
2018-09-04 15:49:37 +00:00
text : 'You are going to unselect ' + count + ' ' + suffix + '.' ,
2018-04-03 15:59:39 +00:00
buttons : true
} )
2018-07-14 03:42:18 +00:00
. then ( function ( proceed ) {
if ( ! proceed ) { return }
2018-04-03 15:59:39 +00:00
2018-07-14 03:42:18 +00:00
var table = document . getElementById ( 'table' )
var checkboxes = table . getElementsByClassName ( 'file-checkbox' )
2018-04-03 15:59:39 +00:00
2018-07-14 04:37:36 +00:00
for ( var i = 0 ; i < checkboxes . length ; i ++ ) {
if ( checkboxes [ i ] . checked ) {
checkboxes [ i ] . checked = false
2018-07-14 03:42:18 +00:00
}
}
2018-04-03 15:59:39 +00:00
2018-07-14 03:42:18 +00:00
page . selectedFiles = [ ]
localStorage . removeItem ( 'selectedFiles' )
2018-04-03 15:59:39 +00:00
2018-07-14 03:42:18 +00:00
var selectAll = document . getElementById ( 'selectAll' )
if ( selectAll ) { selectAll . checked = false }
2018-04-03 15:59:39 +00:00
2018-09-04 15:49:37 +00:00
return swal ( 'Cleared selection!' , 'Unselected ' + count + ' ' + suffix + '.' , 'success' )
2018-07-14 03:42:18 +00:00
} )
2018-04-03 15:59:39 +00:00
}
2018-07-14 03:42:18 +00:00
page . deleteFile = function ( 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-07-14 03:42:18 +00:00
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-07-14 03:42:18 +00:00
. then ( function ( proceed ) {
if ( ! proceed ) { return }
2018-09-04 15:49:37 +00:00
axios . post ( 'api/upload/delete' , { id : id } )
2018-07-14 03:42:18 +00:00
. then ( function ( response ) {
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 )
} )
. catch ( function ( 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-05-06 14:14:57 +00:00
} )
2018-03-29 23:22:08 +00:00
}
2018-07-14 03:42:18 +00:00
page . deleteSelectedFiles = function ( ) {
var 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
}
2018-09-04 15:49:37 +00:00
var suffix = 'file' + ( count === 1 ? '' : 's' )
2018-07-14 03:42:18 +00:00
swal ( {
2018-03-29 23:22:08 +00:00
title : 'Are you sure?' ,
2018-09-04 15:49:37 +00:00
text : 'You won\'t be able to recover ' + count + ' ' + suffix + '!' ,
2018-03-29 23:22:08 +00:00
icon : 'warning' ,
dangerMode : true ,
buttons : {
cancel : true ,
confirm : {
2018-09-04 15:49:37 +00:00
text : 'Yes, nuke the ' + suffix + '!' ,
2018-03-29 23:22:08 +00:00
closeModal : false
}
}
2018-03-31 14:26:53 +00:00
} )
2018-07-14 03:42:18 +00:00
. then ( function ( proceed ) {
if ( ! proceed ) { return }
2018-03-31 14:26:53 +00:00
2018-07-14 03:42:18 +00:00
axios . post ( 'api/upload/bulkdelete' , {
field : 'id' ,
values : page . selectedFiles
} )
. then ( function ( bulkdelete ) {
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' )
}
}
var deleted = count
if ( bulkdelete . data . failed && bulkdelete . data . failed . length ) {
deleted -= bulkdelete . data . failed . length
page . selectedFiles = page . selectedFiles . filter ( function ( id ) {
return bulkdelete . data . failed . includes ( id )
} )
} else {
page . selectedFiles = [ ]
}
localStorage . selectedFiles = JSON . stringify ( page . selectedFiles )
2018-09-04 15:49:37 +00:00
swal ( 'Deleted!' , deleted + ' file' + ( deleted === 1 ? ' has' : 's have' ) + ' been deleted.' , 'success' )
2018-07-14 03:42:18 +00:00
return page . getUploads ( page . currentView . album , page . currentView . pageNum )
} )
. catch ( function ( 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-01-23 20:06:30 +00:00
}
2018-07-14 03:42:18 +00:00
page . deleteByNames = function ( ) {
2018-09-04 15:49:37 +00:00
page . dom . innerHTML =
'<h2 class="subtitle">Delete by names</h2>\n' +
'<div class="field">\n' +
' <label class="label">File names:</label>\n' +
' <div class="control">\n' +
' <textarea id="names" class="textarea"></textarea>\n' +
' </div>\n' +
' <p class="help">Separate each entry with a new line.</p>\n' +
'</div>\n' +
'<div class="field">\n' +
' <div class="control">\n' +
' <a class="button is-danger is-fullwidth" data-action="delete-file-by-names">\n' +
' <span class="icon">\n' +
' <i class="icon-trash"></i>\n' +
' </span>\n' +
' <span>Bulk delete</span>\n' +
' </a>\n' +
' </div>\n' +
'</div>'
2018-05-05 19:44:58 +00:00
}
2018-07-14 03:42:18 +00:00
page . deleteFileByNames = function ( ) {
var names = document . getElementById ( 'names' ) . value
. split ( /\r?\n/ )
. filter ( function ( n ) {
return n . trim ( ) . length
} )
var count = names . length
2018-05-05 19:44:58 +00:00
if ( ! count ) {
return swal ( 'An error occurred!' , 'You have not entered any file names.' , 'error' )
}
2018-09-04 15:49:37 +00:00
var suffix = 'file' + ( count === 1 ? '' : 's' )
2018-07-14 03:42:18 +00:00
swal ( {
2018-05-05 19:44:58 +00:00
title : 'Are you sure?' ,
2018-09-04 15:49:37 +00:00
text : 'You won\'t be able to recover ' + count + ' ' + suffix + '!' ,
2018-05-05 19:44:58 +00:00
icon : 'warning' ,
dangerMode : true ,
buttons : {
cancel : true ,
confirm : {
2018-09-04 15:49:37 +00:00
text : 'Yes, nuke the ' + suffix + '!' ,
2018-05-05 19:44:58 +00:00
closeModal : false
}
}
} )
2018-07-14 03:42:18 +00:00
. then ( function ( proceed ) {
if ( ! proceed ) { return }
2018-05-05 19:44:58 +00:00
2018-07-14 03:42:18 +00:00
axios . post ( 'api/upload/bulkdelete' , {
field : 'name' ,
values : names
} )
. then ( function ( bulkdelete ) {
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' )
}
}
var deleted = count
if ( bulkdelete . data . failed && bulkdelete . data . failed . length ) {
deleted -= bulkdelete . data . failed . length
}
document . getElementById ( 'names' ) . value = bulkdelete . data . failed . join ( '\n' )
2018-09-04 15:49:37 +00:00
swal ( 'Deleted!' , deleted + ' file' + ( deleted === 1 ? ' has' : 's have' ) + ' been deleted.' , 'success' )
2018-07-14 03:42:18 +00:00
} )
. catch ( function ( error ) {
console . log ( error )
swal ( 'An error occurred!' , 'There was an error with the request, please check the console for more information.' , 'error' )
} )
2018-05-05 19:44:58 +00:00
} )
}
2018-07-14 03:42:18 +00:00
page . addSelectedFilesToAlbum = function ( ) {
var 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-07-14 03:42:18 +00:00
page . addFilesToAlbum ( page . selectedFiles , function ( failed ) {
if ( ! failed ) { return }
if ( failed . length ) {
page . selectedFiles = page . selectedFiles . filter ( function ( id ) {
return failed . includes ( id )
} )
} else {
page . selectedFiles = [ ]
}
localStorage . selectedFiles = JSON . stringify ( page . selectedFiles )
page . getUploads ( page . currentView . album , page . currentView . pageNum )
} )
2018-04-28 23:44:25 +00:00
}
2018-07-14 03:42:18 +00:00
page . addSingleFileToAlbum = function ( id ) {
page . addFilesToAlbum ( [ id ] , function ( failed ) {
if ( ! failed ) { return }
page . getUploads ( page . currentView . album , page . currentView . pageNum )
} )
2018-03-30 02:39:53 +00:00
}
2018-07-14 03:42:18 +00:00
page . addFilesToAlbum = function ( ids , callback ) {
var count = ids . length
return swal ( {
2018-03-30 02:39:53 +00:00
title : 'Are you sure?' ,
2018-09-04 15:49:37 +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
}
}
} )
2018-07-14 03:42:18 +00:00
. then ( function ( proceed ) {
if ( ! proceed ) { return }
axios . get ( 'api/albums' )
. then ( function ( list ) {
if ( ! list ) { return }
if ( list . data . success === false ) {
if ( list . data . description === 'No token provided' ) {
page . verifyToken ( page . token )
} else {
swal ( 'An error occurred!' , list . data . description , 'error' )
}
return
}
if ( ! page . selectAlbumContainer ) {
// We want to this to be re-usable
page . selectAlbumContainer = document . createElement ( 'div' )
page . selectAlbumContainer . id = 'selectAlbum'
}
var options = list . data . albums
. map ( function ( album ) {
2018-09-04 15:49:37 +00:00
return '<option value="' + album . id + '">' + album . name + '</option>'
2018-07-14 03:42:18 +00:00
} )
. join ( '\n' )
2018-09-04 15:49:37 +00:00
page . selectAlbumContainer . innerHTML =
'<div class="field">\n' +
' <label class="label">If a file is already in an album, it will be moved.</label>\n' +
' <div class="control">\n' +
' <div class="select is-fullwidth">\n' +
' <select>\n' +
' <option value="-1">Remove from album</option>\n' +
' <option value="" selected disabled>Choose an album</option>\n' +
' ' + options + '\n' +
' </select>\n' +
' </div>\n' +
'</div>'
2018-07-14 03:42:18 +00:00
return swal ( {
content : page . selectAlbumContainer ,
buttons : {
cancel : true ,
confirm : {
text : 'OK' ,
closeModal : false
}
}
} )
. then ( function ( choose ) {
if ( ! choose ) { return }
var albumid = parseInt ( page . selectAlbumContainer . getElementsByTagName ( 'select' ) [ 0 ] . value )
if ( isNaN ( albumid ) ) {
return swal ( 'An error occurred!' , 'You did not choose an album.' , 'error' )
}
2018-09-04 15:49:37 +00:00
axios . post ( 'api/albums/addfiles' , {
ids : ids ,
albumid : albumid
} )
2018-07-14 03:42:18 +00:00
. then ( function ( add ) {
if ( ! add ) { return }
if ( add . data . success === false ) {
if ( add . data . description === 'No token provided' ) {
page . verifyToken ( page . token )
} else {
swal ( 'An error occurred!' , add . data . description , 'error' )
}
return
}
var added = ids . length
if ( add . data . failed && add . data . failed . length ) {
added -= add . data . failed . length
}
2018-09-04 15:49:37 +00:00
var suffix = 'file' + ( ids . length === 1 ? '' : 's' )
2018-07-14 03:42:18 +00:00
if ( ! added ) {
2018-09-04 15:49:37 +00:00
return swal ( 'An error occurred!' , 'Could not add the ' + suffix + ' to the album.' , 'error' )
2018-07-14 03:42:18 +00:00
}
2018-09-04 15:49:37 +00:00
swal ( 'Woohoo!' , 'Successfully ' + ( albumid < 0 ? 'removed' : 'added' ) + ' ' + added + ' ' + suffix + ' ' + ( albumid < 0 ? 'from' : 'to' ) + ' the album.' , 'success' )
2018-07-14 03:42:18 +00:00
return callback ( add . data . failed )
} )
. catch ( function ( error ) {
console . log ( error )
return swal ( 'An error occurred!' , 'There was an error with the request, please check the console for more information.' , 'error' )
} )
} )
} )
. catch ( function ( 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-03-30 02:39:53 +00:00
} )
}
2018-07-14 03:42:18 +00:00
page . getAlbums = function ( ) {
axios . get ( 'api/albums' )
. then ( function ( response ) {
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-05-06 14:14:57 +00:00
2018-07-14 03:42:18 +00:00
page . albums = { }
2018-01-23 20:06:30 +00:00
2018-09-04 15:49:37 +00:00
page . dom . innerHTML =
'<h2 class="subtitle">Create new album</h2>\n' +
'<div class="field">\n' +
' <div class="control">\n' +
' <input id="albumName" class="input" type="text" placeholder="Name">\n' +
' </div>\n' +
'</div>\n' +
'<div class="field">\n' +
' <div class="control">\n' +
' <a id="submitAlbum" class="button is-breeze is-fullwidth" data-action="submit-album">\n' +
' <span class="icon">\n' +
' <i class="icon-paper-plane-empty"></i>\n' +
' </span>\n' +
' <span>Create</span>\n' +
' </a>\n' +
' </div>\n' +
'</div>\n' +
'<hr>\n' +
'<h2 class="subtitle">List of albums</h2>\n' +
'<div class="table-container">\n' +
' <table class="table is-fullwidth is-hoverable">\n' +
' <thead>\n' +
' <tr>\n' +
' <th>ID</th>\n' +
' <th>Name</th>\n' +
' <th>Files</th>\n' +
' <th>Created at</th>\n' +
' <th>Public link</th>\n' +
' <th></th>\n' +
' </tr>\n' +
' </thead>\n' +
' <tbody id="table">\n' +
' </tbody>\n' +
' </table>\n' +
'</div>'
2018-01-23 20:06:30 +00:00
2018-07-14 03:42:18 +00:00
var homeDomain = response . data . homeDomain
var table = document . getElementById ( 'table' )
2018-05-05 19:44:58 +00:00
2018-09-04 15:49:37 +00:00
for ( var i = 0 ; i < response . data . albums . length ; i ++ ) {
var album = response . data . albums [ i ]
var albumUrl = homeDomain + '/a/' + album . identifier
2018-05-05 19:44:58 +00:00
2018-07-14 03:42:18 +00:00
page . albums [ album . id ] = {
name : album . name ,
download : album . download ,
public : album . public
}
2018-01-23 20:06:30 +00:00
2018-07-14 03:42:18 +00:00
var tr = document . createElement ( 'tr' )
2018-09-04 15:49:37 +00:00
tr . innerHTML =
'<tr>\n' +
' <th>' + album . id + '</th>\n' +
' <th>' + album . name + '</th>\n' +
' <th>' + album . files + '</th>\n' +
' <td>' + album . date + '</td>\n' +
' <td><a' + ( album . public ? ( ' href="' + albumUrl + '"' ) : '' ) + ' target="_blank" rel="noopener">' + albumUrl + '</a></td>\n' +
' <td style="text-align: right" data-id="' + album . id + '">\n' +
' <a class="button is-small is-primary" title="Edit album" data-action="edit-album">\n' +
' <span class="icon is-small">\n' +
' <i class="icon-pencil-1"></i>\n' +
' </span>\n' +
' </a>\n' +
' <a class="button is-small is-info clipboard-js" title="Copy link to clipboard" ' + ( album . public ? ( 'data-clipboard-text="' + albumUrl + '"' ) : 'disabled' ) + '>\n' +
' <span class="icon is-small">\n' +
' <i class="icon-clipboard-1"></i>\n' +
' </span>\n' +
' </a>\n' +
' <a class="button is-small is-warning" title="Download album" ' + ( album . download ? ( 'href="api/album/zip/' + album . identifier + '?v=' + album . editedAt + '"' ) : 'disabled' ) + '>\n' +
' <span class="icon is-small">\n' +
' <i class="icon-download"></i>\n' +
' </span>\n' +
' </a>\n' +
' <a class="button is-small is-danger" title="Delete album" data-action="delete-album">\n' +
' <span class="icon is-small">\n' +
' <i class="icon-trash"></i>\n' +
' </span>\n' +
' </a>\n' +
' </td>\n' +
'</tr>'
2018-01-23 20:06:30 +00:00
2018-07-14 03:42:18 +00:00
table . appendChild ( tr )
}
} )
. catch ( function ( 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-07-14 03:42:18 +00:00
page . editAlbum = function ( id ) {
var album = page . albums [ id ]
2018-05-05 19:44:58 +00:00
if ( ! album ) { return }
2018-04-28 17:26:39 +00:00
2018-07-14 03:42:18 +00:00
var div = document . createElement ( 'div' )
2018-09-04 15:49:37 +00:00
div . innerHTML =
'<div class="field">\n' +
' <label class="label">Album name</label>\n' +
' <div class="controls">\n' +
' <input id="_name" class="input" type="text" value="' + ( album . name || '' ) + '">\n' +
' </div>\n' +
'</div>\n' +
'<div class="field">\n' +
' <div class="control">\n' +
' <label class="checkbox">\n' +
' <input id="_download" type="checkbox" ' + ( album . download ? 'checked' : '' ) + '>\n' +
' Enable download\n' +
' </label>\n' +
' </div>\n' +
'</div>\n' +
'<div class="field">\n' +
' <div class="control">\n' +
' <label class="checkbox">\n' +
' <input id="_public" type="checkbox" ' + ( album . public ? 'checked' : '' ) + '>\n' +
' Enable public link\n' +
' </label>\n' +
' </div>\n' +
'</div>\n' +
'<div class="field">\n' +
' <div class="control">\n' +
' <label class="checkbox">\n' +
' <input id="_requestLink" type="checkbox">\n' +
' Request new public link\n' +
' </label>\n' +
' </div>\n' +
'</div>'
2018-07-14 03:42:18 +00:00
swal ( {
2018-04-28 17:26:39 +00:00
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
} )
2018-07-14 03:42:18 +00:00
. then ( function ( value ) {
if ( ! value ) { return }
axios . post ( 'api/albums/edit' , {
2018-09-04 15:49:37 +00:00
id : id ,
2018-07-14 03:42:18 +00:00
name : document . getElementById ( '_name' ) . value ,
download : document . getElementById ( '_download' ) . checked ,
public : document . getElementById ( '_public' ) . checked ,
requestLink : document . getElementById ( '_requestLink' ) . checked
} )
. then ( function ( response ) {
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' )
}
}
if ( response . data . identifier ) {
2018-09-04 15:49:37 +00:00
swal ( 'Success!' , 'Your album\'s new identifier is: ' + response . data . identifier + '.' , 'success' )
2018-07-14 03:42:18 +00:00
} else if ( response . data . name !== album . name ) {
2018-09-04 15:49:37 +00:00
swal ( 'Success!' , 'Your album was renamed to: ' + response . data . name + '.' , 'success' )
2018-07-14 03:42:18 +00:00
} else {
swal ( 'Success!' , 'Your album was edited!' , 'success' )
}
page . getAlbumsSidebar ( )
page . getAlbums ( )
} )
. catch ( function ( 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-07-14 03:42:18 +00:00
page . deleteAlbum = function ( id ) {
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
} )
2018-07-14 03:42:18 +00:00
. then ( function ( proceed ) {
if ( ! proceed ) { return }
2018-09-04 15:49:37 +00:00
console . log ( proceed , proceed === 'purge' )
2018-01-23 20:06:30 +00:00
2018-07-14 03:42:18 +00:00
axios . post ( 'api/albums/delete' , {
2018-09-04 15:49:37 +00:00
id : id ,
2018-07-14 03:42:18 +00:00
purge : proceed === 'purge'
} )
. then ( function ( response ) {
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!' , 'Your album has been deleted.' , 'success' )
page . getAlbumsSidebar ( )
page . getAlbums ( )
} )
. catch ( function ( 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-04-28 17:26:39 +00:00
} )
2018-01-23 20:06:30 +00:00
}
2018-07-14 03:42:18 +00:00
page . submitAlbum = function ( element ) {
2018-04-29 12:47:24 +00:00
page . isLoading ( element , true )
2018-05-06 14:14:57 +00:00
2018-07-14 03:42:18 +00:00
axios . post ( 'api/albums' , {
2018-01-23 20:06:30 +00:00
name : document . getElementById ( 'albumName' ) . value
} )
2018-07-14 03:42:18 +00:00
. then ( function ( response ) {
if ( ! response ) { return }
2018-01-23 20:06:30 +00:00
2018-07-14 03:42:18 +00:00
page . isLoading ( element , false )
2018-01-23 20:06:30 +00:00
2018-07-14 03:42:18 +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-07-14 03:42:18 +00:00
swal ( 'Woohoo!' , 'Album was created successfully' , 'success' )
page . getAlbumsSidebar ( )
page . getAlbums ( )
} )
. catch ( function ( error ) {
2018-03-29 23:22:08 +00:00
console . log ( error )
2018-07-14 03:42:18 +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-07-14 03:42:18 +00:00
}
2018-05-06 14:14:57 +00:00
2018-07-14 03:42:18 +00:00
page . getAlbumsSidebar = function ( ) {
axios . get ( 'api/albums/sidebar' )
. then ( function ( response ) {
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-05-06 14:14:57 +00:00
2018-07-14 03:42:18 +00:00
var albumsContainer = document . getElementById ( 'albumsContainer' )
albumsContainer . innerHTML = ''
2018-05-06 14:14:57 +00:00
2018-07-14 03:42:18 +00:00
if ( response . data . albums === undefined ) { return }
2018-05-06 14:14:57 +00:00
2018-09-04 15:49:37 +00:00
for ( var i = 0 ; i < response . data . albums . length ; i ++ ) {
var album = response . data . albums [ i ]
2018-07-14 03:42:18 +00:00
var li = document . createElement ( 'li' )
var a = document . createElement ( 'a' )
a . id = album . id
a . innerHTML = album . name
2018-05-06 14:14:57 +00:00
2018-07-14 03:42:18 +00:00
a . addEventListener ( 'click' , function ( ) {
page . getAlbum ( this )
} )
2018-05-06 14:14:57 +00:00
2018-07-14 03:42:18 +00:00
li . appendChild ( a )
albumsContainer . appendChild ( li )
}
} )
. catch ( function ( 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-07-14 03:42:18 +00:00
page . getAlbum = function ( album ) {
2018-04-29 12:47:24 +00:00
page . setActiveMenu ( album )
page . getUploads ( album . id )
2018-01-23 20:06:30 +00:00
}
2018-07-14 03:42:18 +00:00
page . changeFileLength = function ( ) {
axios . get ( 'api/filelength/config' )
. then ( function ( response ) {
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-05-06 14:14:57 +00:00
2018-09-04 15:49:37 +00:00
page . dom . innerHTML =
'<h2 class="subtitle">File name length</h2>\n' +
'<div class="field">\n' +
' <div class="field">\n' +
' <label class="label">Your current file name length:</label>\n' +
' <div class="control">\n' +
' <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 ) + '">\n' +
' </div>\n' +
' <p class="help">Default file name length is <b>' + response . data . config . default + '</b> characters. ' + ( response . data . config . userChangeable ? ( 'Range allowed for user is <b>' + response . data . config . min + '</b> to <b>' + response . data . config . max + '</b> characters.' ) : 'Changing file name length is disabled at the moment.' ) + '</p>\n' +
' </div>\n' +
' <div class="field">\n' +
' <div class="control">\n' +
' <a id="setFileLength" class="button is-breeze is-fullwidth">\n' +
' <span class="icon">\n' +
' <i class="icon-paper-plane-empty"></i>\n' +
' </span>\n' +
' <span>Set file name length</span>\n' +
' </a>\n' +
' </div>\n' +
' <div>\n' +
'</div>'
2018-03-24 13:52:47 +00:00
2018-07-14 03:42:18 +00:00
document . getElementById ( 'setFileLength' ) . addEventListener ( 'click' , function ( ) {
page . setFileLength ( document . getElementById ( 'fileLength' ) . value , this )
} )
} )
. catch ( function ( error ) {
2018-03-29 23:22:08 +00:00
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-07-14 03:42:18 +00:00
}
2018-01-23 20:06:30 +00:00
2018-07-14 03:42:18 +00:00
page . setFileLength = function ( fileLength , element ) {
page . isLoading ( element , true )
2018-03-19 16:51:39 +00:00
2018-09-04 15:49:37 +00:00
axios . post ( 'api/filelength/change' , { fileLength : fileLength } )
2018-07-14 03:42:18 +00:00
. then ( function ( response ) {
page . isLoading ( element , false )
2018-05-05 19:44:58 +00:00
2018-07-14 03:42:18 +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-07-14 03:42:18 +00:00
swal ( {
title : 'Woohoo!' ,
text : 'Your file length was successfully changed.' ,
icon : 'success'
} )
. then ( function ( ) {
page . changeFileLength ( )
} )
} )
. catch ( function ( error ) {
2018-03-29 23:22:08 +00:00
console . log ( error )
2018-07-14 03:42:18 +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-07-14 03:42:18 +00:00
}
2018-05-06 14:14:57 +00:00
2018-07-14 03:42:18 +00:00
page . changeToken = function ( ) {
axios . get ( 'api/tokens' )
. then ( function ( response ) {
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-09-04 15:49:37 +00:00
page . dom . innerHTML =
'<h2 class="subtitle">Manage your token</h2>\n' +
'<div class="field">\n' +
' <label class="label">Your current token:</label>\n' +
' <div class="field">\n' +
' <div class="control">\n' +
' <input id="token" readonly class="input" type="text" placeholder="Your token" value="' + response . data . token + '">\n' +
' </div>\n' +
' </div>\n' +
'</div>\n' +
'<div class="field">\n' +
' <div class="control">\n' +
' <a id="getNewToken" class="button is-breeze is-fullwidth" data-action="get-new-token">\n' +
' <span class="icon">\n' +
' <i class="icon-arrows-cw"></i>\n' +
' </span>\n' +
' <span>Request new token</span>\n' +
' </a>\n' +
' </div>\n' +
'</div>'
2018-07-14 03:42:18 +00:00
} )
. catch ( function ( 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-07-14 03:42:18 +00:00
page . getNewToken = function ( element ) {
2018-04-29 12:47:24 +00:00
page . isLoading ( element , true )
2018-01-23 20:06:30 +00:00
2018-07-14 03:42:18 +00:00
axios . post ( 'api/tokens/change' )
. then ( function ( response ) {
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' )
}
}
swal ( {
title : 'Woohoo!' ,
text : 'Your token was successfully changed.' ,
icon : 'success'
} )
. then ( function ( ) {
axios . defaults . headers . common . token = response . data . token
localStorage . token = response . data . token
page . token = response . data . token
page . changeToken ( )
} )
} )
. catch ( function ( error ) {
2018-03-29 23:22:08 +00:00
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-07-14 03:42:18 +00:00
page . changePassword = function ( ) {
2018-09-04 15:49:37 +00:00
page . dom . innerHTML =
'<h2 class="subtitle">Change your password</h2>\n' +
'<div class="field">\n' +
' <label class="label">New password:</label>\n' +
' <div class="control">\n' +
' <input id="password" class="input" type="password">\n' +
' </div>\n' +
'</div>\n' +
'<div class="field">\n' +
' <label class="label">Re-type new password:</label>\n' +
' <div class="control">\n' +
' <input id="passwordConfirm" class="input" type="password">\n' +
' </div>\n' +
'</div>\n' +
'<div class="field">\n' +
' <div class="control">\n' +
' <a id="sendChangePassword" class="button is-breeze is-fullwidth">\n' +
' <span class="icon">\n' +
' <i class="icon-paper-plane-empty"></i>\n' +
' </span>\n' +
' <span>Set new password</span>\n' +
' </a>\n' +
' </div>\n' +
'</div>'
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-07-14 03:42:18 +00:00
page . sendNewPassword = function ( pass , element ) {
2018-04-29 12:47:24 +00:00
page . isLoading ( element , true )
2018-01-23 20:06:30 +00:00
2018-07-14 03:42:18 +00:00
axios . post ( 'api/password/change' , { password : pass } )
. then ( function ( response ) {
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' )
}
}
swal ( {
title : 'Woohoo!' ,
text : 'Your password was successfully changed.' ,
icon : 'success'
} )
. then ( function ( ) {
page . changePassword ( )
} )
} )
. catch ( function ( error ) {
2018-03-29 23:22:08 +00:00
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-07-14 03:42:18 +00:00
page . setActiveMenu = function ( activeItem ) {
var menu = document . getElementById ( 'menu' )
var items = menu . getElementsByTagName ( 'a' )
2018-07-14 04:35:31 +00:00
for ( var i = 0 ; i < items . length ; i ++ ) {
items [ i ] . 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-09-07 15:22:17 +00:00
page . prepareShareX = function ( ) {
if ( page . token ) {
// TODO: "location.origin" is unsuitable if the safe is hosted in a subdir (e.i. http://example.com/safe)
var sharexElement = document . getElementById ( 'ShareX' )
var sharexFile =
'{\r\n' +
' "Name": "' + location . hostname + '",\r\n' +
' "DestinationType": "ImageUploader, FileUploader",\r\n' +
' "RequestType": "POST",\r\n' +
' "RequestURL": "' + location . origin + '/api/upload",\r\n' +
' "FileFormName": "files[]",\r\n' +
' "Headers": {\r\n' +
' "token": "' + page . token + '"\r\n' +
' },\r\n' +
' "ResponseType": "Text",\r\n' +
' "URL": "$json:files[0].url$",\r\n' +
' "ThumbnailURL": "$json:files[0].url$"\r\n' +
'}'
var sharexBlob = new Blob ( [ sharexFile ] , { type : 'application/octet-binary' } )
sharexElement . setAttribute ( 'href' , URL . createObjectURL ( sharexBlob ) )
sharexElement . setAttribute ( 'download' , location . hostname + '.sxcu' )
}
}
2018-07-14 03:42:18 +00:00
window . onload = function ( ) {
2018-03-28 11:36:28 +00:00
// 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-07-14 03:42:18 +00:00
var selectedFiles = localStorage . selectedFiles
2018-03-29 23:22:08 +00:00
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-07-14 03:42:18 +00:00
page . clipboardJS . on ( 'success' , function ( ) {
2018-03-28 20:05:01 +00:00
return swal ( 'Copied!' , 'The link has been copied to clipboard.' , 'success' )
} )
2018-07-14 03:42:18 +00:00
page . clipboardJS . on ( 'error' , function ( 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
}