File size: 9,309 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
<template>
    <div ref="smilesPopupViewerContainer" class="smiles-popup-viewer-container mb-4" :style="containerStyles">
        <span :style="{ cursor: inPopup || !smilesToUse ? 'default' : 'pointer' }">
            <!--
                The v-if="isIntersecting" ensures Mol2DView is only rendered
                when the container is visible.
            -->
            <Mol2DView v-if="isIntersecting && smilesToUse" :smiles="smilesToUse" :maxHeight="maxHeight"
                :drawingWidth="measuredContainerWidth > 0 ? measuredContainerWidth : undefined" :minHeight="30"
                @click="onClick" />
            <!--
                Optional: Add a placeholder div that has a minimum height,
                so the layout doesn't jump when the image loads.
                This placeholder will be observed by IntersectionObserver.
            -->
            <div v-else-if="!isIntersecting && smilesToUse"
                :style="{ minHeight: (maxHeight ? Math.min(maxHeight, 50) : 50) + 'px', width: '100%' }"></div>
        </span>
        <Popup v-if="!inPopup" v-model="showSmilesPopup" title="Molecular Structure" cancelBtnTxt="Close"
            id="molStructurePopupForSmiles">
            <!-- Use Mol2DView for the popup content -->
            <Mol2DView v-if="showSmilesPopup && smilesToUse" :smiles="smilesToUse" :drawingWidth="450"
                :drawingHeight="350" :minHeight="50" :showDownloadButtons="true" />
            <div v-else-if="showSmilesPopup && !smilesToUse" class="text-center p-3">
                No SMILES string provided for popup view.
            </div>
        </Popup>
    </div>
</template>

<script lang="ts">
import { Options, Vue } from "vue-class-component";
import { Prop, Watch } from "vue-property-decorator";
import Popup from "@/UI/MessageAlerts/Popups/Popup.vue";
import Mol2DView from "./Mol2DView.vue"; // Import the new reusable component

/**
 * SmilesPopupViewer component: Displays a 2D rendering of a SMILES string using
 * Mol2DView and provides a popup for a larger view upon clicking the image.
 * The initial Mol2DView is lazy-loaded.
 */
@Options({
    components: {
        Popup,
        Mol2DView,
    },
})
export default class SmilesPopupViewer extends Vue {
    @Prop({ default: "" }) smiles!: string;
    @Prop({ default: "100%" }) width!: string;
    @Prop({ default: undefined }) maxHeight!: number | undefined;
    @Prop({ default: "" }) extraStyles!: string;
    @Prop({ default: false }) inPopup!: boolean; // If this instance itself is already in a popup

    private smilesPopupViewerContainer: HTMLDivElement | undefined = undefined;
    private resizeObserver: ResizeObserver | null = null;
    private intersectionObserver: IntersectionObserver | null = null;

    // Reactive properties
    private measuredContainerWidth = 0;
    private showSmilesPopup = false;
    private isIntersecting = false; // True when the component is visible in the viewport

    /**
     * Watcher for the external width prop.
     * This ensures that if the parent container changes its width definition for this component,
     * we re-measure and potentially re-render.
     */
    @Watch("width")
    onWidthPropChanged() {
        this.$nextTick(() => {
            this.updateMeasuredContainerWidth();
        });
    }

    /**
     * Gets the CSS styles for the main container div.
     * The height is mostly determined by the Mol2DView content or maxHeight.
     * 
     * @returns {string} The style string.
     */
    get containerStyles(): string {
        const displayWidth = this.smilesToUse ? this.width : "0";
        let style: Record<string, string | number> = {
            width: displayWidth,
            minHeight: this.smilesToUse ? '50px' : '0px', // Ensure some height even for errors or loading
            // display: 'flex', // Mol2DView will handle its own centering
            // alignItems: 'center',
            // justifyContent: 'center',
        };
        if (this.extraStyles) {
            try {
                const extra = JSON.parse(`{${this.extraStyles.replace(/'/g, '"')}}`);
                style = { ...style, ...extra };
            } catch (e) {
                console.warn("Could not parse extraStyles for SmilesPopupViewer container", e);
            }
        }
        if (this.maxHeight !== undefined) {
            style['maxHeight'] = `${this.maxHeight}px`;
        }
        if (!this.smilesToUse) { // Collapse if no smiles
            style['height'] = '0px';
            style['minHeight'] = '0px';
            style['padding'] = '0';
            style['margin'] = '0';
            style['border'] = 'none';
        }

        // Convert into a CSS string for inline styles
        let styleString = "";
        for (const [key, value] of Object.entries(style)) {
            styleString += `${key}: ${value}; `;
        }
        return styleString;
    }

