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 / EdTextAreaProps.js < prev    next >
Text File  |  2003-06-08  |  6KB  |  201 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 Text Area 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 textareaElement;
  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.     textareaName:       document.getElementById("TextAreaName"),
  55.     textareaRows:       document.getElementById("TextAreaRows"),
  56.     textareaCols:       document.getElementById("TextAreaCols"),
  57.     textareaWrap:       document.getElementById("TextAreaWrap"),
  58.     textareaReadOnly:   document.getElementById("TextAreaReadOnly"),
  59.     textareaDisabled:   document.getElementById("TextAreaDisabled"),
  60.     textareaTabIndex:   document.getElementById("TextAreaTabIndex"),
  61.     textareaAccessKey:  document.getElementById("TextAreaAccessKey"),
  62.     textareaValue:      document.getElementById("TextAreaValue"),
  63.     MoreSection:        document.getElementById("MoreSection"),
  64.     MoreFewerButton:    document.getElementById("MoreFewerButton")
  65.   };
  66.  
  67.   // Get a single selected text area element
  68.   const kTagName = "textarea";
  69.   try {
  70.     textareaElement = editor.getSelectedElement(kTagName);
  71.   } catch (e) {}
  72.  
  73.   if (textareaElement) {
  74.     // We found an element and don't need to insert one
  75.     insertNew = false;
  76.  
  77.     gDialog.textareaValue.value = textareaElement.value;
  78.   }
  79.   else
  80.   {
  81.     insertNew = true;
  82.  
  83.     // We don't have an element selected,
  84.     //  so create one with default attributes
  85.     try {
  86.       textareaElement = editor.createElementWithDefaults(kTagName);
  87.     } catch(e) {}
  88.  
  89.     if (!textareaElement)
  90.     {
  91.       dump("Failed to get selected element or create a new one!\n");
  92.       window.close();
  93.       return;
  94.     }
  95.     else
  96.       gDialog.textareaValue.value = GetSelectionAsText();
  97.   }
  98.  
  99.   // Make a copy to use for AdvancedEdit
  100.   globalElement = textareaElement.cloneNode(false);
  101.  
  102.   InitDialog();
  103.  
  104.   InitMoreFewer();
  105.  
  106.   SetTextboxFocus(gDialog.textareaName);
  107.   
  108.   SetWindowLocation();
  109. }
  110.  
  111. function InitDialog()
  112. {
  113.   gDialog.textareaName.value = globalElement.getAttribute("name");
  114.   gDialog.textareaRows.value = globalElement.getAttribute("rows");
  115.   gDialog.textareaCols.value = globalElement.getAttribute("cols");
  116.   gDialog.textareaWrap.value = GetHTMLOrCSSStyleValue(globalElement, "wrap", "white-space");
  117.   gDialog.textareaReadOnly.checked = globalElement.hasAttribute("readonly");
  118.   gDialog.textareaDisabled.checked = globalElement.hasAttribute("disabled");
  119.   gDialog.textareaTabIndex.value = globalElement.getAttribute("tabindex");
  120.   gDialog.textareaAccessKey.value = globalElement.getAttribute("accesskey");
  121.   onInput();
  122. }
  123.  
  124. function onInput()
  125. {
  126.   var disabled = !gDialog.textareaName.value || !gDialog.textareaRows.value || !gDialog.textareaCols.value;
  127.   if (gDialog.accept.disabled != disabled)
  128.     gDialog.accept.disabled = disabled;
  129. }
  130.  
  131. function ValidateData()
  132. {
  133.   var attributes = {
  134.     name: gDialog.textareaName.value,
  135.     rows: gDialog.textareaRows.value,
  136.     cols: gDialog.textareaCols.value,
  137.     wrap: gDialog.textareaWrap.value,
  138.     tabindex: gDialog.textareaTabIndex.value,
  139.     accesskey: gDialog.textareaAccessKey.value
  140.   };
  141.   var flags = {
  142.     readonly: gDialog.textareaReadOnly.checked,
  143.     disabled: gDialog.textareaDisabled.checked
  144.   };
  145.   for (var a in attributes)
  146.   {
  147.     if (attributes[a])
  148.       globalElement.setAttribute(a, attributes[a]);
  149.     else
  150.       globalElement.removeAttribute(a);
  151.   }
  152.   for (var f in flags)
  153.   {
  154.     if (flags[f])
  155.       globalElement.setAttribute(f, "");
  156.     else
  157.       globalElement.removeAttribute(f);
  158.   }
  159.   return true;
  160. }
  161.  
  162. function onAccept()
  163. {
  164.   // All values are valid - copy to actual element in doc or
  165.   //   element created to insert
  166.   ValidateData();
  167.  
  168.   var editor = GetCurrentEditor();
  169.  
  170.   editor.beginTransaction();
  171.  
  172.   try {
  173.     editor.cloneAttributes(textareaElement, globalElement);
  174.  
  175.     if (insertNew)
  176.       editor.insertElementAtSelection(textareaElement, true);
  177.  
  178.     // undoably set value
  179.     var initialText = gDialog.textareaValue.value;
  180.     if (initialText != textareaElement.value) {
  181.       editor.setShouldTxnSetSelection(false);
  182.  
  183.       while (textareaElement.hasChildNodes())
  184.         editor.deleteNode(textareaElement.lastChild);
  185.       if (initialText) {
  186.         var textNode = editor.document.createTextNode(initialText);
  187.         editor.insertNode(textNode, textareaElement, 0);
  188.       }
  189.  
  190.       editor.setShouldTxnSetSelection(true);
  191.     }
  192.   } finally {
  193.     editor.endTransaction();
  194.   }
  195.  
  196.   SaveWindowLocation();
  197.  
  198.   return true;
  199. }
  200.  
  201.