search.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * @description Searches through your photos and albums.
  3. * @copyright 2015 by Tobias Reich
  4. */
  5. search = {
  6. hash: null
  7. }
  8. search.find = function(term) {
  9. var albumsData = '',
  10. photosData = '',
  11. html = '';
  12. clearTimeout($(window).data('timeout'));
  13. $(window).data('timeout', setTimeout(function() {
  14. if ($('#search').val().length!==0) {
  15. api.post('search', { term }, function(data) {
  16. // Build albums
  17. if (data&&data.albums) {
  18. albums.json = { albums: data.albums };
  19. $.each(albums.json.albums, function() {
  20. albums.parse(this);
  21. albumsData += build.album(this);
  22. });
  23. }
  24. // Build photos
  25. if (data&&data.photos) {
  26. album.json = { content: data.photos };
  27. $.each(album.json.content, function() {
  28. photosData += build.photo(this);
  29. });
  30. }
  31. // 1. No albums and photos found
  32. // 2. Only photos found
  33. // 3. Only albums found
  34. // 4. Albums and photos found
  35. if (albumsData===''&&photosData==='') html = 'error';
  36. else if (albumsData==='') html = build.divider('Photos') + photosData;
  37. else if (photosData==='') html = build.divider('Albums') + albumsData;
  38. else html = build.divider('Photos') + photosData + build.divider('Albums') + albumsData;
  39. // Only refresh view when search results are different
  40. if (search.hash!==data.hash) {
  41. $('.no_content').remove();
  42. lychee.animate('.album, .photo', 'contentZoomOut');
  43. lychee.animate('.divider', 'fadeOut');
  44. search.hash = data.hash;
  45. setTimeout(function() {
  46. if (html==='error') {
  47. lychee.content.html('');
  48. $('body').append(build.no_content('magnifying-glass'));
  49. } else {
  50. lychee.content.html(html);
  51. lychee.animate('.album, .photo', '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.hash!==null) {
  64. // Trash data
  65. albums.json = null;
  66. album.json = null;
  67. photo.json = null;
  68. search.hash = null;
  69. lychee.animate('.divider', 'fadeOut');
  70. albums.load();
  71. }
  72. }