/** @license Copyright (c) 2023 trading_peter */ import { LitElement, html, css } from 'lit'; class TpHSplitter extends LitElement { static get styles() { return [ css` :host { display: grid; grid-template-rows: 1fr var(--tp-splitter-width, 5px) 1fr; } :host([tophidden]), :host([bottomhidden]) { grid-template-rows: 1fr !important; } [hidden] { display: none !important; } #top, #bottom { position: relative; overflow: hidden; } #splitter { display: flex; flex-direction: row; align-items: center; justify-content: center; background: var(--tp-splitter-line-color, #3b3b3b); cursor: row-resize; opacity: 1; z-index: 2; } #splitter:hover { background: var(--tp-splitter-line-color-hover, #007dd1); opacity: 0.5; } ` ]; } render() { const { topHidden, bottomHidden } = this; return html`
`; } static get properties() { return { topHidden: { type: Boolean, reflect: true }, bottomHidden: { type: Boolean, reflect: true }, dragging: { type: Boolean, reflect: true }, minSideHeight: { type: Number }, lateResize: { type: Boolean }, initialTopHeight: { type: String }, }; } constructor() { super(); this._disableDrag = this._disableDrag.bind(this); this._resize = this._resize.bind(this); this._bufferedDelta = 0; } get top() { return this.shadowRoot.querySelector('#top'); } get bottom() { return this.shadowRoot.querySelector('#bottom'); } get splitter() { return this.shadowRoot.querySelector('#splitter') } firstUpdated() { if (this.initialTopHeight) { this.style.gridTemplateRows = `${this.initialTopHeight} var(--tp-splitter-width, 5px) 1fr`; } } disconnectedCallback() { super.disconnectedCallback(); document.removeEventListener('mouseup', this._disableDrag) document.removeEventListener('mousemove', this._resize); document.body.style['userSelect'] = ''; } _enableDrag(e) { document.addEventListener('mouseup', this._disableDrag) document.addEventListener('mousemove', this._resize); document.body.style['userSelect'] = 'none'; this._startY = e.clientY; this._topHeight = this.top.offsetHeight; this.dragging = true; } _disableDrag() { document.removeEventListener('mouseup', this._disableDrag) document.removeEventListener('mousemove', this._resize); document.body.style['userSelect'] = ''; this.dragging = false; let finalHeight; if (this.lateResize && this._bufferedDelta) { finalHeight = this._topHeight + this._bufferedDelta; this.splitter.style.transform = ''; this.style.gridTemplateRows = `${finalHeight}px 5px 1fr`; this._bufferedDelta = 0; } else { finalHeight = this.top.offsetHeight; } this.dispatchEvent(new CustomEvent('resize-done', { detail: finalHeight, bubbles: true, composed: true })); } _resize(e) { const delta = e.clientY - this._startY; const totalHeight = this.offsetHeight; const minHeight = this.minSideHeight || 0; const splitterWidth = parseInt(getComputedStyle(this).getPropertyValue('--tp-splitter-width')) || 5; // Calculate restricted delta const maxDelta = totalHeight - splitterWidth - minHeight - this._topHeight; const minDelta = -this._topHeight + minHeight; const restrictedDelta = Math.max(minDelta, Math.min(delta, maxDelta)); if (this.lateResize) { this._bufferedDelta = restrictedDelta; this.splitter.style.transform = `translateY(${this._bufferedDelta}px)`; return; } const newTopHeight = this._topHeight + restrictedDelta; this.style.gridTemplateRows = `${newTopHeight}px ${splitterWidth}px 1fr`; this.dispatchEvent(new CustomEvent('resized', { detail: null, bubbles: true, composed: true })); } } window.customElements.define('tp-hsplitter', TpHSplitter);