File: EdImageMap.js

package info (click to toggle)
iceweasel 2.0.0.19-0etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 298,784 kB
  • ctags: 317,912
  • sloc: cpp: 1,796,902; ansic: 987,677; xml: 109,036; makefile: 47,777; asm: 35,201; perl: 26,983; sh: 20,879; cs: 6,232; java: 5,513; python: 3,249; pascal: 459; lex: 306; php: 244; csh: 132; objc: 97; yacc: 79; ada: 49; awk: 14; sql: 4; sed: 4
file content (378 lines) | stat: -rw-r--r-- 12,192 bytes parent folder | download | duplicates (8)
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
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Mozilla Communicator client code, released
 * March 31, 1998.
 *
 * The Initial Developer of the Original Code is
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 1999-2000
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Dan Haddix (dan6992@hotmail.com)
 *   Brian King (briano9@yahoo.com)
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either of the GNU General Public License Version 2 or later (the "GPL"),
 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

var tHide = false;
var highCont = false;
var imageElement = null;
var mapName = '';
var imageMap = null;
var imageEl;
var srcInputValue = null;
var marquee = null;
var frameDoc = null;
var buttonArray = [];

function Startup(){
  if (!GetCurrentEditor())
  {
    window.close();
    return;
  }
  initDialog();
}

function doHelpButton()
{
  openHelp("imagemap_properties");
}

function initDialog(){
  //Get image element from parent
  imageElement = window.arguments[0];
  if (!imageElement) //If not an image close window
  {
    window.close();
    return;
  }

  //Get image map from parent
  imageMap = window.arguments[1];
  if (!imageMap) //If no image map close window
    window.close();

  //find parent inputs
  var srcInput = window.opener.document.getElementById("srcInput");
  var widthInput = window.opener.document.getElementById("widthInput");
  var heightInput = window.opener.document.getElementById("heightInput");

  //check for relative url
  if (!((srcInput.value.indexOf("http://") != -1) || (srcInput.value.indexOf("file://") != -1))){
    if (IsUrlAboutBlank(GetDocumentUrl())){
      alert(GetString("SaveToUseRelativeUrl"));
      window.close();
      //TODO: add option to save document now
    }
    else{
      var edDoc = GetDocumentUrl();
      var imgDoc = srcInput.value;
      imgDoc = imgDoc.split("../");
      var len = imgDoc.length;
      for (var i=0; i<len; i++){
        if (edDoc.length > (String(GetCurrentEditor().document.location.protocol).length+2))
          edDoc = edDoc.substring(0, edDoc.lastIndexOf("/"));
      }
      imgDoc = edDoc+"/"+imgDoc[imgDoc.length-1];
      srcInputValue = imgDoc;
    }
  }
  else{
    srcInputValue = srcInput.value;
  }

  //Set iframe pointer
  frameDoc = window.frames[0].document;

  //Fill button array
  buttonArray[0] = document.getElementById("pointerButton");
  buttonArray[1] = document.getElementById("rectButton");
  buttonArray[2] = document.getElementById("cirButton");
  buttonArray[3] = document.getElementById("polyButton");

  //Create marquee
  var marquee = frameDoc.createElement("div");
  marquee.setAttribute("id", "marquee");
  frameDoc.body.appendChild(marquee);

  //Create background div
  var bgDiv = frameDoc.createElement("div");
  if ( bgDiv ) {
    bgDiv.setAttribute("id", "bgDiv");
    frameDoc.body.appendChild(bgDiv);
  }

  //Place Image
  var newImg = frameDoc.createElement("img");
  if ( newImg ) {
    newImg.setAttribute("src", srcInputValue);
    if (parseInt(widthInput.value) > 0)
      newImg.setAttribute("width", widthInput.value);
    if (parseInt(heightInput.value) > 0)
      newImg.setAttribute("height", heightInput.value);
    newImg.setAttribute("id", "mainImg");
    imageEl = frameDoc.getElementById("bgDiv").appendChild(newImg);
    imageEl.addEventListener("error", imgError, false);
  }

  //Resize background DIV to fit image
  fixBgDiv();

  //Recreate Image Map if it exists
  recreateMap();
}

