summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/compose/content/dialogs/EdHLineProps.js
blob: 4a5393d1dc611e9840abe2c0bf909fb33e024813 (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* 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 EdDialogCommon.js */

var tagName = "hr";
var gHLineElement;
var width;
var height;
var align;
var shading;
const gMaxHRSize = 1000; // This is hard-coded in nsHTMLHRElement::StringToAttribute()

// dialog initialization code

document.addEventListener("dialogaccept", onAccept);
document.addEventListener("dialogcancel", onCancel);

function Startup() {
  var editor = GetCurrentEditor();
  if (!editor) {
    window.close();
    return;
  }
  try {
    // Get the selected horizontal line
    gHLineElement = editor.getSelectedElement(tagName);
  } catch (e) {}

  if (!gHLineElement) {
    // We should never be here if not editing an existing HLine
    window.close();
    return;
  }
  gDialog.heightInput = document.getElementById("height");
  gDialog.widthInput = document.getElementById("width");
  gDialog.leftAlign = document.getElementById("leftAlign");
  gDialog.centerAlign = document.getElementById("centerAlign");
  gDialog.rightAlign = document.getElementById("rightAlign");
  gDialog.alignGroup = gDialog.rightAlign.radioGroup;
  gDialog.shading = document.getElementById("3dShading");
  gDialog.pixelOrPercentMenulist = document.getElementById(
    "pixelOrPercentMenulist"
  );

  // Make a copy to use for AdvancedEdit and onSaveDefault
  globalElement = gHLineElement.cloneNode(false);

  // Initialize control values based on existing attributes
  InitDialog();

  // SET FOCUS TO FIRST CONTROL
  SetTextboxFocus(gDialog.widthInput);

  // Resize window
  window.sizeToContent();

  SetWindowLocation();
}

// Set dialog widgets with attribute data
// We get them from globalElement copy so this can be used
//   by AdvancedEdit(), which is shared by all property dialogs
function InitDialog() {
  // Just to be confusing, "size" is used instead of height because it does
  // not accept % values, only pixels
  var height = GetHTMLOrCSSStyleValue(globalElement, "size", "height");
  if (height.includes("px")) {
    height = height.substr(0, height.indexOf("px"));
  }
  if (!height) {
    height = 2; // Default value
  }

  // We will use "height" here and in UI
  gDialog.heightInput.value = height;

  // Get the width attribute of the element, stripping out "%"
  // This sets contents of menulist (adds pixel and percent menuitems elements)
  gDialog.widthInput.value = InitPixelOrPercentMenulist(
    globalElement,
    gHLineElement,
    "width",
    "pixelOrPercentMenulist"
  );

  var marginLeft = GetHTMLOrCSSStyleValue(
    globalElement,
    "align",
    "margin-left"
  ).toLowerCase();
  var marginRight = GetHTMLOrCSSStyleValue(
    globalElement,
    "align",
    "margin-right"
  ).toLowerCase();
  align = marginLeft + " " + marginRight;
  gDialog.leftAlign.checked = align == "left left" || align == "0px auto";
  gDialog.centerAlign.checked =
    align == "center center" || align == "auto auto" || align == " ";
  gDialog.rightAlign.checked = align == "right right" || align == "auto 0px";

  if (gDialog.centerAlign.checked) {
    gDialog.alignGroup.selectedItem = gDialog.centerAlign;
  } else if (gDialog.rightAlign.checked) {
    gDialog.alignGroup.selectedItem = gDialog.rightAlign;
  } else {
    gDialog.alignGroup.selectedItem = gDialog.leftAlign;
  }

  gDialog.shading.checked = !globalElement.hasAttribute("noshade");
}

function onSaveDefault() {
  // "false" means set attributes on the globalElement,
  //   not the real element being edited
  if (ValidateData()) {
    var alignInt;
    if (align == "left") {
      alignInt = 0;
    } else if (align == "right") {
      alignInt = 2;
    } else {
      alignInt = 1;
    }
    Services.prefs.setIntPref("editor.hrule.align", alignInt);

    var percent;
    var widthInt;
    var heightInt;

    if (width) {
      if (width.includes("%")) {
        percent = true;
        widthInt = Number(width.substr(0, width.indexOf("%")));
      } else {
        percent = false;
        widthInt = Number(width);
      }
    } else {
      percent = true;
      widthInt = Number(100);
    }

    heightInt = height ? Number(height) : 2;

    Services.prefs.setIntPref("editor.hrule.width", widthInt);
    Services.prefs.setBoolPref("editor.hrule.width_percent", percent);
    Services.prefs.setIntPref("editor.hrule.height", heightInt);
    Services.prefs.setBoolPref("editor.hrule.shading", shading);

    // Write the prefs out NOW!
    Services.prefs.savePrefFile(null);
  }
}

// Get and validate data from widgets.
// Set attributes on globalElement so they can be accessed by AdvancedEdit()
function ValidateData() {
  // Height is always pixels
  height = ValidateNumber(
    gDialog.heightInput,
    null,
    1,
    gMaxHRSize,
    globalElement,
    "size",
    false
  );
  if (gValidationError) {
    return false;
  }

  width = ValidateNumber(
    gDialog.widthInput,
    gDialog.pixelOrPercentMenulist,
    1,
    gMaxPixels,
    globalElement,
    "width",
    false
  );
  if (gValidationError) {
    return false;
  }

  align = "left";
  if (gDialog.centerAlign.selected) {
    // Don't write out default attribute
    align = "";
  } else if (gDialog.rightAlign.selected) {
    align = "right";
  }
  if (align) {
    globalElement.setAttribute("align", align);
  } else {
    try {
      GetCurrentEditor().removeAttributeOrEquivalent(
        globalElement,
        "align",
        true
      );
    } catch (e) {}
  }

  if (gDialog.shading.checked) {
    shading = true;
    globalElement.removeAttribute("noshade");
  } else {
    shading = false;
    globalElement.setAttribute("noshade", "noshade");
  }
  return true;
}

function onAccept(event) {
  if (ValidateData()) {
    // Copy attributes from the globalElement to the document element
    try {
      GetCurrentEditor().cloneAttributes(gHLineElement, globalElement);
    } catch (e) {}
    return;
  }
  event.preventDefault();
}