popover.js 2.7 KB
Newer Older
K
Kamran Ahmed 已提交
1
import Element from './element';
K
Kamran Ahmed 已提交
2 3 4 5 6 7 8 9
import {
  CLASS_POPOVER_DESCRIPTION,
  CLASS_POPOVER_TIP,
  CLASS_POPOVER_TITLE,
  ID_POPOVER,
  OVERLAY_PADDING,
  POPOVER_HTML,
} from './constants';
K
Kamran Ahmed 已提交
10

11 12 13 14 15
/**
 * Popover that is displayed on top of the highlighted element
 */
export default class Popover extends Element {
  constructor(options = {
16
    padding: OVERLAY_PADDING,
17 18 19 20 21 22 23
  }, window, document) {
    super();

    this.options = options;
    this.window = window;
    this.document = document;

K
Kamran Ahmed 已提交
24
    this.node = this.makeNode();
25
    this.hide();
26 27
  }

K
Kamran Ahmed 已提交
28
  makeNode() {
29 30 31 32 33 34 35
    let popover = this.document.getElementById(ID_POPOVER);
    if (popover) {
      return popover;
    }

    popover = Popover.createFromString(POPOVER_HTML);
    document.body.appendChild(popover);
36 37 38 39

    return popover;
  }

40 41 42 43 44 45 46 47 48 49 50 51 52
  /**
   * Turn a string into a node
   * @param  {String} htmlString to convert
   * @return {Node}   Converted node element
   */
  static createFromString(htmlString) {
    const div = document.createElement('div');
    div.innerHTML = htmlString.trim();

    // Change this to div.childNodes to support multiple top-level nodes
    return div.firstChild;
  }

53 54 55 56 57 58 59 60 61 62
  getHeight() {
    return Math.max(this.node.scrollHeight, this.node.offsetHeight);
  }

  hide() {
    this.node.style.display = 'none';
  }

  reset() {
    this.node.style.display = 'block';
63 64
    this.node.style.left = '0';
    this.node.style.top = '0';
65 66 67 68 69
    this.node.style.bottom = '';
    this.node.style.right = '';

    // Remove the positional classes from tip
    this.node
K
Kamran Ahmed 已提交
70 71
      .querySelector(`.${CLASS_POPOVER_TIP}`)
      .className = CLASS_POPOVER_TIP;
72 73 74 75 76
  }

  show(position) {
    this.reset();

77
    const popoverTip = this.node.querySelector(`.${CLASS_POPOVER_TIP}`);
K
Kamran Ahmed 已提交
78 79 80 81 82 83 84
    const popoverTitle = this.node.querySelector(`.${CLASS_POPOVER_TITLE}`);
    const popoverDescription = this.node.querySelector(`.${CLASS_POPOVER_DESCRIPTION}`);

    popoverTitle.innerText = this.options.title;
    popoverDescription.innerText = this.options.description;

    const pageHeight = this.getFullPageSize().height;
85
    const popoverMargin = this.options.padding + 10;
86
    const popoverHeight = this.getHeight();
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

    this.node.style.left = `${position.left - this.options.padding}px`;

    // Calculate different dimensions after attaching popover
    const pageHeightAfterPopOver = position.bottom + popoverHeight + popoverMargin;

    // If adding popover would go out of the window height, then show it to the top
    if (pageHeightAfterPopOver >= pageHeight) {
      this.node.style.top = `${position.top - popoverHeight - popoverMargin}px`;
      popoverTip.classList.add('bottom');
    } else {
      this.node.style.top = `${position.bottom + popoverMargin}px`;
      popoverTip.classList.add('top');
    }
  }
}