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 / EdButtonProps.js < prev    next >
Text File  |  2003-06-08  |  5KB  |  172 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.   var tagName = "button";
  65.   buttonElement = editor.getSelectedElement(tagName);
  66.  
  67.   if (buttonElement)
  68.     // We found an element and don't need to insert one
  69.     insertNew = false;
  70.   else
  71.   {
  72.     insertNew = true;
  73.  
  74.     // We don't have an element selected,
  75.     //  so create one with default attributes
  76.  
  77.     buttonElement = editor.createElementWithDefaults(tagName);
  78.     if (!buttonElement)
  79.     {
  80.       dump("Failed to get selected element or create a new one!\n");
  81.       window.close();
  82.       return;
  83.     }
  84.     // Hide button removing existing button
  85.     gDialog.RemoveButton.hidden = true;
  86.   }
  87.  
  88.   // Make a copy to use for AdvancedEdit
  89.   globalElement = buttonElement.cloneNode(false);
  90.  
  91.   InitDialog();
  92.  
  93.   InitMoreFewer();
  94.  
  95.   gDialog.buttonType.focus();
  96.  
  97.   SetWindowLocation();
  98. }
  99.  
  100. function InitDialog()
  101. {
  102.   var type = globalElement.getAttribute("type");
  103.   var index = 0;
  104.   switch (type)
  105.   {
  106.     case "button":
  107.       index = 2;
  108.       break;
  109.     case "reset":
  110.       index = 1;
  111.       break;
  112.   }
  113.   gDialog.buttonType.selectedIndex = index;
  114.   gDialog.buttonName.value = globalElement.getAttribute("name");
  115.   gDialog.buttonValue.value = globalElement.getAttribute("value");
  116.   gDialog.buttonDisabled.setAttribute("checked", globalElement.hasAttribute("disabled"));
  117.   gDialog.buttonTabIndex.value = globalElement.getAttribute("tabindex");
  118.   gDialog.buttonAccessKey.value = globalElement.getAttribute("accesskey");
  119. }
  120.  
  121. function RemoveButton()
  122. {
  123.   RemoveElementKeepingChildren(buttonElement);
  124.   SaveWindowLocation();
  125.   window.close();
  126. }
  127.  
  128. function ValidateData()
  129. {
  130.   var attributes = {
  131.     type: ["", "reset", "button"][gDialog.buttonType.selectedIndex],
  132.     name: gDialog.buttonName.value,
  133.     value: gDialog.buttonValue.value,
  134.     tabindex: gDialog.buttonTabIndex.value,
  135.     accesskey: gDialog.buttonAccessKey.value
  136.   };
  137.   for (var a in attributes)
  138.   {
  139.     if (attributes[a])
  140.       globalElement.setAttribute(a, attributes[a]);
  141.     else
  142.       globalElement.removeAttribute(a);
  143.   }
  144.   if (gDialog.buttonDisabled.checked)
  145.     globalElement.setAttribute("disabled", "");
  146.   else
  147.     globalElement.removeAttribute("disabled");
  148.   return true;
  149. }
  150.  
  151. function onAccept()
  152. {
  153.   // All values are valid - copy to actual element in doc or
  154.   //   element created to insert
  155.   ValidateData();
  156.  
  157.   var editor = GetCurrentEditor();
  158.  
  159.   editor.cloneAttributes(buttonElement, globalElement);
  160.  
  161.   if (insertNew && !InsertElementAroundSelection(buttonElement))
  162.   {
  163.     buttonElement.innerHTML = editor.outputToString("text/html", 1); // OutputSelectionOnly (see nsIDocumentEncoder.h)
  164.     editor.insertElementAtSelection(buttonElement, true);
  165.   }
  166.  
  167.   SaveWindowLocation();
  168.  
  169.   return true;
  170. }
  171.  
  172.