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 / EdFieldSetProps.js < prev    next >
Text File  |  2003-06-08  |  6KB  |  216 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 Field Set 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 fieldsetElement;
  39. var newLegend;
  40. var legendElement;
  41.  
  42. // dialog initialization code
  43.  
  44. function Startup()
  45. {
  46.   var editor = GetCurrentEditor();
  47.   if (!editor)
  48.   {
  49.     dump("Failed to get active editor!\n");
  50.     window.close();
  51.     return;
  52.   }
  53.  
  54.   gDialog.editText = document.getElementById("EditText");
  55.   gDialog.legendText = document.getElementById("LegendText");
  56.   gDialog.legendAlign = document.getElementById("LegendAlign");
  57.   gDialog.RemoveFieldSet = document.getElementById("RemoveFieldSet");
  58.  
  59.   // Get a single selected field set element
  60.   const kTagName = "fieldset";
  61.   try {
  62.     // Find a selected fieldset, or if one is at start or end of selection.
  63.     fieldsetElement = editor.getSelectedElement(kTagName);
  64.     if (!fieldsetElement)
  65.       fieldsetElement = editor.getElementOrParentBykTagName(kTagName, editor.selection.anchorNode);
  66.     if (!fieldsetElement)
  67.       fieldsetElement = editor.getElementOrParentBykTagName(kTagName, editor.selection.focusNode);
  68.   } catch (e) {}
  69.  
  70.   if (fieldsetElement)
  71.     // We found an element and don't need to insert one
  72.     insertNew = false;
  73.   else
  74.   {
  75.     insertNew = true;
  76.  
  77.     // We don't have an element selected,
  78.     //  so create one with default attributes
  79.     try {
  80.       fieldsetElement = editor.createElementWithDefaults(kTagName);
  81.     } catch (e) {}
  82.  
  83.     if (!fieldsetElement)
  84.     {
  85.       dump("Failed to get selected element or create a new one!\n");
  86.       window.close();
  87.       return;
  88.     }
  89.     // Hide button removing existing fieldset
  90.     gDialog.RemoveFieldSet.hidden = true;
  91.   }
  92.  
  93.   legendElement = fieldsetElement.firstChild;
  94.   if (legendElement && legendElement.localName == "LEGEND")
  95.   {
  96.     newLegend = false;
  97.     var range = editor.document.createRange();
  98.     range.setStart(legendElement, 0);
  99.     range.setEnd(legendElement, legendElement.childNodes.length);
  100.     gDialog.legendText.value = range.toString();
  101.     if (/</.test(legendElement.innerHTML))
  102.     {
  103.       gDialog.editText.checked = false;
  104.       gDialog.editText.disabled = false;
  105.       gDialog.legendText.disabled = true;
  106.       gDialog.editText.addEventListener("command", onEditText, false);
  107.       gDialog.RemoveFieldSet.focus();
  108.     }
  109.     else
  110.       SetTextboxFocus(gDialog.legendText);
  111.   }
  112.   else
  113.   {
  114.     newLegend = true;
  115.  
  116.     // We don't have an element selected,
  117.     //  so create one with default attributes
  118.  
  119.     legendElement = editor.createElementWithDefaults("legend");
  120.     if (!legendElement)
  121.     {
  122.       dump("Failed to get selected element or create a new one!\n");
  123.       window.close();
  124.       return;
  125.     }
  126.     SetTextboxFocus(gDialog.legendText);
  127.   }
  128.  
  129.   // Make a copy to use for AdvancedEdit
  130.   globalElement = legendElement.cloneNode(false);
  131.  
  132.   InitDialog();
  133.  
  134.   SetWindowLocation();
  135. }
  136.  
  137. function InitDialog()
  138. {
  139.   gDialog.legendAlign.value = GetHTMLOrCSSStyleValue(globalElement, "align", "caption-side");
  140. }
  141.  
  142. function onEditText()
  143. {
  144.   gDialog.editText.removeEventListener("command", onEditText, false);
  145.   AlertWithTitle(GetString("Alert"), GetString("EditTextWarning"));
  146. }
  147.  
  148. function RemoveFieldSet()
  149. {
  150.   var editor = GetCurrentEditor();
  151.   editor.beginTransaction();
  152.   try {
  153.     if (!newLegend)
  154.       editor.DeleteNode(legendElement);
  155.     // This really needs to call the C++ function RemoveBlockContainer
  156.     // which inserts any <BR>s needed
  157.     RemoveElementKeepingChildren(fieldsetElement);
  158.   } finally {
  159.     editor.endTransaction();
  160.   }
  161.   SaveWindowLocation();
  162.   window.close();
  163. }
  164.  
  165. function ValidateData()
  166. {
  167.   if (gDialog.legendAlign.value)
  168.     globalElement.setAttribute("align", gDialog.legendAlign.value);
  169.   else
  170.     globalElement.removeAttribute("align");
  171.   return true;
  172. }
  173.  
  174. function onAccept()
  175. {
  176.   // All values are valid - copy to actual element in doc
  177.   ValidateData();
  178.  
  179.   var editor = GetCurrentEditor();
  180.  
  181.   editor.beginTransaction();
  182.  
  183.   try {
  184.     editor.cloneAttributes(legendElement, globalElement);
  185.  
  186.     if (insertNew)
  187.       InsertElementAroundSelection(fieldsetElement);
  188.  
  189.     if (gDialog.editText.checked)
  190.     {
  191.       editor.setShouldTxnSetSelection(false);
  192.  
  193.       if (gDialog.legendText.value)
  194.       {
  195.         if (newLegend)
  196.           editor.insertNode(legendElement, fieldsetElement, 0, true);
  197.         else while (legendElement.firstChild)
  198.           editor.deleteNode(legendElement.lastChild);
  199.         editor.insertNode(editor.document.createTextNode(gDialog.legendText.value), legendElement, 0);
  200.       }
  201.       else if (!newLegend)
  202.         editor.DeleteNode(legendElement);
  203.  
  204.       editor.setShouldTxnSetSelection(true);
  205.     }
  206.   }
  207.   finally {
  208.     editor.endTransaction();
  209.   }
  210.  
  211.   SaveWindowLocation();
  212.  
  213.   return true;
  214. }
  215.  
  216.