Skip to content

Commit 130e378

Browse files
author
Erlend Oftedal
committed
Adds Expand to Result button
1 parent d4ee07d commit 130e378

2 files changed

Lines changed: 70 additions & 9 deletions

File tree

src/components/AstronomicalViewer.vue

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,44 @@ const execute = () => {
101101
execute()
102102
103103
const hasResults = computed(() => tree.value !== null)
104+
105+
const expandToResults = () => {
106+
if (!tree.value) return
107+
108+
// Helper function to check if a node has a primitive match
109+
const hasPrimitiveValue = (node: TreeNodeType): boolean => {
110+
if (
111+
node.type === 'Identifier' &&
112+
node.astNode.type === 'Identifier' &&
113+
'name' in node.astNode
114+
) {
115+
return primitiveResults.value.includes((node.astNode as { name: string }).name)
116+
} else if (node.type === 'Literal' && 'value' in node.astNode) {
117+
const value = (node.astNode as { value: unknown }).value
118+
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
119+
return primitiveResults.value.includes(value)
120+
}
121+
}
122+
return false
123+
}
124+
125+
// Deep clone the tree with updated expansion states
126+
const cloneWithExpansion = (node: TreeNodeType): TreeNodeType => {
127+
const shouldExpand = node.isAncestorOfMatch || node.isMatched || hasPrimitiveValue(node)
128+
129+
return {
130+
...node,
131+
isExpanded: shouldExpand,
132+
children: node.children.map((child) => cloneWithExpansion(child)),
133+
}
134+
}
135+
136+
if (Array.isArray(tree.value)) {
137+
tree.value = tree.value.map((node) => cloneWithExpansion(node))
138+
} else {
139+
tree.value = cloneWithExpansion(tree.value)
140+
}
141+
}
104142
</script>
105143

106144
<template>
@@ -131,7 +169,12 @@ const hasResults = computed(() => tree.value !== null)
131169
></textarea>
132170
</div>
133171

134-
<button @click="execute" class="execute-btn">Execute Query</button>
172+
<div class="button-group">
173+
<button @click="execute" class="execute-btn">Execute Query</button>
174+
<button @click="expandToResults" class="expand-btn" :disabled="matchCount === 0">
175+
Expand to Results
176+
</button>
177+
</div>
135178
</div>
136179

137180
<div v-if="errorMessage" class="error-message">
@@ -238,7 +281,13 @@ const hasResults = computed(() => tree.value !== null)
238281
box-shadow: 0 0 0 2px rgba(76, 175, 80, 0.1);
239282
}
240283
241-
.execute-btn {
284+
.button-group {
285+
display: flex;
286+
gap: 10px;
287+
}
288+
289+
.execute-btn,
290+
.expand-btn {
242291
background: #4caf50;
243292
color: white;
244293
border: none;
@@ -250,10 +299,24 @@ const hasResults = computed(() => tree.value !== null)
250299
transition: background-color 0.2s;
251300
}
252301
302+
.expand-btn {
303+
background: #2196f3;
304+
}
305+
253306
.execute-btn:hover {
254307
background: #45a049;
255308
}
256309
310+
.expand-btn:hover:not(:disabled) {
311+
background: #1976d2;
312+
}
313+
314+
.expand-btn:disabled {
315+
background: #ccc;
316+
cursor: not-allowed;
317+
opacity: 0.6;
318+
}
319+
257320
.execute-btn:active {
258321
background: #3d8b40;
259322
}

src/components/TreeNode.vue

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<script setup lang="ts">
2-
import { ref } from 'vue'
32
import type { TreeNode } from '../utils/astTreeBuilder'
43
54
const props = defineProps<{
@@ -8,11 +7,10 @@ const props = defineProps<{
87
level?: number
98
}>()
109
11-
const isExpanded = ref(false)
12-
1310
const toggleExpand = () => {
1411
if (props.node.children.length > 0) {
15-
isExpanded.value = !isExpanded.value
12+
// eslint-disable-next-line vue/no-mutating-props
13+
props.node.isExpanded = !props.node.isExpanded
1614
}
1715
}
1816
@@ -59,21 +57,21 @@ const indent = (props.level || 0) * 20
5957
'ancestor-of-match': node.isAncestorOfMatch,
6058
'primitive-matched': hasMatchedPrimitiveValue(node),
6159
'has-children': node.children.length > 0,
62-
expanded: isExpanded,
60+
expanded: node.isExpanded,
6361
}"
6462
:style="{ paddingLeft: `${indent}px` }"
6563
@click="toggleExpand"
6664
>
6765
<span class="expand-icon" v-if="node.children.length > 0">
68-
{{ isExpanded ? '▼' : '▶' }}
66+
{{ node.isExpanded ? '▼' : '▶' }}
6967
</span>
7068
<span class="node-label">{{ node.label }}</span>
7169
<span v-if="showPrimitiveMatch(node)" class="primitive-match-badge">
7270
✓ matches: {{ showPrimitiveMatch(node) }}
7371
</span>
7472
</div>
7573

76-
<div v-if="isExpanded && node.children.length > 0" class="children">
74+
<div v-if="node.isExpanded && node.children.length > 0" class="children">
7775
<TreeNode
7876
v-for="(child, index) in node.children"
7977
:key="`${index}-${child.type}-${child.start}`"

0 commit comments

Comments
 (0)