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 / EdInputProps.js < prev    next >
Text File  |  2003-06-08  |  11KB  |  355 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. var insertNew;
  38. var inputElement;
  39.  
  40. // dialog initialization code
  41.  
  42. function Startup()
  43. {
  44.   var editor = GetCurrentEditor();
  45.   if (!editor)
  46.   {
  47.     window.close();
  48.     return;
  49.   }
  50.  
  51.   gDialog = {
  52.     accept:             document.documentElement.getButton("accept"),
  53.     inputType:          document.getElementById("InputType"),
  54.     inputNameDeck:      document.getElementById("InputNameDeck"),
  55.     inputName:          document.getElementById("InputName"),
  56.     inputValueDeck:     document.getElementById("InputValueDeck"),
  57.     inputValue:         document.getElementById("InputValue"),
  58.     inputDeck:          document.getElementById("InputDeck"),
  59.     inputChecked:       document.getElementById("InputChecked"),
  60.     inputSelected:      document.getElementById("InputSelected"),
  61.     inputReadOnly:      document.getElementById("InputReadOnly"),
  62.     inputDisabled:      document.getElementById("InputDisabled"),
  63.     inputTabIndex:      document.getElementById("InputTabIndex"),
  64.     inputAccessKey:     document.getElementById("InputAccessKey"),
  65.     inputSize:          document.getElementById("InputSize"),
  66.     inputMaxLength:     document.getElementById("InputMaxLength"),
  67.     inputAccept:        document.getElementById("InputAccept"),
  68.     MoreSection:        document.getElementById("MoreSection"),
  69.     MoreFewerButton:    document.getElementById("MoreFewerButton"),
  70.     AdvancedEditButton: document.getElementById("AdvancedEditButton"),
  71.     AdvancedEditDeck:   document.getElementById("AdvancedEditDeck")
  72.   };
  73.  
  74.   // Get a single selected input element
  75.   var tagName = "input";
  76.   inputElement = editor.getSelectedElement(tagName);
  77.  
  78.   if (inputElement)
  79.     // We found an element and don't need to insert one
  80.     insertNew = false;
  81.   else
  82.   {
  83.     insertNew = true;
  84.  
  85.     // We don't have an element selected,
  86.     //  so create one with default attributes
  87.  
  88.     inputElement = editor.createElementWithDefaults(tagName);
  89.     if (!inputElement)
  90.     {
  91.       dump("Failed to get selected element or create a new one!\n");
  92.       window.close();
  93.       return;
  94.     }
  95.  
  96.     var imageElement = editor.getSelectedElement("img");
  97.     if (imageElement)
  98.     {
  99.       // We found an image element, convert it to an input type="image"
  100.       inputElement.setAttribute("type", "image");
  101.  
  102.       var attributes = ["src", "alt", "width", "height", "hspace", "vspace", "border", "align"];
  103.       for (i in attributes)
  104.         inputElement.setAttribute(attributes[i], imageElement.getAttribute(attributes[i]));
  105.     }
  106.     else
  107.       inputElement.setAttribute("value", GetSelectionAsText());
  108.   }
  109.  
  110.   // Make a copy to use for AdvancedEdit
  111.   globalElement = inputElement.cloneNode(false);
  112.  
  113.   InitDialog();
  114.  
  115.   InitMoreFewer();
  116.  
  117.   gDialog.inputType.focus();
  118.  
  119.   SetWindowLocation();
  120. }
  121.  
  122. function InitDialog()
  123. {
  124.   var type = globalElement.getAttribute("type");
  125.   var index = 0;
  126.   switch (type)
  127.   {
  128.     case "button":
  129.       index = 9;
  130.       break;
  131.     case "checkbox":
  132.       index = 2;
  133.       break;
  134.     case "file":
  135.       index = 6;
  136.       break;
  137.     case "hidden":
  138.       index = 7;
  139.       break;
  140.     case "image":
  141.       index = 8;
  142.       break;
  143.     case "password":
  144.       index = 1;
  145.       break;
  146.     case "radio":
  147.       index = 3;
  148.       break;
  149.     case "reset":
  150.       index = 5;
  151.       break;
  152.     case "submit":
  153.       index = 4;
  154.       break;
  155.   }
  156.   gDialog.inputType.selectedIndex = index;
  157.   gDialog.inputName.value = globalElement.getAttribute("name");
  158.   gDialog.inputValue.value = globalElement.getAttribute("value");
  159.   gDialog.inputChecked.setAttribute("checked", globalElement.hasAttribute("checked"));
  160.   gDialog.inputSelected.setAttribute("checked", globalElement.hasAttribute("checked"));
  161.   gDialog.inputReadOnly.setAttribute("checked", globalElement.hasAttribute("readonly"));
  162.   gDialog.inputDisabled.setAttribute("checked", globalElement.hasAttribute("disabled"));
  163.   gDialog.inputTabIndex.value = globalElement.getAttribute("tabindex");
  164.   gDialog.inputAccessKey.value = globalElement.getAttribute("accesskey");
  165.   gDialog.inputSize.value = globalElement.getAttribute("size");
  166.   gDialog.inputMaxLength.value = globalElement.getAttribute("maxlength");
  167.   gDialog.inputAccept.value = globalElement.getAttribute("accept");
  168.   SelectInputType();
  169. }
  170.  
  171. function SelectInputType()
  172. {
  173.   var index = gDialog.inputType.selectedIndex;
  174.   gDialog.AdvancedEditDeck.setAttribute("selectedIndex", 0);
  175.   gDialog.inputNameDeck.setAttribute("selectedIndex", 0);
  176.   gDialog.inputValueDeck.setAttribute("selectedIndex", 0);
  177.   gDialog.inputValue.disabled = false;
  178.   gDialog.inputChecked.disabled = index != 2;
  179.   gDialog.inputSelected.disabled = index != 3;
  180.   gDialog.inputReadOnly.disabled = index > 1;
  181.   gDialog.inputTabIndex.disabled = index == 7;
  182.   gDialog.inputAccessKey.disabled = index == 7;
  183.   gDialog.inputSize.disabled = index > 1;
  184.   gDialog.inputMaxLength.disabled = index > 1;
  185.   gDialog.inputAccept.disabled = index != 6;
  186.   switch (index)
  187.   {
  188.     case 0:
  189.     case 1:
  190.       gDialog.inputValueDeck.setAttribute("selectedIndex", 1);
  191.       gDialog.inputDeck.setAttribute("selectedIndex", 2);
  192.       break;
  193.     case 2:
  194.       gDialog.inputDeck.setAttribute("selectedIndex", 0);
  195.       break;
  196.     case 3:
  197.       gDialog.inputDeck.setAttribute("selectedIndex", 1);
  198.       gDialog.inputNameDeck.setAttribute("selectedIndex", 1);
  199.       break;
  200.     case 6:
  201.       gDialog.inputValue.disabled = true;
  202.       gDialog.inputAccept.disabled = false;
  203.       break;
  204.     case 8:
  205.       gDialog.inputValue.disabled = true;
  206.       gDialog.AdvancedEditDeck.setAttribute("selectedIndex", 1);
  207.       gDialog.inputName.removeEventListener("input", onInput, false);
  208.       break;
  209.     case 7:
  210.       gDialog.inputValueDeck.setAttribute("selectedIndex", 1);
  211.       break;
  212.   }
  213.   onInput();
  214. }
  215.  
  216. function onInput()
  217. {
  218.   var disabled = false;;
  219.   switch (gDialog.inputType.selectedIndex)
  220.   {
  221.   case 3:
  222.     disabled = disabled || !gDialog.inputValue.value;
  223.   case 4:
  224.   case 5:
  225.     break;
  226.   case 8:
  227.     disabled = !globalElement.hasAttribute("src");
  228.     break;
  229.   default:
  230.     disabled = !gDialog.inputName.value
  231.     break;
  232.   }
  233.   if (gDialog.accept.disabled != disabled)
  234.   {
  235.     gDialog.accept.disabled = disabled;
  236.     gDialog.AdvancedEditButton.disabled = disabled;
  237.   }
  238. }
  239.  
  240. function doImageProperties()
  241. {
  242.   window.openDialog("chrome://editor/content/EdImageProps.xul",
  243.                     "_blank", "chrome,close,titlebar,modal", globalElement);
  244.   window.focus();
  245.   onInput();
  246. }
  247.  
  248. function ValidateData()
  249. {
  250.   var attributes = {
  251.     type: "",
  252.     name: gDialog.inputName.value,
  253.     value: gDialog.inputValue.value,
  254.     tabindex: gDialog.inputTabIndex.value,
  255.     accesskey: "",
  256.     size: "",
  257.     maxlength: "",
  258.     accept: ""
  259.   };
  260.   var index = gDialog.inputType.selectedIndex;
  261.   var flags = {
  262.     checked: false,
  263.     readonly: false,
  264.     disabled: gDialog.inputDisabled.checked
  265.   };
  266.   switch (index)
  267.   {
  268.     case 1:
  269.       attributes.type = "password";
  270.     case 0:
  271.       flags.readonly = gDialog.inputReadOnly.checked;
  272.       attributes.size = gDialog.inputSize.value;
  273.       attributes.maxlength = gDialog.inputMaxLength.value;
  274.       break;
  275.     case 2:
  276.       attributes.type = "checkbox";
  277.       flags.checked = gDialog.inputChecked.checked;
  278.       break;
  279.     case 3:
  280.       attributes.type = "radio";
  281.       flags.checked = gDialog.inputSelected.checked;
  282.       break;
  283.     case 4:
  284.       attributes.type = "submit";
  285.       attributes.accesskey = gDialog.inputAccessKey.value;
  286.       break;
  287.     case 5:
  288.       attributes.type = "reset";
  289.       attributes.accesskey = gDialog.inputAccessKey.value;
  290.       break;
  291.     case 6:
  292.       attributes.type = "file";
  293.       attributes.accept = gDialog.inputAccept.value;
  294.       attributes.value = "";
  295.       break;
  296.     case 7:
  297.       attributes.type = "hidden";
  298.       attributes.tabindex = "";
  299.       break;
  300.     case 8:
  301.       attributes.type = "image";
  302.       attributes.value = "";
  303.       break;
  304.     case 9:
  305.       attributes.type = "button";
  306.       attributes.accesskey = gDialog.inputAccessKey.value;
  307.       break;
  308.   }
  309.   for (var a in attributes)
  310.   {
  311.     if (attributes[a])
  312.       globalElement.setAttribute(a, attributes[a]);
  313.     else
  314.       globalElement.removeAttribute(a);
  315.   }
  316.   for (var f in flags)
  317.   {
  318.     if (flags[f])
  319.       globalElement.setAttribute(f, "");
  320.     else
  321.       globalElement.removeAttribute(f);
  322.   }
  323.   return true;
  324. }
  325.  
  326. function onAccept()
  327. {
  328.   if (ValidateData())
  329.   {
  330.     // All values are valid - copy to actual element in doc or
  331.     //   element created to insert
  332.  
  333.     var editor = GetCurrentEditor();
  334.  
  335.     editor.cloneAttributes(inputElement, globalElement);
  336.  
  337.     if (insertNew)
  338.     {
  339.       try {
  340.         // 'true' means delete the selection before inserting
  341.         // in case were are converting an image to an input type="image"
  342.         editor.insertElementAtSelection(inputElement, true);
  343.       } catch (e) {
  344.         dump(e);
  345.       }
  346.     }
  347.  
  348.     SaveWindowLocation();
  349.  
  350.     return true;
  351.   }
  352.   return false;
  353. }
  354.  
  355.