Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 92x 95x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 98x 98x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 12x 12x 12x 12x 12x 92x 22x 22x 22x 22x 22x 22x 22x 19x 19x 19x 6x 6x 6x 19x 19x 92x 22x 22x 92x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 92x 5x 92x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 92x 2x 2x 2x 2x 2x 2x 2x 92x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 92x 92x 92x 92x 92x 92x 92x 92x 92x | import { css, html, PropertyValues, TemplateResult } from 'lit';
import { styleMap } from 'lit-html/directives/style-map.js';
import { property } from 'lit/decorators.js';
import { FieldElement } from './FieldElement';
import { getClasses } from '../utils';
export class TembaSlider extends FieldElement {
static get styles() {
return css`
:host {
display: block;
}
.track {
height: 2px;
border-top: 0.5em solid #fff;
border-bottom: 0.5em solid #fff;
background: #ddd;
flex-grow: 1;
}
.circle {
margin-bottom: -1.05em;
margin-left: -0.5em;
width: 0.75em;
height: 0.75em;
border: 2px solid #999;
border-radius: 999px;
position: relative;
background: #fff;
box-shadow: 0 0 0 4px rgb(255, 255, 255);
transition: transform 200ms ease-in-out;
}
.grabbed .track {
cursor: pointer;
}
:hover .circle {
border-color: #777;
cursor: pointer;
}
.grabbed .circle {
border-color: var(--color-primary-dark);
background: #fff;
}
.grabbed .circle {
transform: scale(1.2);
}
.wrapper {
display: flex;
align-items: center;
}
.pre,
.post {
font-size: 0.9em;
color: #999;
padding: 0em 1em;
}
`;
}
@property({ type: Boolean })
range = false;
@property({ type: Number })
min = 0;
@property({ type: Number })
max = 100;
circleX = 0;
grabbed = false;
public firstUpdated(changes: Map<string, any>) {
super.firstUpdated(changes);
this.handleMouseMove = this.handleMouseMove.bind(this);
this.handleMouseUp = this.handleMouseUp.bind(this);
}
public willUpdate(changedProperties: PropertyValues): void {
super.willUpdate(changedProperties);
if (changedProperties.has('value')) {
// Normalize value to min/max bounds
let cValue = parseInt(this.value);
if (!cValue || cValue < this.min) {
cValue = this.min;
} else if (cValue > this.max) {
cValue = this.max;
}
this.value = '' + cValue;
}
}
public updated(changedProperties: Map<string, any>): void {
if (changedProperties.has('value')) {
this.updateCircle();
}
}
public updateValue(evt: MouseEvent) {
const track = this.shadowRoot.querySelector('.track') as HTMLDivElement;
const left = evt.pageX - track.offsetLeft;
const pct = left / track.offsetWidth;
const range = this.max - this.min;
const pctAsValue = range * pct + this.min;
this.value =
'' + Math.max(this.min, Math.min(Math.round(pctAsValue), this.max));
}
public handleMouseMove(evt: MouseEvent) {
if (this.grabbed) {
this.updateValue(evt);
}
}
public handleTrackDown(evt: MouseEvent) {
this.grabbed = true;
document.addEventListener('mousemove', this.handleMouseMove);
document.addEventListener('mouseup', this.handleMouseUp);
document.querySelector('html').classList.add('dragging');
this.updateValue(evt);
this.requestUpdate();
}
public handleMouseUp(evt: MouseEvent) {
this.grabbed = false;
this.updateValue(evt);
this.requestUpdate();
document.removeEventListener('mousemove', this.handleMouseMove);
document.removeEventListener('mouseup', this.handleMouseUp);
document.querySelector('html').classList.remove('dragging');
}
public updateCircle() {
const track = this.shadowRoot.querySelector('.track') as HTMLDivElement;
const pre = this.shadowRoot.querySelector('.pre') as HTMLDivElement;
const circle = this.shadowRoot.querySelector('.circle') as HTMLDivElement;
const range = this.max - this.min;
const cValue = parseInt(this.value) || this.min;
const pct = (cValue - this.min) / range;
const pctAsPixels = track.offsetWidth * pct;
this.circleX = Math.round(pctAsPixels + (pre ? pre.offsetWidth : 0));
// directly update DOM to avoid scheduling another update
circle.style.left = this.circleX + 'px';
}
public renderWidget(): TemplateResult {
return html` <div class="${getClasses({ grabbed: this.grabbed })}">
<div
style=${styleMap({ left: this.circleX + 'px' })}
class="circle"
@mousedown=${this.handleTrackDown}
></div>
<div class="wrapper">
${this.range ? html`<div class="pre">${this.min}</div>` : null}
<div class="track" @mousedown=${this.handleTrackDown}></div>
${this.range ? html`<div class="post">${this.max}</div>` : null}
</div>
</div>`;
}
}
|