2014-01-22 10:12:51 +00:00
/ * *
* @ name Album Module
* @ description Takes care of every action an album can handle and execute .
* @ author Tobias Reich
* @ copyright 2014 by Tobias Reich
* /
upload = {
2014-07-23 19:23:10 +00:00
show : function ( title , files , callback ) {
2014-01-22 10:12:51 +00:00
upload . close ( true ) ;
2014-05-12 19:40:25 +00:00
$ ( "body" ) . append ( build . uploadModal ( title , files ) ) ;
2014-01-22 10:12:51 +00:00
2014-07-23 19:23:10 +00:00
if ( callback != null && callback != undefined ) callback ( ) ;
2014-01-22 10:12:51 +00:00
} ,
setIcon : function ( icon ) {
$ ( ".upload_message a" ) . remove ( ) ;
$ ( ".upload_message" ) . prepend ( "<a class='icon-" + icon + "'></a>" ) ;
} ,
setProgress : function ( progress ) {
$ ( ".progressbar div" ) . css ( "width" , progress + "%" ) ;
} ,
setText : function ( text ) {
$ ( ".progressbar" ) . remove ( ) ;
$ ( ".upload_message" ) . append ( "<p>" + text + "</p>" ) ;
} ,
2014-02-17 15:22:53 +00:00
2014-02-04 21:08:23 +00:00
notify : function ( title ) {
2014-02-17 15:22:53 +00:00
2014-02-04 21:08:23 +00:00
var popup ;
2014-02-17 15:22:53 +00:00
2014-02-04 21:08:23 +00:00
if ( ! window . webkitNotifications ) return false ;
2014-02-17 15:22:53 +00:00
2014-02-25 22:37:05 +00:00
if ( window . webkitNotifications . checkPermission ( ) !== 0 ) window . webkitNotifications . requestPermission ( ) ;
2014-02-17 15:22:53 +00:00
2014-02-25 22:37:05 +00:00
if ( window . webkitNotifications . checkPermission ( ) === 0 && title ) {
2014-02-04 21:08:23 +00:00
popup = window . webkitNotifications . createNotification ( "" , title , "You can now manage your new photo(s)." ) ;
popup . show ( ) ;
}
2014-02-17 15:22:53 +00:00
2014-02-04 21:08:23 +00:00
} ,
2014-01-22 10:12:51 +00:00
start : {
local : function ( files ) {
2014-05-11 22:15:01 +00:00
var albumID = album . getID ( ) ,
2014-05-12 19:40:25 +00:00
process = function ( files , file ) {
2014-01-22 10:12:51 +00:00
2014-05-12 19:40:25 +00:00
var formData = new FormData ( ) ,
xhr = new XMLHttpRequest ( ) ,
pre _progress = 0 ,
progress ;
2014-02-17 15:22:53 +00:00
2014-05-12 19:40:25 +00:00
formData . append ( "function" , "upload" ) ;
formData . append ( "albumID" , albumID ) ;
formData . append ( 0 , file ) ;
2014-01-22 10:12:51 +00:00
2014-05-12 19:40:25 +00:00
xhr . open ( "POST" , lychee . api _path ) ;
2014-01-22 10:12:51 +00:00
2014-05-12 19:40:25 +00:00
xhr . onload = function ( ) {
2014-01-22 10:12:51 +00:00
2014-05-12 19:40:25 +00:00
var wait ;
2014-01-22 10:12:51 +00:00
2014-05-12 19:40:25 +00:00
if ( xhr . status === 200 ) {
2014-01-22 10:12:51 +00:00
2014-05-12 19:40:25 +00:00
$ ( ".upload_message .rows .row:nth-child(" + ( file . num + 1 ) + ") .status" )
. html ( "Finished" )
. addClass ( "success" ) ;
2014-01-22 10:12:51 +00:00
2014-05-12 19:40:25 +00:00
file . ready = true ;
wait = false ;
2014-01-22 10:12:51 +00:00
2014-05-12 19:40:25 +00:00
for ( var i = 0 ; i < files . length ; i ++ ) {
2014-01-22 10:12:51 +00:00
2014-05-12 19:40:25 +00:00
if ( files [ i ] . ready === false ) {
wait = true ;
break ;
}
2014-01-22 10:12:51 +00:00
2014-05-11 22:15:01 +00:00
}
2014-01-22 10:12:51 +00:00
2014-05-12 19:40:25 +00:00
if ( wait === false ) {
2014-01-22 10:12:51 +00:00
2014-05-12 19:40:25 +00:00
window . onbeforeunload = null ;
2014-01-22 10:12:51 +00:00
2014-05-12 19:40:25 +00:00
$ ( "#upload_files" ) . val ( "" ) ;
2014-05-11 22:15:01 +00:00
2014-07-18 15:27:45 +00:00
upload . close ( ) ;
2014-05-16 20:11:10 +00:00
2014-05-12 19:40:25 +00:00
if ( album . getID ( ) === false ) lychee . goto ( "0" ) ;
else album . load ( albumID ) ;
}
2014-01-22 10:12:51 +00:00
2014-05-11 22:15:01 +00:00
}
2014-01-22 10:12:51 +00:00
2014-05-12 19:40:25 +00:00
} ;
2014-01-22 10:12:51 +00:00
2014-05-12 19:40:25 +00:00
xhr . upload . onprogress = function ( e ) {
2014-01-22 10:12:51 +00:00
2014-05-12 19:40:25 +00:00
if ( e . lengthComputable ) {
2014-01-22 10:12:51 +00:00
2014-05-12 19:40:25 +00:00
progress = ( e . loaded / e . total * 100 | 0 ) ;
2014-05-11 22:15:01 +00:00
2014-05-12 19:40:25 +00:00
if ( progress > pre _progress ) {
$ ( ".upload_message .rows .row:nth-child(" + ( file . num + 1 ) + ") .status" ) . html ( progress + "%" ) ;
pre _progress = progress ;
}
2014-05-11 22:15:01 +00:00
2014-05-12 19:40:25 +00:00
if ( progress >= 100 ) {
2014-05-11 22:15:01 +00:00
2014-07-18 15:26:24 +00:00
var scrollPos = 0 ;
if ( ( file . num + 1 ) > 4 ) scrollPos = ( file . num + 1 - 4 ) * 40
$ ( ".upload_message .rows" ) . scrollTop ( scrollPos ) ;
2014-05-11 22:15:01 +00:00
2014-05-12 19:40:25 +00:00
$ ( ".upload_message .rows .row:nth-child(" + ( file . num + 1 ) + ") .status" ) . html ( "Processing" ) ;
2014-05-11 22:15:01 +00:00
2014-05-12 19:40:25 +00:00
if ( file . next !== null ) process ( files , file . next ) ;
2014-05-11 22:15:01 +00:00
2014-05-12 19:40:25 +00:00
}
2014-01-22 10:12:51 +00:00
2014-05-12 19:40:25 +00:00
}
2014-01-22 10:12:51 +00:00
2014-05-12 19:40:25 +00:00
} ;
2014-05-11 22:15:01 +00:00
2014-05-12 19:40:25 +00:00
xhr . send ( formData ) ;
2014-05-11 22:15:01 +00:00
2014-05-12 19:40:25 +00:00
}
2014-05-11 22:15:01 +00:00
if ( files . length <= 0 ) return false ;
2014-05-27 08:20:45 +00:00
if ( albumID === false || visible . albums ( ) === true ) albumID = 0 ;
2014-05-11 22:15:01 +00:00
for ( var i = 0 ; i < files . length ; i ++ ) {
files [ i ] . num = i ;
files [ i ] . ready = false ;
2014-05-12 19:40:25 +00:00
files [ i ] . supported = true ;
2014-05-11 22:15:01 +00:00
if ( i < files . length - 1 ) files [ i ] . next = files [ i + 1 ] ;
else files [ i ] . next = null ;
if ( files [ i ] . type !== "image/jpeg" && files [ i ] . type !== "image/jpg" && files [ i ] . type !== "image/png" && files [ i ] . type !== "image/gif" ) {
files [ i ] . ready = true ;
2014-05-12 19:40:25 +00:00
files [ i ] . supported = false ;
2014-05-11 22:15:01 +00:00
2014-01-22 10:12:51 +00:00
}
2014-05-11 22:15:01 +00:00
}
window . onbeforeunload = function ( ) { return "Lychee is currently uploading!" ; } ;
2014-01-22 10:12:51 +00:00
2014-05-12 19:40:25 +00:00
upload . show ( "Uploading" , files ) ;
2014-01-22 10:12:51 +00:00
2014-05-11 22:15:01 +00:00
process ( files , files [ 0 ] ) ;
2014-01-22 10:12:51 +00:00
} ,
url : function ( ) {
var albumID = album . getID ( ) ,
params ,
extension ,
2014-04-15 12:17:32 +00:00
buttons ,
2014-05-12 19:40:25 +00:00
link ,
files = [ ] ;
2014-01-22 10:12:51 +00:00
if ( albumID === false ) albumID = 0 ;
buttons = [
[ "Import" , function ( ) {
link = $ ( ".message input.text" ) . val ( ) ;
if ( link && link . length > 3 ) {
extension = link . split ( '.' ) . pop ( ) ;
2014-01-26 14:16:59 +00:00
if ( extension !== "jpeg" && extension !== "jpg" && extension !== "png" && extension !== "gif" && extension !== "webp" ) {
2014-01-22 10:12:51 +00:00
loadingBar . show ( "error" , "The file format of this link is not supported." ) ;
return false ;
}
2014-05-12 19:40:25 +00:00
files [ 0 ] = {
name : link ,
supported : true
}
2014-07-23 19:23:10 +00:00
upload . show ( "Importing URL" , files , function ( ) {
2014-07-23 19:58:41 +00:00
$ ( ".upload_message .rows .row .status" ) . html ( "Importing" ) ;
2014-07-23 19:23:10 +00:00
} ) ;
2014-07-18 15:27:45 +00:00
2014-01-22 10:12:51 +00:00
params = "importUrl&url=" + escape ( encodeURI ( link ) ) + "&albumID=" + albumID ;
lychee . api ( params , function ( data ) {
upload . close ( ) ;
2014-02-04 21:08:23 +00:00
upload . notify ( "Import complete" ) ;
2014-01-22 10:12:51 +00:00
if ( album . getID ( ) === false ) lychee . goto ( "0" ) ;
else album . load ( albumID ) ;
if ( data !== true ) lychee . error ( null , params , data ) ;
} ) ;
} else loadingBar . show ( "error" , "Link to short or too long. Please try another one!" ) ;
} ] ,
[ "Cancel" , function ( ) { } ]
] ;
2014-02-17 15:22:53 +00:00
2014-01-22 10:12:51 +00:00
modal . show ( "Import from Link" , "Please enter the direct link to a photo to import it: <input class='text' type='text' placeholder='http://' value='http://'>" , buttons ) ;
} ,
server : function ( ) {
var albumID = album . getID ( ) ,
params ,
2014-05-12 20:13:47 +00:00
buttons ,
2014-07-23 19:58:41 +00:00
files = [ ] ,
path ;
2014-01-22 10:12:51 +00:00
if ( albumID === false ) albumID = 0 ;
buttons = [
[ "Import" , function ( ) {
2014-07-23 19:58:41 +00:00
path = $ ( ".message input.text" ) . val ( ) ;
2014-05-12 20:13:47 +00:00
files [ 0 ] = {
2014-07-23 19:58:41 +00:00
name : path ,
2014-05-12 20:13:47 +00:00
supported : true
} ;
2014-07-23 19:58:41 +00:00
upload . show ( "Importing from server" , files , function ( ) {
$ ( ".upload_message .rows .row .status" ) . html ( "Importing" ) ;
} ) ;
2014-01-22 10:12:51 +00:00
2014-07-23 19:58:41 +00:00
params = "importServer&albumID=" + albumID + "&path=" + escape ( encodeURI ( path ) ) ;
2014-01-22 10:12:51 +00:00
lychee . api ( params , function ( data ) {
upload . close ( ) ;
2014-02-04 21:08:23 +00:00
upload . notify ( "Import complete" ) ;
2014-01-22 10:12:51 +00:00
2014-02-09 21:30:16 +00:00
if ( data === "Notice: Import only contains albums!" ) {
if ( visible . albums ( ) ) lychee . load ( ) ;
else lychee . goto ( "" ) ;
}
else if ( album . getID ( ) === false ) lychee . goto ( "0" ) ;
2014-01-22 10:12:51 +00:00
else album . load ( albumID ) ;
2014-02-09 21:30:16 +00:00
if ( data === "Notice: Import only contains albums!" ) return true ;
else if ( data === "Warning: Folder empty!" ) lychee . error ( "Folder empty. No photos imported!" , params , data ) ;
2014-01-22 10:12:51 +00:00
else if ( data !== true ) lychee . error ( null , params , data ) ;
} ) ;
} ] ,
[ "Cancel" , function ( ) { } ]
] ;
2014-02-17 15:22:53 +00:00
2014-07-23 20:04:07 +00:00
modal . show ( "Import from Server" , "This action will import all photos and albums which are located in the following directory. The original files will be deleted after the import. <input class='text' type='text' maxlength='100' placeholder='Absolute path to directory' value='" + lychee . location + "uploads/import/'>" , buttons ) ;
2014-01-22 10:12:51 +00:00
} ,
dropbox : function ( ) {
var albumID = album . getID ( ) ,
2014-05-12 20:13:47 +00:00
params ,
links = "" ;
2014-01-22 10:12:51 +00:00
if ( albumID === false ) albumID = 0 ;
lychee . loadDropbox ( function ( ) {
Dropbox . choose ( {
linkType : "direct" ,
multiselect : true ,
success : function ( files ) {
2014-05-12 20:13:47 +00:00
for ( var i = 0 ; i < files . length ; i ++ ) {
links += files [ i ] . link + "," ;
2014-01-22 10:12:51 +00:00
2014-05-12 20:13:47 +00:00
files [ i ] = {
name : files [ i ] . link ,
supported : true
} ;
}
2014-01-22 10:12:51 +00:00
2014-05-12 20:13:47 +00:00
// Remove last comma
links = links . substr ( 0 , links . length - 1 ) ;
2014-01-22 10:12:51 +00:00
2014-07-23 19:23:10 +00:00
upload . show ( "Importing from Dropbox" , files , function ( ) {
$ ( ".upload_message .rows .row .status" ) . html ( "Importing" ) ;
} ) ;
2014-01-22 10:12:51 +00:00
2014-05-12 20:13:47 +00:00
params = "importUrl&url=" + escape ( links ) + "&albumID=" + albumID ;
2014-01-22 10:12:51 +00:00
lychee . api ( params , function ( data ) {
upload . close ( ) ;
2014-02-04 21:08:23 +00:00
upload . notify ( "Import complete" ) ;
2014-01-22 10:12:51 +00:00
if ( album . getID ( ) === false ) lychee . goto ( "0" ) ;
else album . load ( albumID ) ;
if ( data !== true ) lychee . error ( null , params , data ) ;
} ) ;
}
} ) ;
} ) ;
}
} ,
close : function ( force ) {
if ( force === true ) {
$ ( ".upload_overlay" ) . remove ( ) ;
} else {
upload . setProgress ( 100 ) ;
$ ( ".upload_overlay" ) . removeClass ( "fadeIn" ) . css ( "opacity" , 0 ) ;
setTimeout ( function ( ) { $ ( ".upload_overlay" ) . remove ( ) } , 300 ) ;
}
}
2014-02-25 22:37:05 +00:00
} ;