aboutsummaryrefslogtreecommitdiff
path: root/src/components/Tile.vue
blob: 42927358a0840ba922ec85b4aa84ef55190a2160 (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
<script lang="ts">
import {defineComponent, PropType} from 'vue';
import {LayoutNode} from '../lib';

// Component holds a tree-node structure representing a tile or tile-group to be rendered
export default defineComponent({
	name: 'tile', // Need this to use self in template
	props: {
		layoutNode: {type: Object as PropType<LayoutNode>, required: true},
		isRoot: {type: Boolean, default: false},
		// Settings passed in from parent component
		transitionDuration: {type: Number, required: true},
		headerSz: {type: Number, required: true},
		tileSpacing: {type: Number, required: true},
	},
	data(){
		return {
			borderRadius: '5px',
			// Used during transitions and to emulate/show an apparently-joined div
			zIdx: 0,
			overflow: this.isRoot ? 'hidden' : 'visible',
		}
	},
	computed: {
		showHeader(){
			return (this.layoutNode.showHeader && !this.layoutNode.sepSweptArea) ||
				(this.layoutNode.sepSweptArea && this.layoutNode.sepSweptArea.sweptLeft);
		},
		tileStyles(): Record<string,string> {
			return {
				// Places div using layoutNode, with centering if root
				position: 'absolute',
				left: this.isRoot ? '50%' : this.layoutNode.pos[0] + 'px',
				top: this.isRoot ? '50%' : this.layoutNode.pos[1] + 'px',
				transform: this.isRoot ? 'translate(-50%, -50%)' : 'none',
				width: this.layoutNode.dims[0] + 'px',
				height: this.layoutNode.dims[1] + 'px',
				// Other bindings
				transitionDuration: this.transitionDuration + 'ms',
				zIndex: String(this.zIdx),
				overflow: String(this.overflow),
				// Static styles
				transitionProperty: 'left, top, width, height',
				transitionTimingFunction: 'ease-out',
			};
		},
		leafStyles(): Record<string,string> {
			return {
				width: '100%',
				height: '100%',
				backgroundImage: 'url(\'/img/' + this.layoutNode.tolNode.name.replaceAll('\'', '\\\'') + '.png\')',
				backgroundSize: 'cover',
				borderRadius: this.borderRadius,
				opacity: (this.layoutNode.tolNode.children.length > 0) ? '1' : '0.7',
			};
		},
		leafHeaderStyles(): Record<string,string> {
			return {
				height: this.headerSz + 'px',
				textAlign: 'center',
				overflow: 'hidden',
				textOverflow: 'ellipsis',
				whiteSpace: 'nowrap',
			};
		},
		nonLeafStyles(): Record<string,string> {
			let temp = {
				width: '100%',
				height: '100%',
				backgroundColor: 'white',
				outline: 'black solid 1px',
				borderRadius: this.borderRadius,
			};
			if (this.layoutNode.sepSweptArea != null){
				temp = this.layoutNode.sepSweptArea.sweptLeft ?
					{...temp, borderRadius: `${this.borderRadius} ${this.borderRadius} ${this.borderRadius} 0`} :
					{...temp, borderRadius: `${this.borderRadius} 0 ${this.borderRadius} ${this.borderRadius}`};
			}
			return temp;
		},
		nonLeafHeaderStyles(): Record<string,string> {
			return {
				height: this.headerSz + 'px',
				backgroundColor: 'lightgray',
				borderRadius: `${this.borderRadius} ${this.borderRadius} 0 0`,
				textAlign: 'center',
				overflow: 'hidden',
				textOverflow: 'ellipsis',
				whiteSpace: 'nowrap',
			};
		},
		sepSweptAreaStyles(): Record<string,string> {
			let commonStyles = {
				position: 'absolute',
				backgroundColor: 'white',
				outline: 'black solid 1px',
				transitionDuration: this.transitionDuration + 'ms',
				transitionProperty: 'left, top, width, height',
				transitionTimingFunction: 'ease-out',
			};
			let area = this.layoutNode.sepSweptArea;
			if (area == null){
				return {
					...commonStyles,
					visibility: 'hidden',
					left: '0',
					top: this.headerSz + 'px',
					width: '0',
					height: '0',
				};
			} else {
				return {
					...commonStyles,
					left: area.pos[0] + 'px',
					top: area.pos[1] + 'px',
					width: area.dims[0] + 'px',
					height: area.dims[1] + 'px',
					borderRadius: area.sweptLeft ?
						`${this.borderRadius} 0 0 ${this.borderRadius}` :
						`${this.borderRadius} ${this.borderRadius} 0 0`,
				};
			}
		},
	},
	methods: {
		onLeafClick(){
			this.$emit('leaf-clicked', this.layoutNode);
			// Increase z-index and hide overflow during transition
			this.zIdx = 1;
			this.overflow = 'hidden';
			setTimeout(() => {this.zIdx = 0; this.overflow = 'visible'}, this.transitionDuration);
		},
		onInnerLeafClicked(node: LayoutNode){
			this.$emit('leaf-clicked', node);
		},
		onHeaderClick(){
			this.$emit('header-clicked', this.layoutNode);
			// Increase z-index and hide overflow during transition
			this.zIdx = 1;
			this.overflow = 'hidden';
			setTimeout(() => {this.zIdx = 0; this.overflow = 'visible'}, this.transitionDuration);
		},
		onInnerHeaderClicked(node: LayoutNode){
			this.$emit('header-clicked', node);
		},
	},
});
</script>

<template>
<div :style="tileStyles">
	<div v-if="layoutNode.children.length == 0" :style="leafStyles" class="hover:cursor-pointer" @click="onLeafClick">
		<div :style="leafHeaderStyles">{{layoutNode.tolNode.name}}</div>
	</div>
	<div v-else :style="nonLeafStyles">
		<div v-if="showHeader" :style="nonLeafHeaderStyles" class="hover:cursor-pointer" @click="onHeaderClick">
			{{layoutNode.tolNode.name}}
		</div>
		<div :style="sepSweptAreaStyles"
			:class="layoutNode?.sepSweptArea?.sweptLeft ? 'hide-right-edge' : 'hide-top-edge'">
			<div v-if="layoutNode?.sepSweptArea?.sweptLeft === false"
				:style="nonLeafHeaderStyles" class="hover:cursor-pointer" @click="onHeaderClick">
				{{layoutNode.tolNode.name}}
			</div>
		</div>
		<tile v-for="child in layoutNode.children" :key="child.tolNode.name" :layoutNode="child"
			:headerSz="headerSz" :tileSpacing="tileSpacing" :transitionDuration="transitionDuration"
			@leaf-clicked="onInnerLeafClicked" @header-clicked="onInnerHeaderClicked"/>
	</div>
</div>
</template>

<style>
.hide-right-edge::before {
	content: '';
	position: absolute;
	background-color: white;
	right: -1px;
	bottom: 0;
	width: 1px;
	height: 101%;
}
.hide-top-edge::before {
	content: '';
	position: absolute;
	background-color: white;
	bottom: -1px;
	right: 0;
	width: 101%;
	height: 1px;
}
</style>