deemixer/public/js/app/search.js

169 lines
4.0 KiB
JavaScript
Raw Normal View History

// Load more content when the search page is at the end
$('#content').on('scroll', function () {
if ($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) {
if (
main_selected == 'search_tab' &&
['track_search', 'album_search', 'artist_search', 'playlist_search'].indexOf(search_selected) != -1
) {
2020-04-19 17:22:21 +00:00
scrolledSearch(search_selected.split('_')[0])
2020-04-09 14:06:33 +00:00
}
}
2020-04-09 14:06:33 +00:00
})
2020-04-19 17:22:21 +00:00
function search(type) {
query = MainSearch.results.query
socket.emit('search', {
term: query,
type: type,
2020-04-19 18:43:58 +00:00
start: MainSearch.results[type + 'Tab'].next,
2020-04-19 17:22:21 +00:00
nb: 30
})
}
function scrolledSearch(type) {
query = MainSearch.results.query
2020-04-19 18:43:58 +00:00
if (MainSearch.results[type + 'Tab'].next < MainSearch.results[type + 'Tab'].total) {
socket.emit('search', {
2020-04-19 17:22:21 +00:00
term: query,
type: type,
2020-04-19 18:43:58 +00:00
start: MainSearch.results[type + 'Tab'].next,
2020-04-19 17:22:21 +00:00
nb: 30
})
2020-04-09 14:06:33 +00:00
}
}
2020-04-19 18:43:58 +00:00
function searchUpdate(result) {
2020-04-19 17:22:21 +00:00
let next = 0
if (result.next) next = parseInt(result.next.match(/index=(\d*)/)[1])
else next = result.total
2020-04-19 18:43:58 +00:00
if (MainSearch.results[result.type + 'Tab'].total != result.total)
MainSearch.results[result.type + 'Tab'].total = result.total
if (MainSearch.results[result.type + 'Tab'].next != next) {
MainSearch.results[result.type + 'Tab'].next = next
MainSearch.results[result.type + 'Tab'].data = MainSearch.results[result.type + 'Tab'].data.concat(result.data)
2020-04-09 14:06:33 +00:00
}
2020-04-19 18:43:58 +00:00
MainSearch.results[result.type + 'Tab'].loaded = true
2020-04-09 10:50:05 +00:00
}
2020-04-19 18:43:58 +00:00
socket.on('search', function (result) {
2020-04-19 18:43:58 +00:00
searchUpdate(result)
})
2020-04-09 10:50:05 +00:00
function clickElement(button) {
return document.getElementById(button).click()
2020-04-09 10:24:49 +00:00
}
function sendAddToQueue(url, bitrate = null) {
2020-04-19 18:43:58 +00:00
if (url.indexOf(';') != -1) {
urls = url.split(';')
urls.forEach(url => {
2020-04-18 12:59:58 +00:00
socket.emit('addToQueue', { url: url, bitrate: bitrate })
})
2020-04-19 18:43:58 +00:00
} else if (url != '') {
2020-04-18 12:59:58 +00:00
socket.emit('addToQueue', { url: url, bitrate: bitrate })
}
}
2020-04-19 18:43:58 +00:00
const MainSearch = new Vue({
2020-04-19 17:22:21 +00:00
el: '#search_tab',
data: {
names: {
TOP_RESULT: 'Top Result',
TRACK: 'Tracks',
ARTIST: 'Artists',
ALBUM: 'Albums',
PLAYLIST: 'Playlists'
},
results: {
query: '',
allTab: {
ORDER: [],
2020-04-19 17:22:21 +00:00
TOP_RESULT: [],
ALBUM: {},
ARTIST: {},
TRACK: {},
PLAYLIST: {}
2020-04-19 17:22:21 +00:00
},
trackTab: {
data: [],
next: 0,
total: 0,
loaded: false
},
albumTab: {
data: [],
next: 0,
total: 0,
loaded: false
},
artistTab: {
data: [],
next: 0,
total: 0,
loaded: false
},
playlistTab: {
data: [],
next: 0,
total: 0,
loaded: false
}
}
},
methods: {
changeSearchTab(section) {
if (section != 'TOP_RESULT') clickElement('search_' + section.toLowerCase() + '_tab')
2020-04-10 16:00:42 +00:00
},
2020-04-19 18:43:58 +00:00
addToQueue: function (e) {
e.stopPropagation()
sendAddToQueue(e.currentTarget.dataset.link)
},
openQualityModal: function (e) {
e.preventDefault()
openQualityModal(e.currentTarget.dataset.link)
}
}
2020-04-08 16:43:35 +00:00
})
2020-04-19 18:43:58 +00:00
console.log(MainSearch)
2020-04-07 22:19:27 +00:00
// Search section
2020-04-19 18:43:58 +00:00
$('#searchbar').keyup(function (e) {
if (e.keyCode == 13) {
let term = this.value
if (isValidURL(term)) {
if (e.ctrlKey) {
openQualityModal(term)
} else {
sendAddToQueue(term)
}
} else {
if (term != MainSearch.query || main_selected == 'search_tab') {
document.getElementById('search_tab_content').style.display = 'none'
socket.emit('mainSearch', { term: term })
} else {
document.getElementById('search_all_tab').click()
document.getElementById('search_tab_content').style.display = 'block'
document.getElementById('main_search_tablink').click()
}
2020-04-09 10:24:49 +00:00
}
}
2020-04-07 22:19:27 +00:00
})
function mainSearchHandler(result) {
2020-04-19 18:43:58 +00:00
let resetObj = { data: [], next: 0, total: 0, loaded: false }
2020-04-19 17:22:21 +00:00
MainSearch.results.allTab = result
MainSearch.results.query = result.QUERY
2020-04-19 18:43:58 +00:00
MainSearch.results.trackTab = { ...resetObj }
MainSearch.results.albumTab = { ...resetObj }
MainSearch.results.artistTab = { ...resetObj }
MainSearch.results.playlistTab = { ...resetObj }
document.getElementById('search_all_tab').click()
document.getElementById('search_tab_content').style.display = 'block'
document.getElementById('main_search_tablink').click()
2020-04-07 22:19:27 +00:00
}
socket.on('mainSearch', function (result) {
mainSearchHandler(result)
})