search.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * @description Searches through your photos and albums.
  3. * @copyright 2015 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, .photo', 'contentZoomOut');
  45. lychee.animate('.divider', 'fadeOut');
  46. search.code = md5(code);
  47. setTimeout(function() {
  48. if (code==='error') {
  49. lychee.content.html('');
  50. $('body').append(build.no_content('magnifying-glass'));
  51. } else {
  52. lychee.content.html(code);
  53. lychee.animate('.album, .photo', 'contentZoomIn');
  54. $('img[data-type!="svg"]').retina();
  55. }
  56. }, 300);
  57. }
  58. });
  59. } else search.reset();
  60. }, 250));
  61. }
  62. search.reset = function() {
  63. $('#search').val('');
  64. $('.no_content').remove();
  65. if (search.code!=='') {
  66. // Trash data
  67. albums.json = null;
  68. album.json = null;
  69. photo.json = null;
  70. search.code = '';
  71. lychee.animate('.divider', 'fadeOut');
  72. albums.load();
  73. }
  74. }