2020-12-09 19:03:29 +05:30
|
|
|
<template>
|
2022-07-22 02:21:41 +05:30
|
|
|
<h1 class="text-center my-2" v-text="$route.query.search_query" />
|
2020-12-09 19:03:29 +05:30
|
|
|
|
2021-12-28 04:03:55 +05:30
|
|
|
<label for="ddlSearchFilters">
|
|
|
|
<strong v-text="`${$t('actions.filter')}:`" />
|
|
|
|
</label>
|
2022-07-19 21:46:50 +05:30
|
|
|
<select id="ddlSearchFilters" v-model="selectedFilter" default="all" class="select w-auto" @change="updateFilter()">
|
2022-02-12 23:07:41 +05:30
|
|
|
<option v-for="filter in availableFilters" :key="filter" :value="filter" v-t="`search.${filter}`" />
|
2021-06-15 01:15:19 +05:30
|
|
|
</select>
|
|
|
|
|
|
|
|
<hr />
|
|
|
|
|
2022-01-14 08:24:27 +05:30
|
|
|
<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 06:50:00 +05:30
|
|
|
</div>
|
|
|
|
|
2021-12-27 20:16:22 +05:30
|
|
|
<div v-if="results" class="video-grid">
|
2022-01-31 05:18:27 +05:30
|
|
|
<template v-for="result in results.items" :key="result.url">
|
2022-11-01 16:02:53 +05:30
|
|
|
<ContentItem :content-item="result" />
|
2022-01-31 05:18:27 +05:30
|
|
|
</template>
|
2020-12-09 19:03:29 +05:30
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2022-11-01 16:02:53 +05:30
|
|
|
import ContentItem from "./ContentItem.vue";
|
2021-09-04 00:57:22 +05:30
|
|
|
|
2020-12-09 19:03:29 +05:30
|
|
|
export default {
|
2021-10-09 00:22:51 +05:30
|
|
|
components: {
|
2022-11-01 16:02:53 +05:30
|
|
|
ContentItem,
|
2021-10-09 00:22:51 +05:30
|
|
|
},
|
2020-12-09 19:03:29 +05:30
|
|
|
data() {
|
|
|
|
return {
|
2021-04-07 17:15:40 +05:30
|
|
|
results: null,
|
2021-06-15 01:15:19 +05:30
|
|
|
availableFilters: [
|
|
|
|
"all",
|
|
|
|
"videos",
|
|
|
|
"channels",
|
|
|
|
"playlists",
|
|
|
|
"music_songs",
|
|
|
|
"music_videos",
|
|
|
|
"music_albums",
|
|
|
|
"music_playlists",
|
|
|
|
],
|
2022-07-19 21:46:50 +05:30
|
|
|
selectedFilter: this.$route.query.filter ?? "all",
|
2020-12-09 19:03:29 +05:30
|
|
|
};
|
|
|
|
},
|
|
|
|
mounted() {
|
2022-02-25 17:10:24 +05:30
|
|
|
if (this.handleRedirect()) return;
|
2020-12-09 19:03:29 +05:30
|
|
|
this.updateResults();
|
2022-09-11 23:31:58 +05:30
|
|
|
this.saveQueryToHistory();
|
2021-07-07 19:48:09 +05:30
|
|
|
},
|
|
|
|
activated() {
|
2022-02-25 17:10:24 +05:30
|
|
|
this.handleRedirect();
|
2020-12-14 12:45:09 +05:30
|
|
|
window.addEventListener("scroll", this.handleScroll);
|
|
|
|
},
|
2021-07-07 19:48:09 +05:30
|
|
|
deactivated() {
|
2020-12-14 12:45:09 +05:30
|
|
|
window.removeEventListener("scroll", this.handleScroll);
|
2020-12-09 19:03:29 +05:30
|
|
|
},
|
2021-09-06 02:23:59 +05:30
|
|
|
unmounted() {
|
|
|
|
window.removeEventListener("scroll", this.handleScroll);
|
|
|
|
},
|
2020-12-09 19:03:29 +05:30
|
|
|
methods: {
|
|
|
|
async fetchResults() {
|
2021-07-04 23:56:02 +05:30
|
|
|
return await await this.fetchJson(this.apiUrl() + "/search", {
|
2021-06-15 17:07:35 +05:30
|
|
|
q: this.$route.query.search_query,
|
2022-07-19 21:46:50 +05:30
|
|
|
filter: this.$route.query.filter ?? "all",
|
2021-06-15 17:07:35 +05:30
|
|
|
});
|
2020-12-09 19:03:29 +05:30
|
|
|
},
|
|
|
|
async updateResults() {
|
2020-12-14 12:45:09 +05:30
|
|
|
document.title = this.$route.query.search_query + " - Piped";
|
2021-04-07 17:15:40 +05:30
|
|
|
this.results = this.fetchResults().then(json => (this.results = json));
|
2020-12-14 12:45:09 +05:30
|
|
|
},
|
2022-07-19 21:46:50 +05:30
|
|
|
updateFilter() {
|
|
|
|
this.$router.replace({
|
|
|
|
query: {
|
|
|
|
search_query: this.$route.query.search_query,
|
|
|
|
filter: this.selectedFilter,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
2020-12-14 12:45:09 +05:30
|
|
|
handleScroll() {
|
|
|
|
if (this.loading || !this.results || !this.results.nextpage) return;
|
2021-04-07 17:15:40 +05:30
|
|
|
if (window.innerHeight + window.scrollY >= document.body.offsetHeight - window.innerHeight) {
|
2020-12-14 12:45:09 +05:30
|
|
|
this.loading = true;
|
2021-07-04 23:56:02 +05:30
|
|
|
this.fetchJson(this.apiUrl() + "/nextpage/search", {
|
2021-06-15 17:07:35 +05:30
|
|
|
nextpage: this.results.nextpage,
|
|
|
|
q: this.$route.query.search_query,
|
2022-07-19 21:46:50 +05:30
|
|
|
filter: this.$route.query.filter ?? "all",
|
2021-06-15 17:07:35 +05:30
|
|
|
}).then(json => {
|
2021-02-24 15:05:41 +05:30
|
|
|
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 12:45:09 +05:30
|
|
|
}
|
2021-04-07 17:15:40 +05:30
|
|
|
},
|
2022-02-25 17:10:24 +05:30
|
|
|
handleRedirect() {
|
|
|
|
const query = this.$route.query.search_query;
|
|
|
|
const url =
|
2022-06-06 07:59:47 +05:30
|
|
|
/(?: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 17:10:24 +05:30
|
|
|
.exec(query)?.[1]
|
|
|
|
.replace(/^/, "/watch?v=");
|
|
|
|
if (url) {
|
|
|
|
this.$router.push(url);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
},
|
2022-09-11 23:31:58 +05:30
|
|
|
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 18:53:16 +05:30
|
|
|
if (searchHistory.includes(query)) {
|
|
|
|
const index = searchHistory.indexOf(query);
|
|
|
|
searchHistory.splice(index, 1);
|
|
|
|
}
|
|
|
|
searchHistory.unshift(query);
|
2022-09-11 23:32:34 +05:30
|
|
|
if (searchHistory.length > 10) searchHistory.shift();
|
2022-09-11 23:31:58 +05:30
|
|
|
localStorage.setItem("search_history", JSON.stringify(searchHistory));
|
|
|
|
},
|
2021-09-04 00:57:22 +05:30
|
|
|
},
|
2020-12-09 19:03:29 +05:30
|
|
|
};
|
|
|
|
</script>
|