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