!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 * Pete Collins
24 * Brian King
25 * Ben Goodger
26 * Charles Manske (cmanske@netscape.com)
27 * Neil Rashbrook (neil@parkwaycc.co.uk)
28 *
29 * Alternatively, the contents of this file may be used under the terms of
30 * either of the GNU General Public License Version 2 or later (the "GPL"),
31 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
32 * in which case the provisions of the GPL or the LGPL are applicable instead
33 * of those above. If you wish to allow use of your version of this file only
34 * under the terms of either the GPL or the LGPL, and not to allow others to
35 * use your version of this file under the terms of the MPL, indicate your
36 * decision by deleting the provisions above and replace them with the notice
37 * and other provisions required by the GPL or the LGPL. If you do not delete
38 * the provisions above, a recipient may use your version of this file under
39 * the terms of any one of the MPL, the GPL or the LGPL.
40 *
41 * ***** END LICENSE BLOCK ***** */
42
43 var gAnchorElement = null;
44 var gOriginalHref = "";
45 var gHNodeArray = {};
46
47 // dialog initialization code
48
Startup
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
49 function Startup()
50 {
51 var editor = GetCurrentEditor();
52 if (!editor)
53 {
54 window.close();
55 return;
56 }
57
58 ImageStartup();
59 gDialog.hrefInput = document.getElementById("hrefInput");
60 gDialog.makeRelativeLink = document.getElementById("MakeRelativeLink");
61 gDialog.showLinkBorder = document.getElementById("showLinkBorder");
62 gDialog.linkTab = document.getElementById("imageLinkTab");
63
64 // Get a single selected image element
65 var tagName = "img";
66 if ("arguments" in window && window.arguments[0])
67 {
68 imageElement = window.arguments[0];
69 // We've been called from form field propertes, so we can't insert a link
70 gDialog.linkTab.parentNode.removeChild(gDialog.linkTab);
71 gDialog.linkTab = null;
72 }
73 else
74 {
75 // First check for <input type="image">
76 try {
77 imageElement = editor.getSelectedElement("input");
78
79 if (!imageElement || imageElement.getAttribute("type") != "image") {
80 // Get a single selected image element
81 imageElement = editor.getSelectedElement(tagName);
82 if (imageElement)
83 gAnchorElement = editor.getElementOrParentByTagName("href", imageElement);
84 }
85 } catch (e) {}
86
87 }
88
89 if (imageElement)
90 {
91 // We found an element and don't need to insert one
92 if (imageElement.hasAttribute("src"))
93 {
94 gInsertNewImage = false;
95 gActualWidth = imageElement.naturalWidth;
96 gActualHeight = imageElement.naturalHeight;
97 }
98 }
99 else
100 {
101 gInsertNewImage = true;
102
103 // We don't have an element selected,
104 // so create one with default attributes
105 try {
106 imageElement = editor.createElementWithDefaults(tagName);
107 } catch(e) {}
108
109 if (!imageElement)
110 {
111 dump("Failed to get selected element or create a new one!\n");
112 window.close();
113 return;
114 }
115 try {
116 gAnchorElement = editor.getSelectedElement("href");
117 } catch (e) {}
118 }
119
120 // Make a copy to use for AdvancedEdit
121 globalElement = imageElement.cloneNode(false);
122
123 // We only need to test for this once per dialog load
124 gHaveDocumentUrl = GetDocumentBaseUrl();
125
126 InitDialog();
127 if (gAnchorElement)
128 gOriginalHref = gAnchorElement.getAttribute("href");
129 gDialog.hrefInput.value = gOriginalHref;
130
131 FillLinkMenulist(gDialog.hrefInput, gHNodeArray);
132 ChangeLinkLocation();
133
134 // Save initial source URL
135 gOriginalSrc = gDialog.srcInput.value;
136
137 // By default turn constrain on, but both width and height must be in pixels
138 gDialog.constrainCheckbox.checked =
139 gDialog.widthUnitsMenulist.selectedIndex == 0 &&
140 gDialog.heightUnitsMenulist.selectedIndex == 0;
141
142 // Start in "Link" tab if 2nd arguement is true
143 if (gDialog.linkTab && "arguments" in window && window.arguments[1])
144 {
145 document.getElementById("TabBox").selectedTab = gDialog.linkTab;
146 SetTextboxFocus(gDialog.hrefInput);
147 }
148 else
149 SetTextboxFocus(gDialog.srcInput);
150
151 SetWindowLocation();
152 }
153
154 // Set dialog widgets with attribute data
155 // We get them from globalElement copy so this can be used
156 // by AdvancedEdit(), which is shared by all property dialogs
InitDialog
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
157 function InitDialog()
158 {
159 InitImage();
160 var border = TrimString(gDialog.border.value);
161 gDialog.showLinkBorder.checked = border != "" && border > 0;
162 }
163
ChangeLinkLocation
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
164 function ChangeLinkLocation()
165 {
166 SetRelativeCheckbox(gDialog.makeRelativeLink);
167 gDialog.showLinkBorder.disabled = !TrimString(gDialog.hrefInput.value);
168 }
169
ToggleShowLinkBorder
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
170 function ToggleShowLinkBorder()
171 {
172 if (gDialog.showLinkBorder.checked)
173 {
174 var border = TrimString(gDialog.border.value);
175 if (!border || border == "0")
176 gDialog.border.value = "2";
177 }
178 else
179 {
180 gDialog.border.value = "0";
181 }
182 }
183
184 // Get data from widgets, validate, and set for the global element
185 // accessible to AdvancedEdit() [in EdDialogCommon.js]
ValidateData
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
186 function ValidateData()
187 {
188 return ValidateImage();
189 }
190
onAccept
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
191 function onAccept()
192 {
193 // Use this now (default = false) so Advanced Edit button dialog doesn't trigger error message
194 gDoAltTextError = true;
195
196 if (ValidateData())
197 {
198 if ("arguments" in window && window.arguments[0])
199 {
200 SaveWindowLocation();
201 return true;
202 }
203
204 var editor = GetCurrentEditor();
205
206 editor.beginTransaction();
207
208 try
209 {
210 if (gRemoveImageMap)
211 {
212 globalElement.removeAttribute("usemap");
213 if (gImageMap)
214 {
215 editor.deleteNode(gImageMap);
216 gInsertNewIMap = true;
217 gImageMap = null;
218 }
219 }
220 else if (gImageMap)
221 {
222 // un-comment to see that inserting image maps does not work!
223 /*
224 gImageMap = editor.createElementWithDefaults("map");
225 gImageMap.setAttribute("name", "testing");
226 var testArea = editor.createElementWithDefaults("area");
227 testArea.setAttribute("shape", "circle");
228 testArea.setAttribute("coords", "86,102,52");
229 testArea.setAttribute("href", "test");
230 gImageMap.appendChild(testArea);
231 */
232
233 // Assign to map if there is one
234 var mapName = gImageMap.getAttribute("name");
235 if (mapName != "")
236 {
237 globalElement.setAttribute("usemap", ("#"+mapName));
238 if (globalElement.getAttribute("border") == "")
239 globalElement.setAttribute("border", 0);
240 }
241 }
242
243 // Create or remove the link as appropriate
244 var href = gDialog.hrefInput.value;
245 if (href != gOriginalHref)
246 {
247 if (href && !gInsertNewImage)
248 EditorSetTextProperty("a", "href", href);
249 else
250 EditorRemoveTextProperty("href", "");
251 }
252
253 // If inside a link, always write the 'border' attribute
254 if (href)
255 {
256 if (gDialog.showLinkBorder.checked)
257 {
258 // Use default = 2 if border attribute is empty
259 if (!globalElement.hasAttribute("border"))
260 globalElement.setAttribute("border", "2");
261 }
262 else
263 globalElement.setAttribute("border", "0");
264 }
265
266 if (gInsertNewImage)
267 {
268 if (href) {
269 var linkElement = editor.createElementWithDefaults("a");
270 linkElement.setAttribute("href", href);
271 linkElement.appendChild(imageElement);
272 editor.insertElementAtSelection(linkElement, true);
273 }
274 else
275 // 'true' means delete the selection before inserting
276 editor.insertElementAtSelection(imageElement, true);
277 }
278
279 // Check to see if the link was to a heading
280 // Do this last because it moves the caret (BAD!)
281 if (href in gHNodeArray)
282 {
283 var anchorNode = editor.createElementWithDefaults("a");
284 if (anchorNode)
285 {
286 anchorNode.name = href.substr(1);
287 // Remember to use editor method so it is undoable!
288 editor.insertNode(anchorNode, gHNodeArray[href], 0, false);
289 }
290 }
291 // All values are valid - copy to actual element in doc or
292 // element we just inserted
293 editor.cloneAttributes(imageElement, globalElement);
294
295 // If document is empty, the map element won't insert,
296 // so always insert the image first
297 if (gImageMap && gInsertNewIMap)
298 {
299 // Insert the ImageMap element at beginning of document
300 var body = editor.rootElement;
301 editor.setShouldTxnSetSelection(false);
302 editor.insertNode(gImageMap, body, 0);
303 editor.setShouldTxnSetSelection(true);
304 }
305 }
306 catch (e)
307 {
308 dump(e);
309 }
310
311 editor.endTransaction();
312
313 SaveWindowLocation();
314 return true;
315 }
316
317 gDoAltTextError = false;
318
319 return false;
320 }