|
5 | 5 | Pterodactyl |
6 | 6 | </router-link> |
7 | 7 | </div> |
8 | | - <div class="search-box flex-none" v-if="$route.name !== 'dashboard'"> |
| 8 | + <div class="search-box flex-none" v-if="$route.name !== 'dashboard'" ref="searchContainer"> |
9 | 9 | <input type="text" class="search-input" id="searchInput" placeholder="Search..." |
10 | | - :class="{ 'has-search-results': (servers.length > 0 || loadingResults) && searchActive }" |
| 10 | + :class="{ 'has-search-results': ((servers.length > 0 && searchTerm.length >= 3) || loadingResults) && searchActive }" |
11 | 11 | v-on:focus="searchActive = true" |
12 | | - v-on:blur="searchActive = false" |
13 | 12 | v-on:input="search" |
14 | 13 | v-model="searchTerm" |
15 | 14 | /> |
16 | | - <div class="search-results select-none" :class="{ 'hidden': (servers.length === 0 && !loadingResults) || !searchActive }"> |
| 15 | + <div class="search-results select-none" :class="{ 'hidden': (servers.length === 0 && !loadingResults) || !searchActive || searchTerm.length < 3 }"> |
17 | 16 | <div v-if="loadingResults"> |
18 | 17 | <a href="#"> |
19 | 18 | <div class="flex items-center"> |
|
26 | 25 | </div> |
27 | 26 | </a> |
28 | 27 | </div> |
29 | | - <div v-else> |
30 | | - <router-link |
31 | | - v-for="server in servers" |
32 | | - :key="server.identifier" |
33 | | - :to="{ name: 'server', params: { id: server.identifier } }" |
34 | | - > |
| 28 | + <div v-else v-for="server in servers" :key="server.identifier"> |
| 29 | + <router-link :to="{ name: 'server', params: { id: server.identifier }}" v-on:click.native="searchActive = false"> |
35 | 30 | <div class="flex items-center"> |
36 | 31 | <div class="flex-1"> |
37 | 32 | <span class="font-bold text-grey-darkest">{{ server.name }}</span><br /> |
|
100 | 95 | } |
101 | 96 | }, |
102 | 97 |
|
| 98 | + created: function () { |
| 99 | + document.addEventListener('click', this.documentClick); |
| 100 | + }, |
| 101 | +
|
| 102 | + beforeDestroy: function () { |
| 103 | + document.removeEventListener('click', this.documentClick); |
| 104 | + }, |
| 105 | +
|
103 | 106 | methods: { |
104 | 107 | search: debounce(function () { |
105 | | - if (this.searchTerm.length > 3) { |
| 108 | + if (this.searchTerm.length >= 3) { |
106 | 109 | this.loadingResults = true; |
107 | 110 | this.gatherSearchResults(this.searchTerm); |
108 | 111 | } |
109 | 112 | }, 500), |
110 | 113 |
|
111 | 114 | gatherSearchResults: function () { |
112 | 115 | this.$store.dispatch('dashboard/loadServers') |
113 | | - .then(() => { |
114 | | - if (this.servers.length === 0) { |
115 | | - } |
116 | | - }) |
117 | 116 | .catch(err => { |
118 | 117 | console.error(err); |
119 | 118 | const response = err.response; |
|
132 | 131 | this.$store.commit('auth/logout'); |
133 | 132 | return window.location = this.route('auth.logout'); |
134 | 133 | }, |
| 134 | +
|
| 135 | + documentClick: function (e) { |
| 136 | + if (this.$refs.searchContainer) { |
| 137 | + if (this.$refs.searchContainer !== e.target && !this.$refs.searchContainer.contains(e.target)) { |
| 138 | + this.searchActive = false; |
| 139 | + } |
| 140 | + } |
| 141 | + }, |
135 | 142 | } |
136 | 143 | }; |
137 | 144 | </script> |
0 commit comments