search.js 2.2 KB

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