photo.js 14 KB

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