photo.js 14 KB

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