search.js 2.0 KB

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