File size: 12,248 Bytes
71174bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<template>
    <div class="tree-view-wrapper" ref="scroller" @scroll.passive="handleScroll" @click.self="clearSelection">
        <FilterInput v-if="allTreeDataFlattened.length > 0 && depth === 0" :list="allTreeDataFlattened"
            :extractTextToFilterFunc="extractTextToFilterFunc" @onFilter="onFilter" mb="1" v-model="filterStr">
        </FilterInput>

        <div v-if="isVirtualizable" :style="sizerStyles">
            <div :style="virtualListStyles">
                <div v-for="(treeDatum, idx) in visibleNodes" :key="treeDatum.id" :data-molid="treeDatum.id"
                    :style="styleToUse" :data-idx="idx + startIndex">
                    <TitleBar :treeDatum="treeDatum" :depth="depth" :treeData="treeData" :filterStr="filterStr" />
                </div>
            </div>
        </div>

        <div v-else>
            <div v-for="(treeDatum, idx) in getLocalTreeData" :key="treeDatum.id" :data-molid="treeDatum.id"
                :style="styleToUse" :data-idx="idx">
                <TitleBar :treeDatum="treeDatum" :depth="depth" :treeData="treeData" :filterStr="filterStr" />
                <TreeView v-if="treeDatum?.nodes && treeDatum?.treeExpanded && filteredTreeNodes === null"
                    :treeData="treeDatum?.nodes" :depth="depth + 1" :styleToUse="indentStyle" :ref="treeDatum?.id" />
            </div>
        </div>
    </div>
</template>

<script lang="ts">
/* eslint-disable @typescript-eslint/ban-ts-comment */

import { Options, Vue } from "vue-class-component";
import { Prop } from "vue-property-decorator";
import IconSwitcher from "@/UI/Navigation/TitleBar/IconBar/IconSwitcher.vue";
import IconBar from "@/UI/Navigation/TitleBar/IconBar/IconBar.vue";
import { flexFixedWidthStyle } from "@/UI/Navigation/TitleBar/IconBar/IconBarUtils";
import TitleBar from "@/UI/Navigation/TitleBar/TitleBar.vue";
import { TreeNodeList } from "@/TreeNodes/TreeNodeList/TreeNodeList";
import { TreeNode } from "@/TreeNodes/TreeNode/TreeNode";
import FilterInput from "@/UI/Components/FilterInput.vue";
import ContextMenu from "../ContextMenu/ContextMenu.vue";
import * as api from "@/Api";
/**
 * TreeView component
 */
@Options({
    components: {
        IconSwitcher,
        IconBar,
        TitleBar,
        FilterInput,
        ContextMenu,
    },
})
export default class TreeView extends Vue {
    @Prop({ default: 0 }) depth!: number;
    @Prop({ default: undefined }) treeData!: TreeNodeList | undefined;
    @Prop({ default: undefined }) styleToUse!: string;
    filteredTreeNodes: TreeNode[] | null = null;
    filterStr = "";
    // VIRTUALIZATION STATE
    itemHeight = 24; // Estimated height of one row (TitleBar)
    scrollTop = 0;
    containerHeight = 0;
    resizeObserver: ResizeObserver | null = null;

    /**
     * Clears any selected molecules. This is called when the user clicks on the
     * background of the tree view.
     */
    clearSelection() {
        api.plugins.runPlugin("clearselection");
    }

    /**
     * Get the style for a fixed-width element.
     *
     * @param {number} width  The width of the element.
     * @returns {string}  The style for the element.
     */
    flexFixedWidth(width: number): string {
        return flexFixedWidthStyle(width);
    }

    /**
     * Get the indent style for the title bar.
     *
     * @returns {string} The indent style for the title bar.
     */
    get indentStyle(): string {
        // return `margin-left:${8 * this.depth}px`;
        return "";
    }

    /**
     * Get the molecules from the vuex store.
     *
     * @returns {any} The molecules from the vuex store.
     */
    get storeMolecules(): TreeNodeList {
        return this.$store.state["molecules"];
    }

