photo.js 8.9 KB

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