home *** CD-ROM | disk | FTP | other *** search
/ Practical Internet Web Designer 86 / PIWD86.iso / pc / contents / dreamweaver / software / dwmx2004.exe / Disk1 / data1.cab / Configuration_En / Commands / TableMain.js < prev    next >
Encoding:
JavaScript  |  2003-09-05  |  5.7 KB  |  191 lines

  1. // Copyright 2001, 2002, 2003 Macromedia, Inc. All rights reserved.
  2.  
  3. //---------------   GLOBAL VARIABLES   ---------------
  4.  
  5. var helpDoc = MM.HELP_objTable;
  6. var gDialogShown = false;
  7. var gReturnTag;
  8. var gRepeatRows = false; 
  9.  
  10. //---------------     API FUNCTIONS    ---------------
  11.  
  12. function isDOMRequired() {
  13.     return false;
  14. }
  15.     
  16. function commandButtons(){
  17.    return new Array(MM.BTN_OK,         "onOK()",
  18.                     MM.BTN_Cancel,     "window.close()",
  19.                     MM.BTN_Help,       "displayHelp()" );
  20. }
  21.  
  22. function receiveArguments()
  23.     {    
  24.     gRepeatRows = (arguments.length > 0 && arguments[0] == "repeatRows");
  25.     } //receiveArguments
  26.  
  27. function onOK()
  28.     {
  29.     if (gRepeatRows)
  30.         {
  31.         //Make sure the rows make sense. 
  32.           var Rows = document.theForm.Rows.value;
  33.          var repeatStart = document.theForm.startingRow.value; 
  34.           var repeatEnd = document.theForm.endingRow.value;
  35.           
  36.           if (repeatStart > Rows || repeatEnd < repeatStart || repeatEnd > Rows)
  37.               {
  38.               alert(MM.RepeatTableRowsWarning);
  39.               return;
  40.               }
  41.           }
  42.         
  43.     setTableStr();
  44.     window.close();
  45.     }
  46.  
  47. //Get a row of a table that has edit regions in each cell. 
  48. function getEditTableRow(columnCount)
  49.     {
  50.     var tableRow = ""; 
  51.       for (i=0; i< Columns; i++)
  52.           {
  53.           var curCellName = getUniqueRegionName(MM.EditAutonamePreamble, "MMTemplate:Editable", dw.getDocumentDOM("document")); 
  54.           var curCell = "<TD>\n<MMTemplate:Editable name=\"" + curCellName + "\">" + dw.getDocumentDOM("document").getNBSPChar() + "</MMTemplate:Editable>\n</TD>";
  55.         tableRow += curCell;
  56.            }
  57.            
  58.      return tableRow = "<"+"TR>" + tableRow + "<"+"/TR>";
  59.     }
  60.             
  61. function setTableStr(){
  62.  
  63.   var Columns = document.theForm.Cols.value;
  64.   var Rows = document.theForm.Rows.value;
  65.   var Border = document.theForm.Border.value;
  66.   var Width = document.theForm.Width.value;
  67.   var cellSpacing = document.theForm.Cellspace.value;
  68.   var cellPadding = document.theForm.Cellpad.value;
  69.   var unitChoice = document.forms[0].Units.selectedIndex;
  70.  
  71.   var repeatStart = -1; 
  72.   var repeatEnd = -1; 
  73.   var repeatStartContent = ""; 
  74.   var repeatEndContent = ""; 
  75.   
  76.   if (gRepeatRows)
  77.       {
  78.       repeatStart = document.theForm.startingRow.value;
  79.       repeatEnd = document.theForm.endingRow.value;
  80.       
  81.       var regionName = getUniqueRegionName(MM.RepeatAutonamePreamble, "MMTemplate:Repeat", dw.getDocumentDOM("document"));
  82.       
  83.       repeatStartContent = "<MMTemplate:Repeat name=\"" + regionName + "\">"; 
  84.       repeatEndContent = "</MMTemplate:Repeat>";
  85.       }
  86.       
  87.   var tableCells='<'+'TD>' + dw.getDocumentDOM("document").getNBSPChar() + '<'+'/TD>\n';
  88.   var tableRow='',tableContent='';
  89.   var openTag= '<' + 'table', spaceIndex;
  90.   var widthAttr;
  91.  
  92.   //change any negative or non-numeric row or column value into 1
  93.   Columns = (parseInt(Columns)== Columns && Columns>0)?Columns:1;
  94.   Rows = (parseInt(Rows)== Rows && Rows>0)?Rows:1;
  95.  
  96.   //CREATE TABLE
  97.   //determine contents of 1 table row
  98.   for (i=0; i< Columns; i++)
  99.   {
  100.     tableRow += tableCells;
  101.   }
  102.   tableRow = "<"+"TR>\n" + tableRow + "<"+"/TR>\n";
  103.   
  104.   //determine number of table rows & concatenate rows together
  105.   for (i=0; i< Rows; i++)
  106.   {
  107.     if (gRepeatRows && i == repeatStart-1)
  108.         tableContent += repeatStartContent; 
  109.     
  110.     if (gRepeatRows && i >= repeatStart-1 && i <= repeatEnd -1)
  111.         tableContent += getEditTableRow(Columns); 
  112.     else
  113.     {
  114.       //if first row, Contribute, and multi-column, add width percentages to table cells
  115.       if (i == 0 && dw.appName == "Contribute" && Columns > 1)
  116.       {
  117.         var colWidth = Math.max(1,Math.round(100/Columns));   //compute percent, not less than 1
  118.         tableContent += tableRow.replace(/(<TD)/gi,'$1 WIDTH="'+colWidth+'\%"');  //add WIDTH="nn%" to TD tags
  119.       }
  120.       //otherwise just add table row code
  121.       else
  122.       {
  123.         tableContent += tableRow;
  124.       }
  125.     }
  126.     
  127.     if (gRepeatRows && i == repeatEnd - 1)
  128.         tableContent += repeatEndContent; 
  129.     }
  130.     
  131.     
  132.   //add percent or pixel values to opening tag, if applicable
  133.   if (Width)
  134.     openTag += ' width="'+Width + ((unitChoice == 0)? '%" ' : '"');
  135.   //add border value, if applicable
  136.   if (Border)
  137.     openTag+=' border="' + Border + '" ';
  138.   //add cellspacing value, if applicable
  139.   if (cellSpacing)
  140.     openTag+=' cellspacing="' + cellSpacing + '"';
  141.   //add cellpadding value, if applicable
  142.   if (cellPadding)
  143.     openTag+=' cellpadding="' + cellPadding + '"';
  144.  
  145.   //strip extra space from openTag, if it exists
  146.   spaceIndex = openTag.length-1
  147.   if (escape(openTag.charAt(spaceIndex))=='%20')
  148.     openTag = openTag.substring(0,spaceIndex);
  149.  
  150.   openTag += '>\n' + tableContent;
  151.  
  152.   if (gDialogShown) {
  153.     saveExtension(document)
  154.   }
  155.   gDialogShown = false; // Reset show dialog global.
  156.   
  157.   openTag += '<'+'/table'+'>';
  158.   gReturnTag= openTag;
  159. }
  160.  
  161.  
  162. function createTableStr() {
  163.  
  164.     return gReturnTag;
  165.  
  166. }
  167. //---------------    LOCAL FUNCTIONS   ---------------
  168.  
  169. function saveExtension(curDOM) {
  170.   var curHTML = DWfile.read(curDOM.URL);
  171.   var tempFilename = dw.getConfigurationPath() + '/Shared/MM/Cache/empty.htm';
  172.   if (DWfile.exists(tempFilename)) {
  173.     var tempDOM = dw.getDocumentDOM(tempFilename);
  174.     tempDOM.documentElement.outerHTML = curHTML;
  175.     var atrStr = DWfile.getAttributes(curDOM.URL);
  176.     if (tempDOM.body.outerHTML != curDOM.body.outerHTML && (atrStr.indexOf('R') == -1)){
  177.       tempDOM.body.outerHTML = curDOM.body.outerHTML;
  178.       DWfile.write(curDOM.URL, tempDOM.documentElement.outerHTML);
  179.     }
  180.   }
  181. }
  182.  
  183. function initializeUI()
  184. {    
  185.   document.theForm.Rows.focus(); //set focus on textbox
  186.   document.theForm.Rows.select(); //set insertion point into textbox
  187.   gDialogShown = true;
  188.   gReturnTag = '';
  189. }
  190.  
  191.