summaryrefslogtreecommitdiffstats
path: root/devtools/client/webaudioeditor/views/utils.js
blob: 6d6a96946398c8d93b847200f64b7df747fdbc97 (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
101
102
103
/* 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";

/**
 * Takes an element in an SVG graph and iterates over
 * ancestors until it finds the graph node container. If not found,
 * returns null.
 */

function findGraphNodeParent(el) {
  // Some targets may not contain `classList` property
  if (!el.classList)
    return null;

  while (!el.classList.contains("nodes")) {
    if (el.classList.contains("audionode"))
      return el;
    else
      el = el.parentNode;
  }
  return null;
}

/**
 * Object for use with `mix` into a view.
 * Must have the following properties defined on the view:
 * - `el`
 * - `button`
 * - `_collapseString`
 * - `_expandString`
 * - `_toggleEvent`
 *
 * Optional properties on the view can be defined to specify default
 * visibility options.
 * - `_animated`
 * - `_delayed`
 */
var ToggleMixin = {

  bindToggle: function () {
    this._onToggle = this._onToggle.bind(this);
    this.button.addEventListener("mousedown", this._onToggle, false);
  },

  unbindToggle: function () {
    this.button.removeEventListener("mousedown", this._onToggle);
  },

  show: function () {
    this._viewController({ visible: true });
  },

  hide: function () {
    this._viewController({ visible: false });
  },

  hideImmediately: function () {
    this._viewController({ visible: false, delayed: false, animated: false });
  },

  /**
   * Returns a boolean indicating whether or not the view.
   * is currently being shown.
   */
  isVisible: function () {
    return !this.el.classList.contains("pane-collapsed");
  },

  /**
   * Toggles the visibility of the view.
   *
   * @param object visible
   *        - visible: boolean indicating whether the panel should be shown or not
   *        - animated: boolean indiciating whether the pane should be animated
   *        - delayed: boolean indicating whether the pane's opening should wait
   *                   a few cycles or not
   */
  _viewController: function ({ visible, animated, delayed }) {
    let flags = {
      visible: visible,
      animated: animated != null ? animated : !!this._animated,
      delayed: delayed != null ? delayed : !!this._delayed,
      callback: () => window.emit(this._toggleEvent, visible)
    };

    ViewHelpers.togglePane(flags, this.el);

    if (flags.visible) {
      this.button.classList.remove("pane-collapsed");
      this.button.setAttribute("tooltiptext", this._collapseString);
    }
    else {
      this.button.classList.add("pane-collapsed");
      this.button.setAttribute("tooltiptext", this._expandString);
    }
  },

  _onToggle: function () {
    this._viewController({ visible: !this.isVisible() });
  }
};