136 lines
3.5 KiB
JavaScript
136 lines
3.5 KiB
JavaScript
/**
|
|
@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 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`
|
|
<div id="top" part="top" ?hidden=${topHidden}><slot name="top"></slot></div>
|
|
<div id="splitter" part="splitter" @mousedown=${this._enableDrag} ?hidden=${topHidden || bottomHidden}><slot name="splitter"></slot></div>
|
|
<div id="bottom" part="bottom" ?hidden=${bottomHidden}><slot name="bottom"></slot></div>
|
|
`;
|
|
}
|
|
|
|
static get properties() {
|
|
return {
|
|
topHidden: { type: Boolean, reflect: true },
|
|
bottomHidden: { type: Boolean, reflect: true },
|
|
lateResize: { type: Boolean },
|
|
rows: { type: String },
|
|
dragging: { type: Boolean, reflect: true },
|
|
};
|
|
}
|
|
|
|
constructor() {
|
|
super();
|
|
|
|
this._disableDrag = this._disableDrag.bind(this);
|
|
this._resize = this._resize.bind(this);
|
|
this._bufferedDelta = 0;
|
|
}
|
|
|
|
shouldUpdate(changes) {
|
|
if (changes.has('rows')) {
|
|
this.style.gridTemplateRows = this.rows;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
get top() {
|
|
return this.shadowRoot.querySelector('#top');
|
|
}
|
|
|
|
get bottom() {
|
|
return this.shadowRoot.querySelector('#bottom');
|
|
}
|
|
|
|
get splitter() {
|
|
return this.shadowRoot.querySelector('#splitter')
|
|
}
|
|
|
|
_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;
|
|
|
|
if (this.lateResize && this._bufferedDelta) {
|
|
this.splitter.style.transform = '';
|
|
this.style.gridTemplateRows = `${this._topHeight + this._bufferedDelta}px 5px 1fr`;
|
|
this._bufferedDelta = 0;
|
|
}
|
|
|
|
this.dispatchEvent(new CustomEvent('resize-done', { detail: this.style.gridTemplateRows, bubbles: true, composed: true }));
|
|
}
|
|
|
|
_resize(e) {
|
|
if (this.lateResize) {
|
|
this._bufferedDelta = e.clientY - this._startY;
|
|
this.splitter.style.transform = `translateY(${this._bufferedDelta}px)`;
|
|
return;
|
|
}
|
|
const delta = e.clientY - this._startY;
|
|
this.style.gridTemplateRows = `${this._topHeight + delta}px 5px 1fr`;
|
|
this.dispatchEvent(new CustomEvent('resized', { detail: null, bubbles: true, composed: true }));
|
|
}
|
|
}
|
|
|
|
window.customElements.define('tp-hsplitter', TpHSplitter);
|