home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / comm.jar / content / editor / EdInsertTable.js < prev    next >
Encoding:
JavaScript  |  2002-04-09  |  7.2 KB  |  233 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 prefs = GetPrefs();
  34.  
  35. // dialog initialization code
  36. function Startup()
  37. {
  38.   if (!InitEditorShell())
  39.     return;
  40.  
  41.   tableElement = editorShell.CreateElementWithDefaults(tagName);
  42.   if(!tableElement)
  43.   {
  44.     dump("Failed to create a new table!\n");
  45.     window.close();
  46.     return;
  47.   }
  48.   gDialog.rowsInput    = document.getElementById("rowsInput");
  49.   gDialog.columnsInput = document.getElementById("columnsInput");
  50.   gDialog.widthInput = document.getElementById("widthInput");
  51.   gDialog.borderInput = document.getElementById("borderInput");
  52.   gDialog.widthPixelOrPercentMenulist = document.getElementById("widthPixelOrPercentMenulist");
  53.   gDialog.OkButton = document.documentElement.getButton("accept");
  54.  
  55.   // Make a copy to use for AdvancedEdit
  56.   globalElement = tableElement.cloneNode(false);
  57.   if (prefs.getBoolPref("editor.use_css") && (editorShell.editorType == "html")) {
  58.     // only for Composer and not for htmlmail
  59.     globalElement.setAttribute("style", "text-align: left;");
  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.   gDialog.rowsInput.value = 2;
  70.   gDialog.columnsInput.value = 2;
  71.  
  72.   // If no default value on the width, set to 100%
  73.   if (gDialog.widthInput.value.length == 0)
  74.   {
  75.     gDialog.widthInput.value = "100";
  76.     gDialog.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.   // 2nd param = null: Use current selection to find if parent is table cell or window
  93.   gDialog.widthInput.value = InitPixelOrPercentMenulist(globalElement, null, "width", "widthPixelOrPercentMenulist", gPercent);
  94.   gDialog.borderInput.value = globalElement.getAttribute("border");
  95. }
  96.  
  97. function ChangeRowOrColumn(id)
  98. {
  99.   // Allow only integers
  100.   forceInteger(id);
  101.  
  102.   // Enable OK only if both rows and columns have a value > 0
  103.   var enable = gDialog.rowsInput.value.length > 0 && 
  104.                               gDialog.rowsInput.value > 0 &&
  105.                               gDialog.columnsInput.value.length > 0 &&
  106.                               gDialog.columnsInput.value > 0;
  107.  
  108.   SetElementEnabled(gDialog.OkButton, enable);
  109.   SetElementEnabledById("AdvancedEditButton1", enable);
  110. }
  111.  
  112.  
  113. // Get and validate data from widgets.
  114. // Set attributes on globalElement so they can be accessed by AdvancedEdit()
  115. function ValidateData()
  116. {
  117.   rows = ValidateNumber(gDialog.rowsInput, null, 1, maxRows, null, null, true)
  118.   if (gValidationError)
  119.     return false;
  120.  
  121.   columns = ValidateNumber(gDialog.columnsInput, null, 1, maxColumns, null, null, true)
  122.   if (gValidationError)
  123.     return false;
  124.  
  125.   // Set attributes: NOTE: These may be empty strings (last param = false)
  126.   ValidateNumber(gDialog.borderInput, null, 0, maxPixels, globalElement, "border", false);
  127.   // TODO: Deal with "BORDER" without value issue
  128.   if (gValidationError) return false;
  129.  
  130.   ValidateNumber(gDialog.widthInput, gDialog.widthPixelOrPercentMenulist,
  131.                  1, maxPixels, globalElement, "width", false);
  132.   if (gValidationError)
  133.     return false;
  134.  
  135.   return true;
  136. }
  137.  
  138.  
  139. function onAccept()
  140. {
  141.   if (ValidateData())
  142.   {
  143.     editorShell.BeginBatchChanges();
  144.     editorShell.CloneAttributes(tableElement, globalElement);
  145.  
  146.     // Create necessary rows and cells for the table
  147.     var tableBody = editorShell.CreateElementWithDefaults("tbody");
  148.     if (tableBody)
  149.     {
  150.       tableElement.appendChild(tableBody);
  151.  
  152.       // Create necessary rows and cells for the table
  153.       for (var i = 0; i < rows; i++)
  154.       {
  155.         var newRow = editorShell.CreateElementWithDefaults("tr");
  156.         if (newRow)
  157.         {
  158.           tableBody.appendChild(newRow);
  159.           for (var j = 0; j < columns; j++)
  160.           {
  161.             var newCell = editorShell.CreateElementWithDefaults("td");
  162.             if (newCell)
  163.             {
  164.               newRow.appendChild(newCell);
  165.             }
  166.           }
  167.         }
  168.       }
  169.     }
  170.     // Detect when entire cells are selected:
  171.       // Get number of cells selected
  172.     var tagNameObj = new Object;
  173.     var countObj = new Object;
  174.     tagNameObj.value = "";
  175.     var element = editorShell.GetSelectedOrParentTableElement(tagNameObj, countObj);
  176.     var deletePlaceholder = false;
  177.  
  178.     if (tagNameObj.value == "table")
  179.     {
  180.       //Replace entire selected table with new table, so delete the table
  181.       editorShell.DeleteTable();
  182.     }
  183.     else if (tagNameObj.value == "td")
  184.     {
  185.       if (countObj.value >= 1)
  186.       {
  187.         if (countObj.value > 1)
  188.         {
  189.           // Assume user wants to replace a block of
  190.           //  contiguous cells with a table, so
  191.           //  join the selected cells
  192.           editorShell.JoinTableCells(false);
  193.           
  194.           // Get the cell everything was merged into
  195.           element = editorShell.GetFirstSelectedCell();
  196.           
  197.           // Collapse selection into just that cell
  198.           editorShell.editorSelection.collapse(element,0);
  199.         }
  200.  
  201.         if (element)
  202.         {
  203.           // Empty just the contents of the cell
  204.           editorShell.DeleteTableCellContents();
  205.           
  206.           // Collapse selection to start of empty cell...
  207.           editorShell.editorSelection.collapse(element,0);
  208.           // ...but it will contain a <br> placeholder
  209.           deletePlaceholder = true;
  210.         }
  211.       }
  212.     }
  213.  
  214.     try {
  215.       // true means delete selection when inserting
  216.       editorShell.InsertElementAtSelection(tableElement, true);
  217.     } catch (e) {
  218.       dump("Exception occured in InsertElementAtSelection\n");
  219.     }
  220.     if (deletePlaceholder && tableElement && tableElement.nextSibling)
  221.     {
  222.       // Delete the placeholder <br>
  223.       editorShell.DeleteElement(tableElement.nextSibling);
  224.     }
  225.  
  226.     editorShell.EndBatchChanges();
  227.  
  228.     SaveWindowLocation();
  229.     return true;
  230.   }
  231.   return false;
  232. }
  233.