function imgError(){
  alert(GetString("ImapError")+" " + srcInputValue+"."+GetString("ImapCheck"));
}

function fixBgDiv(){
  imageEl = frameDoc.getElementById("mainImg");
  if (imageEl.offsetWidth != 0){
    frameDoc.getElementById("bgDiv").style.width = imageEl.offsetWidth;
    frameDoc.getElementById("bgDiv").style.height = imageEl.offsetHeight;
  }
  else
    setTimeout("fixBgDiv()", 100);
}

function hideToolbar(){
  // Check to see if toolbar is already hidden
  if (tHide){
    // If it is show it
    document.getElementById("toolbar").collapsed = false;
    // Set the menu items text back to "Hide Toolbar"
    document.getElementById("view_hidetoolbar").setAttribute("label", GetString("HideToolbar"));
    tHide = false
  }
  else{
    // If not hide it
    document.getElementById("toolbar").collapsed = true;
    //Set the menu items text to "Show Toolbar"
    document.getElementById("view_hidetoolbar").setAttribute("label", GetString("ShowToolbar"));
    tHide = true;
  }
}

function highContrast(){
  if (highCont == true){
    frameDoc.getElementById("bgDiv").style.background = "url('chrome://editor/skin/images/Map_checker.gif')";
    frameDoc.getElementById("bgDiv").style.backgroundColor = "white";
    imageEl.style.setProperty("opacity", "1.0", true);
    document.getElementById("Map:Contrast").setAttribute("checked", "false");
    document.getElementById("Map:Contrast").setAttribute("toggled", "false");
    highCont = false;
  }
  else{
    frameDoc.getElementById("bgDiv").style.background = "url('')";
    frameDoc.getElementById("bgDiv").style.backgroundColor = "#D2D2D2";
    imageEl.style.setProperty("opacity", ".3", true);
    document.getElementById("Map:Contrast").setAttribute("checked", "true");
    document.getElementById("Map:Contrast").setAttribute("toggled", "true");
    highCont = true;
  }
}

function recreateMap(){
  var areaCollection = imageMap.childNodes;
  var areaColLen = areaCollection.length;
  for(var j=0; j<areaColLen; j++){
      area = areaCollection[j];
      shape = area.getAttribute("shape");
      shape = shape.toLowerCase();
      coords = area.getAttribute("coords");
      href = area.getAttribute("href");
      target = area.getAttribute("target");
      alt = area.getAttribute("alt");
      if (shape == "rect")
        Rect(coords, href, target, alt, true);
      else if (shape == "circle")
        Circle(coords, href, target, alt, true);
      else
        Poly(coords, href, target, alt, true);
    }
}

function finishMap(){
  if (!setMapName())
    return false;
  if (!deleteAreas())
    return false;

  spots = frameDoc.getElementsByName("hotspot");
  var len = spots.length;
  if (len >= 1){
    for(i=0; i<len; i++){
      dump(i+"\n");
      curSpot = spots[i];
      if (curSpot.getAttribute("class") == "rect")
        createRect(curSpot);
      else if (curSpot.getAttribute("class") == "cir")
        createCir(curSpot);
      else
        createPoly(curSpot);
    }
    //try{
    //  GetCurrentEditor().root.appendChild(imageMap);
    //} catch (e) {}
    //returnValue = "test";
    //try{
    //  window.arguments[0] = "test"; //GetCurrentEditor().insertElementAtSelection(imageMap, false);
    //  dump(window.arguments[0]+"\n");
    //} catch (e) {}
    dump("imageMap.childNodes.length = "+imageMap.childNodes.length+"\n");
  }
  return true;
}

