photo.js 14 KB

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