    /**
     * Get the tree data, with all nodes flattened.
     *
     * @returns {TreeNode[]} The flattened tree data.
     */
    get allTreeDataFlattened(): TreeNode[] {
        return !this.treeData
            ? (this.storeMolecules as TreeNodeList).flattened.toArray()
            : (this.treeData as TreeNodeList).flattened.toArray();
    }

    /**
     * Get the text that will be used for filtering.
     *
     * @param {TreeNode} item  The item to get the text from.
     * @returns {string}  The text to use for filtering.
     */
    extractTextToFilterFunc(item: TreeNode): string {
        return item.title;
    }

    /**
     * Handle the filter event.
     *
     * @param {TreeNode[] | null} filteredNodes  The filtered nodes.
     */
    onFilter(filteredNodes: TreeNode[] | null) {
        this.filteredTreeNodes = filteredNodes;
    }

    /**
     * Get the local tree data.
     *
     * @returns {TreeNode[]} The local tree data. Needs to be converted to
     *     TreeNode[] to be interable in vue.js. If there's no filter, just
     *     return the whole tree as a list. Otherwise, return the filtered
     *     nodes.
     */
    get getLocalTreeData(): TreeNode[] {
        const allTreeData = !this.treeData
            ? (this.storeMolecules as TreeNodeList).toArray()
            : (this.treeData as TreeNodeList).toArray();

        if (this.filteredTreeNodes === null) {
            // No filter. Just return the tree.
            return allTreeData;
        }

        return this.filteredTreeNodes;
    }


    // fixTitle(title: string): string {
    //   // For compounds, remove text and put chain at end.
    //   title = title.replace(/Compounds:(.):(.+)$/g, "$2:$1");
    //   title = title.replace("Compounds:", "");
    //   title = title.replace(/^(.):(.+?):(\d*)$/g, "$2:$3:$1");

    //   // If Any word, :, single letter, remove single letter (using regex)
    //   title = title.replace(/^([^:]+):.$/g, "$1");

    //   return title;
    // }

    // treeChildNodeToUse(curMolCont: TreeNode): TreeNode | null {

    //   return curMolCont;

    //   // NOTE: Below merges children nicely, but I worry is sacrifices clarity for
    //   // conciseness.

    //   // if (curMolCont.nodes === undefined) {
    //   //   // No children (terminal node)
    //   //   return curMolCont;
    //   // }

    //   // if (curMolCont.nodes.length > 1) {
    //   //   // Multiple children
    //   //   return curMolCont;
    //   // }

    //   // if (curMolCont.nodes.length === 0) {
    //   //   // This shouldn't happen. Not a terminal node, but no children.
    //   //   return curMolCont; // null;
    //   // }

    //   // if (!curMolCont.parentId) {
    //   //   // Doing this because I don't want to collapse the names up to the top
    //   //   // one. Stop short of that.
    //   //   return curMolCont;
    //   // }

    //   // if (curMolCont.nodes.length === 1) {
    //   //   if (curMolCont.nodes[0].nodes?.length === 1) {
    //   //     // Single child with single child
    //   //     let title =
    //   //       curMolCont.title +
    //   //       ":" +
    //   //       curMolCont.nodes[0].title +
    //   //       ":" +
    //   //       curMolCont.nodes[0].nodes[0].title;

    //   //     return {
    //   //       ...curMolCont.nodes[0].nodes[0],
    //   //       title: this.fixTitle(title),
    //   //     };
    //   //   }

    //   //   // Single child
    //   //   let title = curMolCont.title + ":" + curMolCont.nodes[0].title;
    //   //   return {
    //   //     ...curMolCont.nodes[0],
    //   //     title: this.fixTitle(title),
    //   //   };
    //   // }

    //   // return curMolCont;
    // }
    /**
     * Determines if the current list is long and flat enough to be virtualized.
     *
     * @returns {boolean} True if virtualization should be used.
     */
    get isVirtualizable(): boolean {
        if (this.depth !== 0) return false;
        if (this.filteredTreeNodes === null) return false;
        return this.filteredTreeNodes.length >= 50;
    }

