home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Complet / thunderbird / chrome / mail.jar / content / editor / EdFieldSetProps.js < prev    next >
Encoding:
JavaScript  |  2003-05-27  |  6.4 KB  |  219 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.getElementOrParentByTagName(kTagName, editor.selection.anchorNode);
  66.     if (!fieldsetElement)
  67.       fieldsetElement = editor.getElementOrParentByTagName(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.selectNode(legendElement);
  99.     gDialog.legendText.value = range.toString();
  100.     if (/</.test(legendElement.innerHTML))
  101.     {
  102.       gDialog.editText.checked = false;
  103.       gDialog.editText.disabled = false;
  104.       gDialog.legendText.disabled = true;
  105.       gDialog.editText.addEventListener("command", onEditText, false);
  106.       gDialog.RemoveFieldSet.focus();
  107.     }
  108.     else
  109.       SetTextboxFocus(gDialog.legendText);
  110.   }
  111.   else
  112.   {
  113.     newLegend = true;
  114.  
  115.     // We don't have an element selected,
  116.     //  so create one with default attributes
  117.  
  118.     legendElement = editor.createElementWithDefaults("legend");
  119.     if (!legendElement)
  120.     {
  121.       dump("Failed to get selected element or create a new one!\n");
  122.       window.close();
  123.       return;
  124.     }
  125.     SetTextboxFocus(gDialog.legendText);
  126.   }
  127.  
  128.   // Make a copy to use for AdvancedEdit
  129.   globalElement = legendElement.cloneNode(false);
  130.  
  131.   InitDialog();
  132.  
  133.   SetWindowLocation();
  134. }
  135.  
  136. function InitDialog()
  137. {
  138.   gDialog.legendAlign.value = GetHTMLOrCSSStyleValue(globalElement, "align", "caption-side");
  139. }
  140.  
  141. function onEditText()
  142. {
  143.   gDialog.editText.removeEventListener("command", onEditText, false);
  144.   AlertWithTitle(GetString("Alert"), GetString("EditTextWarning"));
  145. }
  146.  
  147. function RemoveFieldSet()
  148. {
  149.   var editor = GetCurrentEditor();
  150.   editor.beginTransaction();
  151.   try {
  152.     if (!newLegend)
  153.       editor.deleteNode(legendElement);
  154.     RemoveBlockContainer(fieldsetElement);
  155.   } finally {
  156.     editor.endTransaction();
  157.   }
  158.   SaveWindowLocation();
  159.   window.close();
  160. }
  161.  
  162. function ValidateData()
  163. {
  164.   if (gDialog.legendAlign.value)
  165.     globalElement.setAttribute("align", gDialog.legendAlign.value);
  166.   else
  167.     globalElement.removeAttribute("align");
  168.   return true;
  169. }
  170.  
  171. function onAccept()
  172. {
  173.   // All values are valid - copy to actual element in doc
  174.   ValidateData();
  175.  
  176.   var editor = GetCurrentEditor();
  177.  
  178.   editor.beginTransaction();
  179.  
  180.   try {
  181.     editor.cloneAttributes(legendElement, globalElement);
  182.  
  183.     if (insertNew)
  184.     {
  185.       if (gDialog.legendText.value)
  186.       {
  187.         fieldsetElement.appendChild(legendElement);
  188.         legendElement.appendChild(editor.document.createTextNode(gDialog.legendText.value));
  189.       }
  190.       InsertElementAroundSelection(fieldsetElement);
  191.     }
  192.     else if (gDialog.editText.checked)
  193.     {
  194.       editor.setShouldTxnSetSelection(false);
  195.  
  196.       if (gDialog.legendText.value)
  197.       {
  198.         if (newLegend)
  199.           editor.insertNode(legendElement, fieldsetElement, 0, true);
  200.         else while (legendElement.firstChild)
  201.           editor.deleteNode(legendElement.lastChild);
  202.         editor.insertNode(editor.document.createTextNode(gDialog.legendText.value), legendElement, 0);
  203.       }
  204.       else if (!newLegend)
  205.         editor.deleteNode(legendElement);
  206.  
  207.       editor.setShouldTxnSetSelection(true);
  208.     }
  209.   }
  210.   finally {
  211.     editor.endTransaction();
  212.   }
  213.  
  214.   SaveWindowLocation();
  215.  
  216.   return true;
  217. }
  218.  
  219.