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 / EdInsertTable.js < prev    next >
Text File  |  2003-06-08  |  8KB  |  238 lines

  1. /* 
  2.  * The contents of this file are subject to the Netscape Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/NPL/
  6.  *  
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  *  
  12.  * The Original Code is Mozilla Communicator client code, released
  13.  * March 31, 1998.
  14.  * 
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation. Portions created by Netscape are
  17.  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  * 
  20.  * Contributor(s): 
  21.  */
  22.  
  23. //Cancel() is in EdDialogCommon.js
  24. var gTableElement = null;
  25. var gRows;
  26. var gColumns;
  27. var gActiveEditor;
  28.  
  29. // dialog initialization code
  30. function Startup()
  31. {
  32.   gActiveEditor = GetCurrentTableEditor();
  33.   if (!gActiveEditor)
  34.   {
  35.     dump("Failed to get active editor!\n");
  36.     window.close();
  37.     return;
  38.   }
  39.  
  40.   try {
  41.     gTableElement = gActiveEditor.createElementWithDefaults("table");
  42.   } catch (e) {}
  43.  
  44.   if(!gTableElement)
  45.   {
  46.     dump("Failed to create a new table!\n");
  47.     window.close();
  48.     return;
  49.   }
  50.   gDialog.rowsInput    = document.getElementById("rowsInput");
  51.   gDialog.columnsInput = document.getElementById("columnsInput");
  52.   gDialog.widthInput = document.getElementById("widthInput");
  53.   gDialog.borderInput = document.getElementById("borderInput");
  54.   gDialog.widthPixelOrPercentMenulist = document.getElementById("widthPixelOrPercentMenulist");
  55.   gDialog.OkButton = document.documentElement.getButton("accept");
  56.  
  57.   // Make a copy to use for AdvancedEdit
  58.   globalElement = gTableElement.cloneNode(false);
  59.   try {
  60.     if (GetPrefs().getBoolPref("editor.use_css") && IsHTMLEditor()
  61.         && !(gActiveEditor.flags & Components.interfaces.nsIPlaintextEditor.eEditorMailMask))
  62.     {
  63.       // only for Composer and not for htmlmail
  64.       globalElement.setAttribute("style", "text-align: left;");
  65.     }
  66.   } catch (e) {}
  67.  
  68.   // Initialize all widgets with image attributes
  69.   InitDialog();
  70.  
  71.   // Set initial number to 2 rows, 2 columns:
  72.   // Note, these are not attributes on the table,
  73.   //  so don't put them in InitDialog(),
  74.   //  else the user's values will be trashed when they use 
  75.   //  the Advanced Edit dialog
  76.   gDialog.rowsInput.value = 2;
  77.   gDialog.columnsInput.value = 2;
  78.  
  79.   // If no default value on the width, set to 100%
  80.   if (gDialog.widthInput.value.length == 0)
  81.   {
  82.     gDialog.widthInput.value = "100";
  83.     gDialog.widthPixelOrPercentMenulist.selectedIndex = 1;
  84.   }
  85.  
  86.   SetTextboxFocusById("rowsInput");
  87.  
  88.   SetWindowLocation();
  89. }
  90.  
  91. // Set dialog widgets with attribute data
  92. // We get them from globalElement copy so this can be used
  93. //   by AdvancedEdit(), which is shared by all property dialogs
  94. function InitDialog()
  95. {  
  96.   // Get default attributes set on the created table:
  97.   // Get the width attribute of the element, stripping out "%"
  98.   // This sets contents of menu combobox list
  99.   // 2nd param = null: Use current selection to find if parent is table cell or window
  100.   gDialog.widthInput.value = InitPixelOrPercentMenulist(globalElement, null, "width", "widthPixelOrPercentMenulist", gPercent);
  101.   gDialog.borderInput.value = globalElement.getAttribute("border");
  102. }
  103.  
  104. function ChangeRowOrColumn(id)
  105. {
  106.   // Allow only integers
  107.   forceInteger(id);
  108.  
  109.   // Enable OK only if both rows and columns have a value > 0
  110.   var enable = gDialog.rowsInput.value.length > 0 && 
  111.                               gDialog.rowsInput.value > 0 &&
  112.                               gDialog.columnsInput.value.length > 0 &&
  113.                               gDialog.columnsInput.value > 0;
  114.  
  115.   SetElementEnabled(gDialog.OkButton, enable);
  116.   SetElementEnabledById("AdvancedEditButton1", enable);
  117. }
  118.  
  119.  
  120. // Get and validate data from widgets.
  121. // Set attributes on globalElement so they can be accessed by AdvancedEdit()
  122. function ValidateData()
  123. {
  124.   gRows = ValidateNumber(gDialog.rowsInput, null, 1, gMaxRows, null, null, true)
  125.   if (gValidationError)
  126.     return false;
  127.  
  128.   gColumns = ValidateNumber(gDialog.columnsInput, null, 1, gMaxColumns, null, null, true)
  129.   if (gValidationError)
  130.     return false;
  131.  
  132.   // Set attributes: NOTE: These may be empty strings (last param = false)
  133.   ValidateNumber(gDialog.borderInput, null, 0, gMaxPixels, globalElement, "border", false);
  134.   // TODO: Deal with "BORDER" without value issue
  135.   if (gValidationError) return false;
  136.  
  137.   ValidateNumber(gDialog.widthInput, gDialog.widthPixelOrPercentMenulist,
  138.                  1, gMaxTableSize, globalElement, "width", false);
  139.   if (gValidationError)
  140.     return false;
  141.  
  142.   return true;
  143. }
  144.  
  145.  
  146. function onAccept()
  147. {
  148.   if (ValidateData())
  149.   {
  150.     gActiveEditor.beginTransaction();
  151.     try {
  152.       gActiveEditor.cloneAttributes(gTableElement, globalElement);
  153.  
  154.       // Create necessary rows and cells for the table
  155.       var tableBody = gActiveEditor.createElementWithDefaults("tbody");
  156.       if (tableBody)
  157.       {
  158.         gTableElement.appendChild(tableBody);
  159.  
  160.         // Create necessary rows and cells for the table
  161.         for (var i = 0; i < gRows; i++)
  162.         {
  163.           var newRow = gActiveEditor.createElementWithDefaults("tr");
  164.           if (newRow)
  165.           {
  166.             tableBody.appendChild(newRow);
  167.             for (var j = 0; j < gColumns; j++)
  168.             {
  169.               var newCell = gActiveEditor.createElementWithDefaults("td");
  170.               if (newCell)
  171.               {
  172.                 newRow.appendChild(newCell);
  173.               }
  174.             }
  175.           }
  176.         }
  177.       }
  178.       // Detect when entire cells are selected:
  179.         // Get number of cells selected
  180.       var tagNameObj = { value: "" };
  181.       var countObj = { value: 0 };
  182.       var element = gActiveEditor.getSelectedOrParentTableElement(tagNameObj, countObj);
  183.       var deletePlaceholder = false;
  184.  
  185.       if (tagNameObj.value == "table")
  186.       {
  187.         //Replace entire selected table with new table, so delete the table
  188.         gActiveEditor.deleteTable();
  189.       }
  190.       else if (tagNameObj.value == "td")
  191.       {
  192.         if (countObj.value >= 1)
  193.         {
  194.           if (countObj.value > 1)
  195.           {
  196.             // Assume user wants to replace a block of
  197.             //  contiguous cells with a table, so
  198.             //  join the selected cells
  199.             gActiveEditor.joinTableCells(false);
  200.           
  201.             // Get the cell everything was merged into
  202.             element = gActiveEditor.getFirstSelectedCell();
  203.           
  204.             // Collapse selection into just that cell
  205.             gActiveEditor.selection.collapse(element,0);
  206.           }
  207.  
  208.           if (element)
  209.           {
  210.             // Empty just the contents of the cell
  211.             gActiveEditor.deleteTableCellContents();
  212.           
  213.             // Collapse selection to start of empty cell...
  214.             gActiveEditor.selection.collapse(element,0);
  215.             // ...but it will contain a <br> placeholder
  216.             deletePlaceholder = true;
  217.           }
  218.         }
  219.       }
  220.  
  221.       // true means delete selection when inserting
  222.       gActiveEditor.insertElementAtSelection(gTableElement, true);
  223.  
  224.       if (deletePlaceholder && gTableElement && gTableElement.nextSibling)
  225.       {
  226.         // Delete the placeholder <br>
  227.         gActiveEditor.deleteNode(gTableElement.nextSibling);
  228.       }
  229.     } catch (e) {}
  230.  
  231.     gActiveEditor.endTransaction();
  232.  
  233.     SaveWindowLocation();
  234.     return true;
  235.   }
  236.   return false;
  237. }
  238.