photo.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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(photoID) {
  41. var params,
  42. buttons,
  43. photoTitle;
  44. if (!photoID) return false;
  45. if (visible.photo()) photoTitle = photo.json.title;
  46. else photoTitle = album.json.content[photoID].title;
  47. if (photoTitle=="") photoTitle = "Untitled";
  48. buttons = [
  49. ["Delete Photo", function() {
  50. // Change reference for the next and previous photo
  51. if (album.json.content[photoID].nextPhoto!==""||album.json.content[photoID].previousPhoto!=="") {
  52. nextPhoto = album.json.content[photoID].nextPhoto;
  53. previousPhoto = album.json.content[photoID].previousPhoto;
  54. album.json.content[previousPhoto].nextPhoto = nextPhoto;
  55. album.json.content[nextPhoto].previousPhoto = previousPhoto;
  56. }
  57. album.json.content[photoID] = null;
  58. view.album.content.delete(photoID);
  59. // Only when search is not active
  60. if (!visible.albums()) lychee.goto(album.getID());
  61. params = "deletePhoto&photoID=" + photoID;
  62. lychee.api(params, function(data) {
  63. if (data!==true) lychee.error(null, params, data);
  64. });
  65. }],
  66. ["Keep Photo", function() {}]
  67. ];
  68. modal.show("Delete Photo", "Are you sure you want to delete the photo '" + photoTitle + "'?<br>This action can't be undone!", buttons);
  69. },
  70. setTitle: function(photoID) {
  71. var oldTitle = "",
  72. newTitle,
  73. params,
  74. buttons;
  75. if (!photoID) return false;
  76. if (photo.json) oldTitle = photo.json.title;
  77. else if (album.json) oldTitle = album.json.content[photoID].title;
  78. buttons = [
  79. ["Set Title", function() {
  80. newTitle = $(".message input.text").val();
  81. if (photoID!=null&&photoID&&newTitle.length<31) {
  82. if (visible.photo()) {
  83. photo.json.title = (newTitle==="") ? "Untitled" : newTitle;
  84. view.photo.title(oldTitle);
  85. }
  86. album.json.content[photoID].title = newTitle;
  87. view.album.content.title(photoID);
  88. params = "setPhotoTitle&photoID=" + photoID + "&title=" + escape(encodeURI(newTitle));
  89. lychee.api(params, function(data) {
  90. if (data!==true) lychee.error(null, params, data);
  91. });
  92. } else if (newTitle.length>0) loadingBar.show("error", "New title to short or too long. Please try another one!");
  93. }],
  94. ["Cancel", function() {}]
  95. ];
  96. modal.show("Set Title", "Please enter a new title for this photo: <input class='text' type='text' placeholder='Title' value='" + oldTitle + "'>", buttons);
  97. },
  98. setAlbum: function(albumID, photoID) {
  99. var params;
  100. if (albumID>=0) {
  101. // Change reference for the next and previous photo
  102. if (album.json.content[photoID].nextPhoto!==""||album.json.content[photoID].previousPhoto!=="") {
  103. nextPhoto = album.json.content[photoID].nextPhoto;
  104. previousPhoto = album.json.content[photoID].previousPhoto;
  105. album.json.content[previousPhoto].nextPhoto = nextPhoto;
  106. album.json.content[nextPhoto].previousPhoto = previousPhoto;
  107. }
  108. if (visible.photo) lychee.goto(album.getID());
  109. album.json.content[photoID] = null;
  110. view.album.content.delete(photoID);
  111. params = "setAlbum&photoID=" + photoID + "&albumID=" + albumID;
  112. lychee.api(params, function(data) {
  113. if (data!==true) lychee.error(null, params, data);
  114. });
  115. }
  116. },
  117. setStar: function(photoID) {
  118. var params;
  119. if (visible.photo()) {
  120. photo.json.star = (photo.json.star==0) ? 1 : 0;
  121. view.photo.star();
  122. }
  123. album.json.content[photoID].star = (album.json.content[photoID].star==0) ? 1 : 0;
  124. view.album.content.star(photoID);
  125. params = "setPhotoStar&photoID=" + photoID;
  126. lychee.api(params, function(data) {
  127. if (data!==true) lychee.error(null, params, data);
  128. });
  129. },
  130. setPublic: function(photoID, e) {
  131. var params;
  132. if (photo.json.public==2) {
  133. 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() {}]]);
  134. return false;
  135. }
  136. if (visible.photo()) {
  137. photo.json.public = (photo.json.public==0) ? 1 : 0;
  138. view.photo.public();
  139. if (photo.json.public==1) contextMenu.sharePhoto(photoID, e);
  140. }
  141. album.json.content[photoID].public = (album.json.content[photoID].public==0) ? 1 : 0;
  142. view.album.content.public(photoID);
  143. params = "setPhotoPublic&photoID=" + photoID + "&url=" + photo.getViewLink(photoID);
  144. lychee.api(params, function(data) {
  145. if (data!==true) lychee.error(null, params, data);
  146. });
  147. },
  148. setDescription: function(photoID) {
  149. var oldDescription = photo.json.description,
  150. description,
  151. params,
  152. buttons;
  153. buttons = [
  154. ["Set Description", function() {
  155. description = $(".message input.text").val();
  156. if (description.length<800) {
  157. if (visible.photo()) {
  158. photo.json.description = description;
  159. view.photo.description();
  160. }
  161. params = "setPhotoDescription&photoID=" + photoID + "&description=" + escape(description);
  162. lychee.api(params, function(data) {
  163. if (data!==true) lychee.error(null, params, data);
  164. });
  165. } else loadingBar.show("error", "Description too long. Please try again!");
  166. }],
  167. ["Cancel", function() {}]
  168. ];
  169. modal.show("Set Description", "Please enter a description for this photo: <input class='text' type='text' placeholder='Description' value='" + oldDescription + "'>", buttons);
  170. },
  171. share: function(photoID, service) {
  172. var link = "",
  173. url = photo.getViewLink(photoID),
  174. filename = "unknown";
  175. switch (service) {
  176. case 0:
  177. link = "https://twitter.com/share?url=" + encodeURI(url);
  178. break;
  179. case 1:
  180. link = "http://www.facebook.com/sharer.php?u=" + encodeURI(url) + "&t=" + encodeURI(photo.json.title);
  181. break;
  182. case 2:
  183. link = "mailto:?subject=" + encodeURI(photo.json.title) + "&body=" + encodeURI(url);
  184. break;
  185. case 3:
  186. lychee.loadDropbox(function() {
  187. filename = photo.json.title + "." + photo.getDirectLink().split('.').pop();
  188. Dropbox.save(photo.getDirectLink(), filename);
  189. });
  190. break;
  191. default:
  192. link = "";
  193. break;
  194. }
  195. if (link.length>5) location.href = link;
  196. },
  197. isSmall: function() {
  198. var size = [
  199. ["width", false],
  200. ["height", false]
  201. ];
  202. if (photo.json.width<$(window).width()-60) size["width"] = true;
  203. if (photo.json.height<$(window).height()-100) size["height"] = true;
  204. if (size["width"]&&size["height"]) return true;
  205. else return false;
  206. },
  207. getArchive: function(photoID) {
  208. var link;
  209. if (location.href.indexOf("index.html")>0) link = location.href.replace(location.hash, "").replace("index.html", "php/api.php?function=getPhotoArchive&photoID=" + photoID);
  210. else link = location.href.replace(location.hash, "") + "php/api.php?function=getPhotoArchive&photoID=" + photoID;
  211. if (lychee.publicMode) link += "&password=" + password.value;
  212. location.href = link;
  213. },
  214. getDirectLink: function() {
  215. return $("#imageview #image").css("background-image").replace(/"/g,"").replace(/url\(|\)$/ig, "");
  216. },
  217. getViewLink: function(photoID) {
  218. if (location.href.indexOf("index.html")>0) return location.href.replace("index.html" + location.hash, "view.php?p=" + photoID);
  219. else return location.href.replace(location.hash, "view.php?p=" + photoID);
  220. }
  221. }