photo.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /**
  2. * @name photo.js
  3. * @author Philipp Maurer
  4. * @author Tobias Reich
  5. * @copyright 2013 by Philipp Maurer, Tobias Reich
  6. *
  7. * Photo Module
  8. * Takes care of every action a photo can handle and execute.
  9. */
  10. photo = {
  11. json: null,
  12. getID: function() {
  13. var id;
  14. if (photo.json) id = photo.json.id;
  15. else id = $(".photo:hover, .photo.active").attr("data-id");
  16. if (id) return id;
  17. else return false;
  18. },
  19. load: function(photoID, albumID) {
  20. var params,
  21. checkPasswd;
  22. params = "getPhoto&photoID=" + photoID + "&albumID=" + albumID + "&password=" + password.value;
  23. lychee.api(params, "json", function(data) {
  24. if (data=="HTTP/1.1 403 Wrong password!") {
  25. checkPasswd = function() {
  26. if (password.value!="") photo.load(photoID, albumID);
  27. else setTimeout(checkPasswd, 250);
  28. }
  29. checkPasswd();
  30. return false;
  31. }
  32. photo.json = data;
  33. view.photo.init();
  34. lychee.imageview.show();
  35. setTimeout(function() { lychee.content.show() }, 300);
  36. });
  37. },
  38. parse: function() {
  39. if (!photo.json.title) photo.json.title = "Untitled";
  40. photo.json.url = lychee.upload_path_big + photo.json.url;
  41. },
  42. add: {
  43. files: function(files) {
  44. var pre_progress = 0,
  45. formData = new FormData(),
  46. xhr = new XMLHttpRequest(),
  47. popup,
  48. progress;
  49. $(".upload_overlay").remove();
  50. $("body").append(build.uploadModal());
  51. window.onbeforeunload = function() { return "Lychee is currently uploading!"; };
  52. for (var i = 0; i < files.length; i++) formData.append(i, files[i]);
  53. formData.append("function", "upload");
  54. if (album.getID()=="") formData.append("albumID", 0);
  55. else formData.append("albumID", album.getID());
  56. xhr.open('POST', lychee.api_path);
  57. xhr.onload = function() {
  58. if (xhr.status===200) {
  59. $(".progressbar div").css("width", "100%");
  60. $(".upload_overlay").removeClass("fadeIn").css("opacity", 0);
  61. setTimeout(function() { $(".upload_overlay").remove() }, 300);
  62. if (window.webkitNotifications&&BrowserDetect.browser=="Safari") {
  63. popup = window.webkitNotifications.createNotification("", "Upload complete", "You can now manage your new photo.");
  64. popup.show();
  65. }
  66. window.onbeforeunload = null;
  67. if (album.getID()=="") lychee.goto("a0");
  68. else album.load(album.getID());
  69. }
  70. };
  71. xhr.upload.onprogress = function(event) {
  72. if (event.lengthComputable) {
  73. progress = (event.loaded / event.total * 100 | 0);
  74. if (progress>pre_progress) {
  75. $(".progressbar div").css("width", progress + "%");
  76. pre_progress = progress;
  77. }
  78. if (progress>=100) $(".progressbar div").css("opacity", 0.2);
  79. }
  80. };
  81. $("#upload_files").val("");
  82. xhr.send(formData);
  83. },
  84. url: function(link) {
  85. var albumID = album.getID();
  86. if (!link) link = prompt("Please enter the direct link to a photo to import it:", "");
  87. if (album.getID()=="") albumID = 0;
  88. if (link&&link.length>3) {
  89. modal.close();
  90. $(".upload_overlay").remove();
  91. $("body").append(build.uploadModal());
  92. $(".progressbar div").css({
  93. "opacity": 0.2,
  94. "width": "100%"
  95. });
  96. params = "importUrl&url=" + escape(link) + "&albumID=" + albumID;
  97. lychee.api(params, "text", function(data) {
  98. $(".upload_overlay").removeClass("fadeIn").css("opacity", 0);
  99. setTimeout(function() { $(".upload_overlay").remove() }, 300);
  100. if (data) {
  101. if (album.getID()=="") lychee.goto("a0");
  102. else album.load(album.getID());
  103. } else loadingBar.show("error");
  104. });
  105. } else if (link.length>0) loadingBar.show("error", "Error", "Link to short or too long. Please try another one!");
  106. },
  107. dropbox: function() {
  108. lychee.loadDropbox(function() {
  109. Dropbox.choose({
  110. linkType: "direct",
  111. multiselect: false,
  112. success: function(files) { photo.add.url(files[0].link) }
  113. });
  114. });
  115. }
  116. },
  117. delete: function(photoID) {
  118. var params,
  119. buttons,
  120. photoTitle;
  121. if (!photoID) photoID = photo.getID();
  122. if (visible.photo()) photoTitle = photo.json.title;
  123. else photoTitle = album.json.content[photoID].title;
  124. if (photoTitle=="") photoTitle = "Untitled";
  125. buttons = [
  126. ["Delete Photo", function() {
  127. album.json.content[photoID] = null;
  128. view.album.content.delete(photoID);
  129. // Only when search is not active
  130. if (!visible.albums()) lychee.goto("a" + album.getID());
  131. params = "deletePhoto&photoID=" + photoID;
  132. lychee.api(params, "text", function(data) {
  133. if (!data) loadingBar.show("error");
  134. });
  135. }],
  136. ["Keep Photo", function() {}]
  137. ];
  138. modal.show("Delete Photo", "Are you sure you want to delete the photo '" + photoTitle + "'?<br>This action can't be undone!", buttons);
  139. },
  140. setTitle: function(photoID) {
  141. var oldTitle = "",
  142. newTitle,
  143. params;
  144. if (!photoID) photoID = photo.getID();
  145. if (photo.json) oldTitle = photo.json.title;
  146. else if (album.json) oldTitle = album.json.content[photoID].title;
  147. newTitle = prompt("Please enter a new title for this photo:", oldTitle);
  148. if (photoID!=null&&photoID&&newTitle.length<31) {
  149. if (visible.photo()) {
  150. photo.json.title = (newTitle=="") ? "Untitled" : newTitle;
  151. view.photo.title(oldTitle);
  152. }
  153. album.json.content[photoID].title = newTitle;
  154. view.album.content.title(photoID);
  155. params = "setPhotoTitle&photoID=" + photoID + "&title=" + escape(encodeURI(newTitle));
  156. lychee.api(params, "text", function(data) {
  157. if (!data) loadingBar.show("error");
  158. });
  159. } else if (newTitle.length>0) loadingBar.show("error", "Error", "New title to short or too long. Please try another one!");
  160. },
  161. setAlbum: function(albumID, photoID) {
  162. var params;
  163. if (albumID>=0) {
  164. if (visible.photo) lychee.goto("a" + album.getID());
  165. album.json.content[photoID] = null;
  166. view.album.content.delete(photoID);
  167. params = "setAlbum&photoID=" + photoID + "&albumID=" + albumID;
  168. lychee.api(params, "text", function(data) {
  169. if (!data) loadingBar.show("error");
  170. });
  171. }
  172. },
  173. setStar: function(photoID) {
  174. var params;
  175. if (visible.photo()) {
  176. photo.json.star = (photo.json.star==0) ? 1 : 0;
  177. view.photo.star();
  178. }
  179. album.json.content[photoID].star = (album.json.content[photoID].star==0) ? 1 : 0;
  180. view.album.content.star(photoID);
  181. params = "setPhotoStar&photoID=" + photoID;
  182. lychee.api(params, "text", function(data) {
  183. if (!data) loadingBar.show("error");
  184. });
  185. },
  186. setPublic: function(photoID, e) {
  187. var params;
  188. if (photo.json.public==2) {
  189. 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("a" + photo.json.original_album) }], ["Close", function() {}]]);
  190. return false;
  191. }
  192. if (visible.photo()) {
  193. photo.json.public = (photo.json.public==0) ? 1 : 0;
  194. view.photo.public();
  195. if (photo.json.public==1) contextMenu.sharePhoto(photoID, e);
  196. }
  197. album.json.content[photoID].public = (album.json.content[photoID].public==0) ? 1 : 0;
  198. view.album.content.public(photoID);
  199. params = "setPhotoPublic&photoID=" + photoID + "&url=" + photo.getViewLink(photoID);
  200. lychee.api(params, "text", function(data) {
  201. if (!data) loadingBar.show("error");
  202. });
  203. },
  204. setDescription: function(photoID) {
  205. var oldDescription = photo.json.description,
  206. description = prompt("Please enter a description for this photo:", oldDescription),
  207. params;
  208. if (description.length>0&&description.length<160) {
  209. if (visible.photo()) {
  210. photo.json.description = description;
  211. view.photo.description(oldDescription);
  212. }
  213. params = "setPhotoDescription&photoID=" + photoID + "&description=" + escape(description);
  214. lychee.api(params, "text", function(data) {
  215. if (!data) loadingBar.show("error");
  216. });
  217. } else if (description.length>0) loadingBar.show("error", "Error", "Description to short or too long. Please try another one!");
  218. },
  219. share: function(photoID, service) {
  220. var link = "",
  221. url = photo.getViewLink(photoID),
  222. params,
  223. filename = "unknown";
  224. switch (service) {
  225. case 0:
  226. link = "https://twitter.com/share?url=" + encodeURI(url);
  227. break;
  228. case 1:
  229. link = "http://www.facebook.com/sharer.php?u=" + encodeURI(url) + "&t=" + encodeURI(photo.json.title);
  230. break;
  231. case 2:
  232. link = "mailto:?subject=" + encodeURI(photo.json.title) + "&body=" + encodeURI(url);
  233. break;
  234. case 3:
  235. lychee.loadDropbox(function() {
  236. filename = photo.json.title + "." + photo.getDirectLink().split('.').pop();
  237. Dropbox.save(photo.getDirectLink(), filename);
  238. });
  239. break;
  240. default:
  241. link = "";
  242. break;
  243. }
  244. if (link.length>5) location.href = link;
  245. },
  246. isSmall: function() {
  247. var size = [
  248. ["width", false],
  249. ["height", false]
  250. ];
  251. if (photo.json.width<$(window).width()-60) size["width"] = true;
  252. if (photo.json.height<$(window).height()-100) size["height"] = true;
  253. if (size["width"]&&size["height"]) return true;
  254. else return false;
  255. },
  256. getDirectLink: function() {
  257. return $("#imageview #image").css("background-image").replace(/"/g,"").replace(/url\(|\)$/ig, "");
  258. },
  259. getViewLink: function(photoID) {
  260. if (location.href.indexOf("index.html")>0) return location.href.replace("index.html" + location.hash, "view.php?p=" + photoID);
  261. else return location.href.replace(location.hash, "view.php?p=" + photoID);
  262. }
  263. }