aboutsummaryrefslogtreecommitdiff
path: root/src/components/SearchModal.vue
blob: cd4bede7125a92b7dd76265f42267d3f2fe4322c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<script lang="ts">
import {defineComponent, PropType} from 'vue';
import SearchIcon from './icon/SearchIcon.vue';
import {LayoutNode} from '../layout';
import type {TolMap} from '../tol';

// Displays a search box, and sends search requests
export default defineComponent({
	props: {
		tolMap: {type: Object as PropType<TolMap>, required: true},
		uiOpts: {type: Object, required: true},
	},
	methods: {
		onCloseClick(evt: Event){
			if (evt.target == this.$el || (this.$refs.searchIcon as typeof SearchIcon).$el.contains(evt.target)){
				this.$emit('search-close');
			}
		},
		onSearchEnter(){
			let input = this.$refs.searchInput as HTMLInputElement;
			// Query server
			let url = new URL(window.location.href);
			url.pathname = '/data/search';
			url.search = '?name=' + encodeURIComponent(input.value);
			fetch(url.toString())
				.then(response => response.json())
				.then(tolNodeName => {
					// Search successful. Get nodes in parent-chain, add to tolMap, then emit event.
					url.pathname = '/data/chain';
					url.search = '?name=' + encodeURIComponent(tolNodeName);
					fetch(url.toString())
						.then(response => response.json())
						.then(obj => {
							Object.getOwnPropertyNames(obj).forEach(key => {this.tolMap.set(key, obj[key])});
							this.$emit('search-node', tolNodeName);
						})
						.catch(error => {
							console.log('ERROR loading tolnode chain', error);
						});
				})
				.catch(error => {
					input.value = '';
					// Trigger failure animation
					input.classList.remove('animate-red-then-fade');
					input.offsetWidth; // Triggers reflow
					input.classList.add('animate-red-then-fade');
				});
		},
		focusInput(){
			(this.$refs.searchInput as HTMLInputElement).focus();
		},
	},
	mounted(){
		(this.$refs.searchInput as HTMLInputElement).focus();
	},
	components: {SearchIcon, },
	emits: ['search-node', 'search-close', ],
});
</script>

<template>
<div class="fixed left-0 top-0 w-full h-full bg-black/40" @click="onCloseClick">
	<div class="absolute left-1/2 -translate-x-1/2 top-1/2 -translate-y-1/2 p-3
		bg-stone-50 rounded-md shadow shadow-black flex gap-1">
		<input type="text" class="block border"
			@keyup.enter="onSearchEnter" @keyup.esc="onCloseClick" ref="searchInput"/>
		<search-icon @click.stop="onSearchEnter" ref="searchIcon"
			class="block w-6 h-6 ml-1 hover:cursor-pointer hover:bg-stone-200" />
	</div>
</div>
</template>

<style>
.animate-red-then-fade {
	animation-name: red-then-fade;
	animation-duration: 500ms;
	animation-timing-function: ease-in;
}
@keyframes red-then-fade {
	from {
		background-color: rgba(255,0,0,0.2);
	}
	to {
		background-color: transparent;
	}
}
</style>