    /**
     * Calculates the index of the first visible item in the virtualized list.
     *
     * @returns {number} The start index.
     */
    get startIndex(): number {
        return Math.floor(this.scrollTop / this.itemHeight);
    }

    /**
     * Gets the subset of nodes that should be visible in the virtualized list.
     *
     * @returns {TreeNode[]} An array of TreeNodes to render.
     */
    get visibleNodes(): TreeNode[] {
        if (!this.isVirtualizable || !this.filteredTreeNodes) {
            return this.getLocalTreeData;
        }
        const visibleCount = Math.ceil(this.containerHeight / this.itemHeight) + 2;
        const endIndex = Math.min(
            this.startIndex + visibleCount,
            this.filteredTreeNodes.length
        );
        return this.filteredTreeNodes.slice(this.startIndex, endIndex);
    }

    /**
     * Gets the style for the sizer element, which ensures the scrollbar is the correct size.
     *
     * @returns {object} A style object for the sizer div.
     */
    get sizerStyles(): object {
        if (!this.isVirtualizable || !this.filteredTreeNodes) return {};
        return {
            position: "relative",
            height: `${this.filteredTreeNodes.length * this.itemHeight}px`,
        };
    }

    /**
     * Gets the style for the list wrapper, which positions the visible items correctly.
     *
     * @returns {object} A style object for the list wrapper div.
     */
    get virtualListStyles(): object {
        if (!this.isVirtualizable) return {};
        return {
            position: "absolute",
            top: "0",
            left: "0",
            width: "100%",
            transform: `translateY(${this.startIndex * this.itemHeight}px)`,
        };
    }

    /**
     * Handles the scroll event on the root container.
     *
     * @param {UIEvent} event The scroll event.
     */
    handleScroll(event: UIEvent): void {
        if (this.depth === 0) {
            const target = event.target as HTMLElement;
            this.scrollTop = target.scrollTop;
        }
    }

    /**
     * Sets up a ResizeObserver to monitor the size of the scrollable container.
     */
    setupResizeObserver(): void {
        if (this.depth === 0) {
            const scroller = this.$refs.scroller as HTMLElement;
            if (scroller) {
                this.containerHeight = scroller.clientHeight;
                this.resizeObserver = new ResizeObserver((entries) => {
                    if (entries[0]) {
                        this.containerHeight = entries[0].contentRect.height;
                    }
                });
                this.resizeObserver.observe(scroller);
            }
        }
    }

    /**
     * Lifecycle hook: called when the component is mounted.
     */
    mounted() {
        this.setupResizeObserver();
    }

    /**
     * Lifecycle hook: called before the component is unmounted.
     */
    beforeUnmount() {
        if (this.resizeObserver) {
            this.resizeObserver.disconnect();
            this.resizeObserver = null;
        }
    }
}
</script>

<style lang="scss"></style>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
.tree-view-wrapper {
    height: 100%;
    overflow-y: auto;
    position: relative;
    /* For sizer/virtual list positioning */
}

// See https://codepen.io/kdydesign/pen/VrQZqx
$transition-time: 0.2s;

.slide-enter-active {
    -moz-transition-duration: $transition-time;
    -webkit-transition-duration: $transition-time;
    -o-transition-duration: $transition-time;
    transition-duration: $transition-time;
    -moz-transition-timing-function: ease-in;
    -webkit-transition-timing-function: ease-in;
    -o-transition-timing-function: ease-in;
    transition-timing-function: ease-in;
}

.slide-leave-active {
    -moz-transition-duration: $transition-time;
    -webkit-transition-duration: $transition-time;
    -o-transition-duration: $transition-time;
    transition-duration: $transition-time;
    -moz-transition-timing-function: cubic-bezier(0, 1, 0.5, 1);
    -webkit-transition-timing-function: cubic-bezier(0, 1, 0.5, 1);
    -o-transition-timing-function: cubic-bezier(0, 1, 0.5, 1);
    transition-timing-function: cubic-bezier(0, 1, 0.5, 1);
}

.slide-enter-to,
.slide-leave {
    max-height: 100px;
    overflow: hidden;
}

.slide-enter,
.slide-leave-to {
    overflow: hidden;
    max-height: 0;
}
</style>