You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lychee/src/scripts/photo.js

619 lines
14 KiB

/**
* @description Takes care of every action a photo can handle and execute.
* @copyright 2014 by Tobias Reich
*/
10 years ago
photo = {}
photo.json = null;
photo.cache = null;
10 years ago
photo.getID = function() {
10 years ago
var id;
10 years ago
if (photo.json) id = photo.json.id;
else id = $('.photo:hover, .photo.active').attr('data-id');
10 years ago
if (id) return id;
else return false;
10 years ago
}
10 years ago
photo.load = function(photoID, albumID) {
10 years ago
var params,
checkPasswd;
10 years ago
params = 'getPhoto&photoID=' + photoID + '&albumID=' + albumID + '&password=' + password.value;
lychee.api(params, function(data) {
10 years ago
if (data==='Warning: Wrong password!') {
10 years ago
checkPasswd = function() {
10 years ago
if (password.value!=='') photo.load(photoID, albumID);
10 years ago
else setTimeout(checkPasswd, 250);
};
checkPasswd();
return false;
}
10 years ago
photo.json = data;
if (!visible.photo()) view.photo.show();
view.photo.init();
10 years ago
lychee.imageview.show();
setTimeout(function() {
lychee.content.show();
//photo.preloadNext(photoID, albumID);
}, 300);
10 years ago
});
10 years ago
}
10 years ago
// Preload the next photo for better response time
photo.preloadNext = function(photoID) {
10 years ago
var nextPhoto,
url;
10 years ago
// Never preload on mobile devices with bare RAM and
// mostly mobile internet
if (mobileBrowser()) return false;
10 years ago
if (album.json &&
album.json.content &&
album.json.content[photoID] &&
10 years ago
album.json.content[photoID].nextPhoto!='') {
10 years ago
nextPhoto = album.json.content[photoID].nextPhoto;
url = album.json.content[nextPhoto].url;
10 years ago
photo.cache = new Image();
photo.cache.src = url;
photo.cache.onload = function() { photo.cache = null };
10 years ago
}
10 years ago
}
10 years ago
photo.parse = function() {
10 years ago
if (!photo.json.title) photo.json.title = 'Untitled';
10 years ago
}
10 years ago
photo.previous = function(animate) {
10 years ago
var delay = 0;
10 years ago
if (photo.getID()!==false&&
album.json&&
album.json.content[photo.getID()]&&
10 years ago
album.json.content[photo.getID()].previousPhoto!=='') {
10 years ago
if (animate===true) {
10 years ago
delay = 200;
10 years ago
$('#image').css({
WebkitTransform: 'translateX(100%)',
MozTransform: 'translateX(100%)',
transform: 'translateX(100%)',
opacity: 0
10 years ago
});
}
10 years ago
setTimeout(function() {
if (photo.getID()===false) return false;
10 years ago
lychee.goto(album.getID() + '/' + album.json.content[photo.getID()].previousPhoto)
10 years ago
}, delay);
10 years ago
}
10 years ago
}
10 years ago
photo.next = function(animate) {
10 years ago
var delay = 0;
10 years ago
if (photo.getID()!==false&&
album.json&&
album.json.content[photo.getID()]&&
10 years ago
album.json.content[photo.getID()].nextPhoto!=='') {
10 years ago
if (animate===true) {
10 years ago
delay = 200;
10 years ago
$('#image').css({
WebkitTransform: 'translateX(-100%)',
MozTransform: 'translateX(-100%)',
transform: 'translateX(-100%)',
opacity: 0
10 years ago
});
}
10 years ago
setTimeout(function() {
if (photo.getID()===false) return false;
10 years ago
lychee.goto(album.getID() + '/' + album.json.content[photo.getID()].nextPhoto);
10 years ago
}, delay);
10 years ago
}
10 years ago
}
10 years ago
photo.duplicate = function(photoIDs) {
10 years ago
var params;
10 years ago
if (!photoIDs) return false;
if (photoIDs instanceof Array===false) photoIDs = [photoIDs];
10 years ago
albums.refresh();
10 years ago
params = 'duplicatePhoto&photoIDs=' + photoIDs;
10 years ago
lychee.api(params, function(data) {
10 years ago
if (data!==true) lychee.error(null, params, data);
else album.load(album.getID());
10 years ago
});
10 years ago
10 years ago
}
10 years ago
photo.delete = function(photoIDs) {
10 years ago
var params,
buttons,
photoTitle;
10 years ago
if (!photoIDs) return false;
if (photoIDs instanceof Array===false) photoIDs = [photoIDs];
10 years ago
if (photoIDs.length===1) {
// Get title if only one photo is selected
if (visible.photo()) photoTitle = photo.json.title;
else photoTitle = album.json.content[photoIDs].title;
if (photoTitle==='') photoTitle = 'Untitled';
10 years ago
}
10 years ago
buttons = [
10 years ago
['', function() {
var nextPhoto = '',
previousPhoto = '';
10 years ago
photoIDs.forEach(function(id, index, array) {
10 years ago
// Change reference for the next and previous photo
10 years ago
if (album.json.content[id].nextPhoto!==''||album.json.content[id].previousPhoto!=='') {
nextPhoto = album.json.content[id].nextPhoto;
previousPhoto = album.json.content[id].previousPhoto;
10 years ago
album.json.content[previousPhoto].nextPhoto = nextPhoto;
album.json.content[nextPhoto].previousPhoto = previousPhoto;
10 years ago
}
10 years ago
album.json.content[id] = null;
view.album.content.delete(id);
10 years ago
});
10 years ago
albums.refresh();
10 years ago
// Go to next photo if there is a next photo and
// next photo is not the current one. Show album otherwise.
10 years ago
if (visible.photo()&&nextPhoto!==''&&nextPhoto!==photo.getID()) lychee.goto(album.getID() + '/' + nextPhoto);
10 years ago
else if (!visible.albums()) lychee.goto(album.getID());
10 years ago
params = 'deletePhoto&photoIDs=' + photoIDs;
10 years ago
lychee.api(params, function(data) {
10 years ago
if (data!==true) lychee.error(null, params, data);
10 years ago
});
10 years ago
}],
10 years ago
['', function() {}]
10 years ago
];
10 years ago
if (photoIDs.length===1) {
10 years ago
buttons[0][0] = 'Delete Photo';
buttons[1][0] = 'Keep Photo';
10 years ago
modal.show('Delete Photo', "Are you sure you want to delete the photo '" + photoTitle + "'?<br>This action can't be undone!", buttons);
10 years ago
} else {
10 years ago
buttons[0][0] = 'Delete Photos';
buttons[1][0] = 'Keep Photos';
10 years ago
modal.show('Delete Photos', "Are you sure you want to delete all " + photoIDs.length + " selected photo?<br>This action can't be undone!", buttons);
10 years ago
}
10 years ago
}
10 years ago
photo.setTitle = function(photoIDs) {
10 years ago
var oldTitle = '',
10 years ago
newTitle,
params,
buttons;
10 years ago
if (!photoIDs) return false;
if (photoIDs instanceof Array===false) photoIDs = [photoIDs];
10 years ago
if (photoIDs.length===1) {
// Get old title if only one photo is selected
if (photo.json) oldTitle = photo.json.title;
else if (album.json) oldTitle = album.json.content[photoIDs].title;
10 years ago
oldTitle = oldTitle.replace("'", '&apos;');
10 years ago
}
10 years ago
buttons = [
10 years ago
['Set Title', function() {
10 years ago
// Get input
10 years ago
newTitle = $('.message input.text').val();
10 years ago
// Remove html from input
newTitle = lychee.removeHTML(newTitle);
10 years ago
if (visible.photo()) {
10 years ago
photo.json.title = (newTitle==='') ? 'Untitled' : newTitle;
10 years ago
view.photo.title();
}
10 years ago
photoIDs.forEach(function(id, index, array) {
album.json.content[id].title = newTitle;
view.album.content.title(id);
});
10 years ago
params = 'setPhotoTitle&photoIDs=' + photoIDs + '&title=' + escape(encodeURI(newTitle));
10 years ago
lychee.api(params, function(data) {
10 years ago
if (data!==true) lychee.error(null, params, data);
10 years ago
});
10 years ago
}],
10 years ago
['Cancel', function() {}]
10 years ago
];
10 years ago
if (photoIDs.length===1) modal.show('Set Title', "Enter a new title for this photo: <input class='text' type='text' maxlength='30' placeholder='Title' value='" + oldTitle + "'>", buttons);
else modal.show('Set Titles', "Enter a title for all " + photoIDs.length + " selected photos: <input class='text' type='text' maxlength='30' placeholder='Title' value=''>", buttons);
10 years ago
}
10 years ago
photo.setAlbum = function(photoIDs, albumID) {
10 years ago
var params,
nextPhoto,
previousPhoto;
10 years ago
if (!photoIDs) return false;
if (visible.photo) lychee.goto(album.getID());
if (photoIDs instanceof Array===false) photoIDs = [photoIDs];
10 years ago
photoIDs.forEach(function(id, index, array) {
10 years ago
// Change reference for the next and previous photo
10 years ago
if (album.json.content[id].nextPhoto!==''||album.json.content[id].previousPhoto!=='') {
nextPhoto = album.json.content[id].nextPhoto;
previousPhoto = album.json.content[id].previousPhoto;
10 years ago
album.json.content[previousPhoto].nextPhoto = nextPhoto;
album.json.content[nextPhoto].previousPhoto = previousPhoto;
10 years ago
}
10 years ago
album.json.content[id] = null;
view.album.content.delete(id);
10 years ago
});
10 years ago
albums.refresh();
10 years ago
params = 'setPhotoAlbum&photoIDs=' + photoIDs + '&albumID=' + albumID;
10 years ago
lychee.api(params, function(data) {
10 years ago
if (data!==true) lychee.error(null, params, data);
10 years ago
});
10 years ago
}
10 years ago
photo.setStar = function(photoIDs) {
10 years ago
var params;
10 years ago
if (!photoIDs) return false;
if (visible.photo()) {
photo.json.star = (photo.json.star==0) ? 1 : 0;
view.photo.star();
}
10 years ago
photoIDs.forEach(function(id, index, array) {
album.json.content[id].star = (album.json.content[id].star==0) ? 1 : 0;
view.album.content.star(id);
});
10 years ago
albums.refresh();
10 years ago
params = 'setPhotoStar&photoIDs=' + photoIDs;
10 years ago
lychee.api(params, function(data) {
10 years ago
if (data!==true) lychee.error(null, params, data);
10 years ago
});
10 years ago
}
10 years ago
photo.setPublic = function(photoID, e) {
10 years ago
var params;
10 years ago
if (photo.json.public==2) {
10 years ago
modal.show('Public Album', "This photo is located in a public album. To make this photo private or public, edit the visibility of the associated album.", [['Show Album', function() { lychee.goto(photo.json.original_album) }], ['Close', function() {}]]);
10 years ago
return false;
10 years ago
}
10 years ago
if (visible.photo()) {
10 years ago
photo.json.public = (photo.json.public==0) ? 1 : 0;
view.photo.public();
if (photo.json.public==1) contextMenu.sharePhoto(photoID, e);
10 years ago
}
10 years ago
album.json.content[photoID].public = (album.json.content[photoID].public==0) ? 1 : 0;
view.album.content.public(photoID);
10 years ago
albums.refresh();
10 years ago
params = 'setPhotoPublic&photoID=' + photoID;
10 years ago
lychee.api(params, function(data) {
10 years ago
if (data!==true) lychee.error(null, params, data);
10 years ago
});
10 years ago
}
10 years ago
photo.setDescription = function(photoID) {
10 years ago
var oldDescription = photo.json.description.replace("'", '&apos;'),
10 years ago
description,
params,
buttons;
10 years ago
buttons = [
10 years ago
['Set Description', function() {
10 years ago
10 years ago
// Get input
10 years ago
description = $('.message input.text').val();
10 years ago
// Remove html from input
description = lychee.removeHTML(description);
10 years ago
10 years ago
if (visible.photo()) {
photo.json.description = description;
view.photo.description();
}
10 years ago
10 years ago
params = 'setPhotoDescription&photoID=' + photoID + '&description=' + escape(encodeURI(description));
10 years ago
lychee.api(params, function(data) {
10 years ago
10 years ago
if (data!==true) lychee.error(null, params, data);
10 years ago
});
10 years ago
10 years ago
}],
10 years ago
['Cancel', function() {}]
10 years ago
];
10 years ago
10 years ago
modal.show('Set Description', "Enter a description for this photo: <input class='text' type='text' maxlength='800' placeholder='Description' value='" + oldDescription + "'>", buttons);
10 years ago
}
10 years ago
photo.editTags = function(photoIDs) {
var oldTags = '',
tags = '',
10 years ago
buttons;
10 years ago
10 years ago
if (!photoIDs) return false;
if (photoIDs instanceof Array===false) photoIDs = [photoIDs];
10 years ago
10 years ago
// Get tags
10 years ago
if (visible.photo()) oldTags = photo.json.tags;
if (visible.album()&&photoIDs.length===1) oldTags = album.json.content[photoIDs].tags;
10 years ago
if (visible.album()&&photoIDs.length>1) {
var same = true;
photoIDs.forEach(function(id, index, array) {
if(album.json.content[id].tags===album.json.content[photoIDs[0]].tags&&same===true) same = true;
else same = false;
});
if (same) oldTags = album.json.content[photoIDs[0]].tags;
}
10 years ago
10 years ago
// Improve tags
oldTags = oldTags.replace(/,/g, ', ');
10 years ago
10 years ago
buttons = [
10 years ago
['Set Tags', function() {
10 years ago
10 years ago
tags = $('.message input.text').val();
10 years ago
10 years ago
photo.setTags(photoIDs, tags);
10 years ago
10 years ago
}],
10 years ago
['Cancel', function() {}]
10 years ago
];
10 years ago
if (photoIDs.length===1) modal.show('Set Tags', "Enter your tags for this photo. You can add multiple tags by separating them with a comma: <input class='text' type='text' maxlength='800' placeholder='Tags' value='" + oldTags + "'>", buttons);
else modal.show('Set Tags', "Enter your tags for all " + photoIDs.length + " selected photos. Existing tags will be overwritten. You can add multiple tags by separating them with a comma: <input class='text' type='text' maxlength='800' placeholder='Tags' value='" + oldTags + "'>", buttons);
10 years ago
10 years ago
}
10 years ago
10 years ago
photo.setTags = function(photoIDs, tags) {
10 years ago
var params;
10 years ago
if (!photoIDs) return false;
if (photoIDs instanceof Array===false) photoIDs = [photoIDs];
10 years ago
10 years ago
// Parse tags
tags = tags.replace(/(\ ,\ )|(\ ,)|(,\ )|(,{1,}\ {0,})|(,$|^,)/g, ',');
tags = tags.replace(/,$|^,|(\ ){0,}$/g, '');
10 years ago
10 years ago
// Remove html from input
tags = lychee.removeHTML(tags);
10 years ago
10 years ago
if (visible.photo()) {
photo.json.tags = tags;
view.photo.tags();
}
10 years ago
10 years ago
photoIDs.forEach(function(id, index, array) {
album.json.content[id].tags = tags;
});
10 years ago
10 years ago
params = 'setPhotoTags&photoIDs=' + photoIDs + '&tags=' + tags;
10 years ago
lychee.api(params, function(data) {
10 years ago
10 years ago
if (data!==true) lychee.error(null, params, data);
10 years ago
10 years ago
});
10 years ago
}
10 years ago
photo.deleteTag = function(photoID, index) {
10 years ago
var tags;
10 years ago
// Remove
tags = photo.json.tags.split(',');
tags.splice(index, 1);
10 years ago
// Save
photo.json.tags = tags.toString();
photo.setTags([photoID], photo.json.tags);
10 years ago
}
10 years ago
photo.share = function(photoID, service) {
var link = '',
url = photo.getViewLink(photoID),
filename = 'unknown';
10 years ago
switch (service) {
case 0:
10 years ago
link = 'https://twitter.com/share?url=' + encodeURI(url);
10 years ago
break;
case 1:
10 years ago
link = 'http://www.facebook.com/sharer.php?u=' + encodeURI(url) + '&t=' + encodeURI(photo.json.title);
10 years ago
break;
case 2:
10 years ago
link = 'mailto:?subject=' + encodeURI(photo.json.title) + '&body=' + encodeURI(url);
10 years ago
break;
case 3:
lychee.loadDropbox(function() {
10 years ago
filename = photo.json.title + '.' + photo.getDirectLink().split('.').pop();
10 years ago
Dropbox.save(photo.getDirectLink(), filename);
});
break;
default:
10 years ago
link = '';
10 years ago
break;
}
10 years ago
if (link.length>5) location.href = link;
}
photo.getSize = function() {
10 years ago
// Size can be 'big', 'medium' or 'small'
10 years ago
// Default is big
// Small is centered in the middle of the screen
var size = 'big',
scaled = false,
hasMedium = photo.json.medium!=='',
pixelRatio = window.devicePixelRatio,
10 years ago
view = {
10 years ago
width: $(window).width()-60,
height: $(window).height()-100
10 years ago
};
// Detect if the photo will be shown scaled,
// because the screen size is smaller than the photo
if (photo.json.width>view.width||
photo.json.width>view.height) scaled = true;
// Calculate pixel ratio of screen
if (pixelRatio!==undefined&&pixelRatio>1) {
view.width = view.width * pixelRatio;
view.height = view.height * pixelRatio;
10 years ago
}
10 years ago
// Medium available and
// Medium still bigger than screen
if (hasMedium===true&&
10 years ago
(1920>view.width&&1080>view.height)) size = 'medium';
10 years ago
// Photo not scaled
// Photo smaller then screen
if (scaled===false&&
(photo.json.width<view.width&&
10 years ago
photo.json.width<view.height)) size = 'small';
10 years ago
return size;
10 years ago
}
10 years ago
photo.getArchive = function(photoID) {
10 years ago
var link,
10 years ago
url = 'php/api.php?function=getPhotoArchive&photoID=' + photoID;
10 years ago
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;
10 years ago
if (lychee.publicMode) link += '&password=' + password.value;
10 years ago
location.href = link;
10 years ago
}
10 years ago
photo.getDirectLink = function() {
10 years ago
var url = '';
10 years ago
if (photo.json&&
photo.json.url&&
10 years ago
photo.json.url!=='') url = photo.json.url;
10 years ago
return url;
10 years ago
}
10 years ago
10 years ago
photo.getViewLink = function(photoID) {
10 years ago
var url = 'view.php?p=' + photoID;
10 years ago
10 years ago
if (location.href.indexOf('index.html')>0) return location.href.replace('index.html' + location.hash, url);
10 years ago
else return location.href.replace(location.hash, url);
10 years ago
}