Updated search.js (ES2015)

This commit is contained in:
Tobias Reich 2015-07-11 16:27:26 +02:00
parent 9e976b1856
commit 35f37df707
2 changed files with 44 additions and 43 deletions

BIN
dist/main.js vendored Normal file → Executable file

Binary file not shown.

View File

@ -1,6 +1,6 @@
/** /**
* @description Searches through your photos and albums. * @description Searches through your photos and albums.
* @copyright 2015 by Tobias Reich * @copyright 2015 by Tobias Reich
*/ */
search = { search = {
@ -11,89 +11,90 @@ search = {
search.find = function(term) { search.find = function(term) {
var albumsData = '', clearTimeout($(window).data('timeout'))
photosData = '',
html = '';
clearTimeout($(window).data('timeout'));
$(window).data('timeout', setTimeout(function() { $(window).data('timeout', setTimeout(function() {
if ($('#search').val().length!==0) { if ($('#search').val().length!==0) {
api.post('search', { term }, function(data) { api.post('search', { term }, function(data) {
let html = '',
albumsData = '',
photosData = ''
// Build albums // Build albums
if (data&&data.albums) { if (data && data.albums) {
albums.json = { albums: data.albums }; albums.json = { albums: data.albums }
$.each(albums.json.albums, function() { $.each(albums.json.albums, function() {
albums.parse(this); albums.parse(this)
albumsData += build.album(this); albumsData += build.album(this)
}); })
} }
// Build photos // Build photos
if (data&&data.photos) { if (data && data.photos) {
album.json = { content: data.photos }; album.json = { content: data.photos }
$.each(album.json.content, function() { $.each(album.json.content, function() {
photosData += build.photo(this); photosData += build.photo(this)
}); })
} }
// 1. No albums and photos found // 1. No albums and photos
// 2. Only photos found // 2. Only photos
// 3. Only albums found // 3. Only albums
// 4. Albums and photos found // 4. Albums and photos
if (albumsData===''&&photosData==='') html = 'error'; if (albumsData==='' && photosData==='') html = 'error'
else if (albumsData==='') html = build.divider('Photos') + photosData; else if (albumsData==='') html = build.divider('Photos') + photosData
else if (photosData==='') html = build.divider('Albums') + albumsData; else if (photosData==='') html = build.divider('Albums') + albumsData
else html = build.divider('Photos') + photosData + build.divider('Albums') + albumsData; else html = build.divider('Photos') + photosData + build.divider('Albums') + albumsData
// Only refresh view when search results are different // Only refresh view when search results are different
if (search.hash!==data.hash) { if (search.hash!==data.hash) {
$('.no_content').remove(); $('.no_content').remove()
lychee.animate('#content', 'contentZoomOut'); lychee.animate('#content', 'contentZoomOut')
search.hash = data.hash; search.hash = data.hash
setTimeout(function() { setTimeout(() => {
if (html==='error') { if (html==='error') {
lychee.content.html(''); lychee.content.html('')
$('body').append(build.no_content('magnifying-glass')); $('body').append(build.no_content('magnifying-glass'))
} else { } else {
lychee.content.html(html); lychee.content.html(html)
lychee.animate('#content', 'contentZoomIn'); lychee.animate('#content', 'contentZoomIn')
} }
}, 300); }, 300)
} }
}); })
} else search.reset(); } else search.reset()
}, 250)); }, 250))
} }
search.reset = function() { search.reset = function() {
$('#search').val(''); $('#search').val('')
$('.no_content').remove(); $('.no_content').remove()
if (search.hash!==null) { if (search.hash!==null) {
// Trash data // Trash data
albums.json = null; albums.json = null
album.json = null; album.json = null
photo.json = null; photo.json = null
search.hash = null; search.hash = null
lychee.animate('.divider', 'fadeOut'); lychee.animate('.divider', 'fadeOut')
albums.load(); albums.load()
} }