2020-02-16 18:30:00 +00:00
|
|
|
import Vue from 'vue'
|
|
|
|
import FtInput from '../ft-input/ft-input.vue'
|
|
|
|
import FtSearchFilters from '../ft-search-filters/ft-search-filters.vue'
|
2020-03-24 13:22:29 +00:00
|
|
|
import $ from 'jquery'
|
2020-02-16 18:30:00 +00:00
|
|
|
import router from '../../router/index.js'
|
2020-06-02 02:42:29 +00:00
|
|
|
import debounce from 'lodash.debounce'
|
|
|
|
import ytSuggest from 'youtube-suggest'
|
2020-02-16 18:30:00 +00:00
|
|
|
|
|
|
|
export default Vue.extend({
|
|
|
|
name: 'TopNav',
|
|
|
|
components: {
|
|
|
|
FtInput,
|
|
|
|
FtSearchFilters
|
|
|
|
},
|
|
|
|
data: () => {
|
|
|
|
return {
|
|
|
|
component: this,
|
2020-04-14 02:59:25 +00:00
|
|
|
windowWidth: 0,
|
2020-06-02 02:42:29 +00:00
|
|
|
showFilters: false,
|
|
|
|
searchValue: '',
|
|
|
|
searchSuggestionsDataList: []
|
2020-02-16 18:30:00 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
searchSettings: function () {
|
|
|
|
return this.$store.getters.getSearchSettings
|
|
|
|
},
|
|
|
|
|
|
|
|
isSideNavOpen: function () {
|
|
|
|
return this.$store.getters.getIsSideNavOpen
|
2020-02-27 03:10:56 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
barColor: function () {
|
|
|
|
return this.$store.getters.getBarColor
|
2020-06-02 02:42:29 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
invidiousInstance: function () {
|
|
|
|
return this.$store.getters.getInvidiousInstance
|
|
|
|
},
|
|
|
|
|
|
|
|
backendFallback: function () {
|
|
|
|
return this.$store.getters.getBackendFallback
|
|
|
|
},
|
|
|
|
|
|
|
|
backendPreference: function () {
|
|
|
|
return this.$store.getters.getBackendPreference
|
|
|
|
},
|
2020-02-16 18:30:00 +00:00
|
|
|
},
|
2020-03-24 13:22:29 +00:00
|
|
|
mounted: function () {
|
2020-04-14 02:59:25 +00:00
|
|
|
const appWidth = $(window).width()
|
|
|
|
|
|
|
|
if (appWidth <= 680) {
|
|
|
|
const searchContainer = $('.searchContainer').get(0)
|
|
|
|
searchContainer.style.display = 'none'
|
|
|
|
}
|
|
|
|
|
2020-03-24 13:22:29 +00:00
|
|
|
window.addEventListener('resize', function(event) {
|
|
|
|
const width = event.srcElement.innerWidth
|
|
|
|
const searchContainer = $('.searchContainer').get(0)
|
|
|
|
|
|
|
|
if (width > 680) {
|
2020-03-24 14:34:55 +00:00
|
|
|
searchContainer.style.display = ''
|
2020-03-24 13:22:29 +00:00
|
|
|
} else {
|
|
|
|
searchContainer.style.display = 'none'
|
|
|
|
}
|
|
|
|
})
|
2020-06-02 02:42:29 +00:00
|
|
|
|
|
|
|
this.debounceSearchResults = debounce(this.getSearchSuggestions, 500)
|
2020-03-24 13:22:29 +00:00
|
|
|
},
|
2020-02-16 18:30:00 +00:00
|
|
|
methods: {
|
|
|
|
goToSearch: function (query) {
|
2020-03-24 13:22:29 +00:00
|
|
|
const appWidth = $(window).width()
|
|
|
|
|
|
|
|
if (appWidth <= 680) {
|
|
|
|
const searchContainer = $('.searchContainer').get(0)
|
2020-04-14 02:59:25 +00:00
|
|
|
searchContainer.blur()
|
2020-03-24 13:22:29 +00:00
|
|
|
searchContainer.style.display = 'none'
|
|
|
|
}
|
|
|
|
|
2020-06-01 03:13:03 +00:00
|
|
|
this.$store.dispatch('getVideoIdFromUrl', query).then((result) => {
|
|
|
|
if (result) {
|
|
|
|
this.$router.push(
|
|
|
|
{
|
|
|
|
path: `/watch/${result}`,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
router.push(
|
|
|
|
{
|
|
|
|
path: `/search/${query}`,
|
|
|
|
query: {
|
|
|
|
sortBy: this.searchSettings.sortBy,
|
|
|
|
time: this.searchSettings.time,
|
|
|
|
type: this.searchSettings.type,
|
|
|
|
duration: this.searchSettings.duration
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2020-02-16 18:30:00 +00:00
|
|
|
}
|
2020-06-01 03:13:03 +00:00
|
|
|
})
|
2020-04-14 02:59:25 +00:00
|
|
|
|
|
|
|
this.showFilters = false
|
2020-02-16 18:30:00 +00:00
|
|
|
},
|
|
|
|
|
2020-06-02 02:42:29 +00:00
|
|
|
getSearchSuggestionsDebounce: function (query) {
|
|
|
|
this.debounceSearchResults(query)
|
|
|
|
},
|
|
|
|
|
|
|
|
getSearchSuggestions: function (query) {
|
|
|
|
switch (this.backendPreference) {
|
|
|
|
case 'local':
|
|
|
|
this.getSearchSuggestionsLocal(query)
|
|
|
|
break
|
|
|
|
case 'invidious':
|
|
|
|
this.getSearchSuggestionsInvidious(query)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
getSearchSuggestionsLocal: function (query) {
|
|
|
|
if (query === '') {
|
|
|
|
this.searchSuggestionsDataList = []
|
|
|
|
this.searchValue = ''
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ytSuggest(query).then((results) => {
|
|
|
|
this.searchSuggestionsDataList = results
|
|
|
|
this.searchValue = query
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
getSearchSuggestionsInvidious: function (query) {
|
|
|
|
if (query === '') {
|
|
|
|
this.searchSuggestionsDataList = []
|
|
|
|
this.searchValue = ''
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const searchPayload = {
|
|
|
|
resource: 'search/suggestions',
|
|
|
|
id: '',
|
|
|
|
params: {
|
|
|
|
q: query
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.$store.dispatch('invidiousAPICall', searchPayload).then((results) => {
|
|
|
|
this.searchSuggestionsDataList = results.suggestions
|
|
|
|
this.searchValue = query
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2020-03-24 13:22:29 +00:00
|
|
|
toggleSearchContainer: function () {
|
|
|
|
const searchContainer = $('.searchContainer').get(0)
|
|
|
|
|
2020-03-24 14:34:55 +00:00
|
|
|
if (searchContainer.style.display === 'none') {
|
|
|
|
searchContainer.style.display = ''
|
2020-03-24 13:22:29 +00:00
|
|
|
} else {
|
|
|
|
searchContainer.style.display = 'none'
|
|
|
|
}
|
|
|
|
|
|
|
|
this.showFilters = false
|
|
|
|
},
|
|
|
|
|
2020-02-16 18:30:00 +00:00
|
|
|
historyBack: function () {
|
|
|
|
window.history.back()
|
|
|
|
},
|
|
|
|
|
|
|
|
historyForward: function () {
|
|
|
|
window.history.forward()
|
|
|
|
},
|
|
|
|
|
|
|
|
toggleSideNav: function () {
|
|
|
|
this.$store.commit('toggleSideNav')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|