home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Complet / thunderbird / chrome / mail.jar / content / editor / EdButtonProps.js < prev    next >
Encoding:
JavaScript  |  2003-05-27  |  5.3 KB  |  179 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 Button 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 buttonElement;
  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.     buttonType:       document.getElementById("ButtonType"),
  53.     buttonName:       document.getElementById("ButtonName"),
  54.     buttonValue:      document.getElementById("ButtonValue"),
  55.     buttonDisabled:   document.getElementById("ButtonDisabled"),
  56.     buttonTabIndex:   document.getElementById("ButtonTabIndex"),
  57.     buttonAccessKey:  document.getElementById("ButtonAccessKey"),
  58.     MoreSection:      document.getElementById("MoreSection"),
  59.     MoreFewerButton:  document.getElementById("MoreFewerButton"),
  60.     RemoveButton:     document.getElementById("RemoveButton")
  61.   };
  62.  
  63.   // Get a single selected button element
  64.   const kTagName = "button";
  65.   try {
  66.     buttonElement = editor.getSelectedElement(kTagName);
  67.   } catch (e) {}
  68.  
  69.   if (buttonElement)
  70.     // We found an element and don't need to insert one
  71.     insertNew = false;
  72.   else
  73.   {
  74.     insertNew = true;
  75.  
  76.     // We don't have an element selected,
  77.     //  so create one with default attributes
  78.     try {
  79.       buttonElement = editor.createElementWithDefaults(kTagName);
  80.     } catch (e) {}
  81.  
  82.     if (!buttonElement)
  83.     {
  84.       dump("Failed to get selected element or create a new one!\n");
  85.       window.close();
  86.       return;
  87.     }
  88.     // Hide button removing existing button
  89.     gDialog.RemoveButton.hidden = true;
  90.   }
  91.  
  92.   // Make a copy to use for AdvancedEdit
  93.   globalElement = buttonElement.cloneNode(false);
  94.  
  95.   InitDialog();
  96.  
  97.   InitMoreFewer();
  98.  
  99.   gDialog.buttonType.focus();
  100.  
  101.   SetWindowLocation();
  102. }
  103.  
  104. function InitDialog()
  105. {
  106.   var type = globalElement.getAttribute("type");
  107.   var index = 0;
  108.   switch (type)
  109.   {
  110.     case "button":
  111.       index = 2;
  112.       break;
  113.     case "reset":
  114.       index = 1;
  115.       break;
  116.   }
  117.   gDialog.buttonType.selectedIndex = index;
  118.   gDialog.buttonName.value = globalElement.getAttribute("name");
  119.   gDialog.buttonValue.value = globalElement.getAttribute("value");
  120.   gDialog.buttonDisabled.setAttribute("checked", globalElement.hasAttribute("disabled"));
  121.   gDialog.buttonTabIndex.value = globalElement.getAttribute("tabindex");
  122.   gDialog.buttonAccessKey.value = globalElement.getAttribute("accesskey");
  123. }
  124.  
  125. function RemoveButton()
  126. {
  127.   RemoveContainer(buttonElement);
  128.   SaveWindowLocation();
  129.   window.close();
  130. }
  131.  
  132. function ValidateData()
  133. {
  134.   var attributes = {
  135.     type: ["", "reset", "button"][gDialog.buttonType.selectedIndex],
  136.     name: gDialog.buttonName.value,
  137.     value: gDialog.buttonValue.value,
  138.     tabindex: gDialog.buttonTabIndex.value,
  139.     accesskey: gDialog.buttonAccessKey.value
  140.   };
  141.   for (var a in attributes)
  142.   {
  143.     if (attributes[a])
  144.       globalElement.setAttribute(a, attributes[a]);
  145.     else
  146.       globalElement.removeAttribute(a);
  147.   }
  148.   if (gDialog.buttonDisabled.checked)
  149.     globalElement.setAttribute("disabled", "");
  150.   else
  151.     globalElement.removeAttribute("disabled");
  152.   return true;
  153. }
  154.  
  155. function onAccept()
  156. {
  157.   // All values are valid - copy to actual element in doc or
  158.   //   element created to insert
  159.   ValidateData();
  160.  
  161.   var editor = GetCurrentEditor();
  162.  
  163.   editor.cloneAttributes(buttonElement, globalElement);
  164.  
  165.   if (insertNew)
  166.   {
  167.     if (!InsertElementAroundSelection(buttonElement))
  168.     {
  169.       buttonElement.innerHTML = editor.outputToString("text/html", 1); // OutputSelectionOnly (see nsIDocumentEncoder.h)
  170.       editor.insertElementAtSelection(buttonElement, true);
  171.     }
  172.   }
  173.  
  174.   SaveWindowLocation();
  175.  
  176.   return true;
  177. }
  178.  
  179.