summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/widgets/tooltip/InlineTooltip.js
blob: a99a1a94006ecec786d584a40f7f7f726baef92f (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const EventEmitter = require("devtools/shared/event-emitter");

/**
 * The InlineTooltip can display widgets for the CSS Rules view in an
 * inline container.
 *
 * @param {Document} doc
 *        The toolbox document to attach the InlineTooltip container.
 */
function InlineTooltip(doc) {
  EventEmitter.decorate(this);

  this.doc = doc;

  this.panel = this.doc.createElement("div");

  this.topWindow = this._getTopWindow();
}

InlineTooltip.prototype = {
  /**
   * Show the tooltip. It might be wise to append some content first if you
   * don't want the tooltip to be empty.
   *
   * @param {Node} anchor
   *        Which node below which the tooltip should be shown.
   */
  show(anchor) {
    anchor.parentNode.insertBefore(this.panel, anchor.nextSibling);

    this.emit("shown");
  },

  /**
   * Hide the current tooltip.
   */
  hide() {
    if (!this.isVisible()) {
      return;
    }

    this.panel.parentNode.remove(this.panel);

    this.emit("hidden");
  },

  /**
   * Check if the tooltip is currently displayed.
   *
   * @return {Boolean} true if the tooltip is visible
   */
  isVisible() {
    return (
      typeof this.panel.parentNode !== "undefined" &&
      this.panel.parentNode !== null
    );
  },

  /**
   * Clears the HTML content of the tooltip panel
   */
  clear() {
    this.panel.innerHTML = "";
  },

  /**
   * Set the content of this tooltip. Will first clear the tooltip and then
   * append the new content element.
   *
   * @param {DOMNode} content
   *        A node that can be appended in the tooltip
   */
  setContent(content) {
    this.clear();

    this.panel.appendChild(content);
  },

  get content() {
    return this.panel.firstChild;
  },

  _getTopWindow: function() {
    return this.doc.defaultView;
  },

  destroy() {
    this.hide();
    this.doc = null;
    this.panel = null;
  },
};

module.exports = InlineTooltip;