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