summaryrefslogtreecommitdiffstats
path: root/comm/chat/content/imAccountOptionsHelper.js
blob: cbe8c486d8cb4615e07c67abf030d465bd4dc2eb (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
119
120
121
/* 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/. */

var accountOptionsHelper = {
  /**
   * Create a new label and a corresponding input.
   *
   * @param {string} aType - The input type ("number" or "text").
   * @param {string} aValue - The initial value for the input.
   * @param {string} aLabel - The text for the label.
   * @param {string} aName - The id for the input.
   * @param {Element} grid - A container with a two column grid display to
   *   append the new elements to.
   */
  createTextbox(aType, aValue, aLabel, aName, grid) {
    let label = document.createXULElement("label");
    label.textContent = aLabel;
    label.setAttribute("control", aName);
    label.classList.add("label-inline");
    grid.appendChild(label);

    let input = document.createElementNS(
      "http://www.w3.org/1999/xhtml",
      "input"
    );
    if (aType == "number") {
      input.classList.add("input-number-inline");
    } else {
      input.classList.add("input-inline");
    }
    if (aType) {
      input.setAttribute("type", aType);
    }
    input.setAttribute("value", aValue);
    input.setAttribute("id", aName);

    grid.appendChild(input);
  },

  /**
   * Create a new label and a corresponding menulist.
   *
   * @param {object[]} aList - The list of items to fill the menulist with.
   * @param {string} aList[].label - The label for the menuitem.
   * @param {string} aList[].value - The value for the menuitem.
   * @param {string} aLabel - The text for the label.
   * @param {string} aName - The id for the menulist.
   * @param {Element} grid - A container with a two column grid display to
   *   append the new elements to.
   */
  createMenulist(aList, aLabel, aName, grid) {
    let label = document.createXULElement("label");
    label.setAttribute("value", aLabel);
    label.setAttribute("control", aName);
    label.classList.add("label-inline");
    grid.appendChild(label);

    let menulist = document.createXULElement("menulist");
    menulist.setAttribute("id", aName);
    menulist.setAttribute("flex", "1");
    menulist.classList.add("input-inline");
    let popup = menulist.appendChild(document.createXULElement("menupopup"));
    for (let elt of aList) {
      let item = document.createXULElement("menuitem");
      item.setAttribute("label", elt.name);
      item.setAttribute("value", elt.value);
      popup.appendChild(item);
    }
    grid.appendChild(menulist);
  },

  // Adds options with specific prefix for ids to UI according to their types
  // with optional attributes for each type and returns true if at least one
  // option has been added to UI, otherwise returns false.
  addOptions(aIdPrefix, aOptions, aAttributes) {
    let grid = document.getElementById("protoSpecific");
    while (grid.hasChildNodes()) {
      grid.lastChild.remove();
    }

    let haveOptions = false;
    for (let opt of aOptions) {
      let text = opt.label;
      let name = aIdPrefix + opt.name;
      switch (opt.type) {
        case Ci.prplIPref.typeBool:
          let chk = document.createXULElement("checkbox");
          chk.setAttribute("label", text);
          chk.setAttribute("id", name);
          if (opt.getBool()) {
            chk.setAttribute("checked", "true");
          }
          // Span two columns.
          chk.classList.add("grid-item-span-row");
          grid.appendChild(chk);
          break;
        case Ci.prplIPref.typeInt:
          this.createTextbox("number", opt.getInt(), text, name, grid);
          break;
        case Ci.prplIPref.typeString:
          this.createTextbox("text", opt.getString(), text, name, grid);
          break;
        case Ci.prplIPref.typeList:
          this.createMenulist(opt.getList(), text, name, grid);
          document.getElementById(name).value = opt.getListDefault();
          break;
        default:
          throw new Error("unknown preference type " + opt.type);
      }
      if (aAttributes && aAttributes[opt.type]) {
        let element = document.getElementById(name);
        for (let attr of aAttributes[opt.type]) {
          element.setAttribute(attr.name, attr.value);
        }
      }
      haveOptions = true;
    }
    return haveOptions;
  },
};