summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/compose/content/dialogs/EdNamedAnchorProps.js
blob: c943cc28330b0955d5e6d88f49bc7bae7c3743a5 (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
/* 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 gInsertNew = true;
var gAnchorElement = null;
var gOriginalName = "";
const kTagName = "anchor";

// dialog initialization code

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

function Startup() {
  var editor = GetCurrentEditor();
  if (!editor) {
    window.close();
    return;
  }

  gDialog.OkButton = document.querySelector("dialog").getButton("accept");
  gDialog.NameInput = document.getElementById("nameInput");

  // Get a single selected element of the desired type
  gAnchorElement = editor.getSelectedElement(kTagName);

  if (gAnchorElement) {
    // We found an element and don't need to insert one
    gInsertNew = false;

    // Make a copy to use for AdvancedEdit
    globalElement = gAnchorElement.cloneNode(false);
    gOriginalName = ConvertToCDATAString(gAnchorElement.name);
  } else {
    gInsertNew = true;
    // We don't have an element selected,
    //  so create one with default attributes
    gAnchorElement = editor.createElementWithDefaults(kTagName);
    if (gAnchorElement) {
      // Use the current selection as suggested name
      var name = GetSelectionAsText();
      // Get 40 characters of the selected text and don't add "...",
      //  replace whitespace with "_" and strip non-word characters
      name = ConvertToCDATAString(TruncateStringAtWordEnd(name, 40, false));
      // Be sure the name is unique to the document
      if (AnchorNameExists(name)) {
        name += "_";
      }

      // Make a copy to use for AdvancedEdit
      globalElement = gAnchorElement.cloneNode(false);
      globalElement.setAttribute("name", name);
    }
  }
  if (!gAnchorElement) {
    dump("Failed to get selected element or create a new one!\n");
    window.close();
    return;
  }

  InitDialog();

  DoEnabling();
  SetTextboxFocus(gDialog.NameInput);
  SetWindowLocation();
}

function InitDialog() {
  gDialog.NameInput.value = globalElement.getAttribute("name");
}

function ChangeName() {
  if (gDialog.NameInput.value.length > 0) {
    // Replace spaces with "_" and strip other non-URL characters
    // Note: we could use ConvertAndEscape, but then we'd
    //  have to UnEscapeAndConvert beforehand - too messy!
    gDialog.NameInput.value = ConvertToCDATAString(gDialog.NameInput.value);
  }
  DoEnabling();
}

function DoEnabling() {
  var enable = gDialog.NameInput.value.length > 0;
  SetElementEnabled(gDialog.OkButton, enable);
  SetElementEnabledById("AdvancedEditButton1", enable);
}

function AnchorNameExists(name) {
  var anchorList;
  try {
    anchorList = GetCurrentEditor().document.anchors;
  } catch (e) {}

  if (anchorList) {
    for (var i = 0; i < anchorList.length; i++) {
      if (anchorList[i].name == name) {
        return true;
      }
    }
  }
  return false;
}

// Get and validate data from widgets.
// Set attributes on globalElement so they can be accessed by AdvancedEdit()
function ValidateData() {
  var name = TrimString(gDialog.NameInput.value);
  if (!name) {
    ShowInputErrorMessage(GetString("MissingAnchorNameError"));
    SetTextboxFocus(gDialog.NameInput);
    return false;
  }
  // Replace spaces with "_" and strip other characters
  // Note: we could use ConvertAndEscape, but then we'd
  //  have to UnConverAndEscape beforehand - too messy!
  name = ConvertToCDATAString(name);

  if (gOriginalName != name && AnchorNameExists(name)) {
    ShowInputErrorMessage(
      GetString("DuplicateAnchorNameError").replace(/%name%/, name)
    );
    SetTextboxFocus(gDialog.NameInput);
    return false;
  }
  globalElement.name = name;

  return true;
}

function onAccept(event) {
  if (ValidateData()) {
    if (gOriginalName != globalElement.name) {
      var editor = GetCurrentEditor();
      editor.beginTransaction();

      try {
        // "false" = don't delete selected text when inserting
        if (gInsertNew) {
          // We must insert element before copying CSS style attribute,
          //  but we must set the name else it won't insert at all
          gAnchorElement.name = globalElement.name;
          editor.insertElementAtSelection(gAnchorElement, false);
        }

        // Copy attributes to element we are changing or inserting
        editor.cloneAttributes(gAnchorElement, globalElement);
      } catch (e) {}

      editor.endTransaction();
    }
    SaveWindowLocation();
    return;
  }
  event.preventDefault();
}