search.js 2.2 KB

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