search.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. clearTimeout($(window).data('timeout'))
  10. $(window).data('timeout', setTimeout(function() {
  11. if (header.dom('.header__search').val().length!==0) {
  12. api.post('search', { term }, function(data) {
  13. let html = ''
  14. let albumsData = ''
  15. let photosData = ''
  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
  32. // 2. Only photos
  33. // 3. Only albums
  34. // 4. Albums and photos
  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('.content', 'contentZoomOut')
  43. search.hash = data.hash
  44. setTimeout(() => {
  45. if (html==='error') {
  46. lychee.content.html('')
  47. $('body').append(build.no_content('magnifying-glass'))
  48. } else {
  49. lychee.content.html(html)
  50. lychee.animate(lychee.content, 'contentZoomIn')
  51. }
  52. }, 300)
  53. }
  54. })
  55. } else search.reset()
  56. }, 250))
  57. }
  58. search.reset = function() {
  59. header.dom('.header__search').val('')
  60. $('.no_content').remove()
  61. if (search.hash!=null) {
  62. // Trash data
  63. albums.json = null
  64. album.json = null
  65. photo.json = null
  66. search.hash = null
  67. lychee.animate('.divider', 'fadeOut')
  68. albums.load()
  69. }
  70. }