summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/compose/content/dialogs/EdListProps.js
blob: c33efc9bb13e962eb055c5798c20a139272e9cba (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/* 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 */

// Cancel() is in EdDialogCommon.js
var gBulletStyleType = "";
var gNumberStyleType = "";
var gListElement;
var gOriginalListType = "";
var gListType = "";
var gMixedListSelection = false;
var gStyleType = "";
var gOriginalStyleType = "";
const gOnesArray = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"];
const gTensArray = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"];
const gHundredsArray = [
  "",
  "C",
  "CC",
  "CCC",
  "CD",
  "D",
  "DC",
  "DCC",
  "DCCC",
  "CM",
];
const gThousandsArray = [
  "",
  "M",
  "MM",
  "MMM",
  "MMMM",
  "MMMMM",
  "MMMMMM",
  "MMMMMMM",
  "MMMMMMMM",
  "MMMMMMMMM",
];
const gRomanDigits = { I: 1, V: 5, X: 10, L: 50, C: 100, D: 500, M: 1000 };
const A = "A".charCodeAt(0);
const gArabic = "1";
const gUpperRoman = "I";
const gLowerRoman = "i";
const gUpperLetters = "A";
const gLowerLetters = "a";
const gDecimalCSS = "decimal";
const gUpperRomanCSS = "upper-roman";
const gLowerRomanCSS = "lower-roman";
const gUpperAlphaCSS = "upper-alpha";
const gLowerAlphaCSS = "lower-alpha";

// dialog initialization code

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

function Startup() {
  var editor = GetCurrentEditor();
  if (!editor) {
    window.close();
    return;
  }
  gDialog.ListTypeList = document.getElementById("ListType");
  gDialog.BulletStyleList = document.getElementById("BulletStyle");
  gDialog.BulletStyleLabel = document.getElementById("BulletStyleLabel");
  gDialog.StartingNumberInput = document.getElementById("StartingNumber");
  gDialog.StartingNumberLabel = document.getElementById("StartingNumberLabel");
  gDialog.AdvancedEditButton = document.getElementById("AdvancedEditButton1");
  gDialog.RadioGroup = document.getElementById("RadioGroup");
  gDialog.ChangeAllRadio = document.getElementById("ChangeAll");
  gDialog.ChangeSelectedRadio = document.getElementById("ChangeSelected");

  // Try to get an existing list(s)
  var mixedObj = { value: null };
  try {
    gListType = editor.getListState(mixedObj, {}, {}, {});

    // We may have mixed list and non-list, or > 1 list type in selection
    gMixedListSelection = mixedObj.value;

    // Get the list element at the anchor node
    gListElement = editor.getElementOrParentByTagName("list", null);
  } catch (e) {}

  // The copy to use in AdvancedEdit
  if (gListElement) {
    globalElement = gListElement.cloneNode(false);
  }

  // Show extra options for changing entire list if we have one already.
  gDialog.RadioGroup.collapsed = !gListElement;
  if (gListElement) {
    // Radio button index is persistent
    if (gDialog.RadioGroup.getAttribute("index") == "1") {
      gDialog.RadioGroup.selectedItem = gDialog.ChangeSelectedRadio;
    } else {
      gDialog.RadioGroup.selectedItem = gDialog.ChangeAllRadio;
    }
  }

  InitDialog();

  gOriginalListType = gListType;

  gDialog.ListTypeList.focus();

  SetWindowLocation();
}

function InitDialog() {
  // Note that if mixed, we we pay attention
  //   only to the anchor node's list type
  // (i.e., don't confuse user with "mixed" designation)
  if (gListElement) {
    gListType = gListElement.nodeName.toLowerCase();
  } else {
    gListType = "";
  }

  gDialog.ListTypeList.value = gListType;
  gDialog.StartingNumberInput.value = "";

  // Last param = true means attribute value is case-sensitive
  var type = globalElement
    ? GetHTMLOrCSSStyleValue(globalElement, "type", "list-style-type")
    : null;

  if (gListType == "ul") {
    if (type) {
      type = type.toLowerCase();
      gBulletStyleType = type;
      gOriginalStyleType = type;
    }
  } else if (gListType == "ol") {
    // Translate CSS property strings
    switch (type.toLowerCase()) {
      case gDecimalCSS:
        type = gArabic;
        break;
      case gUpperRomanCSS:
        type = gUpperRoman;
        break;
      case gLowerRomanCSS:
        type = gLowerRoman;
        break;
      case gUpperAlphaCSS:
        type = gUpperLetters;
        break;
      case gLowerAlphaCSS:
        type = gLowerLetters;
        break;
    }
    if (type) {
      gNumberStyleType = type;
      gOriginalStyleType = type;
    }

    // Convert attribute number to appropriate letter or roman numeral
    gDialog.StartingNumberInput.value = ConvertStartAttrToUserString(
      globalElement.getAttribute("start"),
      type
    );
  }
  BuildBulletStyleList();
}

// Convert attribute number to appropriate letter or roman numeral
function ConvertStartAttrToUserString(startAttr, type) {
  switch (type) {
    case gUpperRoman:
      startAttr = ConvertArabicToRoman(startAttr);
      break;
    case gLowerRoman:
      startAttr = ConvertArabicToRoman(startAttr).toLowerCase();
      break;
    case gUpperLetters:
      startAttr = ConvertArabicToLetters(startAttr);
      break;
    case gLowerLetters:
      startAttr = ConvertArabicToLetters(startAttr).toLowerCase();
      break;
  }
  return startAttr;
}

function BuildBulletStyleList() {
  gDialog.BulletStyleList.removeAllItems();
  var label;

  if (gListType == "ul") {
    gDialog.BulletStyleList.removeAttribute("disabled");
    gDialog.BulletStyleLabel.removeAttribute("disabled");
    gDialog.StartingNumberInput.setAttribute("disabled", "true");
    gDialog.StartingNumberLabel.setAttribute("disabled", "true");

    label = GetString("BulletStyle");

    gDialog.BulletStyleList.appendItem(GetString("Automatic"), "");
    gDialog.BulletStyleList.appendItem(GetString("SolidCircle"), "disc");
    gDialog.BulletStyleList.appendItem(GetString("OpenCircle"), "circle");
    gDialog.BulletStyleList.appendItem(GetString("SolidSquare"), "square");

    gDialog.BulletStyleList.value = gBulletStyleType;
  } else if (gListType == "ol") {
    gDialog.BulletStyleList.removeAttribute("disabled");
    gDialog.BulletStyleLabel.removeAttribute("disabled");
    gDialog.StartingNumberInput.removeAttribute("disabled");
    gDialog.StartingNumberLabel.removeAttribute("disabled");
    label = GetString("NumberStyle");

    gDialog.BulletStyleList.appendItem(GetString("Automatic"), "");
    gDialog.BulletStyleList.appendItem(GetString("Style_1"), gArabic);
    gDialog.BulletStyleList.appendItem(GetString("Style_I"), gUpperRoman);
    gDialog.BulletStyleList.appendItem(GetString("Style_i"), gLowerRoman);
    gDialog.BulletStyleList.appendItem(GetString("Style_A"), gUpperLetters);
    gDialog.BulletStyleList.appendItem(GetString("Style_a"), gLowerLetters);

    gDialog.BulletStyleList.value = gNumberStyleType;
  } else {
    gDialog.BulletStyleList.setAttribute("disabled", "true");
    gDialog.BulletStyleLabel.setAttribute("disabled", "true");
    gDialog.StartingNumberInput.setAttribute("disabled", "true");
    gDialog.StartingNumberLabel.setAttribute("disabled", "true");
  }

  // Disable advanced edit button if changing to "normal"
  if (gListType) {
    gDialog.AdvancedEditButton.removeAttribute("disabled");
  } else {
    gDialog.AdvancedEditButton.setAttribute("disabled", "true");
  }

  if (label) {
    gDialog.BulletStyleLabel.textContent = label;
  }
}

function SelectListType() {
  // Each list type is stored in the "value" of each menuitem
  var NewType = gDialog.ListTypeList.value;

  if (NewType == "ol") {
    SetTextboxFocus(gDialog.StartingNumberInput);
  }

  if (gListType != NewType) {
    gListType = NewType;

    // Create a newlist object for Advanced Editing
    try {
      if (gListType) {
        globalElement = GetCurrentEditor().createElementWithDefaults(gListType);
      }
    } catch (e) {}

    BuildBulletStyleList();
  }
}

function SelectBulletStyle() {
  // Save the selected index so when user changes
  //   list style, restore index to associated list
  // Each bullet or number type is stored in the "value" of each menuitem
  if (gListType == "ul") {
    gBulletStyleType = gDialog.BulletStyleList.value;
  } else if (gListType == "ol") {
    var type = gDialog.BulletStyleList.value;
    if (gNumberStyleType != type) {
      // Convert existing input value to attr number first,
      //   then convert to the appropriate format for the newly-selected
      gDialog.StartingNumberInput.value = ConvertStartAttrToUserString(
        ConvertUserStringToStartAttr(gNumberStyleType),
        type
      );

      gNumberStyleType = type;
      SetTextboxFocus(gDialog.StartingNumberInput);
    }
  }
}

function ValidateData() {
  gBulletStyleType = gDialog.BulletStyleList.value;
  // globalElement should already be of the correct type

  if (globalElement) {
    var editor = GetCurrentEditor();
    if (gListType == "ul") {
      if (gBulletStyleType && gDialog.ChangeAllRadio.selected) {
        globalElement.setAttribute("type", gBulletStyleType);
      } else {
        try {
          editor.removeAttributeOrEquivalent(globalElement, "type", true);
        } catch (e) {}
      }
    } else if (gListType == "ol") {
      if (gBulletStyleType) {
        globalElement.setAttribute("type", gBulletStyleType);
      } else {
        try {
          editor.removeAttributeOrEquivalent(globalElement, "type", true);
        } catch (e) {}
      }

      var startingNumber = ConvertUserStringToStartAttr(gBulletStyleType);
      if (startingNumber) {
        globalElement.setAttribute("start", startingNumber);
      } else {
        globalElement.removeAttribute("start");
      }
    }
  }
  return true;
}

function ConvertUserStringToStartAttr(type) {
  var startingNumber = TrimString(gDialog.StartingNumberInput.value);

  switch (type) {
    case gUpperRoman:
    case gLowerRoman:
      // If the input isn't an integer, assume it's a roman numeral. Convert it.
      if (!Number(startingNumber)) {
        startingNumber = ConvertRomanToArabic(startingNumber);
      }
      break;
    case gUpperLetters:
    case gLowerLetters:
      // Get the number equivalent of the letters
      if (!Number(startingNumber)) {
        startingNumber = ConvertLettersToArabic(startingNumber);
      }
      break;
  }
  return startingNumber;
}

function ConvertRomanToArabic(num) {
  num = num.toUpperCase();
  if (num && !/[^MDCLXVI]/i.test(num)) {
    var Arabic = 0;
    var last_digit = 1000;
    for (var i = 0; i < num.length; i++) {
      var digit = gRomanDigits[num.charAt(i)];
      if (last_digit < digit) {
        Arabic -= 2 * last_digit;
      }

      last_digit = digit;
      Arabic += last_digit;
    }
    return Arabic;
  }

  return "";
}

function ConvertArabicToRoman(num) {
  if (/^\d{1,4}$/.test(num)) {
    var digits = ("000" + num).substr(-4);
    return (
      gThousandsArray[digits.charAt(0)] +
      gHundredsArray[digits.charAt(1)] +
      gTensArray[digits.charAt(2)] +
      gOnesArray[digits.charAt(3)]
    );
  }
  return "";
}

function ConvertLettersToArabic(letters) {
  letters = letters.toUpperCase();
  if (!letters || /[^A-Z]/.test(letters)) {
    return "";
  }

  var num = 0;
  for (var i = 0; i < letters.length; i++) {
    num = num * 26 + letters.charCodeAt(i) - A + 1;
  }
  return num;
}

function ConvertArabicToLetters(num) {
  var letters = "";
  while (num) {
    num--;
    letters = String.fromCharCode(A + (num % 26)) + letters;
    num = Math.floor(num / 26);
  }
  return letters;
}

function onAccept(event) {
  if (ValidateData()) {
    // Coalesce into one undo transaction
    var editor = GetCurrentEditor();

    editor.beginTransaction();

    var changeEntireList =
      gDialog.RadioGroup.selectedItem == gDialog.ChangeAllRadio;

    // Remember which radio button was selected
    if (gListElement) {
      gDialog.RadioGroup.setAttribute("index", changeEntireList ? "0" : "1");
    }

    var changeList;
    if (gListElement && gDialog.ChangeAllRadio.selected) {
      changeList = true;
    } else {
      changeList =
        gMixedListSelection ||
        gListType != gOriginalListType ||
        gBulletStyleType != gOriginalStyleType;
    }
    if (changeList) {
      try {
        if (gListType) {
          editor.makeOrChangeList(
            gListType,
            changeEntireList,
            gBulletStyleType != gOriginalStyleType ? gBulletStyleType : null
          );

          // Get the new list created:
          gListElement = editor.getElementOrParentByTagName(gListType, null);

          editor.cloneAttributes(gListElement, globalElement);
        } else {
          // Remove all existing lists
          if (gListElement && changeEntireList) {
            editor.selectElement(gListElement);
          }

          editor.removeList("ol");
          editor.removeList("ul");
          editor.removeList("dl");
        }
      } catch (e) {}
    }

    editor.endTransaction();

    SaveWindowLocation();

    return;
  }
  event.preventDefault();
}