home *** CD-ROM | disk | FTP | other *** search
/ ftp.swcp.com / ftp.swcp.com.zip / ftp.swcp.com / mac / mozilla-macos9-1.3.1.sea.bin / Mozilla1.3.1 / Chrome / comm.jar / content / editor / EdImageMap.js < prev    next >
Text File  |  2003-06-08  |  11KB  |  364 lines

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