photo.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. /**
  2. * @description Takes care of every action a photo can handle and execute.
  3. * @copyright 2014 by Tobias Reich
  4. */
  5. photo = {
  6. json: null,
  7. cache: null
  8. }
  9. photo.getID = function() {
  10. var id;
  11. if (photo.json) id = photo.json.id;
  12. else id = $('.photo:hover, .photo.active').attr('data-id');
  13. if (id) return id;
  14. else return false;
  15. }
  16. photo.load = function(photoID, albumID) {
  17. var params,
  18. checkPasswd;
  19. params = 'getPhoto&photoID=' + photoID + '&albumID=' + albumID + '&password=' + password.value;
  20. lychee.api(params, function(data) {
  21. if (data==='Warning: Wrong password!') {
  22. checkPasswd = function() {
  23. if (password.value!=='') photo.load(photoID, albumID);
  24. else setTimeout(checkPasswd, 250);
  25. };
  26. checkPasswd();
  27. return false;
  28. }
  29. photo.json = data;
  30. if (!visible.photo()) view.photo.show();
  31. view.photo.init();
  32. lychee.imageview.show();
  33. setTimeout(function() {
  34. lychee.content.show();
  35. //photo.preloadNext(photoID, albumID);
  36. }, 300);
  37. });
  38. }
  39. // Preload the next photo for better response time
  40. photo.preloadNext = function(photoID) {
  41. var nextPhoto,
  42. url;
  43. // Never preload on mobile devices with bare RAM and
  44. // mostly mobile internet
  45. if (mobileBrowser()) return false;
  46. if (album.json &&
  47. album.json.content &&
  48. album.json.content[photoID] &&
  49. album.json.content[photoID].nextPhoto!='') {
  50. nextPhoto = album.json.content[photoID].nextPhoto;
  51. url = album.json.content[nextPhoto].url;
  52. photo.cache = new Image();
  53. photo.cache.src = url;
  54. photo.cache.onload = function() { photo.cache = null };
  55. }
  56. }
  57. photo.parse = function() {
  58. if (!photo.json.title) photo.json.title = 'Untitled';
  59. }
  60. photo.previous = function(animate) {
  61. var delay = 0;
  62. if (photo.getID()!==false&&
  63. album.json&&
  64. album.json.content[photo.getID()]&&
  65. album.json.content[photo.getID()].previousPhoto!=='') {
  66. if (animate===true) {
  67. delay = 200;
  68. $('#image').css({
  69. WebkitTransform: 'translateX(100%)',
  70. MozTransform: 'translateX(100%)',
  71. transform: 'translateX(100%)',
  72. opacity: 0
  73. });
  74. }
  75. setTimeout(function() {
  76. if (photo.getID()===false) return false;
  77. lychee.goto(album.getID() + '/' + album.json.content[photo.getID()].previousPhoto)
  78. }, delay);
  79. }
  80. }
  81. photo.next = function(animate) {
  82. var delay = 0;
  83. if (photo.getID()!==false&&
  84. album.json&&
  85. album.json.content[photo.getID()]&&
  86. album.json.content[photo.getID()].nextPhoto!=='') {
  87. if (animate===true) {
  88. delay = 200;
  89. $('#image').css({
  90. WebkitTransform: 'translateX(-100%)',
  91. MozTransform: 'translateX(-100%)',
  92. transform: 'translateX(-100%)',
  93. opacity: 0
  94. });
  95. }
  96. setTimeout(function() {
  97. if (photo.getID()===false) return false;
  98. lychee.goto(album.getID() + '/' + album.json.content[photo.getID()].nextPhoto);
  99. }, delay);
  100. }
  101. }
  102. photo.duplicate = function(photoIDs) {
  103. var params;
  104. if (!photoIDs) return false;
  105. if (photoIDs instanceof Array===false) photoIDs = [photoIDs];
  106. albums.refresh();
  107. params = 'duplicatePhoto&photoIDs=' + photoIDs;
  108. lychee.api(params, function(data) {
  109. if (data!==true) lychee.error(null, params, data);
  110. else album.load(album.getID());
  111. });
  112. }
  113. photo.delete = function(photoIDs) {
  114. var params,
  115. buttons,
  116. photoTitle;
  117. if (!photoIDs) return false;
  118. if (photoIDs instanceof Array===false) photoIDs = [photoIDs];
  119. if (photoIDs.length===1) {
  120. // Get title if only one photo is selected
  121. if (visible.photo()) photoTitle = photo.json.title;
  122. else photoTitle = album.json.content[photoIDs].title;
  123. // Fallback for photos without a title
  124. if (photoTitle==='') photoTitle = 'Untitled';
  125. }
  126. buttons = [
  127. ['', function() {
  128. var nextPhoto = '',
  129. previousPhoto = '';
  130. photoIDs.forEach(function(id, index, array) {
  131. // Change reference for the next and previous photo
  132. if (album.json.content[id].nextPhoto!==''||album.json.content[id].previousPhoto!=='') {
  133. nextPhoto = album.json.content[id].nextPhoto;
  134. previousPhoto = album.json.content[id].previousPhoto;
  135. album.json.content[previousPhoto].nextPhoto = nextPhoto;
  136. album.json.content[nextPhoto].previousPhoto = previousPhoto;
  137. }
  138. album.json.content[id] = null;
  139. view.album.content.delete(id);
  140. });
  141. albums.refresh();
  142. // Go to next photo if there is a next photo and
  143. // next photo is not the current one. Show album otherwise.
  144. if (visible.photo()&&nextPhoto!==''&&nextPhoto!==photo.getID()) lychee.goto(album.getID() + '/' + nextPhoto);
  145. else if (!visible.albums()) lychee.goto(album.getID());
  146. params = 'deletePhoto&photoIDs=' + photoIDs;
  147. lychee.api(params, function(data) {
  148. if (data!==true) lychee.error(null, params, data);
  149. });
  150. }],
  151. ['', function() {}]
  152. ];
  153. if (photoIDs.length===1) {
  154. buttons[0][0] = 'Delete Photo';
  155. buttons[1][0] = 'Keep Photo';
  156. modal.show('Delete Photo', "Are you sure you want to delete the photo '" + photoTitle + "'?<br>This action can't be undone!", buttons);
  157. } else {
  158. buttons[0][0] = 'Delete Photos';
  159. buttons[1][0] = 'Keep Photos';
  160. modal.show('Delete Photos', "Are you sure you want to delete all " + photoIDs.length + " selected photo?<br>This action can't be undone!", buttons);
  161. }
  162. }
  163. photo.setTitle = function(photoIDs) {
  164. var oldTitle = '',
  165. newTitle,
  166. params,
  167. buttons;
  168. if (!photoIDs) return false;
  169. if (photoIDs instanceof Array===false) photoIDs = [photoIDs];
  170. if (photoIDs.length===1) {
  171. // Get old title if only one photo is selected
  172. if (photo.json) oldTitle = photo.json.title;
  173. else if (album.json) oldTitle = album.json.content[photoIDs].title;
  174. oldTitle = oldTitle.replace("'", '&apos;');
  175. }
  176. buttons = [
  177. ['Set Title', function() {
  178. // Get input
  179. newTitle = $('.message input.text').val();
  180. // Remove html from input
  181. newTitle = lychee.removeHTML(newTitle);
  182. if (visible.photo()) {
  183. photo.json.title = (newTitle==='') ? 'Untitled' : newTitle;
  184. view.photo.title();
  185. }
  186. photoIDs.forEach(function(id, index, array) {
  187. album.json.content[id].title = newTitle;
  188. view.album.content.title(id);
  189. });
  190. params = 'setPhotoTitle&photoIDs=' + photoIDs + '&title=' + escape(encodeURI(newTitle));
  191. lychee.api(params, function(data) {
  192. if (data!==true) lychee.error(null, params, data);
  193. });
  194. }],
  195. ['Cancel', function() {}]
  196. ];
  197. 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);
  198. 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);
  199. }
  200. photo.setAlbum = function(photoIDs, albumID) {
  201. var params,
  202. nextPhoto,
  203. previousPhoto;
  204. if (!photoIDs) return false;
  205. if (visible.photo) lychee.goto(album.getID());
  206. if (photoIDs instanceof Array===false) photoIDs = [photoIDs];
  207. photoIDs.forEach(function(id, index, array) {
  208. // Change reference for the next and previous photo
  209. if (album.json.content[id].nextPhoto!==''||album.json.content[id].previousPhoto!=='') {
  210. nextPhoto = album.json.content[id].nextPhoto;
  211. previousPhoto = album.json.content[id].previousPhoto;
  212. album.json.content[previousPhoto].nextPhoto = nextPhoto;
  213. album.json.content[nextPhoto].previousPhoto = previousPhoto;
  214. }
  215. album.json.content[id] = null;
  216. view.album.content.delete(id);
  217. });
  218. albums.refresh();
  219. params = 'setPhotoAlbum&photoIDs=' + photoIDs + '&albumID=' + albumID;
  220. lychee.api(params, function(data) {
  221. if (data!==true) lychee.error(null, params, data);
  222. });
  223. }
  224. photo.setStar = function(photoIDs) {
  225. var params;
  226. if (!photoIDs) return false;
  227. if (visible.photo()) {
  228. photo.json.star = (photo.json.star==0) ? 1 : 0;
  229. view.photo.star();
  230. }
  231. photoIDs.forEach(function(id, index, array) {
  232. album.json.content[id].star = (album.json.content[id].star==0) ? 1 : 0;
  233. view.album.content.star(id);
  234. });
  235. albums.refresh();
  236. params = 'setPhotoStar&photoIDs=' + photoIDs;
  237. lychee.api(params, function(data) {
  238. if (data!==true) lychee.error(null, params, data);
  239. });
  240. }
  241. photo.setPublic = function(photoID, e) {
  242. var params;
  243. if (photo.json.public==2) {
  244. 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() {}]]);
  245. return false;
  246. }
  247. if (visible.photo()) {
  248. photo.json.public = (photo.json.public==0) ? 1 : 0;
  249. view.photo.public();
  250. if (photo.json.public==1) contextMenu.sharePhoto(photoID, e);
  251. }
  252. album.json.content[photoID].public = (album.json.content[photoID].public==0) ? 1 : 0;
  253. view.album.content.public(photoID);
  254. albums.refresh();
  255. params = 'setPhotoPublic&photoID=' + photoID;
  256. lychee.api(params, function(data) {
  257. if (data!==true) lychee.error(null, params, data);
  258. });
  259. }
  260. photo.setDescription = function(photoID) {
  261. var oldDescription = photo.json.description.replace("'", '&apos;'),
  262. description,
  263. params,
  264. buttons;
  265. buttons = [
  266. ['Set Description', function() {
  267. // Get input
  268. description = $('.message input.text').val();
  269. // Remove html from input
  270. description = lychee.removeHTML(description);
  271. if (visible.photo()) {
  272. photo.json.description = description;
  273. view.photo.description();
  274. }
  275. params = 'setPhotoDescription&photoID=' + photoID + '&description=' + escape(encodeURI(description));
  276. lychee.api(params, function(data) {
  277. if (data!==true) lychee.error(null, params, data);
  278. });
  279. }],
  280. ['Cancel', function() {}]
  281. ];
  282. modal.show('Set Description', "Enter a description for this photo: <input class='text' type='text' maxlength='800' placeholder='Description' value='" + oldDescription + "'>", buttons);
  283. }
  284. photo.editTags = function(photoIDs) {
  285. var oldTags = '',
  286. tags = '',
  287. buttons;
  288. if (!photoIDs) return false;
  289. if (photoIDs instanceof Array===false) photoIDs = [photoIDs];
  290. // Get tags
  291. if (visible.photo()) oldTags = photo.json.tags;
  292. if (visible.album()&&photoIDs.length===1) oldTags = album.json.content[photoIDs].tags;
  293. if (visible.album()&&photoIDs.length>1) {
  294. var same = true;
  295. photoIDs.forEach(function(id, index, array) {
  296. if(album.json.content[id].tags===album.json.content[photoIDs[0]].tags&&same===true) same = true;
  297. else same = false;
  298. });
  299. if (same) oldTags = album.json.content[photoIDs[0]].tags;
  300. }
  301. // Improve tags
  302. oldTags = oldTags.replace(/,/g, ', ');
  303. buttons = [
  304. ['Set Tags', function() {
  305. tags = $('.message input.text').val();
  306. photo.setTags(photoIDs, tags);
  307. }],
  308. ['Cancel', function() {}]
  309. ];
  310. 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);
  311. 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);
  312. }
  313. photo.setTags = function(photoIDs, tags) {
  314. var params;
  315. if (!photoIDs) return false;
  316. if (photoIDs instanceof Array===false) photoIDs = [photoIDs];
  317. // Parse tags
  318. tags = tags.replace(/(\ ,\ )|(\ ,)|(,\ )|(,{1,}\ {0,})|(,$|^,)/g, ',');
  319. tags = tags.replace(/,$|^,|(\ ){0,}$/g, '');
  320. // Remove html from input
  321. tags = lychee.removeHTML(tags);
  322. if (visible.photo()) {
  323. photo.json.tags = tags;
  324. view.photo.tags();
  325. }
  326. photoIDs.forEach(function(id, index, array) {
  327. album.json.content[id].tags = tags;
  328. });
  329. params = 'setPhotoTags&photoIDs=' + photoIDs + '&tags=' + tags;
  330. lychee.api(params, function(data) {
  331. if (data!==true) lychee.error(null, params, data);
  332. });
  333. }
  334. photo.deleteTag = function(photoID, index) {
  335. var tags;
  336. // Remove
  337. tags = photo.json.tags.split(',');
  338. tags.splice(index, 1);
  339. // Save
  340. photo.json.tags = tags.toString();
  341. photo.setTags([photoID], photo.json.tags);
  342. }
  343. photo.share = function(photoID, service) {
  344. var link = '',
  345. url = photo.getViewLink(photoID),
  346. filename = 'unknown';
  347. switch (service) {
  348. case 0:
  349. link = 'https://twitter.com/share?url=' + encodeURI(url);
  350. break;
  351. case 1:
  352. link = 'http://www.facebook.com/sharer.php?u=' + encodeURI(url) + '&t=' + encodeURI(photo.json.title);
  353. break;
  354. case 2:
  355. link = 'mailto:?subject=' + encodeURI(photo.json.title) + '&body=' + encodeURI(url);
  356. break;
  357. case 3:
  358. lychee.loadDropbox(function() {
  359. filename = photo.json.title + '.' + photo.getDirectLink().split('.').pop();
  360. Dropbox.save(photo.getDirectLink(), filename);
  361. });
  362. break;
  363. default:
  364. link = '';
  365. break;
  366. }
  367. if (link.length>5) location.href = link;
  368. }
  369. photo.getSize = function() {
  370. // Size can be 'big', 'medium' or 'small'
  371. // Default is big
  372. // Small is centered in the middle of the screen
  373. var size = 'big',
  374. scaled = false,
  375. hasMedium = photo.json.medium!=='',
  376. pixelRatio = window.devicePixelRatio,
  377. view = {
  378. width: $(window).width()-60,
  379. height: $(window).height()-100
  380. };
  381. // Detect if the photo will be shown scaled,
  382. // because the screen size is smaller than the photo
  383. if (photo.json.width>view.width||
  384. photo.json.width>view.height) scaled = true;
  385. // Calculate pixel ratio of screen
  386. if (pixelRatio!==undefined&&pixelRatio>1) {
  387. view.width = view.width * pixelRatio;
  388. view.height = view.height * pixelRatio;
  389. }
  390. // Medium available and
  391. // Medium still bigger than screen
  392. if (hasMedium===true&&
  393. (1920>view.width&&1080>view.height)) size = 'medium';
  394. // Photo not scaled
  395. // Photo smaller then screen
  396. if (scaled===false&&
  397. (photo.json.width<view.width&&
  398. photo.json.width<view.height)) size = 'small';
  399. return size;
  400. }
  401. photo.getArchive = function(photoID) {
  402. var link,
  403. url = 'php/api.php?function=getPhotoArchive&photoID=' + photoID;
  404. if (location.href.indexOf('index.html')>0) link = location.href.replace(location.hash, '').replace('index.html', url);
  405. else link = location.href.replace(location.hash, '') + url;
  406. if (lychee.publicMode) link += '&password=' + password.value;
  407. location.href = link;
  408. }
  409. photo.getDirectLink = function() {
  410. var url = '';
  411. if (photo.json&&
  412. photo.json.url&&
  413. photo.json.url!=='') url = photo.json.url;
  414. return url;
  415. }
  416. photo.getViewLink = function(photoID) {
  417. var url = 'view.php?p=' + photoID;
  418. if (location.href.indexOf('index.html')>0) return location.href.replace('index.html' + location.hash, url);
  419. else return location.href.replace(location.hash, url);
  420. }