photo.js 14 KB

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