!import
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Original Code is Mozilla Communicator client code, released
15 * March 31, 1998.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-1999
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38 var gInsertNew = true;
39 var gAnchorElement = null;
40 var gOriginalName = "";
41 const kTagName = "anchor";
42
43 // dialog initialization code
Startup
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
44 function Startup()
45 {
46 var editor = GetCurrentEditor();
47 if (!editor)
48 {
49 window.close();
50 return;
51 }
52
53 gDialog.OkButton = document.documentElement.getButton("accept");
54 gDialog.NameInput = document.getElementById("nameInput");
55
56 // Get a single selected element of the desired type
57 gAnchorElement = editor.getSelectedElement(kTagName);
58
59 if (gAnchorElement) {
60 // We found an element and don't need to insert one
61 gInsertNew = false;
62
63 // Make a copy to use for AdvancedEdit
64 globalElement = gAnchorElement.cloneNode(false);
65 gOriginalName = ConvertToCDATAString(gAnchorElement.name);
66 } else {
67 gInsertNew = true;
68 // We don't have an element selected,
69 // so create one with default attributes
70 gAnchorElement = editor.createElementWithDefaults(kTagName);
71 if (gAnchorElement) {
72 // Use the current selection as suggested name
73 var name = GetSelectionAsText();
74 // Get 40 characters of the selected text and don't add "...",
75 // replace whitespace with "_" and strip non-word characters
76 name = ConvertToCDATAString(TruncateStringAtWordEnd(name, 40, false));
77 //Be sure the name is unique to the document
78 if (AnchorNameExists(name))
79 name += "_"
80
81 // Make a copy to use for AdvancedEdit
82 globalElement = gAnchorElement.cloneNode(false);
83 globalElement.setAttribute("name",name);
84 }
85 }
86 if(!gAnchorElement)
87 {
88 dump("Failed to get selected element or create a new one!\n");
89 window.close();
90 return;
91 }
92
93 InitDialog();
94
95 DoEnabling();
96 SetTextboxFocus(gDialog.NameInput);
97 SetWindowLocation();
98 }
99
InitDialog
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
100 function InitDialog()
101 {
102 gDialog.NameInput.value = globalElement.getAttribute("name");
103 }
104
ChangeName
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
105 function ChangeName()
106 {
107 if (gDialog.NameInput.value.length > 0)
108 {
109 // Replace spaces with "_" and strip other non-URL characters
110 // Note: we could use ConvertAndEscape, but then we'd
111 // have to UnEscapeAndConvert beforehand - too messy!
112 gDialog.NameInput.value = ConvertToCDATAString(gDialog.NameInput.value);
113 }
114 DoEnabling();
115 }
116
DoEnabling
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
117 function DoEnabling()
118 {
119 var enable = gDialog.NameInput.value.length > 0;
120 SetElementEnabled(gDialog.OkButton, enable);
121 SetElementEnabledById("AdvancedEditButton1", enable);
122 }
123
AnchorNameExists
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
124 function AnchorNameExists(name)
125 {
126 var anchorList;
127 try {
128 anchorList = GetCurrentEditor().document.anchors;
129 } catch (e) {}
130
131 if (anchorList) {
132 for (var i = 0; i < anchorList.length; i++) {
133 if (anchorList[i].name == name)
134 return true;
135 }
136 }
137 return false;
138 }
139
140 // Get and validate data from widgets.
141 // Set attributes on globalElement so they can be accessed by AdvancedEdit()
ValidateData
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
142 function ValidateData()
143 {
144 var name = TrimString(gDialog.NameInput.value);
145 if (!name)
146 {
147 ShowInputErrorMessage(GetString("MissingAnchorNameError"));
148 SetTextboxFocus(gDialog.NameInput);
149 return false;
150 } else {
151 // Replace spaces with "_" and strip other characters
152 // Note: we could use ConvertAndEscape, but then we'd
153 // have to UnConverAndEscape beforehand - too messy!
154 name = ConvertToCDATAString(name);
155
156 if (gOriginalName != name && AnchorNameExists(name))
157 {
158 ShowInputErrorMessage(GetString("DuplicateAnchorNameError").replace(/%name%/,name));
159 SetTextboxFocus(gDialog.NameInput);
160 return false;
161 }
162 globalElement.name = name;
163 }
164 return true;
165 }
166
onAccept
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
167 function onAccept()
168 {
169 if (ValidateData())
170 {
171 if (gOriginalName != globalElement.name)
172 {
173 var editor = GetCurrentEditor();
174 editor.beginTransaction();
175
176 try {
177 // "false" = don't delete selected text when inserting
178 if (gInsertNew)
179 {
180 // We must insert element before copying CSS style attribute,
181 // but we must set the name else it won't insert at all
182 gAnchorElement.name = globalElement.name;
183 editor.insertElementAtSelection(gAnchorElement, false);
184 }
185
186 // Copy attributes to element we are changing or inserting
187 editor.cloneAttributes(gAnchorElement, globalElement);
188
189 } catch (e) {}
190
191 editor.endTransaction();
192 }
193 SaveWindowLocation();
194 return true;
195 }
196 return false;
197 }