photo.js 13 KB

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