mirror of
				https://github.com/TeamPiped/Piped.git
				synced 2024-08-14 23:57:27 +00:00 
			
		
		
		
	Make suggestions interactive
This commit is contained in:
		
							parent
							
								
									f190558610
								
							
						
					
					
						commit
						48aa6c826f
					
				
					 2 changed files with 75 additions and 23 deletions
				
			
		| 
						 | 
				
			
			@ -6,11 +6,7 @@
 | 
			
		|||
    >
 | 
			
		||||
        <div class="uk-navbar-left">
 | 
			
		||||
            <router-link class="uk-navbar-item uk-logo uk-text-bold" to="/"
 | 
			
		||||
                ><img
 | 
			
		||||
                    src="/img/icons/logo.svg"
 | 
			
		||||
                    height="32"
 | 
			
		||||
                    width="32"
 | 
			
		||||
                />iped</router-link
 | 
			
		||||
                ><img src="/img/icons/logo.svg" height="32" width="32" />iped</router-link
 | 
			
		||||
            >
 | 
			
		||||
        </div>
 | 
			
		||||
        <div class="uk-navbar-center uk-flex uk-visible@m">
 | 
			
		||||
| 
						 | 
				
			
			@ -19,7 +15,7 @@
 | 
			
		|||
                type="text"
 | 
			
		||||
                placeholder="Search"
 | 
			
		||||
                v-model="searchText"
 | 
			
		||||
                @keypress="onChange($event)"
 | 
			
		||||
                @keydown="onKeyDown"
 | 
			
		||||
                @focus="onInputFocus"
 | 
			
		||||
                @blur="onInputBlur"
 | 
			
		||||
            />
 | 
			
		||||
| 
						 | 
				
			
			@ -44,20 +40,20 @@
 | 
			
		|||
            type="text"
 | 
			
		||||
            placeholder="Search"
 | 
			
		||||
            v-model="searchText"
 | 
			
		||||
            @keypress="onChange($event)"
 | 
			
		||||
            @keydown="onKeyDown"
 | 
			
		||||
            @focus="onInputFocus"
 | 
			
		||||
            @blur="onInputBlur"
 | 
			
		||||
        />
 | 
			
		||||
    </div>
 | 
			
		||||
    <SearchSuggestions
 | 
			
		||||
        v-if="searchText && suggestionsVisible && searchSuggestions.length > 0"
 | 
			
		||||
        :searchSuggestions="searchSuggestions"
 | 
			
		||||
        v-show="searchText && suggestionsVisible"
 | 
			
		||||
        :searchText="searchText"
 | 
			
		||||
        @searchchange="onSearchTextChange"
 | 
			
		||||
    />
 | 
			
		||||
</template>
 | 
			
		||||
 | 
			
		||||
<script>
 | 
			
		||||
import SearchSuggestions from "@/components/SearchSuggestions";
 | 
			
		||||
import Constants from "@/Constants.js";
 | 
			
		||||
 | 
			
		||||
