home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 January / 01_02.iso / software / netscape62win / browser.xpi / bin / chrome / comm.jar / content / editor / EdInsertTable.js < prev    next >
Encoding:
JavaScript  |  2001-06-12  |  7.1 KB  |  229 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 tagName = "table"
  25. var tableElement = null;
  26. var rowElement = null;
  27. var cellElement = null;
  28. var maxRows = 10000;
  29. var maxColumns = 10000;
  30. var maxPixels = 10000;
  31. var rows;
  32. var columns;
  33. var dialog;
  34.  
  35. // dialog initialization code
  36. function Startup()
  37. {
  38.   if (!InitEditorShell())
  39.     return;
  40.  
  41.   doSetOKCancel(onOK, onCancel);
  42.  
  43.   tableElement = editorShell.CreateElementWithDefaults(tagName);
  44.   if(!tableElement)
  45.   {
  46.     dump("Failed to create a new table!\n");
  47.     window.close();
  48.     return;
  49.   }
  50.   // Create dialog object to store controls for easy access
  51.   dialog = new Object;
  52.   dialog.rowsInput    = document.getElementById("rowsInput");
  53.   dialog.columnsInput = document.getElementById("columnsInput");
  54.   dialog.widthInput = document.getElementById("widthInput");
  55.   dialog.borderInput = document.getElementById("borderInput");
  56.   dialog.widthPixelOrPercentMenulist = document.getElementById("widthPixelOrPercentMenulist");
  57.  
  58.   // Make a copy to use for AdvancedEdit
  59.   globalElement = tableElement.cloneNode(false);
  60.   
  61.   // Initialize all widgets with image attributes
  62.   InitDialog();
  63.  
  64.   // Set initial number to 2 rows, 2 columns:
  65.   // Note, these are not attributes on the table,
  66.   //  so don't put them in InitDialog(),
  67.   //  else the user's values will be trashed when they use 
  68.   //  the Advanced Edit dialog
  69.   dialog.rowsInput.value = 2;
  70.   dialog.columnsInput.value = 2;
  71.  
  72.   // If no default value on the width, set to 100%
  73.   if (dialog.widthInput.value.length == 0)
  74.   {
  75.     dialog.widthInput.value = "100";
  76.     dialog.widthPixelOrPercentMenulist.selectedIndex = 1;
  77.   }
  78.  
  79.   SetTextboxFocusById("rowsInput");
  80.  
  81.   SetWindowLocation();
  82. }
  83.  
  84. // Set dialog widgets with attribute data
  85. // We get them from globalElement copy so this can be used
  86. //   by AdvancedEdit(), which is shared by all property dialogs
  87. function InitDialog()
  88. {  
  89.   // Get default attributes set on the created table:
  90.   // Get the width attribute of the element, stripping out "%"
  91.   // This sets contents of menu combobox list
  92.   dialog.widthInput.value = InitPixelOrPercentMenulist(globalElement, tableElement, "width", "widthPixelOrPercentMenulist", gPercent);
  93.   dialog.borderInput.value = globalElement.getAttribute("border");
  94. }
  95.  
  96. function ChangeRowOrColumn(id)
  97. {
  98.   // Allow only integers
  99.   forceInteger(id);
  100.  
  101.   // Enable OK only if both rows and columns have a value > 0
  102.   SetElementEnabledById("ok", dialog.rowsInput.value.length > 0 && 
  103.                               dialog.rowsInput.value > 0 &&
  104.                               dialog.columnsInput.value.length > 0 &&
  105.                               dialog.columnsInput.value > 0);
  106. }
  107.  
  108.  
  109. // Get and validate data from widgets.
  110. // Set attributes on globalElement so they can be accessed by AdvancedEdit()
  111. function ValidateData()
  112. {
  113.   rows = ValidateNumber(dialog.rowsInput, null, 1, maxRows, null, null, true)
  114.   if (gValidationError)
  115.     return false;
  116.  
  117.   columns = ValidateNumber(dialog.columnsInput, null, 1, maxColumns, null, null, true)
  118.   if (gValidationError)
  119.     return false;
  120.  
  121.   // Set attributes: NOTE: These may be empty strings (last param = false)
  122.   ValidateNumber(dialog.borderInput, null, 0, maxPixels, globalElement, "border", false);
  123.   // TODO: Deal with "BORDER" without value issue
  124.   if (gValidationError) return false;
  125.  
  126.   ValidateNumber(dialog.widthInput, dialog.widthPixelOrPercentMenulist,
  127.                  1, maxPixels, globalElement, "width", false);
  128.   if (gValidationError)
  129.     return false;
  130.  
  131.   return true;
  132. }
  133.  
  134.  
  135. function onOK()
  136. {
  137.   if (ValidateData())
  138.   {
  139.     editorShell.BeginBatchChanges();
  140.     editorShell.CloneAttributes(tableElement, globalElement);
  141.  
  142.     // Create necessary rows and cells for the table
  143.     var tableBody = editorShell.CreateElementWithDefaults("tbody");
  144.     if (tableBody)
  145.     {
  146.       tableElement.appendChild(tableBody);
  147.  
  148.       // Create necessary rows and cells for the table
  149.       for (var i = 0; i < rows; i++)
  150.       {
  151.         var newRow = editorShell.CreateElementWithDefaults("tr");
  152.         if (newRow)
  153.         {
  154.           tableBody.appendChild(newRow);
  155.           for (var j = 0; j < columns; j++)
  156.           {
  157.             var newCell = editorShell.CreateElementWithDefaults("td");
  158.             if (newCell)
  159.             {
  160.               newRow.appendChild(newCell);
  161.             }
  162.           }
  163.         }
  164.       }
  165.     }
  166.     // Detect when entire cells are selected:
  167.       // Get number of cells selected
  168.     var tagNameObj = new Object;
  169.     var countObj = new Object;
  170.     tagNameObj.value = "";
  171.     var element = editorShell.GetSelectedOrParentTableElement(tagNameObj, countObj);
  172.     var deletePlaceholder = false;
  173.  
  174.     if (tagNameObj.value == "table")
  175.     {
  176.       //Replace entire selected table with new table, so delete the table
  177.       editorShell.DeleteTable();
  178.     }
  179.     else if (tagNameObj.value == "td")
  180.     {
  181.       if (countObj.value >= 1)
  182.       {
  183.         if (countObj.value > 1)
  184.         {
  185.           // Assume user wants to replace a block of
  186.           //  contiguous cells with a table, so
  187.           //  join the selected cells
  188.           editorShell.JoinTableCells(false);
  189.           
  190.           // Get the cell everything was merged into
  191.           element = editorShell.GetFirstSelectedCell();
  192.           
  193.           // Collapse selection into just that cell
  194.           editorShell.editorSelection.collapse(element,0);
  195.         }
  196.  
  197.         if (element)
  198.         {
  199.           // Empty just the contents of the cell
  200.           editorShell.DeleteTableCellContents();
  201.           
  202.           // Collapse selection to start of empty cell...
  203.           editorShell.editorSelection.collapse(element,0);
  204.           // ...but it will contain a <br> placeholder
  205.           deletePlaceholder = true;
  206.         }
  207.       }
  208.     }
  209.  
  210.     try {
  211.       // true means delete selection when inserting
  212.       editorShell.InsertElementAtSelection(tableElement, true);
  213.     } catch (e) {
  214.       dump("Exception occured in InsertElementAtSelection\n");
  215.     }
  216.     if (deletePlaceholder && tableElement && tableElement.nextSibling)
  217.     {
  218.       // Delete the placeholder <br>
  219.       editorShell.DeleteElement(tableElement.nextSibling);
  220.     }
  221.  
  222.     editorShell.EndBatchChanges();
  223.  
  224.     SaveWindowLocation();
  225.     return true;
  226.   }
  227.   return false;
  228. }
  229.