search.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * @name Search Module
  3. * @description Searches through your photos and albums.
  4. * @author Tobias Reich
  5. * @copyright 2014 by Tobias Reich
  6. */
  7. search = {
  8. code: null,
  9. find: function(term) {
  10. var params,
  11. albumsData = "",
  12. photosData = "",
  13. code;
  14. clearTimeout($(window).data("timeout"));
  15. $(window).data("timeout", setTimeout(function() {
  16. if ($("#search").val().length!==0) {
  17. params = "search&term=" + term;
  18. lychee.api(params, function(data) {
  19. if (data&&data.albums) {
  20. albums.json = { content: data.albums };
  21. $.each(albums.json.content, function() {
  22. albums.parse(this);
  23. albumsData += build.album(this);
  24. });
  25. }
  26. if (data&&data.photos) {
  27. album.json = { content: data.photos };
  28. $.each(album.json.content, function() {
  29. album.parse(this);
  30. photosData += build.photo(this);
  31. });
  32. }
  33. if (albumsData===""&&photosData==="") code = "error";
  34. else if (albumsData==="") code = build.divider("Photos")+photosData;
  35. else if (photosData==="") code = build.divider("Albums")+albumsData;
  36. else code = build.divider("Photos")+photosData+build.divider("Albums")+albumsData;
  37. if (search.code!==md5(code)) {
  38. $(".no_content").remove();
  39. lychee.animate(".album:nth-child(-n+50), .photo:nth-child(-n+50)", "contentZoomOut");
  40. lychee.animate(".divider", "fadeOut");
  41. search.code = md5(code);
  42. setTimeout(function() {
  43. if (code==="error") $("body").append(build.no_content("search"));
  44. else {
  45. lychee.content.html(code);
  46. lychee.animate(".album:nth-child(-n+50), .photo:nth-child(-n+50)", "contentZoomIn");
  47. $("img[data-type!='svg']").retina();
  48. }
  49. }, 300);
  50. }
  51. });
  52. } else search.reset();
  53. }, 250));
  54. },
  55. reset: function() {
  56. $("#search").val("");
  57. $(".no_content").remove();
  58. if (search.code!=="") {
  59. // Trash data
  60. albums.json = null;
  61. album.json = null;
  62. photo.json = null;
  63. search.code = "";
  64. lychee.animate(".divider", "fadeOut");
  65. albums.load();
  66. }
  67. }
  68. };