export default {
 | 
			
		||||
    components: {
 | 
			
		||||
| 
						 | 
				
			
			@ -66,31 +62,29 @@ export default {
 | 
			
		|||
    data() {
 | 
			
		||||
        return {
 | 
			
		||||
            searchText: "",
 | 
			
		||||
            searchSuggestions: [],
 | 
			
		||||
            suggestionsVisible: false
 | 
			
		||||
        };
 | 
			
		||||
    },
 | 
			
		||||
    methods: {
 | 
			
		||||
        async onChange(e) {
 | 
			
		||||
        onKeyDown(e) {
 | 
			
		||||
            if (e.key === "Enter") {
 | 
			
		||||
                this.$router.push({
 | 
			
		||||
                    name: "SearchResults",
 | 
			
		||||
                    query: { search_query: this.searchText }
 | 
			
		||||
                });
 | 
			
		||||
                return;
 | 
			
		||||
            } else if (e.key === "ArrowUp" || e.key === "ArrowDown") {
 | 
			
		||||
                e.preventDefault();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            this.searchSuggestions = await this.fetchJson(
 | 
			
		||||
                Constants.BASE_URL +
 | 
			
		||||
                    "/suggestions?query=" +
 | 
			
		||||
                    encodeURI(this.searchText + e.key)
 | 
			
		||||
            );
 | 
			
		||||
        },
 | 
			
		||||
        onInputFocus() {
 | 
			
		||||
            this.suggestionsVisible = true;
 | 
			
		||||
        },
 | 
			
		||||
        onInputBlur() {
 | 
			
		||||
            this.suggestionsVisible = false;
 | 
			
		||||
        },
 | 
			
		||||
        onSearchTextChange(searchText) {
 | 
			
		||||
            this.searchText = searchText;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,9 +1,12 @@
 | 
			
		|||
<template>
 | 
			
		||||
    <div
 | 
			
		||||
        class="uk-position-absolute uk-panel uk-box-shadow-large uk-padding-small suggestions-container"
 | 
			
		||||
    >
 | 
			
		||||
    <div class="uk-position-absolute uk-panel uk-box-shadow-large suggestions-container">
 | 
			
		||||
        <ul class="uk-list uk-margin-remove uk-text-secondary">
 | 
			
		||||
            <li v-for="(suggestion, i) in searchSuggestions" :key="i">
 | 
			
		||||
            <li
 | 
			
		||||
                v-for="(suggestion, i) in searchSuggestions"
 | 
			
		||||
                :key="i"
 | 
			
		||||
                :class="{ selected: selected === i }"
 | 
			
		||||
                class="uk-margin-remove suggestion"
 | 
			
		||||
            >
 | 
			
		||||
                {{ suggestion }}
 | 
			
		||||
            </li>
 | 
			
		||||
        </ul>
 | 
			
		||||
| 
						 | 
				
			
			@ -11,9 +14,57 @@
 | 
			
		|||
</template>
 | 
			
		||||
 | 
			
		||||
<script>
 | 
			
		||||
import Constants from "@/Constants.js";
 | 
			
		||||
 | 
			
		||||
export default {
 | 
			
		||||
    props: {
 | 
			
		||||
        searchSuggestions: Array
 | 
			
		||||
        searchText: String
 | 
			
		||||
    },
 | 
			
		||||
    data() {
 | 
			
		||||
        return {
 | 
			
		||||
            selected: 0,
 | 
			
		||||
            searchSuggestions: []
 | 
			
		||||
        };
 | 
			
		||||
    },
 | 
			
		||||
    methods: {
 | 
			
		||||
        onKeyUp(e) {
 | 
			
		||||
            if (e.key === "ArrowUp") {
 | 
			
		||||
                if (this.selected <= 0) {
 | 
			
		||||
                    this.selected = this.searchSuggestions.length - 1;
 | 
			
		||||
                } else {
 | 
			
		||||
                    this.selected -= 1;
 | 
			
		||||
                }
 | 
			
		||||
                e.preventDefault();
 | 
			
		||||
            } else if (e.key === "ArrowDown") {
 | 
			
		||||
                if (this.selected >= this.searchSuggestions.length - 1) {
 | 
			
		||||
                    this.selected = 0;
 | 
			
		||||
                } else {
 | 
			
		||||
                    this.selected += 1;
 | 
			
		||||
                }
 | 
			
		||||
                e.preventDefault();
 | 
			
		||||
            } else {
 | 
			
		||||
                this.refreshSuggestions();
 | 
			
		||||
            }
 | 
			
		||||
        },
 | 
			
		||||
        async refreshSuggestions() {
 | 
			
		||||
            this.searchSuggestions = await this.fetchJson(
 | 
			
		||||
                Constants.BASE_URL + "/suggestions?query=" + encodeURI(this.searchText)
 | 
			
		||||
            );
 | 
			
		||||
            this.searchSuggestions.unshift(this.searchText);
 | 
			
		||||
        }
 | 
			
		||||
    },
 | 
			
		||||
    watch: {
 | 
			
		||||
        selected(newValue, oldValue) {
 | 
			
		||||
            if (newValue !== oldValue && this.searchSuggestions[this.selected]) {
 | 
			
		||||
                this.$emit("searchchange", this.searchSuggestions[this.selected]);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    },
 | 
			
		||||
    mounted() {
 | 
			
		||||
        window.addEventListener("keyup", this.onKeyUp);
 | 
			
		||||
    },
 | 
			
		||||
    beforeUnmount() {
 | 
			
		||||
        window.removeEventListener("keyup", this.onKeyUp);
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
</script>
 | 
			
		||||
| 
						 | 
				
			
			@ -26,6 +77,13 @@ export default {
 | 
			
		|||
    max-width: 640px;
 | 
			
		||||
    width: 100%;
 | 
			
		||||
    box-sizing: border-box;
 | 
			
		||||
    padding: 5px 0;
 | 
			
		||||
}
 | 
			
		||||
.suggestion {
 | 
			
		||||
    padding: 4px 15px;
 | 
			
		||||
}
 | 
			
		||||
.suggestion.selected {
 | 
			
		||||
    background-color: #393d3d;
 | 
			
		||||
}
 | 
			
		||||
@media screen and (max-width: 959px) {
 | 
			
		||||
    .suggestions-container {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue