|
| 1 | +import Vue from 'vue'; |
| 2 | +import { debounce, isObject } from 'lodash'; |
| 3 | +import { mapState } from 'vuex'; |
| 4 | +import {AxiosError} from "axios"; |
| 5 | + |
| 6 | +export default Vue.component('navigation', { |
| 7 | + data: function () { |
| 8 | + return { |
| 9 | + loadingResults: false, |
| 10 | + searchActive: false, |
| 11 | + }; |
| 12 | + }, |
| 13 | + |
| 14 | + computed: { |
| 15 | + ...mapState('dashboard', ['servers']), |
| 16 | + searchTerm: { |
| 17 | + get: function (): string { |
| 18 | + return this.$store.getters['dashboard/getSearchTerm']; |
| 19 | + }, |
| 20 | + set: function (value: string): void { |
| 21 | + this.$store.dispatch('dashboard/setSearchTerm', value); |
| 22 | + } |
| 23 | + } |
| 24 | + }, |
| 25 | + |
| 26 | + created: function () { |
| 27 | + document.addEventListener('click', this.documentClick); |
| 28 | + }, |
| 29 | + |
| 30 | + beforeDestroy: function () { |
| 31 | + document.removeEventListener('click', this.documentClick); |
| 32 | + }, |
| 33 | + |
| 34 | + methods: { |
| 35 | + search: debounce(function (): void { |
| 36 | + // @todo why is this not liked? |
| 37 | + // if (this.searchTerm.length >= 3) { |
| 38 | + // this.loadingResults = true; |
| 39 | + // this.gatherSearchResults(); |
| 40 | + // } |
| 41 | + }, 500), |
| 42 | + |
| 43 | + gatherSearchResults: function (): void { |
| 44 | + this.$store.dispatch('dashboard/loadServers') |
| 45 | + .catch((err: AxiosError) => { |
| 46 | + console.error(err); |
| 47 | + |
| 48 | + const response = err.response; |
| 49 | + if (response && isObject(response.data.errors)) { |
| 50 | + response.data.errors.forEach((error: any) => { |
| 51 | + this.$flash.error(error.detail); |
| 52 | + }); |
| 53 | + } |
| 54 | + }) |
| 55 | + .then(() => { |
| 56 | + this.loadingResults = false; |
| 57 | + }); |
| 58 | + }, |
| 59 | + |
| 60 | + doLogout: function () { |
| 61 | + this.$store.commit('auth/logout'); |
| 62 | + window.location.assign(this.route('auth.logout')); |
| 63 | + }, |
| 64 | + |
| 65 | + documentClick: function (e: Event) { |
| 66 | + if (this.$refs.searchContainer) { |
| 67 | + if (this.$refs.searchContainer !== e.target && !(this.$refs.searchContainer as HTMLElement).contains(e.target as HTMLElement)) { |
| 68 | + this.searchActive = false; |
| 69 | + } |
| 70 | + } |
| 71 | + }, |
| 72 | + }, |
| 73 | + |
| 74 | + template: ` |
| 75 | + <div class="nav flex"> |
| 76 | + <div class="logo flex-1"> |
| 77 | + <router-link :to="{ name: 'dashboard' }"> |
| 78 | + Pterodactyl |
| 79 | + </router-link> |
| 80 | + </div> |
| 81 | + <div class="search-box flex-none" v-if="$route.name !== 'dashboard'" ref="searchContainer"> |
| 82 | + <input type="text" class="search-input" id="searchInput" placeholder="Search..." |
| 83 | + :class="{ 'has-search-results': ((servers.length > 0 && searchTerm.length >= 3) || loadingResults) && searchActive }" |
| 84 | + v-on:focus="searchActive = true" |
| 85 | + v-on:input="search" |
| 86 | + v-model="searchTerm" |
| 87 | + /> |
| 88 | + <div class="search-results select-none" :class="{ 'hidden': (servers.length === 0 && !loadingResults) || !searchActive || searchTerm.length < 3 }"> |
| 89 | + <div v-if="loadingResults"> |
| 90 | + <a href="#"> |
| 91 | + <div class="flex items-center"> |
| 92 | + <div class="flex-1"> |
| 93 | + <span class="text-sm text-grey-darker">Loading...</span> |
| 94 | + </div> |
| 95 | + <div class="flex-none"> |
| 96 | + <span class="spinner spinner-relative"></span> |
| 97 | + </div> |
| 98 | + </div> |
| 99 | + </a> |
| 100 | + </div> |
| 101 | + <div v-else v-for="server in servers" :key="server.identifier"> |
| 102 | + <router-link :to="{ name: 'server', params: { id: server.identifier }}" v-on:click.native="searchActive = false"> |
| 103 | + <div class="flex items-center"> |
| 104 | + <div class="flex-1"> |
| 105 | + <span class="font-bold text-grey-darkest">{{ server.name }}</span><br /> |
| 106 | + <span class="font-light text-grey-dark text-sm" v-if="server.description.length > 0">{{ server.description }}</span> |
| 107 | + </div> |
| 108 | + <div class="flex-none"> |
| 109 | + <span class="pillbox bg-indigo">{{ server.node }}</span> |
| 110 | + </div> |
| 111 | + </div> |
| 112 | + </router-link> |
| 113 | + </div> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + <div class="menu flex-none"> |
| 117 | + <ul> |
| 118 | + <li> |
| 119 | + <router-link :to="{ name: 'dashboard' }"> |
| 120 | + <icon name="server" aria-label="Server dashboard" class="h-4"/> |
| 121 | + </router-link> |
| 122 | + </li> |
| 123 | + <li> |
| 124 | + <router-link :to="{ name: 'account' }"> |
| 125 | + <icon name="user" aria-label="Profile management" class="h-4"/> |
| 126 | + </router-link> |
| 127 | + </li> |
| 128 | + <li> |
| 129 | + <a :href="this.route('admin.index')"> |
| 130 | + <icon name="settings" aria-label="Administrative controls" class="h-4"/> |
| 131 | + </a> |
| 132 | + </li> |
| 133 | + <li> |
| 134 | + <a :href="this.route('auth.logout')" v-on:click.prevent="doLogout"> |
| 135 | + <icon name="log-out" aria-label="Sign out" class="h-4"/> |
| 136 | + </a> |
| 137 | + </li> |
| 138 | + </ul> |
| 139 | + </div> |
| 140 | + </div> |
| 141 | + ` |
| 142 | +}) |
0 commit comments