2019-12-31 16:21:34 +00:00
|
|
|
<template>
|
|
|
|
<form :class="['message-search', {opened: searchOpened}]" @submit.prevent="searchMessages">
|
|
|
|
<div class="input-wrapper">
|
|
|
|
<input
|
|
|
|
ref="searchInputField"
|
2021-04-12 22:11:43 +00:00
|
|
|
v-model="searchInput"
|
2021-05-27 07:23:44 +00:00
|
|
|
type="search"
|
2019-12-31 16:21:34 +00:00
|
|
|
name="search"
|
|
|
|
class="input"
|
2020-03-07 11:01:07 +00:00
|
|
|
placeholder="Search messages…"
|
2019-12-31 16:21:34 +00:00
|
|
|
@blur="closeSearch"
|
2021-11-18 21:27:52 +00:00
|
|
|
@keyup.esc="closeSearch"
|
2019-12-31 16:21:34 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<button
|
2021-04-12 22:55:57 +00:00
|
|
|
v-if="!onSearchPage"
|
2019-12-31 16:21:34 +00:00
|
|
|
class="search"
|
|
|
|
type="button"
|
|
|
|
aria-label="Search messages in this channel"
|
2020-01-04 18:03:56 +00:00
|
|
|
@mousedown.prevent="toggleSearch"
|
2019-12-31 16:21:34 +00:00
|
|
|
/>
|
|
|
|
</form>
|
|
|
|
</template>
|
|
|
|
|
2020-03-07 12:56:50 +00:00
|
|
|
<style>
|
|
|
|
form.message-search {
|
|
|
|
display: flex;
|
|
|
|
}
|
|
|
|
|
|
|
|
form.message-search .input-wrapper {
|
|
|
|
display: flex;
|
|
|
|
}
|
|
|
|
|
|
|
|
form.message-search input {
|
|
|
|
width: 100%;
|
|
|
|
height: auto !important;
|
|
|
|
margin: 7px 0;
|
|
|
|
border: 0;
|
|
|
|
color: inherit;
|
2021-04-12 22:55:57 +00:00
|
|
|
background-color: #fafafa;
|
2021-10-18 05:11:08 +00:00
|
|
|
appearance: none;
|
2020-03-07 12:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
form.message-search input::placeholder {
|
2021-04-12 22:55:57 +00:00
|
|
|
color: rgba(0, 0, 0, 0.35);
|
2020-03-07 12:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@media (min-width: 480px) {
|
|
|
|
form.message-search input {
|
|
|
|
min-width: 140px;
|
|
|
|
}
|
|
|
|
|
|
|
|
form.message-search input:focus {
|
|
|
|
min-width: 220px;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-12 22:55:57 +00:00
|
|
|
form.message-search .input-wrapper {
|
|
|
|
position: absolute;
|
|
|
|
top: 45px;
|
|
|
|
left: 0;
|
|
|
|
right: 0;
|
|
|
|
z-index: 1;
|
|
|
|
height: 0;
|
|
|
|
overflow: hidden;
|
|
|
|
background: var(--window-bg-color);
|
|
|
|
}
|
2020-03-07 12:56:50 +00:00
|
|
|
|
2021-04-12 22:55:57 +00:00
|
|
|
form.message-search .input-wrapper input {
|
|
|
|
margin: 7px;
|
|
|
|
}
|
2020-03-07 12:56:50 +00:00
|
|
|
|
2021-04-12 22:55:57 +00:00
|
|
|
form.message-search.opened .input-wrapper {
|
|
|
|
height: 50px;
|
|
|
|
}
|
2020-03-07 12:56:50 +00:00
|
|
|
|
2021-04-12 22:55:57 +00:00
|
|
|
#chat form.message-search button {
|
|
|
|
display: flex;
|
|
|
|
color: #607992;
|
2020-03-07 12:56:50 +00:00
|
|
|
}
|
|
|
|
</style>
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
<script lang="ts">
|
|
|
|
import {computed, defineComponent, onMounted, PropType, ref, watch} from "vue";
|
|
|
|
import {useRoute, useRouter} from "vue-router";
|
|
|
|
import eventbus from "../js/eventbus";
|
|
|
|
import {ClientNetwork, ClientChan} from "../js/types";
|
|
|
|
|
|
|
|
export default defineComponent({
|
2019-12-31 16:21:34 +00:00
|
|
|
name: "MessageSearchForm",
|
|
|
|
props: {
|
2022-06-19 00:25:21 +00:00
|
|
|
network: {type: Object as PropType<ClientNetwork>, required: true},
|
|
|
|
channel: {type: Object as PropType<ClientChan>, required: true},
|
2021-04-12 22:11:43 +00:00
|
|
|
},
|
2022-06-19 00:25:21 +00:00
|
|
|
setup(props) {
|
|
|
|
const searchOpened = ref(false);
|
|
|
|
const searchInput = ref("");
|
|
|
|
const router = useRouter();
|
|
|
|
const route = useRoute();
|
2021-05-06 01:24:20 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
const searchInputField = ref<HTMLInputElement | null>(null);
|
|
|
|
|
|
|
|
const onSearchPage = computed(() => {
|
|
|
|
return route.name === "SearchResults";
|
|
|
|
});
|
|
|
|
|
|
|
|
watch(route, (newValue) => {
|
|
|
|
if (newValue.query.q) {
|
|
|
|
searchInput.value = String(newValue.query.q);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
searchInput.value = String(route.query.q || "");
|
|
|
|
searchOpened.value = onSearchPage.value;
|
|
|
|
|
|
|
|
if (searchInputField.value && !searchInput.value && searchOpened.value) {
|
|
|
|
searchInputField.value.focus();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const closeSearch = () => {
|
|
|
|
if (!onSearchPage.value) {
|
|
|
|
searchInput.value = "";
|
|
|
|
searchOpened.value = false;
|
2021-04-12 22:55:57 +00:00
|
|
|
}
|
2022-06-19 00:25:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const toggleSearch = () => {
|
|
|
|
if (searchOpened.value) {
|
|
|
|
searchInputField.value?.blur();
|
2020-01-04 18:03:56 +00:00
|
|
|
return;
|
2019-12-31 16:21:34 +00:00
|
|
|
}
|
2020-01-04 18:03:56 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
searchOpened.value = true;
|
|
|
|
searchInputField.value?.focus();
|
|
|
|
};
|
|
|
|
|
|
|
|
const searchMessages = (event: Event) => {
|
2019-12-31 16:21:34 +00:00
|
|
|
event.preventDefault();
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
if (!searchInput.value) {
|
2019-12-31 16:21:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
router
|
2021-04-12 22:45:15 +00:00
|
|
|
.push({
|
|
|
|
name: "SearchResults",
|
|
|
|
params: {
|
2022-06-19 00:25:21 +00:00
|
|
|
id: props.channel.id,
|
2021-04-12 22:45:15 +00:00
|
|
|
},
|
|
|
|
query: {
|
2022-06-19 00:25:21 +00:00
|
|
|
q: searchInput.value,
|
2021-04-12 22:45:15 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
if (err.name === "NavigationDuplicated") {
|
|
|
|
// Search for the same query again
|
2022-06-19 00:25:21 +00:00
|
|
|
eventbus.emit("re-search");
|
2021-04-12 22:45:15 +00:00
|
|
|
}
|
|
|
|
});
|
2022-06-19 00:25:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
searchOpened,
|
|
|
|
searchInput,
|
|
|
|
searchInputField,
|
|
|
|
closeSearch,
|
|
|
|
toggleSearch,
|
|
|
|
searchMessages,
|
|
|
|
onSearchPage,
|
|
|
|
};
|
2019-12-31 16:21:34 +00:00
|
|
|
},
|
2022-06-19 00:25:21 +00:00
|
|
|
});
|
2019-12-31 16:21:34 +00:00
|
|
|
</script>
|