Sha256: 6af15a49903edd4f28d456322d3394c715eda48ae1621397e206d7dcfa4837d5

Contents?: true

Size: 1.38 KB

Versions: 25

Compression:

Stored size: 1.38 KB

Contents

import { computeBoundingBox } from './util';

const ZINDEX = 2000000000;

export default class Specimen {
  constructor(element, key, callback) {
    this.element = element;
    this.key = key;
    this.callback = callback;
  }

  show() {
    this.box = this.makeBox();
    if (this.box === null) return;

    this.box.addEventListener('click', () => {
      this.callback(this.key);
    });

    document.body.appendChild(this.box);
  }

  remove() {
    if (!this.box) {
      return;
    }
    this.box.remove();
    this.box = null;
  }

  makeBox() {
    const box = document.createElement('div');
    box.classList.add('copyray-specimen');
    box.classList.add('Specimen');

    const bounds = computeBoundingBox(this.element);
    if (bounds === null) return null;

    Object.keys(bounds).forEach((key) => {
      const value = bounds[key];
      box.style[key] = `${value}px`;
    });
    box.style.zIndex = ZINDEX;

    const { position, top, left } = getComputedStyle(this.element);
    if (position === 'fixed') {
      this.box.style.position = 'fixed';
      this.box.style.top = `${top}px`;
      this.box.style.left = `${left}px`;
    }

    box.appendChild(this.makeLabel());
    return box;
  }

  makeLabel() {
    const div = document.createElement('div');
    div.classList.add('copyray-specimen-handle');
    div.classList.add('Specimen');
    div.textContent = this.key;
    return div;
  }
}

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
copy_tuner_client-0.12.0 src/specimen.js
copy_tuner_client-0.11.0 src/specimen.js
copy_tuner_client-0.10.0 src/specimen.js
copy_tuner_client-0.9.0 src/specimen.js
copy_tuner_client-0.8.1 src/specimen.js
copy_tuner_client-0.8.0 src/specimen.js
copy_tuner_client-0.7.0 src/specimen.js
copy_tuner_client-0.6.2 src/specimen.js
copy_tuner_client-0.6.1 src/specimen.js
copy_tuner_client-0.5.3 src/specimen.js
copy_tuner_client-0.5.2 src/specimen.js
copy_tuner_client-0.5.1 src/specimen.js
copy_tuner_client-0.5.0 src/specimen.js
copy_tuner_client-0.5.0.pre src/specimen.js
copy_tuner_client-0.4.12 src/specimen.js
copy_tuner_client-0.4.11 src/specimen.js
copy_tuner_client-0.4.10 src/specimen.js
copy_tuner_client-0.4.9 src/specimen.js
copy_tuner_client-0.4.8 src/specimen.js
copy_tuner_client-0.4.7 src/specimen.js