function setMapName() {
  //try {
  //  imageMap = GetCurrentEditor().createElementWithDefaults("map");
  //} catch (e) {}
  //dump(imageMap+"\n");
  //imageMap = frameDoc.createElement("map");

  mapName = imageMap.getAttribute("name");
  if (mapName == ""){
    mapName = String(frameDoc.getElementById("mainImg").getAttribute("src"));
  mapName = mapName.substring(mapName.lastIndexOf("/"), mapName.length);
  mapName = mapName.substring(mapName.lastIndexOf("\\"), mapName.length);
    mapName = mapName.substring(1, mapName.lastIndexOf("."));
    if (mapName == ""){
      // BUG causes substring to return nothing when
      // parameters are 1 & 13 (i.e. string.substring(1, 13);)
      mapName = "hack";
  }
  imageMap.setAttribute("name", mapName);
}
  return true;
}

function createRect(which){
  var newRect;
  //try {
  //  newRect = editor.createElementWithDefaults("area");
  //} catch (e) {}
  newRect = frameDoc.createElement("area");
  newRect.setAttribute("shape", "rect");
  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));
  newRect.setAttribute("coords", coords);
  if (which.getAttribute("hsHref") != ""){
    newRect.setAttribute("href", which.getAttribute("hsHref"));
  }
  else{
    newRect.setAttribute("nohref", "");
  }
  if (which.getAttribute("hsTarget") != ""){
  newRect.setAttribute("target", which.getAttribute("hsTarget"));
  }
  if (which.getAttribute("hsAlt") != ""){
  newRect.setAttribute("alt", which.getAttribute("hsAlt"));
  }
  //newRect.removeAttribute("id");
  imageMap.appendChild(newRect);
}

function createCir(which){
  var newCir;
  //try {
  //  newCir = editor.createElementWithDefaults("area");
  //} catch (e) {}
  newCir = frameDoc.createElement("area");
  if ( !newCir )
    return;

  newCir.setAttribute("shape", "circle");
  radius = Math.floor(parseInt(which.style.width)/2);
  coords = (parseInt(which.style.left)+radius)+","+(parseInt(which.style.top)+radius)+","+radius;
  newCir.setAttribute("coords", coords);
  if (which.getAttribute("hsHref") != "")
    newCir.setAttribute("href", which.getAttribute("hsHref"));
  else{
    newCir.setAttribute("nohref", "");
  }
  if (which.getAttribute("hsTarget") != ""){
    newCir.setAttribute("target", which.getAttribute("hsTarget"));
  }
  if (which.getAttribute("hsAlt") != ""){
    newCir.setAttribute("alt", which.getAttribute("hsAlt"));
  }
  //newCir.removeAttribute("id");
  imageMap.appendChild(newCir);
}

function createPoly(which){
  var newPoly;
  //try {
  //  newPoly = editor.createElementWithDefaults("area");
  //} catch (e) {}
  newPoly = frameDoc.createElement("area");
  if ( !newPoly )
    return;

  newPoly.setAttribute("shape", "poly");
  var coords = '';
  var len = which.childNodes.length;
  for(l=0; l<len; l++){
    coords += (parseInt(which.style.left)+parseInt(which.childNodes[l].style.left))+","+(parseInt(which.style.top)+parseInt(which.childNodes[l].style.top))+",";
  }
  coords = coords.substring(0, (coords.length-1));
  newPoly.setAttribute("coords", coords);
  if (which.getAttribute("hsHref") != "")
    newPoly.setAttribute("href", which.getAttribute("hsHref"));
  else{
    newPoly.setAttribute("nohref", "");
  }
  if (which.getAttribute("hsTarget") != ""){
    newPoly.setAttribute("target", which.getAttribute("hsTarget"));
  }
  if (which.getAttribute("hsAlt") != ""){
    newPoly.setAttribute("alt", which.getAttribute("hsAlt"));
  }
  //newPoly.removeAttribute("id");
  imageMap.appendChild(newPoly);
}

function hotSpotProps(which){
  var currentRect = null;
  var currentCir = null;
  if (which == null)
    return;
  hotSpotWin = window.openDialog("chrome://editor/content/EdImageMapHotSpot.xul", "_blank", "chrome,close,titlebar,modal", which);
}

function deleteAreas(){
  dump("deleteAreas called\n");
  area = imageMap.firstChild;
  while (area != null){
    dump(area+"\n");
    imageMap.removeChild(area);
    area = imageMap.firstChild;
  }
  return true;
}