summaryrefslogtreecommitdiffstats
path: root/toolkit/content/aboutwebrtc/configurationList.mjs
blob: d9c209b9df6337244008393c9bec95d543baf3b2 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* 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/. */

import { CopyButton } from "chrome://global/content/aboutwebrtc/copyButton.mjs";

function getPref(path) {
  switch (Services.prefs.getPrefType(path)) {
    case Services.prefs.PREF_BOOL:
      return Services.prefs.getBoolPref(path);
    case Services.prefs.PREF_INT:
      return Services.prefs.getIntPref(path);
    case Services.prefs.PREF_STRING:
      return Services.prefs.getStringPref(path);
  }
  return "";
}

/*
 * This provides a visual list of configuration settings given an array of
 * configuration paths. To change the list one can call setPrefPaths.
 */
class ConfigurationList {
  constructor(aPreferencePaths) {
    this.list = document.createElement("list");
    this.list.classList.add("prefList");
    this.setPrefPaths(aPreferencePaths);
  }

  /** @return {Element} */
  view() {
    return this.list;
  }

  /**
   * @return {Element[]}
   */
  getPrefListItems() {
    return [...this.list.children].flatMap(e =>
      e.dataset.prefPath !== undefined ? [e] : []
    );
  }

  /**
   * @return {string[]}
   */
  getPrefPaths() {
    return [...this.getPrefListItems()].map(e => e.dataset.prefPath);
  }

  //     setPrefPaths adds and removes list items from the list and updates
  //     existing elements

  setPrefPaths(aPreferencePaths) {
    const currentPaths = this.getPrefPaths();
    // Take the difference of the two arrays of preferences. There are three
    // groups of paths: those removed from the current list, those to remain
    // in the current list, and those to be added.
    const { kept: keptPaths, removed: removedPaths } = currentPaths.reduce(
      (acc, p) => {
        if (aPreferencePaths.includes(p)) {
          acc.kept.push(p);
        } else {
          acc.removed.push(p);
        }
        return acc;
      },
      { removed: [], kept: [] }
    );

    const addedPaths = aPreferencePaths.filter(p => !keptPaths.includes(p));

    // Remove items
    this.getPrefListItems()
      .filter(e => removedPaths.includes(e.dataset.prefPath))
      .forEach(e => e.remove() /* Remove from DOM*/);

    const addItemForPath = path => {
      const item = document.createElement("li");
      item.dataset.prefPath = path;

      item.appendChild(new CopyButton(() => path).element);

      const pathSpan = document.createElement("span");
      pathSpan.textContent = path;
      pathSpan.classList.add(["pathDisplay"]);
      item.appendChild(pathSpan);

      const valueSpan = document.createElement("span");
      valueSpan.classList.add(["valueDisplay"]);
      item.appendChild(valueSpan);

      this.list.appendChild(item);
    };

    // Add items
    addedPaths.forEach(addItemForPath);

    // Update all pref values
    this.updatePrefValues();
  }

  updatePrefValues() {
    for (const e of this.getPrefListItems()) {
      const value = getPref(e.dataset.prefPath);
      const valueSpan = e.getElementsByClassName("valueDisplay").item(0);
      if ("prefPath" in e.dataset) {
        valueSpan.textContent = value;
      }
    }
  }

  update() {
    this.updatePrefValues();
  }
}

export { ConfigurationList };