photo.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. 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. 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() { lychee.content.show() }, 300);
  34. });
  35. },
  36. parse: function() {
  37. if (!photo.json.title) photo.json.title = "Untitled";
  38. photo.json.url = lychee.upload_path_big + photo.json.url;
  39. },
  40. previous: function(animate) {
  41. var delay = 0;
  42. if (photo.getID()!==false&&
  43. album.json&&
  44. album.json.content[photo.getID()]&&
  45. album.json.content[photo.getID()].previousPhoto!=="") {
  46. if (animate===true) {
  47. delay = 200;
  48. $("#image").css({
  49. WebkitTransform: 'translateX(100%)',
  50. MozTransform: 'translateX(100%)',
  51. transform: 'translateX(100%)',
  52. opacity: 0
  53. });
  54. }
  55. setTimeout(function() {
  56. if (photo.getID()===false) return false;
  57. lychee.goto(album.getID() + "/" + album.json.content[photo.getID()].previousPhoto)
  58. }, delay);
  59. }
  60. },
  61. next: 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()].nextPhoto!=="") {
  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()].nextPhoto);
  79. }, delay);
  80. }
  81. },
  82. delete: function(photoIDs) {
  83. var params,
  84. buttons,
  85. photoTitle;
  86. if (!photoIDs) return false;
  87. if (photoIDs instanceof Array===false) photoIDs = [photoIDs];
  88. if (photoIDs.length===1) {
  89. // Get title if only one photo is selected
  90. if (visible.photo()) photoTitle = photo.json.title;
  91. else photoTitle = album.json.content[photoIDs].title;
  92. if (photoTitle==="") photoTitle = "Untitled";
  93. }
  94. buttons = [
  95. ["", function() {
  96. photoIDs.forEach(function(id, index, array) {
  97. // Change reference for the next and previous photo
  98. if (album.json.content[id].nextPhoto!==""||album.json.content[id].previousPhoto!=="") {
  99. nextPhoto = album.json.content[id].nextPhoto;
  100. previousPhoto = album.json.content[id].previousPhoto;
  101. album.json.content[previousPhoto].nextPhoto = nextPhoto;
  102. album.json.content[nextPhoto].previousPhoto = previousPhoto;
  103. }
  104. album.json.content[id] = null;
  105. view.album.content.delete(id);
  106. });
  107. // Only when search is not active
  108. if (!visible.albums()) lychee.goto(album.getID());
  109. params = "deletePhoto&photoIDs=" + photoIDs;
  110. lychee.api(params, function(data) {
  111. if (data!==true) lychee.error(null, params, data);
  112. });
  113. }],
  114. ["", function() {}]
  115. ];
  116. if (photoIDs.length===1) {
  117. buttons[0][0] = "Delete Photo";
  118. buttons[1][0] = "Keep Photo";
  119. modal.show("Delete Photo", "Are you sure you want to delete the photo '" + photoTitle + "'?<br>This action can't be undone!", buttons);
  120. } else {
  121. buttons[0][0] = "Delete Photos";
  122. buttons[1][0] = "Keep Photos";
  123. modal.show("Delete Photos", "Are you sure you want to delete all " + photoIDs.length + " selected photo?<br>This action can't be undone!", buttons);
  124. }
  125. },
  126. setTitle: function(photoIDs) {
  127. var oldTitle = "",
  128. newTitle,
  129. params,
  130. buttons;
  131. if (!photoIDs) return false;
  132. if (photoIDs instanceof Array===false) photoIDs = [photoIDs];
  133. if (photoIDs.length===1) {
  134. // Get old title if only one photo is selected
  135. if (photo.json) oldTitle = photo.json.title;
  136. else if (album.json) oldTitle = album.json.content[photoIDs].title;
  137. oldTitle = oldTitle.replace("'", "&apos;");
  138. }
  139. buttons = [
  140. ["Set Title", function() {
  141. newTitle = $(".message input.text").val();
  142. if (visible.photo()) {
  143. photo.json.title = (newTitle==="") ? "Untitled" : newTitle;
  144. view.photo.title();
  145. }
  146. photoIDs.forEach(function(id, index, array) {
  147. album.json.content[id].title = newTitle;
  148. view.album.content.title(id);
  149. });
  150. params = "setPhotoTitle&photoIDs=" + photoIDs + "&title=" + escape(encodeURI(newTitle));
  151. lychee.api(params, function(data) {
  152. if (data!==true) lychee.error(null, params, data);
  153. });
  154. }],
  155. ["Cancel", function() {}]
  156. ];
  157. 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);
  158. 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);
  159. },
  160. setAlbum: function(photoIDs, albumID) {
  161. var params,
  162. nextPhoto,
  163. previousPhoto;
  164. if (!photoIDs) return false;
  165. if (visible.photo) lychee.goto(album.getID());
  166. if (photoIDs instanceof Array===false) photoIDs = [photoIDs];
  167. photoIDs.forEach(function(id, index, array) {
  168. // Change reference for the next and previous photo
  169. if (album.json.content[id].nextPhoto!==""||album.json.content[id].previousPhoto!=="") {
  170. nextPhoto = album.json.content[id].nextPhoto;
  171. previousPhoto = album.json.content[id].previousPhoto;
  172. album.json.content[previousPhoto].nextPhoto = nextPhoto;
  173. album.json.content[nextPhoto].previousPhoto = previousPhoto;
  174. }
  175. album.json.content[id] = null;
  176. view.album.content.delete(id);
  177. });
  178. params = "setPhotoAlbum&photoIDs=" + photoIDs + "&albumID=" + albumID;
  179. lychee.api(params, function(data) {
  180. if (data!==true) lychee.error(null, params, data);
  181. });
  182. },
  183. setStar: function(photoIDs) {
  184. var params;
  185. if (!photoIDs) return false;
  186. if (visible.photo()) {
  187. photo.json.star = (photo.json.star==0) ? 1 : 0;
  188. view.photo.star();
  189. }
  190. photoIDs.forEach(function(id, index, array) {
  191. album.json.content[id].star = (album.json.content[id].star==0) ? 1 : 0;
  192. view.album.content.star(id);
  193. });
  194. params = "setPhotoStar&photoIDs=" + photoIDs;
  195. lychee.api(params, function(data) {
  196. if (data!==true) lychee.error(null, params, data);
  197. });
  198. },
  199. setPublic: function(photoID, e) {
  200. var params;
  201. if (photo.json.public==2) {
  202. 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() {}]]);
  203. return false;
  204. }
  205. if (visible.photo()) {
  206. photo.json.public = (photo.json.public==0) ? 1 : 0;
  207. view.photo.public();
  208. if (photo.json.public==1) contextMenu.sharePhoto(photoID, e);
  209. }
  210. album.json.content[photoID].public = (album.json.content[photoID].public==0) ? 1 : 0;
  211. view.album.content.public(photoID);
  212. params = "setPhotoPublic&photoID=" + photoID;
  213. lychee.api(params, function(data) {
  214. if (data!==true) lychee.error(null, params, data);
  215. });
  216. },
  217. setDescription: function(photoID) {
  218. var oldDescription = photo.json.description.replace("'", "&apos;"),
  219. description,
  220. params,
  221. buttons;
  222. buttons = [
  223. ["Set Description", function() {
  224. description = $(".message input.text").val();
  225. if (visible.photo()) {
  226. photo.json.description = description;
  227. view.photo.description();
  228. }
  229. params = "setPhotoDescription&photoID=" + photoID + "&description=" + escape(description);
  230. lychee.api(params, function(data) {
  231. if (data!==true) lychee.error(null, params, data);
  232. });
  233. }],
  234. ["Cancel", function() {}]
  235. ];
  236. modal.show("Set Description", "Enter a description for this photo: <input class='text' type='text' maxlength='800' placeholder='Description' value='" + oldDescription + "'>", buttons);
  237. },
  238. editTags: function(photoIDs) {
  239. var oldTags = "",
  240. tags = "",
  241. buttons;
  242. if (!photoIDs) return false;
  243. if (photoIDs instanceof Array===false) photoIDs = [photoIDs];
  244. // Get tags
  245. if (visible.photo()) oldTags = photo.json.tags;
  246. if (visible.album()&&photoIDs.length===1) oldTags = album.json.content[photoIDs].tags;
  247. if (visible.album()&&photoIDs.length>1) {
  248. var same = true;
  249. photoIDs.forEach(function(id, index, array) {
  250. if(album.json.content[id].tags===album.json.content[photoIDs[0]].tags&&same===true) same = true;
  251. else same = false;
  252. });
  253. if (same) oldTags = album.json.content[photoIDs[0]].tags;
  254. }
  255. // Improve tags
  256. oldTags = oldTags.replace(/,/g, ', ');
  257. buttons = [
  258. ["Set Tags", function() {
  259. tags = $(".message input.text").val();
  260. photo.setTags(photoIDs, tags);
  261. }],
  262. ["Cancel", function() {}]
  263. ];
  264. 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);
  265. 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);
  266. },
  267. setTags: function(photoIDs, tags) {
  268. var params;
  269. if (!photoIDs) return false;
  270. if (photoIDs instanceof Array===false) photoIDs = [photoIDs];
  271. // Parse tags
  272. tags = tags.replace(/(\ ,\ )|(\ ,)|(,\ )|(,{1,}\ {0,})|(,$|^,)/g, ',');
  273. tags = tags.replace(/,$|^,|(\ ){0,}$/g, '');
  274. if (visible.photo()) {
  275. photo.json.tags = tags;
  276. view.photo.tags();
  277. }
  278. photoIDs.forEach(function(id, index, array) {
  279. album.json.content[id].tags = tags;
  280. });
  281. params = "setPhotoTags&photoIDs=" + photoIDs + "&tags=" + tags;
  282. lychee.api(params, function(data) {
  283. if (data!==true) lychee.error(null, params, data);
  284. });
  285. },
  286. deleteTag: function(photoID, index) {
  287. var tags;
  288. // Remove
  289. tags = photo.json.tags.split(',');
  290. tags.splice(index, 1);
  291. // Save
  292. photo.json.tags = tags.toString();
  293. photo.setTags([photoID], photo.json.tags);
  294. },
  295. share: function(photoID, service) {
  296. var link = "",
  297. url = photo.getViewLink(photoID),
  298. filename = "unknown";
  299. switch (service) {
  300. case 0:
  301. link = "https://twitter.com/share?url=" + encodeURI(url);
  302. break;
  303. case 1:
  304. link = "http://www.facebook.com/sharer.php?u=" + encodeURI(url) + "&t=" + encodeURI(photo.json.title);
  305. break;
  306. case 2:
  307. link = "mailto:?subject=" + encodeURI(photo.json.title) + "&body=" + encodeURI(url);
  308. break;
  309. case 3:
  310. lychee.loadDropbox(function() {
  311. filename = photo.json.title + "." + photo.getDirectLink().split('.').pop();
  312. Dropbox.save(photo.getDirectLink(), filename);
  313. });
  314. break;
  315. default:
  316. link = "";
  317. break;
  318. }
  319. if (link.length>5) location.href = link;
  320. },
  321. isSmall: function() {
  322. var size = {
  323. width: false,
  324. height: false
  325. };
  326. if (photo.json.width<$(window).width()-60) size.width = true;
  327. if (photo.json.height<$(window).height()-100) size.height = true;
  328. if (size.width&&size.height) return true;
  329. else return false;
  330. },
  331. getArchive: function(photoID) {
  332. var link;
  333. if (location.href.indexOf("index.html")>0) link = location.href.replace(location.hash, "").replace("index.html", "php/api.php?function=getPhotoArchive&photoID=" + photoID);
  334. else link = location.href.replace(location.hash, "") + "php/api.php?function=getPhotoArchive&photoID=" + photoID;
  335. if (lychee.publicMode) link += "&password=" + password.value;
  336. location.href = link;
  337. },
  338. getDirectLink: function() {
  339. return $("#imageview #image").css("background-image").replace(/"/g,"").replace(/url\(|\)$/ig, "");
  340. },
  341. getViewLink: function(photoID) {
  342. if (location.href.indexOf("index.html")>0) return location.href.replace("index.html" + location.hash, "view.php?p=" + photoID);
  343. else return location.href.replace(location.hash, "view.php?p=" + photoID);
  344. }
  345. };