2020-12-09 13:33:29 +00:00
|
|
|
<template>
|
2022-07-21 20:51:41 +00:00
|
|
|
<h1 class="text-center my-2" v-text="$route.query.search_query" />
|
2020-12-09 13:33:29 +00:00
|
|
|
|
2021-12-27 22:33:55 +00:00
|
|
|
<label for="ddlSearchFilters">
|
|
|
|
<strong v-text="`${$t('actions.filter')}:`" />
|
|
|
|
</label>
|
2022-07-19 16:16:50 +00:00
|
|
|
<select id="ddlSearchFilters" v-model="selectedFilter" default="all" class="select w-auto" @change="updateFilter()">
|
2022-02-12 17:37:41 +00:00
|
|
|
<option v-for="filter in availableFilters" :key="filter" :value="filter" v-t="`search.${filter}`" />
|
2021-06-14 19:45:19 +00:00
|
|
|
</select>
|
|
|
|
|
|
|
|
<hr />
|
|
|
|
|
2022-01-14 02:54:27 +00:00
|
|
|
<div v-if="results && results.corrected">
|
|
|
|
<i18n-t keypath="search.did_you_mean" tag="div" class="text-lg">
|
|
|
|
<router-link :to="{ name: 'SearchResults', query: { search_query: results.suggestion } }">
|
|
|
|
<em v-text="results.suggestion" />
|
|
|
|
</router-link>
|
|
|
|
</i18n-t>
|
2021-10-10 01:20:00 +00:00
|
|
|
</div>
|
|
|
|
|
2023-03-13 13:39:07 +00:00
|
|
|
<LoadingIndicatorPage :show-content="results != null && results.items?.length" class="video-grid">
|
2022-01-30 23:48:27 +00:00
|
|
|
<template v-for="result in results.items" :key="result.url">
|
2022-11-01 12:12:54 +00:00
|
|
|
<ContentItem :item="result" height="94" width="168" />
|
2022-01-30 23:48:27 +00:00
|
|
|
</template>
|
2023-03-13 13:39:07 +00:00
|
|
|
</LoadingIndicatorPage>
|
2020-12-09 13:33:29 +00:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2022-11-01 10:32:53 +00:00
|
|
|
import ContentItem from "./ContentItem.vue";
|
2023-03-13 13:39:07 +00:00
|
|
|
import LoadingIndicatorPage from "./LoadingIndicatorPage.vue";
|
2021-09-03 19:27:22 +00:00
|
|
|
|
2020-12-09 13:33:29 +00:00
|
|
|
export default {
|
2021-10-08 18:52:51 +00:00
|
|
|
components: {
|
2022-11-01 10:32:53 +00:00
|
|
|
ContentItem,
|
2023-03-13 13:39:07 +00:00
|
|
|
LoadingIndicatorPage,
|
2021-10-08 18:52:51 +00:00
|
|
|
},
|
2020-12-09 13:33:29 +00:00
|
|
|
data() {
|
|
|
|
return {
|
2021-04-07 11:45:40 +00:00
|
|
|
results: null,
|
2021-06-14 19:45:19 +00:00
|
|
|
availableFilters: [
|
|
|
|
"all",
|
|
|
|
"videos",
|
|
|
|
"channels",
|
|
|
|
"playlists",
|
|
|
|
"music_songs",
|
|
|
|
"music_videos",
|
|
|
|
"music_albums",
|
|
|
|
"music_playlists",
|
|
|
|
],
|
2022-07-19 16:16:50 +00:00
|
|
|
selectedFilter: this.$route.query.filter ?? "all",
|
2020-12-09 13:33:29 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
mounted() {
|
2022-02-25 11:40:24 +00:00
|
|
|
if (this.handleRedirect()) return;
|
2020-12-09 13:33:29 +00:00
|
|
|
this.updateResults();
|
2022-09-11 18:01:58 +00:00
|
|
|
this.saveQueryToHistory();
|
2021-07-07 14:18:09 +00:00
|
|
|
},
|
|
|
|
activated() {
|
2022-02-25 11:40:24 +00:00
|
|
|
this.handleRedirect();
|
2020-12-14 07:15:09 +00:00
|
|
|
window.addEventListener("scroll", this.handleScroll);
|
|
|
|
},
|
2021-07-07 14:18:09 +00:00
|
|
|
deactivated() {
|
2020-12-14 07:15:09 +00:00
|
|
|
window.removeEventListener("scroll", this.handleScroll);
|
2020-12-09 13:33:29 +00:00
|
|
|
},
|
2021-09-05 20:53:59 +00:00
|
|
|
unmounted() {
|
|
|
|
window.removeEventListener("scroll", this.handleScroll);
|
|
|
|
},
|
2020-12-09 13:33:29 +00:00
|
|
|
methods: {
|
|
|
|
async fetchResults() {
|
2021-07-04 18:26:02 +00:00
|
|
|
return await await this.fetchJson(this.apiUrl() + "/search", {
|
2021-06-15 11:37:35 +00:00
|
|
|
q: this.$route.query.search_query,
|
2022-07-19 16:16:50 +00:00
|
|
|
filter: this.$route.query.filter ?? "all",
|
2021-06-15 11:37:35 +00:00
|
|
|
});
|
2020-12-09 13:33:29 +00:00
|
|
|
},
|
|
|
|
async updateResults() {
|
2020-12-14 07:15:09 +00:00
|
|
|
document.title = this.$route.query.search_query + " - Piped";
|
2023-02-19 13:41:13 +00:00
|
|
|
this.results = this.fetchResults().then(json => {
|
|
|
|
this.results = json;
|
|
|
|
this.updateWatched(this.results.items);
|
|
|
|
});
|
2020-12-14 07:15:09 +00:00
|
|
|
},
|
2022-07-19 16:16:50 +00:00
|
|
|
updateFilter() {
|
|
|
|
this.$router.replace({
|
|
|
|
query: {
|
|
|
|
search_query: this.$route.query.search_query,
|
|
|
|
filter: this.selectedFilter,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
2020-12-14 07:15:09 +00:00
|
|
|
handleScroll() {
|
|
|
|
if (this.loading || !this.results || !this.results.nextpage) return;
|
2021-04-07 11:45:40 +00:00
|
|
|
if (window.innerHeight + window.scrollY >= document.body.offsetHeight - window.innerHeight) {
|
2020-12-14 07:15:09 +00:00
|
|
|
this.loading = true;
|
2021-07-04 18:26:02 +00:00
|
|
|
this.fetchJson(this.apiUrl() + "/nextpage/search", {
|
2021-06-15 11:37:35 +00:00
|
|
|
nextpage: this.results.nextpage,
|
|
|
|
q: this.$route.query.search_query,
|
2022-07-19 16:16:50 +00:00
|
|
|
filter: this.$route.query.filter ?? "all",
|
2021-06-15 11:37:35 +00:00
|
|
|
}).then(json => {
|
2021-02-24 09:35:41 +00:00
|
|
|
this.results.nextpage = json.nextpage;
|
|
|
|
this.results.id = json.id;
|
|
|
|
this.loading = false;
|
|
|
|
json.items.map(stream => this.results.items.push(stream));
|
|
|
|
});
|
2020-12-14 07:15:09 +00:00
|
|
|
}
|
2021-04-07 11:45:40 +00:00
|
|
|
},
|
2022-02-25 11:40:24 +00:00
|
|
|
handleRedirect() {
|
|
|
|
const query = this.$route.query.search_query;
|
|
|
|
const url =
|
2022-06-06 02:29:47 +00:00
|
|
|
/(?:http(?:s)?:\/\/)?(?:www\.)?youtube\.com(\/[/a-zA-Z0-9_?=&-]*)/gm.exec(query)?.[1] ??
|
|
|
|
/(?:http(?:s)?:\/\/)?(?:www\.)?youtu\.be\/(?:watch\?v=)?([/a-zA-Z0-9_?=&-]*)/gm
|
2022-02-25 11:40:24 +00:00
|
|
|
.exec(query)?.[1]
|
|
|
|
.replace(/^/, "/watch?v=");
|
|
|
|
if (url) {
|
|
|
|
this.$router.push(url);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
},
|
2022-09-11 18:01:58 +00:00
|
|
|
saveQueryToHistory() {
|
|
|
|
if (!this.getPreferenceBoolean("searchHistory", false)) return;
|
|
|
|
const query = this.$route.query.search_query;
|
|
|
|
if (!query) return;
|
|
|
|
const searchHistory = JSON.parse(localStorage.getItem("search_history")) ?? [];
|
2022-10-02 13:23:16 +00:00
|
|
|
if (searchHistory.includes(query)) {
|
|
|
|
const index = searchHistory.indexOf(query);
|
|
|
|
searchHistory.splice(index, 1);
|
|
|
|
}
|
|
|
|
searchHistory.unshift(query);
|
2022-09-11 18:02:34 +00:00
|
|
|
if (searchHistory.length > 10) searchHistory.shift();
|
2022-09-11 18:01:58 +00:00
|
|
|
localStorage.setItem("search_history", JSON.stringify(searchHistory));
|
|
|
|
},
|
2021-09-03 19:27:22 +00:00
|
|
|
},
|
2020-12-09 13:33:29 +00:00
|
|
|
};
|
|
|
|
</script>
|