!import
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) 1999-2000
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 * Dan Haddix (dan6992@hotmail.com)
24 * Brian King (briano9@yahoo.com)
25 *
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
37 *
38 * ***** END LICENSE BLOCK ***** */
39
40 var tHide = false;
41 var highCont = false;
42 var imageElement = null;
43 var mapName = '';
44 var imageMap = null;
45 var imageEl;
46 var srcInputValue = null;
47 var marquee = null;
48 var frameDoc = null;
49 var buttonArray = [];
50
Startup
51 function Startup(){
52 if (!GetCurrentEditor())
53 {
54 window.close();
55 return;
56 }
57 initDialog();
58 }
59
initDialog
60 function initDialog(){
61 //Get image element from parent
62 imageElement = window.arguments[0];
63 if (!imageElement) //If not an image close window
64 {
65 window.close();
66 return;
67 }
68
69 //Get image map from parent
70 imageMap = window.arguments[1];
71 if (!imageMap) //If no image map close window
72 window.close();
73
74 //find parent inputs
75 var srcInput = window.opener.document.getElementById("srcInput");
76 var widthInput = window.opener.document.getElementById("widthInput");
77 var heightInput = window.opener.document.getElementById("heightInput");
78
79 //check for relative url
80 if (!((srcInput.value.indexOf("http://") != -1) || (srcInput.value.indexOf("file://") != -1))){
81 if (IsUrlAboutBlank(GetDocumentUrl())){
82 alert(GetString("SaveToUseRelativeUrl"));
83 window.close();
84 //TODO: add option to save document now
85 }
86 else{
87 var edDoc = GetDocumentUrl();
88 var imgDoc = srcInput.value;
89 imgDoc = imgDoc.split("../");
90 var len = imgDoc.length;
91 for (var i=0; i<len; i++){
92 if (edDoc.length > (String(GetCurrentEditor().document.location.protocol).length+2))
93 edDoc = edDoc.substring(0, edDoc.lastIndexOf("/"));
94 }
95 imgDoc = edDoc+"/"+imgDoc[imgDoc.length-1];
96 srcInputValue = imgDoc;
97 }
98 }
99 else{
100 srcInputValue = srcInput.value;
101 }
102
103 //Set iframe pointer
104 frameDoc = window.frames[0].document;
105
106 //Fill button array
107 buttonArray[0] = document.getElementById("pointerButton");
108 buttonArray[1] = document.getElementById("rectButton");
109 buttonArray[2] = document.getElementById("cirButton");
110 buttonArray[3] = document.getElementById("polyButton");
111
112 //Create marquee
113 var marquee = frameDoc.createElement("div");
114 marquee.setAttribute("id", "marquee");
115 frameDoc.body.appendChild(marquee);
116
117 //Create background div
118 var bgDiv = frameDoc.createElement("div");
119 if ( bgDiv ) {
120 bgDiv.setAttribute("id", "bgDiv");
121 frameDoc.body.appendChild(bgDiv);
122 }
123
124 //Place Image
125 var newImg = frameDoc.createElement("img");
126 if ( newImg ) {
127 newImg.setAttribute("src", srcInputValue);
128 if (parseInt(widthInput.value) > 0)
129 newImg.setAttribute("width", widthInput.value);
130 if (parseInt(heightInput.value) > 0)
131 newImg.setAttribute("height", heightInput.value);
132 newImg.setAttribute("id", "mainImg");
133 imageEl = frameDoc.getElementById("bgDiv").appendChild(newImg);
134 imageEl.addEventListener("error", imgError, false);
135 }
136
137 //Resize background DIV to fit image
138 fixBgDiv();
139
140 //Recreate Image Map if it exists
141 recreateMap();
142 }
143
imgError
144 function imgError(){
145 alert(GetString("ImapError")+" " + srcInputValue+"."+GetString("ImapCheck"));
146 }
147
fixBgDiv
148 function fixBgDiv(){
149 imageEl = frameDoc.getElementById("mainImg");
150 if (imageEl.offsetWidth != 0){
151 frameDoc.getElementById("bgDiv").style.width = imageEl.offsetWidth;
152 frameDoc.getElementById("bgDiv").style.height = imageEl.offsetHeight;
153 }
154 else
155 setTimeout("fixBgDiv()", 100);
156 }
157
hideToolbar
158 function hideToolbar(){
159 // Check to see if toolbar is already hidden
160 if (tHide){
161 // If it is show it
162 document.getElementById("toolbar").collapsed = false;
163 // Set the menu items text back to "Hide Toolbar"
164 document.getElementById("view_hidetoolbar").setAttribute("label", GetString("HideToolbar"));
165 tHide = false
166 }
167 else{
168 // If not hide it
169 document.getElementById("toolbar").collapsed = true;
170 //Set the menu items text to "Show Toolbar"
171 document.getElementById("view_hidetoolbar").setAttribute("label", GetString("ShowToolbar"));
172 tHide = true;
173 }
174 }
175
highContrast
176 function highContrast(){
177 if (highCont == true){
178 frameDoc.getElementById("bgDiv").style.background = "url('chrome://editor/skin/images/Map_checker.gif')";
179 frameDoc.getElementById("bgDiv").style.backgroundColor = "white";
180 imageEl.style.setProperty("opacity", "1.0", true);
181 document.getElementById("Map:Contrast").setAttribute("checked", "false");
182 document.getElementById("Map:Contrast").setAttribute("toggled", "false");
183 highCont = false;
184 }
185 else{
186 frameDoc.getElementById("bgDiv").style.background = "url('')";
187 frameDoc.getElementById("bgDiv").style.backgroundColor = "#D2D2D2";
188 imageEl.style.setProperty("opacity", ".3", true);
189 document.getElementById("Map:Contrast").setAttribute("checked", "true");
190 document.getElementById("Map:Contrast").setAttribute("toggled", "true");
191 highCont = true;
192 }
193 }
194
recreateMap
195 function recreateMap(){
196 var areaCollection = imageMap.childNodes;
197 var areaColLen = areaCollection.length;
198 for(var j=0; j<areaColLen; j++){
199 area = areaCollection[j];
200 shape = area.getAttribute("shape");
201 shape = shape.toLowerCase();
202 coords = area.getAttribute("coords");
203 href = area.getAttribute("href");
204 target = area.getAttribute("target");
205 alt = area.getAttribute("alt");
206 if (shape == "rect")
207 Rect(coords, href, target, alt, true);
208 else if (shape == "circle")
209 Circle(coords, href, target, alt, true);
210 else
211 Poly(coords, href, target, alt, true);
212 }
213 }
214
finishMap
215 function finishMap(){
216 if (!setMapName())
217 return false;
218 if (!deleteAreas())
219 return false;
220
221 spots = frameDoc.getElementsByName("hotspot");
222 var len = spots.length;
223 if (len >= 1){
224 for(i=0; i<len; i++){
225 dump(i+"\n");
226 curSpot = spots[i];
227 if (curSpot.getAttribute("class") == "rect")
228 createRect(curSpot);
229 else if (curSpot.getAttribute("class") == "cir")
230 createCir(curSpot);
231 else
232 createPoly(curSpot);
233 }
234 //try{
235 // GetCurrentEditor().root.appendChild(imageMap);
236 //} catch (e) {}
237 //returnValue = "test";
238 //try{
239 // window.arguments[0] = "test"; //GetCurrentEditor().insertElementAtSelection(imageMap, false);
240 // dump(window.arguments[0]+"\n");
241 //} catch (e) {}
242 dump("imageMap.childNodes.length = "+imageMap.childNodes.length+"\n");
243 }
244 return true;
245 }
246
setMapName
247 function setMapName() {
248 //try {
249 // imageMap = GetCurrentEditor().createElementWithDefaults("map");
250 //} catch (e) {}
251 //dump(imageMap+"\n");
252 //imageMap = frameDoc.createElement("map");
253
254 mapName = imageMap.getAttribute("name");
255 if (mapName == ""){
256 mapName = String(frameDoc.getElementById("mainImg").getAttribute("src"));
257 mapName = mapName.substring(mapName.lastIndexOf("/"), mapName.length);
258 mapName = mapName.substring(mapName.lastIndexOf("\\"), mapName.length);
259 mapName = mapName.substring(1, mapName.lastIndexOf("."));
260 if (mapName == ""){
261 // BUG causes substring to return nothing when
262 // parameters are 1 & 13 (i.e. string.substring(1, 13);)
263 mapName = "hack";
264 }
265 imageMap.setAttribute("name", mapName);
266 }
267 return true;
268 }
269
createRect
270 function createRect(which){
271 var newRect;
272 //try {
273 // newRect = editor.createElementWithDefaults("area");
274 //} catch (e) {}
275 newRect = frameDoc.createElement("area");
276 newRect.setAttribute("shape", "rect");
277 coords = parseInt(which.style.left)+","+parseInt(which.style.top)+","+(parseInt(which.style.left)+parseInt(which.style.width))+","+(parseInt(which.style.top)+parseInt(which.style.height));
278 newRect.setAttribute("coords", coords);
279 if (which.getAttribute("hsHref") != ""){
280 newRect.setAttribute("href", which.getAttribute("hsHref"));
281 }
282 else{
283 newRect.setAttribute("nohref", "");
284 }
285 if (which.getAttribute("hsTarget") != ""){
286 newRect.setAttribute("target", which.getAttribute("hsTarget"));
287 }
288 if (which.getAttribute("hsAlt") != ""){
289 newRect.setAttribute("alt", which.getAttribute("hsAlt"));
290 }
291 //newRect.removeAttribute("id");
292 imageMap.appendChild(newRect);
293 }
294
createCir
295 function createCir(which){
296 var newCir;
297 //try {
298 // newCir = editor.createElementWithDefaults("area");
299 //} catch (e) {}
300 newCir = frameDoc.createElement("area");
301 if ( !newCir )
302 return;
303
304 newCir.setAttribute("shape", "circle");
305 radius = Math.floor(parseInt(which.style.width)/2);
306 coords = (parseInt(which.style.left)+radius)+","+(parseInt(which.style.top)+radius)+","+radius;
307 newCir.setAttribute("coords", coords);
308 if (which.getAttribute("hsHref") != "")
309 newCir.setAttribute("href", which.getAttribute("hsHref"));
310 else{
311 newCir.setAttribute("nohref", "");
312 }
313 if (which.getAttribute("hsTarget") != ""){
314 newCir.setAttribute("target", which.getAttribute("hsTarget"));
315 }
316 if (which.getAttribute("hsAlt") != ""){
317 newCir.setAttribute("alt", which.getAttribute("hsAlt"));
318 }
319 //newCir.removeAttribute("id");
320 imageMap.appendChild(newCir);
321 }
322
createPoly
323 function createPoly(which){
324 var newPoly;
325 //try {
326 // newPoly = editor.createElementWithDefaults("area");
327 //} catch (e) {}
328 newPoly = frameDoc.createElement("area");
329 if ( !newPoly )
330 return;
331
332 newPoly.setAttribute("shape", "poly");
333 var coords = '';
334 var len = which.childNodes.length;
335 for(l=0; l<len; l++){
336 coords += (parseInt(which.style.left)+parseInt(which.childNodes[l].style.left))+","+(parseInt(which.style.top)+parseInt(which.childNodes[l].style.top))+",";
337 }
338 coords = coords.substring(0, (coords.length-1));
339 newPoly.setAttribute("coords", coords);
340 if (which.getAttribute("hsHref") != "")
341 newPoly.setAttribute("href", which.getAttribute("hsHref"));
342 else{
343 newPoly.setAttribute("nohref", "");
344 }
345 if (which.getAttribute("hsTarget") != ""){
346 newPoly.setAttribute("target", which.getAttribute("hsTarget"));
347 }
348 if (which.getAttribute("hsAlt") != ""){
349 newPoly.setAttribute("alt", which.getAttribute("hsAlt"));
350 }
351 //newPoly.removeAttribute("id");
352 imageMap.appendChild(newPoly);
353 }
354
hotSpotProps
355 function hotSpotProps(which){
356 var currentRect = null;
357 var currentCir = null;
358 if (which == null)
359 return;
360 hotSpotWin = window.openDialog("chrome://editor/content/EdImageMapHotSpot.xul", "_blank", "chrome,close,titlebar,modal", which);
361 }
362
deleteAreas
363 function deleteAreas(){
364 dump("deleteAreas called\n");
365 area = imageMap.firstChild;
366 while (area != null){
367 dump(area+"\n");
368 imageMap.removeChild(area);
369 area = imageMap.firstChild;
370 }
371 return true;
372 }