summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/compose/content/dialogs/EdAEJSEAttributes.js
blob: 8f902b74cd13a5b7772fc05f257c7dcba3678b95 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* 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-globals-from ../editorUtilities.js */
/* import-globals-from EdAdvancedEdit.js */
/* import-globals-from EdDialogCommon.js */

function BuildJSEAttributeNameList() {
  gDialog.AddJSEAttributeNameList.removeAllItems();

  // Get events specific to current element
  var elementName = gElement.localName;
  if (elementName in gJSAttr) {
    var attNames = gJSAttr[elementName];
    var i;
    var popup;
    var sep;

    if (attNames && attNames.length) {
      // Since we don't allow user-editable JS events yet (but we will soon)
      //  simply remove the JS tab to not allow adding JS events
      if (attNames[0] == "noJSEvents") {
        var tab = document.getElementById("tabJSE");
        if (tab) {
          tab.remove();
        }

        return;
      }

      for (i = 0; i < attNames.length; i++) {
        gDialog.AddJSEAttributeNameList.appendItem(attNames[i], attNames[i]);
      }

      popup = gDialog.AddJSEAttributeNameList.firstElementChild;
      if (popup) {
        sep = document.createXULElement("menuseparator");
        if (sep) {
          popup.appendChild(sep);
        }
      }
    }
  }

  // Always add core JS events unless we aborted above
  for (i = 0; i < gCoreJSEvents.length; i++) {
    if (gCoreJSEvents[i] == "-") {
      if (!popup) {
        popup = gDialog.AddJSEAttributeNameList.firstElementChild;
      }

      sep = document.createXULElement("menuseparator");

      if (popup && sep) {
        popup.appendChild(sep);
      }
    } else {
      gDialog.AddJSEAttributeNameList.appendItem(
        gCoreJSEvents[i],
        gCoreJSEvents[i]
      );
    }
  }

  gDialog.AddJSEAttributeNameList.selectedIndex = 0;

  // Use current name and value of first tree item if it exists
  onSelectJSETreeItem();
}

// build attribute list in tree form from element attributes
function BuildJSEAttributeTable() {
  var nodeMap = gElement.attributes;
  if (nodeMap.length > 0) {
    var added = false;
    for (var i = 0; i < nodeMap.length; i++) {
      let name = nodeMap[i].nodeName.toLowerCase();
      if (CheckAttributeNameSimilarity(nodeMap[i].nodeName, JSEAttrs)) {
        // Repeated or non-JS handler, ignore this one and go to next.
        continue;
      }
      if (!name.startsWith("on")) {
        // Attribute isn't an event handler.
        continue;
      }
      var value = gElement.getAttribute(nodeMap[i].nodeName);
      if (AddTreeItem(name, value, "JSEAList", JSEAttrs)) {
        // add item to tree
        added = true;
      }
    }

    // Select first item
    if (added) {
      gDialog.AddJSEAttributeTree.selectedIndex = 0;
    }
  }
}

function onSelectJSEAttribute() {
  if (!gDoOnSelectTree) {
    return;
  }

  gDialog.AddJSEAttributeValueInput.value = GetAndSelectExistingAttributeValue(
    gDialog.AddJSEAttributeNameList.label,
    "JSEAList"
  );
}

function onSelectJSETreeItem() {
  var tree = gDialog.AddJSEAttributeTree;
  if (tree && tree.view.selection.count) {
    // Select attribute name in list
    gDialog.AddJSEAttributeNameList.value = GetTreeItemAttributeStr(
      getSelectedItem(tree)
    );

    // Set value input to that in tree (no need to update this in the tree)
    gUpdateTreeValue = false;
    gDialog.AddJSEAttributeValueInput.value = GetTreeItemValueStr(
      getSelectedItem(tree)
    );
    gUpdateTreeValue = true;
  }
}

function onInputJSEAttributeValue() {
  if (gUpdateTreeValue) {
    var name = TrimString(gDialog.AddJSEAttributeNameList.label);
    var value = TrimString(gDialog.AddJSEAttributeValueInput.value);

    // Update value in the tree list
    // Since we have a non-editable menulist,
    //   we MUST automatically add the event attribute if it doesn't exist
    if (!UpdateExistingAttribute(name, value, "JSEAList") && value) {
      AddTreeItem(name, value, "JSEAList", JSEAttrs);
    }
  }
}

function editJSEAttributeValue(targetCell) {
  if (IsNotTreeHeader(targetCell)) {
    gDialog.AddJSEAttributeValueInput.select();
  }
}

function UpdateJSEAttributes() {
  var JSEAList = document.getElementById("JSEAList");
  var i;

  // remove removed attributes
  for (i = 0; i < JSERAttrs.length; i++) {
    var name = JSERAttrs[i];

    if (gElement.hasAttribute(name)) {
      doRemoveAttribute(name);
    }
  }

  // Add events
  for (i = 0; i < JSEAList.children.length; i++) {
    var item = JSEAList.children[i];

    // set the event handler
    doSetAttribute(GetTreeItemAttributeStr(item), GetTreeItemValueStr(item));
  }
}

function RemoveJSEAttribute() {
  // This differs from HTML and CSS panels:
  // We reselect after removing, because there is not
  //  editable attribute name input, so we can't clear that
  //  like we do in other panels
  var newIndex = gDialog.AddJSEAttributeTree.selectedIndex;

  // We only allow 1 selected item
  if (gDialog.AddJSEAttributeTree.view.selection.count) {
    var item = getSelectedItem(gDialog.AddJSEAttributeTree);

    // Name is the text of the treecell
    var attr = GetTreeItemAttributeStr(item);

    // remove the item from the attribute array
    if (newIndex >= JSEAttrs.length - 1) {
      newIndex--;
    }

    // remove the item from the attribute array
    JSERAttrs[JSERAttrs.length] = attr;
    RemoveNameFromAttArray(attr, JSEAttrs);

    // Remove the item from the tree
    item.remove();

    // Reselect an item
    gDialog.AddJSEAttributeTree.selectedIndex = newIndex;
  }
}