    /**
     * The SMILES string to use for rendering, trimmed and cleaned.
     * 
     * @returns {string} The processed SMILES string.
     */
    get smilesToUse(): string {
        return this.smiles.replace(/\t/g, " ").split(" ")[0].trim();
    }

    /**
     * Handles click on the inline Mol2DView image to show a larger version in a popup.
     */
    onClick() {
        if (!this.inPopup && this.smilesToUse) {
            this.showSmilesPopup = true;
        }
    }

    /**
     * Sets the `measuredContainerWidth` based on the actual width of the DOM container.
     * This width is then passed to the inline Mol2DView.
     */
    updateMeasuredContainerWidth() {
        if (this.smilesPopupViewerContainer) {
            const newWidth = this.smilesPopupViewerContainer.offsetWidth;
            if (this.measuredContainerWidth !== newWidth) {
                this.measuredContainerWidth = newWidth;
            }
        }
    }

    /**
     * Sets up the IntersectionObserver to detect when the component is visible.
     */
    private setupIntersectionObserver(): void {
        this.smilesPopupViewerContainer = this.$refs["smilesPopupViewerContainer"] as HTMLDivElement;
        if (!this.smilesPopupViewerContainer) return;

        const options = {
            root: null, // relative to the viewport
            rootMargin: "0px",
            threshold: 0.01, // as soon as 1% of the element is visible
        };

        this.intersectionObserver = new IntersectionObserver((entries) => {
            entries.forEach(entry => {
                if (entry.isIntersecting) {
                    this.isIntersecting = true;
                    // Once it's visible, we don't need to observe anymore
                    if (this.smilesPopupViewerContainer && this.intersectionObserver) {
                        this.intersectionObserver.unobserve(this.smilesPopupViewerContainer);
                    }
                }
            });
        }, options);

        this.intersectionObserver.observe(this.smilesPopupViewerContainer);
    }

    /**
     * Sets up a ResizeObserver to handle changes in the size of the root container.
     */
    private setupResizeObserver(): void {
        if (!this.smilesPopupViewerContainer) return;
        this.resizeObserver = new ResizeObserver((entries) => {
            if (entries && entries.length > 0) {
                this.updateMeasuredContainerWidth();
            }
        });
        this.resizeObserver.observe(this.smilesPopupViewerContainer);
    }
    /**
     * Lifecycle hook called when the component is unmounted.
     * Clears the resize interval and disconnects the IntersectionObserver.
     */
    unmounted() {
        if (this.resizeObserver) {
            if (this.smilesPopupViewerContainer) {
                this.resizeObserver.unobserve(this.smilesPopupViewerContainer);
            }
            this.resizeObserver.disconnect();
            this.resizeObserver = null;
        }
        if (this.intersectionObserver) {
            this.intersectionObserver.disconnect();
            this.intersectionObserver = null;
        }
    }

    /**
     * Lifecycle hook called when the component is mounted.
     * Sets up an interval to check for container width changes and initializes the IntersectionObserver.
     */
    async mounted() {
        this.smilesPopupViewerContainer = this.$refs["smilesPopupViewerContainer"] as HTMLDivElement;

        this.$nextTick(() => {
            this.updateMeasuredContainerWidth();
            // Only set up observer if there's a SMILES string to potentially display
            if (this.smilesToUse) {
                this.setupIntersectionObserver();
            } else {
                // If there's no SMILES, we can consider it "intersecting" to avoid blank space
                // if it were to get SMILES later and not be observed. Or simply, do nothing.
                // For now, if no smiles, it won't render Mol2DView anyway.
            }
            this.setupResizeObserver();
        });
    }
}
</script>

<style scoped lang="scss">
.smiles-popup-viewer-container {
    display: inline-block;
    /* Default display, can be overridden by extraStyles */
    position: relative;
    // Adding a default border for clarity, can be removed or overridden
    // border: 1px solid #e0e0e0; 
    // padding: 5px; 
    box-sizing: border-box;
}
</style>