functions.js 27 KB

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