photo.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /**
  2. * @name Photo Module
  3. * @description Takes care of every action a photo can handle and execute.
  4. * @author Tobias Reich
  5. * @copyright 2014 by Tobias Reich
  6. */
  7. photo = {
  8. json: null,
  9. cache: null,
  10. getID: function() {
  11. var id;
  12. if (photo.json) id = photo.json.id;
  13. else id = $(".photo:hover, .photo.active").attr("data-id");
  14. if (id) return id;
  15. else return false;
  16. },
  17. load: function(photoID, albumID) {
  18. var params,
  19. checkPasswd;
  20. params = "getPhoto&photoID=" + photoID + "&albumID=" + albumID + "&password=" + password.value;
  21. lychee.api(params, function(data) {
  22. if (data==="Warning: Wrong password!") {
  23. checkPasswd = function() {
  24. if (password.value!=="") photo.load(photoID, albumID);
  25. else setTimeout(checkPasswd, 250);
  26. };
  27. checkPasswd();
  28. return false;
  29. }
  30. photo.json = data;
  31. if (!visible.photo()) view.photo.show();
  32. view.photo.init();
  33. lychee.imageview.show();
  34. setTimeout(function() {
  35. lychee.content.show();
  36. //photo.preloadNext(photoID, albumID);
  37. }, 300);
  38. });
  39. },
  40. // Preload the next photo for better response time
  41. preloadNext: function(photoID) {
  42. var nextPhoto,
  43. url;
  44. // Never preload on mobile devices with bare RAM and
  45. // mostly mobile internet
  46. if (mobileBrowser()) return false;
  47. if (album.json &&
  48. album.json.content &&
  49. album.json.content[photoID] &&
  50. album.json.content[photoID].nextPhoto!="") {
  51. nextPhoto = album.json.content[photoID].nextPhoto;
  52. url = album.json.content[nextPhoto].url;
  53. photo.cache = new Image();
  54. photo.cache.src = url;
  55. photo.cache.onload = function() { photo.cache = null };
  56. }
  57. },
  58. parse: function() {
  59. if (!photo.json.title) photo.json.title = "Untitled";
  60. },
  61. previous: function(animate) {
  62. var delay = 0;
  63. if (photo.getID()!==false&&
  64. album.json&&
  65. album.json.content[photo.getID()]&&
  66. album.json.content[photo.getID()].previousPhoto!=="") {
  67. if (animate===true) {
  68. delay = 200;
  69. $("#image").css({
  70. WebkitTransform: 'translateX(100%)',
  71. MozTransform: 'translateX(100%)',
  72. transform: 'translateX(100%)',
  73. opacity: 0
  74. });
  75. }
  76. setTimeout(function() {
  77. if (photo.getID()===false) return false;
  78. lychee.goto(album.getID() + "/" + album.json.content[photo.getID()].previousPhoto)
  79. }, delay);
  80. }
  81. },
  82. next: function(animate) {
  83. var delay = 0;
  84. if (photo.getID()!==false&&
  85. album.json&&
  86. album.json.content[photo.getID()]&&
  87. album.json.content[photo.getID()].nextPhoto!=="") {
  88. if (animate===true) {
  89. delay = 200;
  90. $("#image").css({
  91. WebkitTransform: 'translateX(-100%)',
  92. MozTransform: 'translateX(-100%)',
  93. transform: 'translateX(-100%)',
  94. opacity: 0
  95. });
  96. }
  97. setTimeout(function() {
  98. if (photo.getID()===false) return false;
  99. lychee.goto(album.getID() + "/" + album.json.content[photo.getID()].nextPhoto);
  100. }, delay);
  101. }
  102. },
  103. duplicate: function(photoIDs) {
  104. var params;
  105. if (!photoIDs) return false;
  106. if (photoIDs instanceof Array===false) photoIDs = [photoIDs];
  107. albums.refresh();
  108. params = "duplicatePhoto&photoIDs=" + photoIDs;
  109. lychee.api(params, function(data) {
  110. if (data!==true) lychee.error(null, params, data);
  111. else album.load(album.getID(), false);
  112. });
  113. },
  114. delete: function(photoIDs) {
  115. var params,
  116. buttons,
  117. photoTitle;
  118. if (!photoIDs) return false;
  119. if (photoIDs instanceof Array===false) photoIDs = [photoIDs];
  120. if (photoIDs.length===1) {
  121. // Get title if only one photo is selected
  122. if (visible.photo()) photoTitle = photo.json.title;
  123. else photoTitle = album.json.content[photoIDs].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. 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. 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. 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. 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. 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. 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. 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. 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. 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. getSize: function() {
  370. // Size can be 'big, medium, 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. 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. getDirectLink: function() {
  410. return $("#imageview #image").css("background-image").replace(/"/g,"").replace(/url\(|\)$/ig, "");
  411. },
  412. getViewLink: function(photoID) {
  413. var url = "view.php?p=" + photoID;
  414. if (location.href.indexOf("index.html")>0) return location.href.replace("index.html" + location.hash, url);
  415. else return location.href.replace(location.hash, url);
  416. }
  417. };