photo.js 13 KB

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