import { getElement } from './utils'; import * as styles from './styles'; import { DisplayTarget } from './indexer'; /** * The {@link DisplayTargetToggle} class represents a UI component for switching between two * display targets, structures and atom-centered environments, by clicking on the respective buttons. * * It automatically adjusts the height of the map element to ensure there is space for the toggle. * The component uses Shadow DOM to encapsulate styles. * A callback function (`onchange`) is triggered whenever the toggle value (structure/atom) is changed */ export class DisplayTargetToggle { /// Shadow root for isolation private _shadow: ShadowRoot; /// Toggle buttons container element private _toggleContainer: HTMLElement; /// Reference to the container element private _containerElement: HTMLElement; /// Callback fired when the user changes the toggle value public onchange: (target: DisplayTarget) => void; /** * Create a new {@link DisplayTargetToggle} instance * * @param element HTML element or HTML id of the DOM element where the toggle will be attached * @param target display target, either per environements or structures */ constructor(element: string | HTMLElement, target: DisplayTarget) { // Create a container element this._containerElement = getElement(element); const hostElement = document.createElement('div'); this._containerElement.appendChild(hostElement); // Attach to shadow this._shadow = hostElement.attachShadow({ mode: 'open' }); this._shadow.adoptedStyleSheets = [styles.bootstrap, styles.chemiscope]; // Create a toggle element this._toggleContainer = this._createToggleElement(target === 'atom'); this._shadow.appendChild(this._toggleContainer); // Decrease size of map to get space for display target toggle this._containerElement.style.setProperty( 'height', `calc(100% - ${this._toggleContainer.offsetHeight}px)` ); // Set up events this.onchange = () => {}; } /** * Create the HTML structure for the toggle element * * @param isPerAtom flag indicating if the atom target should be checked * @returns the container element of the toggle buttons */ private _createToggleElement(isPerAtom: boolean): HTMLElement { const toggleContainer = document.createElement('div'); toggleContainer.innerHTML = `