File size: 5,055 Bytes
af2e17e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<script lang="ts">
	import {afterUpdate } from "svelte";
	import Dmol from "3dmol";

	import {BlockLabel, Empty } from "@gradio/atoms";
	import {Image} from "@gradio/icons";

	export let show_label = true;
	export let label: string;
	export let value: { molecule: string; caption: string | null }[] | null = null;
	export let columns: number | number[] | undefined = [2];
	export let rows: number | number[] | undefined = undefined;
	export let height: number | "auto" = "auto";
	export let automatic_rotation: boolean = true;

	// tracks whether the value of the gallery was reset
	let was_reset = true;

	$: was_reset = value == null || value.length == 0 ? true : was_reset;

	let _value: { molecule: string; caption: string | null }[] | null = null;

	$: _value =
		value === null
			? null
			: value.map((data, i) => ({
					molecule: data.molecule,
					caption: data.caption,
			  }));

	let client_height = 0;
	let window_height = 0;

	// Function to initialize 3Dmol.js for each molecule
	function initializeMoleculeViewer(molecule: string, containerId: string): Dmol.GLViewer {
		let viewer = Dmol.createViewer(containerId, {});
		viewer.addModel(molecule, "pdb" + {_value});
		viewer.setStyle({ stick: {} });
		viewer.setBackgroundColor("white");
		viewer.zoomTo();
		viewer.render();
		return viewer;
	}

	// Rotate the viewer automatically
	function rotateMolecule(viewer: Dmol.GLViewer) {
			viewer.rotate(0.3, 'y')
			viewer.rotate(0.3, 'x')
			requestAnimationFrame((time) => rotateMolecule(viewer));
	}

	// function to download the image on right click
	function handleContextMenu(event) {
		event.preventDefault();
		
		// Get the data URL of the canvas
		const canvas = event.target;
		const canvas_id = event.currentTarget.id;
		console.log(canvas_id);
		var dt = canvas.toDataURL('image/png');
		
		// Trigger the download
		var link = document.createElement('a');
		link.href = dt;
		link.download = canvas_id + '.png';
		document.body.appendChild(link);
		link.click();
		document.body.removeChild(link);
	}

	afterUpdate(() => {
		// Trigger initialization when the component is mounted
		if (_value) {
			_value.forEach((entry, i) => {
				const containerId = 'mol-canvas-id-' + (i + 1);
				let viewer = initializeMoleculeViewer(entry.molecule, containerId);
				if (automatic_rotation) {
					rotateMolecule(viewer);
				}
			});
		}
	});
</script>

<svelte:window bind:innerHeight={window_height} />

{#if show_label}
	<BlockLabel {show_label} Icon={Image} label={label || "Gallery"} />
{/if}
{#if value === null || _value === null || _value.length === 0}
	<Empty unpadded_box={true} size="large"><Image /></Empty>
{:else}
	<div
		bind:clientHeight={client_height}
		class="grid-wrap"
		class:fixed-height={!height || height == "auto"}
	>
		<div
			class="grid-container"
			style="--grid-cols:{columns}; --grid-rows:{rows}; height: {height};"
			class:pt-6={show_label}
		>
			{#each _value as entry, i}
				<div
					class="molecule-item molecule-lg, mol-container"
					aria-label={"Molecule " + (i + 1) + " of " + _value.length}
				>
					<!-- svelte-ignore a11y-no-static-element-interactions -->
					<div on:contextmenu={handleContextMenu} id={'mol-canvas-id-' + (i + 1)}  class="mol-canvas"></div>
					{#if entry.caption}
						<div class="caption-label">
							{entry.caption}
						</div>
					{/if}
				</div>
			{/each}
		</div>
	</div>
{/if}

<style lang="postcss">
	.fixed-height {
		min-height: var(--size-80);
		max-height: 55vh;
	}

	@media (--screen-xl) {
		.fixed-height {
			min-height: 450px;
		}
	}

	.molecule-item {
		--ring-color: transparent;
		position: relative;
		box-shadow:
			0 0 0 2px var(--ring-color),
			var(--shadow-drop);
		border: 1px solid var(--border-color-primary);
		border-radius: var(--button-small-radius);
		background: var(--background-fill-secondary);
		aspect-ratio: var(--ratio-square);
		width: var(--size-full);
		height: var(--size-full);
		overflow: clip;
	}

	.molecule-item:hover {
		--ring-color: var(--color-accent);
		filter: brightness(1.1);
	}

	.grid-wrap {
		position: relative;
		padding: var(--size-2);
		height: var(--size-full);
		overflow-y: scroll;
	}

	.grid-container {
		display: grid;
		position: relative;
		grid-template-rows: repeat(var(--grid-rows), minmax(100px, 1fr));
		grid-template-columns: repeat(var(--grid-cols), minmax(100px, 1fr));
		grid-auto-rows: minmax(100px, 1fr);
		gap: var(--spacing-lg);
	}

	.mol-canvas {
		width: 100%;
		height: 100%;
		position: absolute;
		top: 0;
		left: 0;
	}

	.caption-label {
		position: absolute;
		right: var(--block-label-margin);
		bottom: var(--block-label-margin);
		z-index: var(--layer-1);
		border-top: 1px solid var(--border-color-primary);
		border-left: 1px solid var(--border-color-primary);
		border-radius: var(--block-label-radius);
		background: var(--background-fill-secondary);
		padding: var(--block-label-padding);
		max-width: 80%;
		overflow: hidden;
		font-size: var(--block-label-text-size);
		text-align: left;
		text-overflow: ellipsis;
		white-space: nowrap;
	}
</style>