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 / EdInputImage.js < prev    next >
Text File  |  2003-06-08  |  6KB  |  212 lines

  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 Input Tag Properties Dialog.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Neil Rashbrook.
  18.  * Portions created by the Initial Developer are Copyright (C) 2001
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s): Neil Rashbrook <neil@parkwaycc.co.uk>
  22.  *
  23.  * Alternatively, the contents of this file may be used under the terms of
  24.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  25.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26.  * in which case the provisions of the GPL or the LGPL are applicable instead
  27.  * of those above. If you wish to allow use of your version of this file only
  28.  * under the terms of either the GPL or the LGPL, and not to allow others to
  29.  * use your version of this file under the terms of the MPL, indicate your
  30.  * decision by deleting the provisions above and replace them with the notice
  31.  * and other provisions required by the GPL or the LGPL. If you do not delete
  32.  * the provisions above, a recipient may use your version of this file under
  33.  * the terms of any one of the MPL, the GPL or the LGPL.
  34.  *
  35.  * ***** END LICENSE BLOCK ***** */
  36.  
  37. // dialog initialization code
  38.  
  39. function Startup()
  40. {
  41.   var editor = GetCurrentEditor();
  42.   if (!editor)
  43.   {
  44.     window.close();
  45.     return;
  46.   }
  47.  
  48.   gDialog = {
  49.     inputName:      document.getElementById( "InputName" ),
  50.     inputDisabled:  document.getElementById( "InputDisabled" ),
  51.     inputTabIndex:  document.getElementById( "InputTabIndex" )
  52.   };
  53.  
  54.   ImageStartup();
  55.  
  56.   // Get a single selected input element
  57.   var tagName = "input";
  58.   try {
  59.     imageElement = editor.getSelectedElement(tagName);
  60.   } catch (e) {}
  61.  
  62.   if (imageElement)
  63.   {
  64.     // We found an element and don't need to insert one
  65.     gInsertNewImage = false;
  66.   }
  67.   else
  68.   {
  69.     gInsertNewImage = true;
  70.  
  71.     // We don't have an element selected,
  72.     //  so create one with default attributes
  73.     try {
  74.       imageElement = editor.createElementWithDefaults(tagName);
  75.     } catch(e) {}
  76.  
  77.     if (!imageElement )
  78.     {
  79.       dump("Failed to get selected element or create a new one!\n");
  80.       window.close();
  81.       return;
  82.     }
  83.     var imgElement;
  84.     try {
  85.       imgElement = editor.getSelectedElement("img");
  86.     } catch(e) {}
  87.  
  88.     if (imgElement)
  89.     {
  90.       // We found an image element, convert it to an input type="image"
  91.       var attributes = ["src", "alt", "width", "height", "hspace", "vspace", "border", "align", "usemap", "ismap"];
  92.       for (i in attributes)
  93.         imageElement.setAttribute(attributes[i], imageElement.getAttribute(attributes[i]));
  94.     }
  95.   }
  96.  
  97.   // Make a copy to use for AdvancedEdit
  98.   globalElement = imageElement.cloneNode(false);
  99.  
  100.   // We only need to test for this once per dialog load
  101.   gHaveDocumentUrl = GetDocumentBaseUrl();
  102.  
  103.   InitDialog();
  104.  
  105.   // Save initial source URL
  106.   gOriginalSrc = gDialog.srcInput.value;
  107.  
  108.   // By default turn constrain on, but both width and height must be in pixels
  109.   gDialog.constrainCheckbox.checked =
  110.     gDialog.widthUnitsMenulist.selectedIndex == 0 &&
  111.     gDialog.heightUnitsMenulist.selectedIndex == 0;
  112.  
  113.   SetTextboxFocus(gDialog.inputName);
  114.  
  115.   SetWindowLocation();
  116. }
  117.  
  118. function InitDialog()
  119. {
  120.   InitImage();
  121.   gDialog.inputName.value = globalElement.getAttribute("name");
  122.   gDialog.inputDisabled.setAttribute("checked", globalElement.hasAttribute("disabled"));
  123.   gDialog.inputTabIndex.value = globalElement.getAttribute("tabindex");
  124. }
  125.  
  126. function ValidateData()
  127. {
  128.   if (!ValidateImage())
  129.     return false;
  130.   if (gDialog.inputName.value)
  131.     globalElement.setAttribute("name", gDialog.inputName.value);
  132.   else
  133.     globalElement.removeAttribute("name");
  134.   if (gDialog.inputTabIndex.value)
  135.     globalElement.setAttribute("tabindex", gDialog.inputTabIndex.value);
  136.   else
  137.     globalElement.removeAttribute("tabindex");
  138.   if (gDialog.inputDisabled.checked)
  139.     globalElement.setAttribute("disabled", "");
  140.   else
  141.     globalElement.removeAttribute("disabled");
  142.   globalElement.setAttribute("type", "image");
  143.   return true;
  144. }
  145.  
  146. function onAccept()
  147. {
  148.   // Show alt text error only once
  149.   // (we don't initialize doAltTextError=true
  150.   //  so Advanced edit button dialog doesn't trigger that error message)
  151.   // Use this now (default = false) so Advanced Edit button dialog doesn't trigger error message
  152.   gDoAltTextError = true;
  153.  
  154.   if (ValidateData())
  155.   {
  156.  
  157.     var editor = GetCurrentEditor();
  158.     editor.beginTransaction();
  159.  
  160.     try {
  161.       if (gRemoveImageMap)
  162.       {
  163.         globalElement.removeAttribute("usemap");
  164.         if (gImageMap)
  165.         {
  166.           editor.deleteNode(gImageMap);
  167.           gInsertNewIMap = true;
  168.           gImageMap = null;
  169.         }
  170.       }
  171.       else if (gImageMap)
  172.       {
  173.         // Assign to map if there is one
  174.         var mapName = gImageMap.getAttribute("name");
  175.         if (mapName != "")
  176.         {
  177.           globalElement.setAttribute("usemap", ("#"+mapName));
  178.           if (globalElement.getAttribute("border") == "")
  179.             globalElement.setAttribute("border", 0);
  180.         }
  181.       }
  182.  
  183.       if (gInsertNewImage)
  184.       {
  185.         // 'true' means delete the selection before inserting
  186.         // in case were are converting an image to an input type="image"
  187.         editor.insertElementAtSelection(imageElement, true);
  188.       }
  189.       editor.cloneAttributes(imageElement, globalElement);
  190.  
  191.       // If document is empty, the map element won't insert,
  192.       //  so always insert the image element first
  193.       if (gImageMap && gInsertNewIMap)
  194.       {
  195.         // Insert the ImageMap element at beginning of document
  196.         var body = editor.rootElement;
  197.         editor.setShouldTxnSetSelection(false);
  198.         editor.insertNode(gImageMap, body, 0);
  199.         editor.setShouldTxnSetSelection(true);
  200.       }
  201.     } catch (e) {}
  202.  
  203.     editor.endTransaction();
  204.  
  205.     SaveWindowLocation();
  206.  
  207.     return true;
  208.   }
  209.   return false;
  210. }
  211.  
  212.