functions.js 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. /**
  2. * @name functions.js
  3. * @author Philipp Maurer
  4. * @author Tobias Reich
  5. * @copyright 2012 by Philipp Maurer, Tobias Reich
  6. */
  7. /*
  8. Init Function
  9. This function is called when the site is loaded.
  10. */
  11. function init() {
  12. $("#tools_albums").show();
  13. $(".tools").tipsy({gravity: 'n'});
  14. params = "function=loggedIn";
  15. $.ajax({type: "POST", url: api_path, data: params, success: function(data) {
  16. if (data!=1) {
  17. $("body").append(buildSignInModal());
  18. $("#username").focus();
  19. } else if (data) {
  20. if (headerTitle.html().length<1&&BrowserDetect.browser=="Firefox") getURL();
  21. }
  22. }});
  23. }
  24. /*
  25. Session Function
  26. This functions are called when the user tries to login or logout.
  27. */
  28. function login() {
  29. user = $("input#username").val();
  30. password = $("input#password").val();
  31. params = "function=login&user=" + user + "&password=" + password;
  32. $.ajax({type: "POST", url: api_path, data: params, success: function(data) {
  33. if (data==1) {
  34. getURL();
  35. closeModal();
  36. } else {
  37. $("#password").val("").addClass("error");
  38. $(".message .button.active").removeClass("pressed");
  39. }
  40. }});
  41. }
  42. function logout() {
  43. params = "function=logout";
  44. $.ajax({type: "POST", url: api_path, data: params, success: function(data) {
  45. window.location.reload();
  46. }});
  47. }
  48. /*
  49. URL Function
  50. This functions are used to get and set the URL.
  51. */
  52. function setURL(website_url) {
  53. document.location.hash = website_url;
  54. }
  55. function getURL() {
  56. closeContextMenu();
  57. hash = document.location.hash.replace("#", "");
  58. albumID = "";
  59. photoID = "";
  60. if (hash.indexOf("a")!=-1) albumID = hash.split("p")[0].replace("a", "");
  61. if (hash.indexOf("p")!=-1) photoID = hash.split("p")[1];
  62. if (hash=="upload") {
  63. loadAlbums();
  64. $("body").append(buildAddModal);
  65. } else if (albumID&&photoID) {
  66. content.hide();
  67. showImageview(photoID);
  68. if (content.html()=="") loadPhotos(albumID, true);
  69. } else if (albumID) {
  70. if (!visibleControls()) showControls();
  71. if (visibleImageview()) hideImageview();
  72. else loadPhotos(albumID, false);
  73. } else {
  74. loadAlbums();
  75. }
  76. }
  77. /*
  78. Button Functions
  79. This functions are called from Buttons.
  80. */
  81. function deleteAlbum() {
  82. albumTitle = headerTitle.html().replace($("#title span").html(), "").replace("<span></span>", "");
  83. f1 = "loadDeleteAlbum(" + content.attr("data-id") + ", true);";
  84. f2 = "loadDeleteAlbum(" + content.attr("data-id") + ", false);";
  85. modal = buildModal("Delete Album", "Are you sure you want to delete the album '" + albumTitle + "' and all of the photos it contains? This action can't be undone!", ["Delete Album and Photos", "Keep Photos"], [f1, f2]);
  86. $("body").append(modal);
  87. }
  88. function deleteUnsorted() {
  89. albumTitle = headerTitle.html().replace($("#title span").html(), "").replace("<span></span>", "");
  90. f1 = "loadDeleteAlbum(" + content.attr("data-id") + ", true);";
  91. f2 = "";
  92. modal = buildModal("Clear Unsorted", "Are you sure you want to delete all photos from 'Unsorted'?<br>This action can't be undone!", ["Clear Unsorted", "Keep Photos"], [f1, f2]);
  93. $("body").append(modal);
  94. }
  95. function deletePhoto(photoID) {
  96. if (photoID==undefined) photoTitle = " '" + headerTitle.html() + "'"; else photoTitle = "";
  97. if (photoID==undefined) photoID = image_view.attr("data-id");
  98. f1 = "loadDeletePhoto(" + photoID + ");";
  99. f2 = "";
  100. modal = buildModal("Delete Photo", "Are you sure you want to delete the photo" + photoTitle + "?<br>This action can't be undone!", ["Delete Photo", "Keep Photo"], [f1, f2]);
  101. $("body").append(modal);
  102. }
  103. function closeModal() {
  104. $(".message_overlay").removeClass("fadeIn").css("opacity", 0);
  105. $.timer(300,function(){ $(".message_overlay").remove() });
  106. }
  107. /*
  108. Show/Hide Functions
  109. This functions are used to show/hide content.
  110. */
  111. function loadingFadeIn(status, errorTitle, errorText) {
  112. switch (status) {
  113. case "error":
  114. loading.removeClass("loading uploading error").addClass(status);
  115. if (!errorTitle||!errorText) {
  116. errorTitle = "Error";
  117. errorText = "Whoops, it looks like something went wrong. Please reload the site and try again!"
  118. }
  119. loading.html("<h1>" + errorTitle + ": <span>" + errorText + "</span></h1>");
  120. loading.css("height", "40px");
  121. header.css("margin-Top", "40px");
  122. $.timer(3000,function(){ loadingFadeOut() });
  123. break;
  124. case "loading":
  125. loading.removeClass("loading uploading error").addClass(status);
  126. if (visibleControls()) header.css("margin-Top", "3px");
  127. break;
  128. }
  129. }
  130. function loadingFadeOut() {
  131. loading.html("").css("height", "3px");
  132. if (visibleControls()) header.css("marginTop", "0px");
  133. }
  134. function showImageview(photoID) {
  135. // Change toolbar-buttons
  136. $("#tools_albums, #tools_album").hide();
  137. $("#tools_photo").show();
  138. // Make body scrollable
  139. $("body").css("overflow", "hidden");
  140. // Load photo
  141. loadPhotoInfo(photoID);
  142. }
  143. function hideImageview() {
  144. // Change toolbar-buttons
  145. $("#tools_photo, #tools_albums").hide();
  146. $("#tools_album").show();
  147. // Make body scrollable
  148. $("body").css("overflow", "scroll");
  149. // Change website title and url by using loadAlbumInfo
  150. loadAlbumInfo(content.attr("data-id"));
  151. // Hide ImageViewer
  152. image_view.removeClass("fadeIn").addClass("fadeOut");
  153. $.timer(300,function(){ image_view.hide() });
  154. }
  155. function visibleImageview() {
  156. if ($("#image_view.fadeIn").length>0) return true;
  157. else return false;
  158. }
  159. function showControls() {
  160. clearTimeout($(window).data("timeout"));
  161. if (visibleImageview()&&!runningDiashow()) {
  162. $("#image_view a#previous").css("left", "20px");
  163. $("#image_view a#next").css("right", "20px");
  164. image_view.css("background-color", "rgba(30,30,30,.99)");
  165. loading.css("opacity", 1);
  166. header.css("margin-Top", "0px");
  167. if ($("#image_view #image.small").length>0) {
  168. $("#image_view #image").css({
  169. marginTop: -1*($("#image_view #image").height()/2)+20
  170. });
  171. } else {
  172. $("#image_view #image").css({
  173. top: 70,
  174. right: 30,
  175. bottom: 30,
  176. left: 30
  177. });
  178. }
  179. }
  180. }
  181. function hideControls() {
  182. if (visibleImageview()) {
  183. clearTimeout($(window).data("timeout"));
  184. $(window).data("timeout", setTimeout( function () {
  185. $("#image_view a#previous").css("left", "-50px");
  186. $("#image_view a#next").css("right", "-50px");
  187. image_view.css("background-color", "#111");
  188. loading.css("opacity", 0);
  189. header.css("margin-Top", "-45px");
  190. if ($("#image_view #image.small").length>0) {
  191. $("#image_view #image").css({
  192. marginTop: -1*($("#image_view #image").height()/2)
  193. });
  194. } else {
  195. $("#image_view #image").css({
  196. top: 0,
  197. right: 0,
  198. bottom: 0,
  199. left: 0
  200. });
  201. }
  202. }, 500));
  203. }
  204. }
  205. function visibleControls() {
  206. if (loading.css("opacity")>0) return true;
  207. else return false;
  208. }
  209. function showContextMenuPhoto(photoID, mouse_x, mouse_y) {
  210. mouse_y -= $(document).scrollTop();
  211. items = [
  212. ["Rename", "renamePhoto(" + photoID + ")"],
  213. ["Move to Album", "showContextMenuMove(" + photoID + ", " + (mouse_x+150) + ", " + (mouse_y+$(document).scrollTop()) + ")"],
  214. ["Delete", "deletePhoto(" + photoID + ")"]
  215. ];
  216. closeContextMenu();
  217. $("body").css("overflow", "hidden");
  218. $(".photo[data-id='" + photoID + "']").addClass("active");
  219. $("body").append(buildContextMenu(items));
  220. $(".contextmenu").css({
  221. "top": mouse_y,
  222. "left": mouse_x
  223. });
  224. }
  225. function showContextMenuMove(photoID, mouse_x, mouse_y) {
  226. mouse_y -= $(document).scrollTop();
  227. params = "function=getAlbums";
  228. $.ajax({type: "POST", url: api_path, data: params, dataType: "json", success: function(data) {
  229. if (content.attr("data-id")==0) {
  230. items = new Array();
  231. } else {
  232. items = [
  233. ["Unsorted", "loadMovePhoto(" + photoID + ", 0)"]
  234. ];
  235. }
  236. $.each(data, function(index) {
  237. if (this.id!=content.attr("data-id")) {
  238. if(!this.title) this.title = "Untitled";
  239. items[items.length] = new Array(this.title, "loadMovePhoto(" + photoID + ", " + this.id + ")");
  240. } else {
  241. items[items.length] = new Array("", "");
  242. }
  243. });
  244. if (items.length==0) {
  245. items = [
  246. ["Create new Album", "addAlbum()"]
  247. ];
  248. }
  249. closeContextMenu();
  250. $("body").css("overflow", "hidden");
  251. $(".photo[data-id='" + photoID + "']").addClass("active");
  252. $("body").append(buildContextMenu(items));
  253. $(".contextmenu").css({
  254. "top": mouse_y,
  255. "left": mouse_x-150
  256. });
  257. }, error: ajaxError });
  258. }
  259. function showContextMenuShare(photoID, mouse_x, mouse_y) {
  260. mouse_y -= $(document).scrollTop();
  261. items = [
  262. ["<a class='icon-eye-close'></a> Make Private", "setPhotoPublic()"],
  263. ["<a class='icon-twitter'></a> Twitter", "loadSharePhoto(0, " + photoID + ")"],
  264. ["<a class='icon-facebook'></a> Facebook", "loadSharePhoto(1, " + photoID + ")"],
  265. ["<a class='icon-sign-blank'></a> Tumblr", "loadSharePhoto(2, " + photoID + ")"],
  266. ["<a class='icon-pinterest'></a> Pinterest", "loadSharePhoto(3, " + photoID + ")"],
  267. ["<a class='icon-envelope'></a> Mail", "loadSharePhoto(4, " + photoID + ")"],
  268. ["<a class='icon-link'></a> Copy Link", "loadSharePhoto(5, " + photoID + ")"],
  269. ["<a class='icon-link'></a> Copy Shortlink", "loadSharePhoto(6, " + photoID + ")"]
  270. ];
  271. closeContextMenu();
  272. $("body").css("overflow", "hidden");
  273. $(".photo[data-id='" + photoID + "']").addClass("active");
  274. $("body").append(buildContextMenu(items));
  275. $(".contextmenu").css({
  276. "top": mouse_y,
  277. "left": mouse_x
  278. });
  279. }
  280. function closeContextMenu() {
  281. $(".contextmenu_bg, .contextmenu").remove();
  282. $(".photo.active").removeClass("active");
  283. $("body").css("overflow", "scroll");
  284. }
  285. function hidePhoto(photoID) {
  286. $(".photo[data-id='" + photoID + "']").css("opacity", 0).animate({
  287. width: 0,
  288. marginLeft: 0
  289. }, 300, function() {
  290. $(this).remove();
  291. imgNum = parseInt($("#title span").html().replace("- ", "").replace(" photos", ""))-1;
  292. $("#title span").html(" - " + imgNum + " photos");
  293. });
  294. }
  295. function startDiashow() {
  296. image_view.attr("data-diashow", "play");
  297. clearInterval($(window).data("diashow"));
  298. hideControls();
  299. $(window).data("diashow", setInterval(function() { loadNextPhoto() }, 3000));
  300. }
  301. function stopDiashow() {
  302. image_view.attr("data-diashow", "stop");
  303. clearInterval($(window).data("diashow"));
  304. showControls();
  305. }
  306. function runningDiashow() {
  307. if (image_view.attr("data-diashow")=="play") return true;
  308. else return false;
  309. }
  310. function showInfobox() {
  311. $("body").append("<div id='infobox_overlay'></div>");
  312. infobox.css("right", "0px");
  313. }
  314. function hideInfobox() {
  315. $("#infobox_overlay").remove();
  316. infobox.css("right", "-320px");
  317. }
  318. function visibleInfobox() {
  319. if (parseInt(infobox.css("right").replace("px", ""))<0) return false;
  320. else return true;
  321. }
  322. function isPhotoSmall(photo) {
  323. size = [
  324. ["width", false],
  325. ["height", false]
  326. ];
  327. if (photo.width<$(window).width()-60) size["width"] = true;
  328. if (photo.height<$(window).height()-100) size["height"] = true;
  329. if (size["width"]&&size["height"]) return true;
  330. else return false;
  331. }
  332. /*
  333. Key Function
  334. This function triggers events when a special key is pressed.
  335. */
  336. function key(e) {
  337. code = (e.keyCode ? e.keyCode : e.which);
  338. if (code==13||code==37||code==39||code==32||code==27) e.preventDefault();
  339. if (code==13&&$(".message .button.active").length) $(".message .button.active").addClass("pressed").click();
  340. if (code==37&&visibleImageview()) loadPreviousPhoto();
  341. if (code==39&&visibleImageview()) loadNextPhoto();
  342. if (code==32&&visibleImageview()&&!runningDiashow()) startDiashow();
  343. else if (code==32&&visibleImageview()&&runningDiashow()) stopDiashow();
  344. if (code==27&&$(".message").length&&$(".sign_in").length==0) closeModal();
  345. else if (code==27&&visibleInfobox()) { hideInfobox(); e.preventDefault(); }
  346. else if (code==27&&visibleImageview()&&runningDiashow()) stopDiashow();
  347. else if (code==27&&visibleImageview()&&!runningDiashow()) { showControls(); setURL("a" + content.attr("data-id")); };
  348. }
  349. function getViewLink(photoID) {
  350. if (location.href.indexOf("index.html")>0) return location.href.replace("index.html" + location.hash, "view.php?p=" + photoID);
  351. else return location.href.replace(location.hash, "view.php?p=" + photoID);
  352. }
  353. /*
  354. Ajax Functions
  355. This functions are used to get and process data from the Api.
  356. */
  357. function addAlbum() {
  358. title = prompt("Please enter a title for this album:", "Untitled");
  359. closeModal();
  360. if (title.length>2&&title.length<31) {
  361. loadingFadeIn("loading");
  362. params = "function=addAlbum&title=" + escape(title);
  363. $.ajax({type: "POST", url: api_path, data: params, success: function(data) {
  364. if (data==0) loadingFadeIn("error");
  365. else setURL("a" + data);
  366. }, error: ajaxError });
  367. } else if (title.length>0) loadingFadeIn("error", "Error", "Title to short or too long. Please try another one!");
  368. }
  369. function renameAlbum() {
  370. oldTitle = headerTitle.html().replace($("#title span").html(), "").replace("<span></span>", "");
  371. newTitle = prompt("Please enter a new title for this album:", oldTitle);
  372. albumID = content.attr("data-id");
  373. if (albumID!=""&&albumID!=null&&albumID!=undefined&&newTitle.length>2&&newTitle.length<31) {
  374. loadingFadeIn("loading");
  375. params = "function=setAlbumTitle&albumID=" + albumID + "&title=" + encodeURI(newTitle);
  376. $.ajax({type: "POST", url: api_path, data: params, success: function(data) {
  377. if (data==1) {
  378. headerTitle.html(newTitle + "<span>" + $("#title span").html() + "</span>");
  379. document.title = "Lychee - " + newTitle;
  380. loadingFadeOut();
  381. } else loadingFadeIn("error");
  382. }, error: ajaxError });
  383. } else if (newTitle.length>0) loadingFadeIn("error", "Error", "New title to short or too long. Please try another one!");
  384. }
  385. function renamePhoto(photoID) {
  386. if (photoID==undefined) {
  387. // Function called from ImageViewer
  388. oldTitle = headerTitle.html();
  389. photoID = image_view.attr("data-id");
  390. } else oldTitle = "";
  391. newTitle = prompt("Please enter a new title for this photo:", oldTitle);
  392. if (photoID!=null&&photoID!=undefined&&newTitle.length<31) {
  393. loadingFadeIn("loading");
  394. params = "function=setPhotoTitle&photoID=" + photoID + "&title=" + encodeURI(newTitle);
  395. $.ajax({type: "POST", url: api_path, data: params, success: function(data) {
  396. if (data==1) {
  397. if (oldTitle!="") {
  398. headerTitle.html(newTitle);
  399. document.title = "Lychee - " + newTitle;
  400. }
  401. $(".photo[data-id='" + photoID + "'] .overlay h1").html(newTitle);
  402. loadingFadeOut();
  403. } else loadingFadeIn("error");
  404. }, error: ajaxError });
  405. } else if (newTitle.length>0) loadingFadeIn("error", "Error", "New title to short or too long. Please try another one!");
  406. }
  407. function loadAlbums() {
  408. loadingFadeIn("loading");
  409. $(".album, .photo").removeClass("contentZoomIn").addClass("contentZoomOut");
  410. startTime = new Date().getTime();
  411. params = "function=getAlbums";
  412. $.ajax({type: "POST", url: api_path, data: params, dataType: "json", success: function(data) {
  413. durationTime = (new Date().getTime() - startTime);
  414. if (durationTime>300) waitTime = 0; else waitTime = 300 - durationTime;
  415. $.timer(waitTime,function(){
  416. $("#tools_album, #tools_photo").hide();
  417. $("#tools_albums").show();
  418. content.attr("data-id", "");
  419. /* Smart Albums */
  420. unsortedAlbum = new Object();
  421. unsortedAlbum.id = 0;
  422. unsortedAlbum.title = "Unsorted";
  423. unsortedAlbum.sysdate = "";
  424. unsortedAlbum.unsorted = 1;
  425. starredAlbum = new Object();
  426. starredAlbum.id = "f";
  427. starredAlbum.title = "Starred";
  428. starredAlbum.sysdate = "";
  429. starredAlbum.star = 1;
  430. sharedAlbum = new Object();
  431. sharedAlbum.id = "s";
  432. sharedAlbum.title = "Public";
  433. sharedAlbum.sysdate = "";
  434. sharedAlbum.public = 1;
  435. content.html("").append(buildDivider("Smart Albums") + buildAlbum(unsortedAlbum) + buildAlbum(starredAlbum) + buildAlbum(sharedAlbum));
  436. if (data!=false) {
  437. /* Albums */
  438. albums = "";
  439. $.each(data, function() { albums += buildAlbum(this); });
  440. content.append(buildDivider("Albums") + albums);
  441. $(".album, .photo").removeClass("contentZoomOut").addClass("contentZoomIn");
  442. }
  443. document.title = "Lychee";
  444. headerTitle.html("Albums").removeClass("editable");
  445. loadSmartAlbums();
  446. });
  447. }, error: ajaxError });
  448. }
  449. function loadSmartAlbums() {
  450. params = "function=getSmartInfo";
  451. $.ajax({type: "POST", url: api_path, data: params, dataType: "json", success: function(data) {
  452. $(".album[data-id='0'] img:nth-child(1)").attr("src", data.unsortThumb2);
  453. $(".album[data-id='0'] img:nth-child(2)").attr("src", data.unsortThumb1);
  454. $(".album[data-id='0'] img:nth-child(3)").attr("src", data.unsortThumb0);
  455. $(".album[data-id='0'] .overlay a").html(data.unsortNum + " photos");
  456. $(".album[data-id='s'] img:nth-child(1)").attr("src", data.publicThumb2);
  457. $(".album[data-id='s'] img:nth-child(2)").attr("src", data.publicThumb1);
  458. $(".album[data-id='s'] img:nth-child(3)").attr("src", data.publicThumb0);
  459. $(".album[data-id='s'] .overlay a").html(data.publicNum + " photos");
  460. $(".album[data-id='f'] img:nth-child(1)").attr("src", data.starredThumb2);
  461. $(".album[data-id='f'] img:nth-child(2)").attr("src", data.starredThumb1);
  462. $(".album[data-id='f'] img:nth-child(3)").attr("src", data.starredThumb0);
  463. $(".album[data-id='f'] .overlay a").html(data.starredNum + " photos");
  464. loadingFadeOut();
  465. }, error: ajaxError });
  466. }
  467. function loadPhotos(albumID, refresh) {
  468. // If refresh is true the function will only refresh the content and not change the toolbar buttons either the title
  469. if (!refresh) {
  470. loadingFadeIn("loading");
  471. if (visibleImageview()) hideImageview();
  472. $(".album, .photo").removeClass("contentZoomIn").addClass("contentZoomOut");
  473. $(".divider").removeClass("fadeIn").addClass("fadeOut");
  474. }
  475. startTime = new Date().getTime();
  476. params = "function=getPhotos&albumID=" + albumID;
  477. $.ajax({type: "POST", url: api_path, data: params, dataType: "json", success: function(data) {
  478. durationTime = (new Date().getTime() - startTime);
  479. if (durationTime>300) waitTime = 0; else if (refresh) waitTime = 0; else waitTime = 300 - durationTime;
  480. $.timer(waitTime,function(){
  481. content.attr("data-id", albumID);
  482. photos = "";
  483. $.each(data, function() { photos += buildPhoto(this); });
  484. content.html("").append(photos);
  485. if (!refresh) {
  486. $(".album, .photo").removeClass("contentZoomOut").addClass("contentZoomIn");
  487. $("#tools_albums, #tools_photo").hide();
  488. $("#tools_album").show();
  489. loadAlbumInfo(albumID);
  490. }
  491. });
  492. }, error: ajaxError });
  493. }
  494. function loadAlbumInfo(albumID) {
  495. if (albumID=="f"||albumID=="s"||albumID==0) {
  496. params = "function=getSmartInfo";
  497. $.ajax({type: "POST", url: api_path, data: params, dataType: "json", success: function(data) {
  498. switch (albumID) {
  499. case "f":
  500. document.title = "Lychee - Starred";
  501. headerTitle.html("Starred<span> - " + data.starredNum + " photos</span>");
  502. $("#button_edit_album, #button_trash_album, .button_divider").hide();
  503. break;
  504. case "s":
  505. document.title = "Lychee - Public";
  506. headerTitle.html("Public<span> - " + data.publicNum + " photos</span>");
  507. $("#button_edit_album, #button_trash_album .button_divider").hide();
  508. break;
  509. case "0":
  510. document.title = "Lychee - Unsorted";
  511. headerTitle.html("Unsorted<span> - " + data.unsortNum + " photos</span>");
  512. $("#button_edit_album").hide();
  513. $("#button_trash_album, .button_divider").show();
  514. break;
  515. }
  516. loadingFadeOut();
  517. }, error: ajaxError });
  518. } else {
  519. params = "function=getAlbumInfo&albumID=" + albumID;
  520. $.ajax({type: "POST", url: api_path, data: params, dataType: "json", success: function(data) {
  521. $("#button_edit_album, #button_trash_album, .button_divider").show();
  522. if (!data.title) data.title = "Untitled";
  523. document.title = "Lychee - " + data.title;
  524. headerTitle.html(data.title + "<span> - " + data.num + " photos</span>").addClass("editable");
  525. loadingFadeOut();
  526. }, error: ajaxError });
  527. }
  528. }
  529. function loadPhotoInfo(photoID) {
  530. loadingFadeIn("loading");
  531. params = "function=getPhotoInfo&photoID=" + photoID;
  532. $.ajax({type: "POST", url: api_path, data: params, dataType: "json", success: function(data) {
  533. if (!data.title) data.title = "Untitled";
  534. document.title = "Lychee - " + data.title;
  535. headerTitle.html(data.title).addClass("editable");
  536. $("#button_star a").removeClass("icon-star-empty icon-star");
  537. if (data.star=="1") {
  538. $("#button_star a").addClass("icon-star");
  539. $("#button_star").attr("title", "Unstar Photo");
  540. } else {
  541. $("#button_star a").addClass("icon-star-empty");
  542. $("#button_star").attr("title", "Star Photo");
  543. }
  544. if (data.public=="1") {
  545. $("#button_share a").addClass("active");
  546. $("#button_share").attr("title", "Make Photo Private");
  547. } else {
  548. $("#button_share a").removeClass("active");
  549. $("#button_share").attr("title", "Share Photo");
  550. }
  551. image_view.attr("data-id", photoID);
  552. if (visibleControls()&&isPhotoSmall(data)) image_view.html("").append("<a id='previous' class='icon-caret-left'></a><a id='next' class='icon-caret-right'></a><div id='image' class='small' style='background-image: url(" + data.url + "); width: " + data.width + "px; height: " + data.height + "px; margin-top: -" + parseInt(data.height/2-20) + "px; margin-left: -" + data.width/2 + "px;'></div>");
  553. else if (visibleControls()) image_view.html("").append("<a id='previous' class='icon-caret-left'></a><a id='next' class='icon-caret-right'></a><div id='image' style='background-image: url(" + data.url + ")'></div>");
  554. else if (isPhotoSmall(data)) image_view.html("").append("<a id='previous' style='left: -50px' class='icon-caret-left'></a><a id='next' style='right: -50px' class='icon-caret-right'></a><div id='image' class='small' style='background-image: url(" + data.url + "); width: " + data.width + "px; height: " + data.height + "px; margin-top: -" + parseInt(data.height/2) + "px; margin-left: -" + data.width/2 + "px;'></div>");
  555. else image_view.html("").append("<a id='previous' style='left: -50px' class='icon-caret-left'></a><a id='next' style='right: -50px' class='icon-caret-right'></a><div id='image' style='background-image: url(" + data.url + "); top: 0px; right: 0px; bottom: 0px; left: 0px;'></div>");
  556. image_view.removeClass("fadeOut").addClass("fadeIn").show();
  557. if (!visibleControls()) hideControls(true);
  558. infobox.html(buildInfobox(data)).show();
  559. $.timer(300,function(){ content.show(); });
  560. loadingFadeOut();
  561. }, error: ajaxError });
  562. }
  563. function setPhotoStar() {
  564. loadingFadeIn("loading");
  565. photoID = image_view.attr("data-id");
  566. params = "function=setPhotoStar&photoID=" + photoID;
  567. $.ajax({type: "POST", url: api_path, data: params, success: function(data) {
  568. if (data==1) {
  569. if ($("#button_star a.icon-star-empty").length) {
  570. $("#button_star a").removeClass("icon-star-empty icon-star").addClass("icon-star");
  571. $("#button_star").attr("title", "Unstar Photo");
  572. } else {
  573. $("#button_star a").removeClass("icon-star-empty icon-star").addClass("icon-star-empty");
  574. $("#button_star").attr("title", "Star Photo");
  575. }
  576. loadPhotos(content.attr("data-id"), true);
  577. loadingFadeOut();
  578. } else loadingFadeIn("error");
  579. }, error: ajaxError });
  580. }
  581. function setPhotoPublic(e) {
  582. loadingFadeIn("loading");
  583. photoID = image_view.attr("data-id");
  584. params = "function=setPhotoPublic&photoID=" + photoID + "&url=" + getViewLink(photoID);
  585. $.ajax({type: "POST", url: api_path, data: params, success: function(data) {
  586. if (data==1) {
  587. if ($("#button_share a.active").length) {
  588. $("#button_share a").removeClass("active");
  589. $("#button_share").attr("title", "Make Private");
  590. } else {
  591. $("#button_share a").addClass("active");
  592. $("#button_share").attr("title", "Share Photo");
  593. showContextMenuShare(photoID, e.pageX, e.pageY);
  594. }
  595. loadPhotos(content.attr("data-id"), true);
  596. loadingFadeOut();
  597. } else loadingFadeIn("error");
  598. }, error: ajaxError });
  599. }
  600. function setPhotoDescription() {
  601. description = prompt("Please enter a description for this photo:", "");
  602. photoID = image_view.attr("data-id");
  603. if (description.length>0&&description.length<160) {
  604. loadingFadeIn("loading");
  605. params = "function=setPhotoDescription&photoID=" + photoID + "&description=" + escape(description);
  606. $.ajax({type: "POST", url: api_path, data: params, success: function(data) {
  607. if (data==0) loadingFadeIn("error");
  608. else loadPhotoInfo(photoID);
  609. }, error: ajaxError });
  610. } else if (description.length>0) loadingFadeIn("error", "Error", "Description to short or too long. Please try another one!");
  611. }
  612. function loadDeleteAlbum(albumID, delAll) {
  613. loadingFadeIn("loading");
  614. params = "function=deleteAlbum&albumID=" + albumID + "&delAll=" + delAll;
  615. $.ajax({type: "POST", url: api_path, data: params, success: function(data) {
  616. if (data==1) setURL("");
  617. else loadingFadeIn("error");
  618. }, error: ajaxError });
  619. }
  620. function loadDeletePhoto(photoID) {
  621. loadingFadeIn("loading");
  622. params = "function=deletePhoto&photoID=" + photoID;
  623. $.ajax({type: "POST", url: api_path, data: params, success: function(data) {
  624. if (data==1) {
  625. hidePhoto(photoID);
  626. setURL("a" + content.attr("data-id"));
  627. loadingFadeOut();
  628. }
  629. else loadingFadeIn("error");
  630. }, error: ajaxError });
  631. }
  632. function loadSharePhoto(service, photoID) {
  633. loadingFadeIn("loading");
  634. params = "function=sharePhoto&photoID=" + photoID + "&url=" + getViewLink(photoID);
  635. $.ajax({type: "POST", url: api_path, data: params, dataType: "json", success: function(data) {
  636. switch (service) {
  637. case 0:
  638. link = data.twitter;
  639. break;
  640. case 1:
  641. link = data.facebook;
  642. break;
  643. case 2:
  644. link = data.tumblr;
  645. break;
  646. case 3:
  647. link = data.pinterest;
  648. break;
  649. case 4:
  650. link = data.mail;
  651. break;
  652. case 5:
  653. link = "copy";
  654. modal = buildModal("Copy Link", "You can use this link to share your image with other people: <input class='copylink' value='" + getViewLink(photoID) + "'>", ["Close"], [""]);
  655. $("body").append(modal);
  656. $(".copylink").click();
  657. break;
  658. case 6:
  659. link = "copy";
  660. modal = buildModal("Copy Shortlink", "You can use this link to share your image with other people: <input class='copylink' value='" + data.shortlink + "'>", ["Close"], [""]);
  661. $("body").append(modal);
  662. $(".copylink").click();
  663. break;
  664. default:
  665. link = "";
  666. break;
  667. }
  668. if (link=="copy") loadingFadeOut();
  669. else if (link.length>5) {
  670. location.href = link;
  671. loadingFadeOut();
  672. } else loadingFadeIn("error");
  673. }, error: ajaxError });
  674. }
  675. function getAlbumArchive() {
  676. albumID = content.attr("data-id");
  677. if (location.href.indexOf("index.html")>0) link = location.href.replace(location.hash, "").replace("index.html", "php/api.php?function=getAlbumArchive&albumID=" + albumID);
  678. else link = location.href.replace(location.hash, "") + "php/api.php?function=getAlbumArchive&albumID=" + albumID;
  679. location.href = link;
  680. }
  681. function loadMovePhoto(photoID, albumID) {
  682. if (albumID>=0) {
  683. loadingFadeIn("loading");
  684. params = "function=movePhoto&photoID=" + photoID + "&albumID=" + albumID;
  685. $.ajax({type: "POST", url: api_path, data: params, success: function(data) {
  686. if (data==1) {
  687. hidePhoto(photoID);
  688. setURL("a" + content.attr("data-id"));
  689. loadingFadeOut();
  690. } else loadingFadeIn("error");
  691. }, error: ajaxError });
  692. }
  693. }
  694. function loadPreviousPhoto() {
  695. albumID = content.attr("data-id");
  696. photoID = image_view.attr("data-id");
  697. params = "function=previousPhoto&photoID=" + photoID + "&albumID=" + albumID;
  698. $.ajax({type: "POST", url: api_path, data: params, dataType: "json", success: function(data) {
  699. if (data!=false) setURL("a" + albumID + "p" + data.id);
  700. }, error: ajaxError });
  701. }
  702. function loadNextPhoto() {
  703. albumID = content.attr("data-id");
  704. photoID = image_view.attr("data-id");
  705. params = "function=nextPhoto&photoID=" + photoID + "&albumID=" + albumID;
  706. $.ajax({type: "POST", url: api_path, data: params, dataType: "json", success: function(data) {
  707. if (data) setURL("a" + albumID + "p" + data.id);
  708. }, error: ajaxError });
  709. }
  710. function syncFolder() {
  711. params = "function=syncFolder";
  712. $.ajax({type: "POST", url: api_path, data: params, success: function(data) {
  713. if (data==1) setURL("a0");
  714. else loadingFadeIn("error");
  715. }, error: ajaxError });
  716. }
  717. function search(term) {
  718. clearTimeout($(window).data("timeout"));
  719. $(window).data("timeout", setTimeout(function() {
  720. if ($("#search").val().length<=2) {
  721. $(".divider").removeClass("fadeIn").addClass("fadeOut");
  722. loadAlbums();
  723. } else {
  724. $(".album, .photo").removeClass("contentZoomIn").addClass("contentZoomOut");
  725. $(".divider").removeClass("fadeIn").addClass("fadeOut");
  726. startTime = new Date().getTime();
  727. params = "function=search&term=" + term;
  728. $.ajax({type: "POST", url: api_path, data: params, dataType: "json", success: function(data) {
  729. console.log(data);
  730. albums = "";
  731. if (data.albums!=undefined&&data.albums!=null) $.each(data.albums, function() { albums += buildAlbum(this); });
  732. photos = "";
  733. if (data.photos!=undefined&&data.photos!=null) $.each(data.photos, function() { photos += buildPhoto(this); });
  734. durationTime = (new Date().getTime() - startTime);
  735. if (durationTime>300) waitTime = 0; else waitTime = 300 - durationTime;
  736. $.timer(waitTime,function(){
  737. if (albums==""&&photos=="") code = "";
  738. else if (albums=="") code = buildDivider("Photos")+photos;
  739. else if (photos=="") code = buildDivider("Albums")+albums;
  740. else code = buildDivider("Photos")+photos+buildDivider("Albums")+albums;
  741. content.html("").append(code);
  742. $(".album, .photo").removeClass("contentZoomOut").addClass("contentZoomIn");
  743. });
  744. }, error: ajaxError });
  745. }
  746. }, 250));
  747. }
  748. /*
  749. Error Function
  750. This function is called when an ajax-request fails.
  751. */
  752. function ajaxError(jqXHR, textStatus, errorThrown) {
  753. console.log(jqXHR);
  754. console.log(textStatus);
  755. console.log(errorThrown);
  756. loadingFadeIn("error", textStatus, errorThrown);
  757. }