photo.js 11 KB

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