All files / src/form TembaSlider.ts

100% Statements 169/169
100% Branches 20/20
100% Functions 10/10
100% Lines 169/169

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 170125x 128x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 131x 131x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 12x 12x 12x 12x 12x 125x 22x 22x 22x 22x 22x 22x 22x 19x 19x 19x 6x 6x 6x 19x 19x 125x 22x 22x 125x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 125x 5x 125x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 125x 2x 2x 2x 2x 2x 2x 2x 125x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 125x 125x 125x 125x 125x 125x 125x 125x 125x  
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 var(--surface);
        border-bottom: 0.5em solid var(--surface);
        background: var(--border-strong);
        flex-grow: 1;
      }
 
      .circle {
        margin-bottom: -1.05em;
        margin-left: -0.5em;
        width: 0.75em;
        height: 0.75em;
        border: 2px solid var(--border-strong);
        border-radius: 999px;
        position: relative;
        background: var(--surface);
        box-shadow: 0 0 0 4px var(--surface);
        transition: transform 200ms ease-in-out;
      }
 
      .grabbed .track {
        cursor: pointer;
      }
 
      :hover .circle {
        border-color: var(--text-2);
        cursor: pointer;
      }
 
      .grabbed .circle {
        border-color: var(--accent);
        background: var(--surface);
      }
 
      .grabbed .circle {
        transform: scale(1.2);
      }
 
      .wrapper {
        display: flex;
        align-items: center;
      }
 
      .pre,
      .post {
        font-size: 0.9em;
        color: var(--text-3);
        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>`;
  }
}