2013-09-03 09:59:30 +00:00
/ * *
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
2013-09-03 09:59:30 +00:00
* /
album = {
json : null ,
getID : function ( ) {
var id ;
if ( photo . json ) id = photo . json . album ;
else if ( album . json ) id = album . json . id ;
else id = $ ( ".album:hover, .album.active" ) . attr ( "data-id" ) ;
// Search
2014-07-12 16:41:58 +00:00
if ( ! id ) id = $ ( ".album:hover, .album.active" ) . attr ( "data-id" ) ;
2013-09-03 09:59:30 +00:00
if ( ! id ) id = $ ( ".photo:hover, .photo.active" ) . attr ( "data-album-id" ) ;
if ( id ) return id ;
else return false ;
} ,
load : function ( albumID , refresh ) {
var startTime ,
params ,
durationTime ,
2014-01-22 10:12:51 +00:00
waitTime ;
2013-09-03 09:59:30 +00:00
password . get ( albumID , function ( ) {
if ( ! refresh ) {
loadingBar . show ( ) ;
2014-07-12 18:30:46 +00:00
lychee . animate ( ".album:nth-child(-n+50), .photo:nth-child(-n+50)" , "contentZoomOut" ) ;
2013-09-03 09:59:30 +00:00
lychee . animate ( ".divider" , "fadeOut" ) ;
}
startTime = new Date ( ) . getTime ( ) ;
params = "getAlbum&albumID=" + albumID + "&password=" + password . value ;
2014-01-22 10:12:51 +00:00
lychee . api ( params , function ( data ) {
2013-09-03 09:59:30 +00:00
2014-01-22 10:12:51 +00:00
if ( data === "Warning: Album private!" ) {
2014-02-14 18:03:26 +00:00
if ( document . location . hash . replace ( "#" , "" ) . split ( "/" ) [ 1 ] != undefined ) {
// Display photo only
lychee . setMode ( "view" ) ;
} else {
// Album not public
lychee . content . show ( ) ;
lychee . goto ( "" ) ;
}
2013-09-03 09:59:30 +00:00
return false ;
}
2014-01-22 10:12:51 +00:00
if ( data === "Warning: Wrong password!" ) {
2013-09-03 09:59:30 +00:00
album . load ( albumID , refresh ) ;
return false ;
}
album . json = data ;
durationTime = ( new Date ( ) . getTime ( ) - startTime ) ;
if ( durationTime > 300 ) waitTime = 0 ; else if ( refresh ) waitTime = 0 ; else waitTime = 300 - durationTime ;
if ( ! visible . albums ( ) && ! visible . photo ( ) && ! visible . album ( ) ) waitTime = 0 ;
2013-09-08 17:50:31 +00:00
setTimeout ( function ( ) {
2014-01-22 10:12:51 +00:00
2013-09-03 09:59:30 +00:00
view . album . init ( ) ;
2014-01-22 10:12:51 +00:00
2013-09-03 09:59:30 +00:00
if ( ! refresh ) {
2014-07-12 18:30:46 +00:00
lychee . animate ( ".album:nth-child(-n+50), .photo:nth-child(-n+50)" , "contentZoomIn" ) ;
2013-09-03 09:59:30 +00:00
view . header . mode ( "album" ) ;
}
2014-01-22 10:12:51 +00:00
2013-09-08 17:50:31 +00:00
} , waitTime ) ;
2013-09-03 09:59:30 +00:00
} ) ;
} ) ;
} ,
2014-08-31 20:11:04 +00:00
parse : function ( ) {
2013-09-03 09:59:30 +00:00
2014-07-12 18:12:38 +00:00
if ( ! album . json . title ) album . json . title = "Untitled" ;
2013-09-03 09:59:30 +00:00
} ,
add : function ( ) {
2014-01-22 10:12:51 +00:00
var title ,
params ,
2014-04-11 20:47:09 +00:00
buttons ,
isNumber = function ( n ) { return ! isNaN ( parseFloat ( n ) ) && isFinite ( n ) } ;
2013-09-03 09:59:30 +00:00
2014-01-22 10:12:51 +00:00
buttons = [
[ "Create Album" , function ( ) {
2013-09-03 09:59:30 +00:00
2014-01-22 10:12:51 +00:00
title = $ ( ".message input.text" ) . val ( ) ;
2013-09-03 09:59:30 +00:00
2014-02-08 20:19:19 +00:00
if ( title . length === 0 ) title = "Untitled" ;
2013-09-03 09:59:30 +00:00
2014-02-08 20:19:19 +00:00
modal . close ( ) ;
2013-09-03 09:59:30 +00:00
2014-02-08 20:19:19 +00:00
params = "addAlbum&title=" + escape ( encodeURI ( title ) ) ;
lychee . api ( params , function ( data ) {
2014-01-22 10:12:51 +00:00
2014-04-11 20:47:09 +00:00
if ( data === true ) data = 1 ; // Avoid first album to be true
2014-09-15 20:17:05 +00:00
if ( data !== false && isNumber ( data ) ) {
2014-09-16 12:20:35 +00:00
albums . refresh ( ) ;
lychee . goto ( data ) ;
}
2014-04-11 20:47:09 +00:00
else lychee . error ( null , params , data ) ;
2014-01-22 10:12:51 +00:00
2014-02-08 20:19:19 +00:00
} ) ;
2014-01-22 10:12:51 +00:00
} ] ,
[ "Cancel" , function ( ) { } ]
] ;
2014-02-17 15:22:53 +00:00
2014-02-08 20:19:19 +00:00
modal . show ( "New Album" , "Enter a title for this album: <input class='text' type='text' maxlength='30' placeholder='Title' value='Untitled'>" , buttons ) ;
2013-09-03 09:59:30 +00:00
} ,
2014-01-31 20:22:25 +00:00
delete : function ( albumIDs ) {
2013-09-03 09:59:30 +00:00
var params ,
buttons ,
albumTitle ;
2014-02-17 15:22:53 +00:00
2014-01-31 20:22:25 +00:00
if ( ! albumIDs ) return false ;
if ( albumIDs instanceof Array === false ) albumIDs = [ albumIDs ] ;
2013-09-03 09:59:30 +00:00
buttons = [
2014-01-31 20:22:25 +00:00
[ "" , function ( ) {
2013-09-03 09:59:30 +00:00
2014-01-31 20:22:25 +00:00
params = "deleteAlbum&albumIDs=" + albumIDs ;
2014-01-22 10:12:51 +00:00
lychee . api ( params , function ( data ) {
2013-09-03 09:59:30 +00:00
2014-01-22 10:12:51 +00:00
if ( visible . albums ( ) ) {
2014-02-17 15:22:53 +00:00
2014-08-31 20:11:04 +00:00
albumIDs . forEach ( function ( id ) {
2014-01-31 20:22:25 +00:00
albums . json . num -- ;
view . albums . content . delete ( id ) ;
2014-09-16 12:20:35 +00:00
delete albums . json . content [ id ]
2014-01-31 20:22:25 +00:00
} ) ;
2014-02-17 15:22:53 +00:00
2014-01-22 10:12:51 +00:00
} else lychee . goto ( "" ) ;
2013-09-03 09:59:30 +00:00
2014-01-22 10:12:51 +00:00
if ( data !== true ) lychee . error ( null , params , data ) ;
2013-09-03 09:59:30 +00:00
} ) ;
} ] ,
2014-01-31 20:22:25 +00:00
[ "" , function ( ) { } ]
2013-09-03 09:59:30 +00:00
] ;
2014-03-16 20:00:29 +00:00
if ( albumIDs . toString ( ) === "0" ) {
2013-09-03 09:59:30 +00:00
buttons [ 0 ] [ 0 ] = "Clear Unsorted" ;
2014-01-31 20:22:25 +00:00
buttons [ 1 ] [ 0 ] = "Keep Unsorted" ;
2014-02-17 15:22:53 +00:00
2014-02-25 21:14:25 +00:00
modal . show ( "Clear Unsorted" , "Are you sure you want to delete all photos from 'Unsorted'?<br>This action can't be undone!" , buttons ) ;
2013-09-03 09:59:30 +00:00
2014-01-31 20:22:25 +00:00
} else if ( albumIDs . length === 1 ) {
2014-02-17 15:22:53 +00:00
2014-01-31 20:22:25 +00:00
buttons [ 0 ] [ 0 ] = "Delete Album and Photos" ;
buttons [ 1 ] [ 0 ] = "Keep Album" ;
2014-02-17 15:22:53 +00:00
2014-01-31 20:22:25 +00:00
// Get title
2013-09-03 09:59:30 +00:00
if ( album . json ) albumTitle = album . json . title ;
2014-01-31 20:22:25 +00:00
else if ( albums . json ) albumTitle = albums . json . content [ albumIDs ] . title ;
2014-02-17 15:22:53 +00:00
2013-09-03 09:59:30 +00:00
modal . show ( "Delete Album" , "Are you sure you want to delete the album '" + albumTitle + "' and all of the photos it contains? This action can't be undone!" , buttons ) ;
2014-01-31 20:22:25 +00:00
} else {
2014-02-17 15:22:53 +00:00
2014-01-31 20:22:25 +00:00
buttons [ 0 ] [ 0 ] = "Delete Albums and Photos" ;
buttons [ 1 ] [ 0 ] = "Keep Albums" ;
2014-02-17 15:22:53 +00:00
2014-01-31 20:22:25 +00:00
modal . show ( "Delete Albums" , "Are you sure you want to delete all " + albumIDs . length + " selected albums and all of the photos they contain? This action can't be undone!" , buttons ) ;
2014-02-17 15:22:53 +00:00
2013-09-03 09:59:30 +00:00
}
} ,
2014-01-31 20:22:25 +00:00
setTitle : function ( albumIDs ) {
2013-09-03 09:59:30 +00:00
var oldTitle = "" ,
newTitle ,
2014-01-22 10:12:51 +00:00
params ,
buttons ;
2013-09-03 09:59:30 +00:00
2014-01-31 20:22:25 +00:00
if ( ! albumIDs ) return false ;
if ( albumIDs instanceof Array === false ) albumIDs = [ albumIDs ] ;
2014-02-17 15:22:53 +00:00
2014-01-31 20:22:25 +00:00
if ( albumIDs . length === 1 ) {
// Get old title if only one album is selected
if ( album . json ) oldTitle = album . json . title ;
else if ( albums . json ) oldTitle = albums . json . content [ albumIDs ] . title ;
2014-07-12 16:41:58 +00:00
if ( ! oldTitle ) oldTitle = "" ;
2014-02-28 14:08:05 +00:00
oldTitle = oldTitle . replace ( "'" , "'" ) ;
2014-01-31 20:22:25 +00:00
}
2013-09-03 09:59:30 +00:00
2014-01-22 10:12:51 +00:00
buttons = [
[ "Set Title" , function ( ) {
2013-09-03 09:59:30 +00:00
2014-06-14 19:38:37 +00:00
// Get input
newTitle = $ ( ".message input.text" ) . val ( ) ;
// Remove html from input
newTitle = lychee . removeHTML ( newTitle ) ;
// Set to Untitled when empty
newTitle = ( newTitle === "" ) ? "Untitled" : newTitle ;
2013-09-03 09:59:30 +00:00
2014-02-08 20:19:19 +00:00
if ( visible . album ( ) ) {
2013-09-03 09:59:30 +00:00
2014-02-08 20:19:19 +00:00
album . json . title = newTitle ;
view . album . title ( ) ;
2013-09-03 09:59:30 +00:00
2014-02-08 20:19:19 +00:00
} else if ( visible . albums ( ) ) {
2014-02-17 15:22:53 +00:00
2014-08-31 20:11:04 +00:00
albumIDs . forEach ( function ( id ) {
2014-02-08 20:19:19 +00:00
albums . json . content [ id ] . title = newTitle ;
view . albums . content . title ( id ) ;
} ) ;
2013-09-03 09:59:30 +00:00
2014-02-08 20:19:19 +00:00
}
2014-01-22 10:12:51 +00:00
2014-02-08 20:19:19 +00:00
params = "setAlbumTitle&albumIDs=" + albumIDs + "&title=" + escape ( encodeURI ( newTitle ) ) ;
lychee . api ( params , function ( data ) {
2014-01-22 10:12:51 +00:00
2014-02-08 20:19:19 +00:00
if ( data !== true ) lychee . error ( null , params , data ) ;
2014-01-22 10:12:51 +00:00
2014-02-08 20:19:19 +00:00
} ) ;
2014-01-22 10:12:51 +00:00
} ] ,
[ "Cancel" , function ( ) { } ]
] ;
2014-02-17 15:22:53 +00:00
2014-02-08 20:19:19 +00:00
if ( albumIDs . length === 1 ) modal . show ( "Set Title" , "Enter a new title for this album: <input class='text' type='text' maxlength='30' placeholder='Title' value='" + oldTitle + "'>" , buttons ) ;
else modal . show ( "Set Titles" , "Enter a title for all " + albumIDs . length + " selected album: <input class='text' type='text' maxlength='30' placeholder='Title' value='" + oldTitle + "'>" , buttons ) ;
2014-01-22 10:12:51 +00:00
} ,
setDescription : function ( photoID ) {
2014-02-28 14:08:05 +00:00
var oldDescription = album . json . description . replace ( "'" , "'" ) ,
2014-01-22 10:12:51 +00:00
description ,
params ,
buttons ;
buttons = [
[ "Set Description" , function ( ) {
2014-06-14 19:38:37 +00:00
// Get input
2014-01-22 10:12:51 +00:00
description = $ ( ".message input.text" ) . val ( ) ;
2014-06-14 19:38:37 +00:00
// Remove html from input
description = lychee . removeHTML ( description ) ;
2014-02-08 20:19:19 +00:00
if ( visible . album ( ) ) {
album . json . description = description ;
view . album . description ( ) ;
}
2014-01-22 10:12:51 +00:00
2014-06-15 17:11:54 +00:00
params = "setAlbumDescription&albumID=" + photoID + "&description=" + escape ( encodeURI ( description ) ) ;
2014-02-08 20:19:19 +00:00
lychee . api ( params , function ( data ) {
2013-09-03 09:59:30 +00:00
2014-02-08 20:19:19 +00:00
if ( data !== true ) lychee . error ( null , params , data ) ;
2014-01-22 10:12:51 +00:00
2014-02-08 20:19:19 +00:00
} ) ;
2014-01-22 10:12:51 +00:00
} ] ,
[ "Cancel" , function ( ) { } ]
] ;
2014-02-17 15:22:53 +00:00
2014-02-02 22:56:20 +00:00
modal . show ( "Set Description" , "Please enter a description for this album: <input class='text' type='text' maxlength='800' placeholder='Description' value='" + oldDescription + "'>" , buttons ) ;
2013-09-03 09:59:30 +00:00
} ,
setPublic : function ( albumID , e ) {
2014-07-21 20:16:30 +00:00
var params ,
password = "" ,
2014-08-09 15:57:31 +00:00
listed = false ,
downloadable = false ;
2014-09-16 12:20:35 +00:00
albums . refresh ( ) ;
2014-01-22 10:12:51 +00:00
2014-04-14 15:19:56 +00:00
if ( ! visible . message ( ) && album . json . public == 0 ) {
2014-08-17 17:06:20 +00:00
modal . show ( "Share Album" , "This album will be shared with the following properties:</p><form><div class='choice'><input type='checkbox' name='listed' value='listed' checked><h2>Visible</h2><p>Listed to visitors of your Lychee.</p></div><div class='choice'><input type='checkbox' name='downloadable' value='downloadable'><h2>Downloadable</h2><p>Visitors of your Lychee can download this album.</p></div><div class='choice'><input type='checkbox' name='password' value='password'><h2>Password protected</h2><p>Only accessible with a valid password.<input class='text' type='password' placeholder='password' value='' style='display: none;'></p></div></form><p style='display: none;'>" , [ [ "Share Album" , function ( ) { album . setPublic ( album . getID ( ) , e ) } ] , [ "Cancel" , function ( ) { } ] ] , - 170 ) ;
2014-04-21 12:55:13 +00:00
2014-07-21 20:16:30 +00:00
$ ( ".message .choice input[name='password']" ) . on ( "change" , function ( ) {
2014-04-21 12:55:13 +00:00
2014-07-21 20:16:30 +00:00
if ( $ ( this ) . prop ( 'checked' ) === true ) $ ( ".message .choice input.text" ) . show ( ) ;
2014-04-21 12:55:13 +00:00
else $ ( ".message .choice input.text" ) . hide ( ) ;
} ) ;
2014-09-16 12:20:35 +00:00
2014-04-14 15:19:56 +00:00
return true ;
}
2014-07-21 20:16:30 +00:00
if ( visible . message ( ) ) {
2014-01-22 10:12:51 +00:00
2014-07-21 20:16:30 +00:00
if ( $ ( ".message .choice input[name='password']:checked" ) . val ( ) === "password" ) {
password = md5 ( $ ( ".message input.text" ) . val ( ) ) ;
2014-07-25 19:33:55 +00:00
album . json . password = 1 ;
2014-07-21 20:16:30 +00:00
} else {
password = "" ;
2014-07-25 19:33:55 +00:00
album . json . password = 0 ;
2014-07-21 20:16:30 +00:00
}
2014-01-22 10:12:51 +00:00
2014-07-21 20:16:30 +00:00
if ( $ ( ".message .choice input[name='listed']:checked" ) . val ( ) === "listed" ) listed = true ;
2014-08-09 15:57:31 +00:00
if ( $ ( ".message .choice input[name='downloadable']:checked" ) . val ( ) === "downloadable" ) downloadable = true ;
2014-01-22 10:12:51 +00:00
2013-09-08 17:50:31 +00:00
}
2013-09-03 09:59:30 +00:00
2014-08-09 15:57:31 +00:00
params = "setAlbumPublic&albumID=" + albumID + "&password=" + password + "&visible=" + listed + "&downloadable=" + downloadable ;
2014-07-21 20:16:30 +00:00
2013-09-03 09:59:30 +00:00
if ( visible . album ( ) ) {
album . json . public = ( album . json . public == 0 ) ? 1 : 0 ;
2014-07-25 19:33:55 +00:00
album . json . password = ( album . json . public == 0 ) ? 0 : album . json . password ;
2013-09-03 09:59:30 +00:00
view . album . public ( ) ;
2014-01-22 10:12:51 +00:00
view . album . password ( ) ;
2014-07-25 19:33:55 +00:00
2013-09-03 09:59:30 +00:00
if ( album . json . public == 1 ) contextMenu . shareAlbum ( albumID , e ) ;
}
2014-01-22 10:12:51 +00:00
lychee . api ( params , function ( data ) {
if ( data !== true ) lychee . error ( null , params , data ) ;
2013-09-03 09:59:30 +00:00
} ) ;
} ,
share : function ( service ) {
var link = "" ,
url = location . href ;
switch ( service ) {
case 0 :
link = "https://twitter.com/share?url=" + encodeURI ( url ) ;
break ;
case 1 :
link = "http://www.facebook.com/sharer.php?u=" + encodeURI ( url ) + "&t=" + encodeURI ( album . json . title ) ;
break ;
case 2 :
2014-05-31 10:55:18 +00:00
link = "mailto:?subject=" + encodeURI ( album . json . title ) + "&body=" + encodeURI ( url ) ;
2013-09-03 09:59:30 +00:00
break ;
default :
link = "" ;
break ;
}
if ( link . length > 5 ) location . href = link ;
} ,
getArchive : function ( albumID ) {
2014-09-16 18:24:48 +00:00
var link ,
url = "php/api.php?function=getAlbumArchive&albumID=" + albumID ;
2013-09-03 09:59:30 +00:00
2014-09-16 18:24:48 +00:00
if ( location . href . indexOf ( "index.html" ) > 0 ) link = location . href . replace ( location . hash , "" ) . replace ( "index.html" , url ) ;
else link = location . href . replace ( location . hash , "" ) + url ;
2014-01-22 10:12:51 +00:00
if ( lychee . publicMode ) link += "&password=" + password . value ;
2013-09-03 09:59:30 +00:00
location . href = link ;
}
2014-02-25 22:37:05 +00:00
} ;