aboutsummaryrefslogtreecommitdiff
path: root/src/components/TileInfoModal.vue
blob: 83155ba94ae8f38efefc15072fb0fc83d8b0103d (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<script lang="ts">
import {defineComponent, PropType} from 'vue';
import CloseIcon from './icon/CloseIcon.vue';
import Tile from './Tile.vue'
import {LayoutNode} from '../layout';
import type {LayoutOptions} from '../layout';
import type {TolMap} from '../lib';
import {TolNode, DescInfo, ImgInfo, TileInfoResponse} from '../lib';
import {capitalizeWords} from '../lib';

// Displays information about a tree-of-life node
export default defineComponent({
	data(){
		return {
			tolNode: null as null | TolNode,
			descInfo: null as null | DescInfo,
			descInfo1: null as null | DescInfo,
			descInfo2: null as null | DescInfo,
			imgInfo: null as null | ImgInfo,
			imgInfo1: null as null | ImgInfo,
			imgInfo2: null as null | ImgInfo,
		};
	},
	props: {
		nodeName: {type: String, required: true},
		tolMap: {type: Object as PropType<TolMap>, required: true},
		lytOpts: {type: Object as PropType<LayoutOptions>, required: true},
		uiOpts: {type: Object, required: true},
	},
	computed: {
		displayName(): string {
			if (this.tolNode == null || this.tolNode.commonName == null){
				return capitalizeWords(this.nodeName);
			} else {
				return `${capitalizeWords(this.tolNode.commonName)} (aka ${capitalizeWords(this.nodeName)})`;
			}
		},
		subName1(): string {
			return this.displayName.substring(1, this.displayName.indexOf(' + '));
		},
		subName2(): string {
			return this.displayName.substring(this.displayName.indexOf(' + ') + 3, this.displayName.length - 1);
		},
		imgStyles(): Record<string,string> {
			return this.getImgStyles(this.tolNode == null ? null : this.tolNode.imgName as string);
		},
		firstImgStyles(): Record<string,string> {
			return this.getImgStyles(this.tolNode!.imgName![0]);
		},
		secondImgStyles(): Record<string,string> {
			return this.getImgStyles(this.tolNode!.imgName![1]);
		},
		dummyNode(): LayoutNode {
			let newNode = new LayoutNode(this.nodeName, []);
			newNode.dims = [this.uiOpts.infoModalImgSz, this.uiOpts.infoModalImgSz];
			return newNode;
		},
	},
	methods: {
		onCloseClick(evt: Event){
			if (evt.target == this.$el || (this.$refs.closeIcon as typeof CloseIcon).$el.contains(evt.target)){
				this.$emit('close');
			}
		},
		getImgStyles(imgName: string | null){
			return {
				boxShadow: this.uiOpts.shadowNormal,
				borderRadius: this.uiOpts.borderRadius + 'px',
				backgroundImage: imgName != null ?
					'url(\'/img/' + imgName.replaceAll('\'', '\\\'') + '\')' :
					'none',
				backgroundColor: '#1c1917',
				backgroundSize: 'cover',
				width: this.uiOpts.infoModalImgSz + 'px',
				height: this.uiOpts.infoModalImgSz + 'px',
			};
		},
		licenseToUrl(license: string){
			license = license.toLowerCase().replaceAll('-', ' ');
			if (license == 'cc0'){
				return 'https://creativecommons.org/publicdomain/zero/1.0/';
			} else if (license == 'cc publicdomain'){
				return 'https://creativecommons.org/licenses/publicdomain/';
			} else {
				const regex = /cc by( nc)?( sa)?( ([0-9.]+)( [a-z]+)?)?/;
				let results = regex.exec(license);
				if (results != null){
					let url = 'https://creativecommons.org/licenses/by';
					if (results[1] != null){
						url += '-nc';
					}
					if (results[2] != null){
						url += '-sa';
					}
					if (results[4] != null){
						url += '/' + results[4];
					} else {
						url += '/4.0';
					}
					if (results[5] != null){
						url += '/' + results[5].substring(1);
					}
					return url;
				}
				return "[INVALID LICENSE]";
			}
		},
	},
	created(){
		let url = new URL(window.location.href);
		url.pathname = '/data/info';
		url.search = '?name=' + encodeURIComponent(this.nodeName);
		url.search += this.uiOpts.useReducedTree ? '&tree=reduced' : '';
		fetch(url.toString())
			.then(response => response.json())
			.then(obj => {
				this.tolNode = obj.tolNode;
				if (!Array.isArray(obj.descData)){
					this.descInfo = obj.descData;
				} else {
					[this.descInfo1, this.descInfo2] = obj.descData;
				}
				if (!Array.isArray(obj.imgData)){
					this.imgInfo = obj.imgData;
				} else {
					[this.imgInfo1, this.imgInfo2] = obj.imgData;
				}
			});
	},
	components: {CloseIcon, Tile, },
	emits: ['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 w-4/5 max-h-[80%] overflow-y-auto top-1/2 -translate-y-1/2 p-4
		bg-stone-50 rounded-md shadow shadow-black">
		<close-icon @click.stop="onCloseClick" ref="closeIcon"
			class="block absolute top-2 right-2 w-6 h-6 hover:cursor-pointer"/>
		<h1 class="text-center text-xl font-bold mb-2">
			{{displayName}}
			<div v-if="tolNode != null">
				({{tolNode.children.length}} children, {{tolNode.tips}} tips,
					<a :href="'https://tree.opentreeoflife.org/opentree/argus/opentree13.4@' + tolNode.otolId">
						{{tolNode.otolId}}</a>)
			</div>
		</h1>
		<hr class="mb-4 border-stone-400"/>
		<div class="flex">
			<div class="mr-4">
				<div v-if="tolNode == null"/>
				<div v-else-if="!Array.isArray(tolNode.imgName)">
					<div :style="imgStyles"/>
					<ul v-if="imgInfo != null">
						<li>Obtained via: {{imgInfo.src}}</li>
						<li>License: <a :href="licenseToUrl(imgInfo.license)">{{imgInfo.license}}</a></li>
						<li><a :href="imgInfo.url" class="underline">Source URL</a></li>
						<li>Artist: {{imgInfo.artist}}</li>
						<li v-if="imgInfo.credit != ''">Credit: {{imgInfo.credit}}</li>
					</ul>
				</div>
				<div v-else>
					<div v-if="tolNode.imgName[0] != null" :style="firstImgStyles"/>
					<ul v-if="imgInfo1 != null">
						<li>Obtained via: {{imgInfo1.src}}</li>
						<li>License: <a :href="licenseToUrl(imgInfo1.license)">{{imgInfo1.license}}</a></li>
						<li><a :href="imgInfo1.url" class="underline">Source URL</a></li>
						<li>Artist: {{imgInfo1.artist}}</li>
						<li v-if="imgInfo1.credit != ''">Credit: {{imgInfo1.credit}}</li>
					</ul>
					<div v-if="tolNode.imgName[1] != null" :style="secondImgStyles"/>
					<ul v-if="imgInfo2 != null">
						<li>Obtained via: {{imgInfo2.src}}</li>
						<li>License: <a :href="licenseToUrl(imgInfo2.license)">{{imgInfo2.license}}</a></li>
						<li><a :href="imgInfo2.url" class="underline">Source URL</a></li>
						<li>Artist: {{imgInfo2.artist}}</li>
						<li v-if="imgInfo2.credit != ''">Credit: {{imgInfo2.credit}}</li>
					</ul>
				</div>
			</div>
			<div v-if="descInfo == null && descInfo1 == null && descInfo2 == null">
				(No description found)
			</div>
			<div v-else-if="descInfo != null">
				<div>
					Redirected: {{descInfo.fromRedirect}} <br/>
					Short-description from {{descInfo.fromDbp ? 'DBpedia' : 'Wikipedia'}} <br/>
					<a :href="'https://en.wikipedia.org/?curid=' + descInfo.wikiId" class="underline">
						Wikipedia Link
					</a>
				</div>
				<hr/>
				<div>{{descInfo.text}}</div>
			</div>
			<div v-else>
				<div>
					<h2 class="font-bold">{{subName1}}</h2>
					<div v-if="descInfo1 != null">{{descInfo1.text}}</div>
					<div v-else>(No description found)</div>
				</div>
				<div>
					<h2 class="font-bold">{{subName2}}</h2>
					<div v-if="descInfo2 != null">{{descInfo2.text}}</div>
					<div v-else>(No description found)</div>
				</div>
			</div>
		</div>
		
	</div>
</div>
</template>