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 / ccTableOptions.js < prev    next >
Encoding:
JavaScript  |  2003-09-05  |  15.5 KB  |  488 lines

  1. //
  2. // Copyright 1999, 2000, 2001, 2002, 2003 Macromedia, Inc. All rights reserved.
  3. // ----------------------------------------------------
  4. //
  5. // ccTableOptions.js
  6. //
  7. // reads in the form in ccTableCommands.htm. and make a tablestr to insert into the document.
  8. //
  9. // Version 1.0
  10. // Added functions...
  11. // ----------------------------------------------------
  12.  
  13. //---------------   GLOBAL VARIABLES   ---------------
  14. var helpDoc = MM.HELP_objTable;
  15. var gDialogShown = false;
  16. var targetDom = null;
  17. var returnTag='';
  18. var TBL_HDR;  //instance of iconSelectorClass, used for selecting table header icons
  19.  
  20. var DEFAULT_TR = '<tr valign="top">';  //by default, set vertical alignment to top
  21.  
  22. //---------------     API FUNCTIONS    ---------------
  23.  
  24. function isDOMRequired() {
  25.     return false;
  26. }
  27.  
  28. function commandButtons()
  29. {
  30.     return new Array( "PutButtonsOnBottom", "OkButton", MM.BTN_OK, "if (setTableStr()) window.close()",
  31.                       "PutButtonOnLeft", MM.BTN_Help,    "displayHelp()",
  32.                       "CancelButton", MM.BTN_Cancel,  "window.close()");
  33.           
  34. }
  35.  
  36. function setTableStr(){
  37.  
  38.   // read all the values out of the form.
  39.  
  40.   var cmdFile = dreamweaver.getConfigurationPath() + "/Commands/ccTableCommands.htm";
  41.   var cmdDOM = dreamweaver.getDocumentDOM(cmdFile);
  42.  
  43.   var Columns = cmdDOM.theForm.Cols.value;
  44.   var Rows = cmdDOM.theForm.Rows.value;
  45.   var Border = cmdDOM.theForm.Border.value;
  46.   var Width = cmdDOM.theForm.Width.value;
  47.   var cellSpacing = cmdDOM.theForm.Cellspace.value;
  48.   var cellPadding = cmdDOM.theForm.Cellpad.value;
  49.   
  50.   // unit choice is either 0 for pixels, or 1 for percent.
  51.   var unitChoice = cmdDOM.theForm.Units.selectedIndex; 
  52.   var useWidthInColumns = true;
  53.   
  54.   // width choice is the radio buttons. Either default, which makes each cell 100. Or specify size, 
  55.   // which allows the user to specify the size of the table.
  56.   var specifyWidth;
  57.   var i;
  58.   var cellWidth; 
  59.   var alertString;
  60.   
  61.    //alert when the number of columns are not valid.
  62.   if ( (!(parseInt(Columns)== Columns)) || (Columns < 1) || (Columns > 50))
  63.   {
  64.     alertString = dw.loadString ("ccTableOptions/numColumns");
  65.       alert (alertString);
  66.     return false;
  67.   }
  68.   
  69.   //alert when the number of rows are not valid.
  70.   if ((!(parseInt(Rows)== Rows)) || (Rows < 1) || (Rows > 50))
  71.   {
  72.     alertString = dw.loadString ("ccTableOptions/numRows");
  73.       alert (alertString);
  74.     return false;    
  75.   }
  76.   
  77.   // alert when cellSpacing is invalid: non-blank & (non-number, or num<0 or num>50)
  78.   if ((cellSpacing != "") && ((!(parseInt(cellSpacing)== cellSpacing)) || (cellSpacing < 0) || (cellSpacing > 50)))
  79.   {
  80.       alertString = dw.loadString ("ccTableOptions/cellSpacing");
  81.       alert (alertString);
  82.       return false;    
  83.   }
  84.  
  85.   // alert when cellPadding is invalid: non-blank, non-number, or num<0 or num>50
  86.   if ((cellPadding != "") && ((!(parseInt(cellPadding)== cellPadding)) || (cellPadding < 0) || (cellPadding > 50)))
  87.   {
  88.       alertString = dw.loadString ("ccTableOptions/cellPadding");
  89.       alert (alertString);
  90.       return false;    
  91.   }
  92.  
  93.   // Display an error message if (#78375): 
  94.   // 1. if it's not a number and the border is > 50 or < 0  OR 
  95.   // 2. it's not a number but the border is not an empty string (which we now allow) 
  96.   
  97.   if ((dwscripts.isNumber(Border) && (Border > 50 || Border < 0)) || 
  98.         (!dwscripts.isNumber(Border) && Border != "")) 
  99.   {
  100.     alertString = dw.loadString ("ccTableOptions/border");
  101.     alert (alertString);
  102.     return false;    
  103.   }
  104.  
  105.   // alert when then width is not valid.
  106.   if ((!(parseInt(Width) == Width)) || (Width < 0) && specifyWidth!="default")
  107.   {
  108.     alertString=dw.loadString ("ccTableOptions/width");
  109.     alert (alertString);
  110.     return false;
  111.   }
  112.   
  113.   // figure out whether the user is specifying a width or just using the default width.
  114.   for (i=0; i<cmdDOM.theForm.RadioGroup1.length;i++)
  115.   {
  116.     if (cmdDOM.theForm.RadioGroup1[i].checked)
  117.         specifyWidth = cmdDOM.theForm.RadioGroup1[i].value;
  118.   }
  119.   
  120.   if (specifyWidth == 'default')
  121.   {
  122.        // cap the width at 1000% or 5000 pixels.
  123.     cellWidth = 100;
  124.       Width = cellWidth * Columns;
  125.   
  126.       // if width is pixels, then cap at 5000. otherwise, cap at 1000%.
  127.       if (unitChoice == 0)
  128.     {
  129.           if (Width > 5000) Width = 5000;
  130.         cellWidth = Math.round (Width / Columns);
  131.     }    
  132.   }
  133.   
  134.   // if the width is specified.
  135.   else
  136.   {
  137.     // if the unit choice is %, then we dont want to set a width on the table columns.
  138.       if (unitChoice != 0) useWidthInColumns = false;
  139.     else
  140.   //        cellWidth = Math.round (Width / Columns);
  141.           useWidthInColumns = false;
  142.  
  143.     // cap the width at 1000% or 5000 pixels.
  144.     if (unitChoice == 0)
  145.     {
  146.           if (Width > 5000) Width = 5000;
  147.         cellWidth = Math.round (Width / Columns);
  148.     }    
  149.     else
  150.     {
  151.         if (Width > 1000) Width = 1000;
  152.         cellWidth = Math.round (Width/Columns);
  153.     }
  154.   }
  155.   
  156.   // header choice can be "none", "row", "column" or "both"
  157.   var i = 0;
  158.   var headerchoice;
  159.   headerChoice = TBL_HDR.value;
  160.      
  161.   //CREATE TABLE
  162.   // there are 4 types of cells, Header cells with width, Header cells with no width, Regular cells with width, and Regular cells with 
  163.   // no width.
  164.    
  165.   var tableHeaderColumnCellsWithWidth='<'+'th width="' + cellWidth + '" scope="row">' + dw.getDocumentDOM("document").getNBSPChar() + '<'+'/th>';
  166.   var tableHeaderColumnCellsNoWidth='<th scope="row">'  + dw.getDocumentDOM("document").getNBSPChar() + '</th>';
  167.   var tableHeaderRowCellsWithWidth='<'+'th width="' + cellWidth + '" scope="col">' + dw.getDocumentDOM("document").getNBSPChar() + '<'+'/th>';
  168.   var tableHeaderRowCellsWithNoWidth='<th scope="col">' + dw.getDocumentDOM("document").getNBSPChar() + '</th>';
  169.  
  170.  
  171.   var tableRegularCellsWithWidth = '<' + 'td width="' + cellWidth + '">' + dw.getDocumentDOM("document").getNBSPChar() + '</td>';
  172.   var tableRegularCellsNoWidth ='<'+'td>' + dw.getDocumentDOM("document").getNBSPChar() + '<'+'/td>';
  173.  
  174.   //Calculate the table width, which is --
  175.   //    width=table width in pixels
  176.   //    n=number of columns
  177.   //    border=table border width
  178.   //    cellborder=(border > 0 ? 1 : 0)
  179.   //    space=cell spacing
  180.   //    x = sum of explicit column widths
  181.   //    width = x + 2*border + (n+1)*space + 2*n*cellborder
  182.  
  183.   
  184.   var tableWidth = -1;
  185.   var cellBorder;
  186.  
  187.   if (Border!= "")
  188.     cellBorder = 1;
  189.   else
  190.     cellBorder = 0;
  191.  
  192.   //if the table width was specified in pixels
  193.  
  194.   if (useWidthInColumns)
  195.   {
  196.     tableWidth = Width + 2*Border + (parseInt(Columns) + 1) * cellSpacing + 2*Columns*cellBorder;
  197.   }
  198.  
  199.   // set the table opening tag. Thats the actual table tag
  200.   var openTag= '<' + 'table';
  201.  
  202.   // see if we need to add a value to the open tag. If we didnt set width in the columns, that means we are setting a width on the
  203.   // table tag itself. and then it is always a %.
  204.  
  205.   if ((!useWidthInColumns) && Width > 0) 
  206.   {
  207.     if (unitChoice != 0)
  208.         openTag += ' width="' + Width + '%"';
  209.     else
  210.         openTag += ' width="' + Width + '"';
  211.   
  212.   }
  213.   else
  214.     if (tableWidth > -1) openTag += ' width="' + tableWidth + '"';
  215.  
  216.   // add cellSpacing and Padding
  217.   if (cellSpacing != "") openTag += ' cellspacing="' + cellSpacing + '"';
  218.   if (cellPadding != "") openTag += ' cellpadding="' + cellPadding + '"';
  219.  
  220.   // add the border if there is one.
  221.   if (Border != "") openTag += ' border="' + Border + '"';
  222.  
  223.   //strip extra space from openTag, if it exists
  224.   spaceIndex = openTag.length-1;
  225.   if (escape(openTag.charAt(spaceIndex))=='%20')
  226.     openTag = openTag.substring(0,spaceIndex);
  227.  
  228.   // close off the opening tag.
  229.   openTag += '>';
  230.   
  231.  
  232.  // make the first row. This is a special case because we sometimes set the width in the columns.
  233.   var firstRow = DEFAULT_TR;
  234.   
  235.   // if we want row headers of both row and column headers, then we set the first row as headers.
  236.  
  237.   if (headerChoice == "row" || headerChoice == "both")
  238.   {
  239.     for (i = 0; i < Columns; i++)
  240.     {
  241.         if (useWidthInColumns) firstRow += tableHeaderRowCellsWithWidth;
  242.         else firstRow += tableHeaderRowCellsWithNoWidth;
  243.     }    
  244.   }
  245.  
  246.   // if the header choice is column, we want to set all the cells in the first column to be <th> 's.
  247.   else if (headerChoice == "col")
  248.   {
  249.     for (i = 0; i < Columns; i++)
  250.     {
  251.         // first cell in the row. set it to <th>... </th>
  252.         if (i == 0)
  253.         {
  254.             if (useWidthInColumns) firstRow += tableHeaderColumnCellsWithWidth;
  255.             else firstRow += tableHeaderColumnCellsNoWidth;
  256.         }
  257.         
  258.         // not the first cell, so not part of the column of headers. so just set to <td>
  259.         else
  260.         {
  261.             if (useWidthInColumns) firstRow += tableRegularCellsWithWidth;
  262.             else firstRow += tableRegularCellsNoWidth;
  263.         }
  264.     }
  265.   }
  266.  
  267.   else if (headerChoice == "none")
  268.   {
  269.     // no headers, so just put in regular cells.
  270.     for (i = 0; i < Columns; i++)
  271.     {
  272.         if (useWidthInColumns) firstRow += tableRegularCellsWithWidth;
  273.         else firstRow += tableRegularCellsNoWidth;
  274.     }
  275.   }
  276.   
  277.   firstRow += "</tr>"
  278.  
  279.   // make the rest of the table.
  280.  
  281.   var restOfTable = "";
  282.   var i,j;
  283.  
  284.   if (headerChoice == "col" || headerChoice == "both")
  285.   {
  286.     // since the first row is already set, we start at 1 instead of 0.
  287.     for (i = 1; i < Rows; i++)
  288.     {
  289.         for (j = 0; j < Columns; j ++)
  290.         {
  291.             // if its the first column, we want to put in header cells.
  292.             if (j == 0) restOfTable += DEFAULT_TR + tableHeaderColumnCellsNoWidth;
  293.             
  294.             // otherwise, we're in the rest of the table, so we put in regular table cells.
  295.             else
  296.             {
  297.                 restOfTable += tableRegularCellsNoWidth;
  298.                 // last cell in a row, we need to close off the tr tag
  299.                 if (j == Columns - 1) restOfTable += "</tr>";
  300.             }
  301.         }
  302.      }
  303.   }
  304.  
  305.   // we are not setting any headers, so just put in regular table cells.
  306.   else
  307.   {
  308.      for (i = 1; i < Rows; i++)
  309.      {
  310.         for (j = 0; j < Columns; j++)
  311.         {
  312.             if (j == 0) restOfTable += DEFAULT_TR;
  313.             restOfTable += tableRegularCellsNoWidth;
  314.             if (j == Columns - 1) restOfTable += "</tr>";
  315.         }
  316.      }
  317.   }
  318.  
  319.   returnTag = openTag + firstRow + restOfTable + "</table>";
  320.   return true;
  321.  
  322. }
  323.  
  324. function createTableStr()
  325. {
  326.     return returnTag;
  327. }
  328.  
  329. //---------------    LOCAL FUNCTIONS   ---------------
  330.  
  331. function saveExtension(curDOM) {
  332.   var curHTML = DWfile.read(curDOM.URL);
  333.   var tempFilename = dw.getConfigurationPath() + '/Shared/MM/Cache/empty.htm';
  334.   if (DWfile.exists(tempFilename)) {
  335.     var tempDOM = dw.getDocumentDOM(tempFilename);
  336.     tempDOM.documentElement.outerHTML = curHTML;
  337.     var atrStr = DWfile.getAttributes(curDOM.URL);
  338.     if (tempDOM.body.outerHTML != curDOM.body.outerHTML && (atrStr.indexOf('R') == -1)){
  339.       tempDOM.body.outerHTML = curDOM.body.outerHTML;
  340.       DWfile.write(curDOM.URL, tempDOM.documentElement.outerHTML);
  341.     }
  342.   }
  343. }
  344.  
  345. // this function just sets the default text box to something reasonable when the user switches between units.
  346. function onChangeUnits()
  347. {
  348.     var cmdFile = dreamweaver.getConfigurationPath() + "/Commands/ccTableCommands.htm";
  349.     var cmdDOM = dreamweaver.getDocumentDOM(cmdFile);
  350.     var unitChoice = cmdDOM.theForm.Units.selectedIndex; 
  351.     // if it changed to pixels, set the default pixel value to like 500.
  352.     if (unitChoice == 0)
  353.         cmdDOM.theForm.Width.value = 500;
  354.     else
  355.         cmdDOM.theForm.Width.value = 100;
  356. }
  357.  
  358. function toggleWidthControls()
  359. {
  360.     var cmdFile = dreamweaver.getConfigurationPath() + "/Commands/ccTableCommands.htm";
  361.     var cmdDOM = dreamweaver.getDocumentDOM(cmdFile);
  362.     var specifyWidthChoice;
  363.     
  364.     for (i=0; i<cmdDOM.theForm.RadioGroup1.length;i++)
  365.     {
  366.         if (cmdDOM.theForm.RadioGroup1[i].checked)
  367.         {
  368.             specifyWidthChoice = cmdDOM.theForm.RadioGroup1[i].value;
  369.         }
  370.     }
  371.     // disable the text box and the units.dropdown.
  372.     if (specifyWidthChoice == 'default')
  373.     {
  374.         cmdDOM.theForm.Width.disabled = true;
  375.         cmdDOM.theForm.Units.disabled = true;
  376.     }
  377.     // enable the text box and the units dropdown.
  378.     else
  379.     {
  380.         cmdDOM.theForm.Width.disabled = false;
  381.         cmdDOM.theForm.Units.disabled = false;
  382.     }
  383. }
  384.  
  385.  
  386.  
  387.  
  388.  
  389. //////////////////////////////////////////////////////////////////
  390. // Initialize UI:                                   //
  391. // set default values for radio buttons, visibility for layers  //
  392. // and values of caption and summary to blank.                  //
  393. //////////////////////////////////////////////////////////////////
  394.  
  395. function initializeUI()
  396. {    
  397.       var cmdFile = dreamweaver.getConfigurationPath() + "/Commands/ccTableCommands.htm";
  398.       var cmdDOM = dreamweaver.getDocumentDOM(cmdFile);
  399.  
  400.       var headerChoice='both';
  401.       cmdDOM.theForm.Units.selectedIndex = 0;
  402.  
  403.     //initialize Table Header selector, default value "none"
  404.     TBL_HDR = new IconSelectorClass(new Array("none","row","col","both"),"none");
  405.       
  406.       cmdDOM.theForm.Width.editText = "default";
  407.       cmdDOM.theForm.Width.selectedIndex = 0;
  408.  
  409.  
  410.     cmdDOM.theForm.Rows.focus(); //set focus on textbox
  411.     cmdDOM.theForm.Rows.select(); //set insertion point into textbox
  412.     
  413. //      cmdDOM.theForm.Width.select(); //set insertion point into textbox
  414.  
  415.  
  416. //      cmdDOM.theForm.Rows.focus(); //set focus on textbox
  417. //      cmdDOM.theForm.Rows.select(); //set insertion point into textbox
  418.       gDialogShown = true;
  419.  
  420.  
  421.  
  422. } //initializeUI
  423.  
  424.  
  425.  
  426. ///--------------------------------------------------------------------
  427. // CLASS:
  428. //   IconSelectorClass
  429. //
  430. // DESCRIPTION:
  431. //   This class is used to provide radio-button functionality to images.
  432. // When clicking images in table cells, their cells will highlight. To
  433. // find out which image is selected, get the value property of the object.
  434. //
  435. // To set it up, place images in table cells, name the table cells uniquely,
  436. // and add onMouse handlers to each image that call the update method and
  437. // pass the name of their table cells.
  438. //
  439. // PUBLIC PROPERTIES:
  440. //   value (read-only) - the name of the table cell currently selected.
  441. //
  442. // PUBLIC FUNCTIONS:
  443. //   update(value) - passed by the click handler to select a new cell.
  444. //
  445. // CONSTRUCTOR:
  446. //   tableCellNames - an array of all table cell names to be used
  447. //   initialName (optional) - initial cell to select (otherwise 1st)
  448. //
  449. //--------------------------------------------------------------------
  450.  
  451. function IconSelectorClass(tableCellNames, initialName)
  452. {
  453.   //Private properties: set these to 
  454.   this.selectedColor = "#716F64";   //background color for selected table cell
  455.   this.unselectedColor = "";        //background color for unselected table cell (#9D9CA7 on table)
  456.   this.selectedTextColor = "#FFFFFF";   //color for selected text (white)
  457.   this.unselectedTextColor = "#000000"; //color for unselected text (black)
  458.  
  459.   //Clear out all selections
  460.   for (var i=0; i<tableCellNames.length; i++)
  461.   {
  462.     document[tableCellNames[i]].bgColor = this.unselectedColor;
  463.   }
  464.  
  465.   //determine initial value (use if passed in, otherwise 1st item)
  466.   this.value = initialName;
  467.   if (!this.value) this.value = tableCellNames[0];  //if now initial, use item 0
  468.  
  469.   this.update(this.value); //update, to select the default table cell
  470. }
  471. IconSelectorClass.prototype.update = IconSelectorClass_update;
  472.  
  473.  
  474.  
  475. //Update method. Pass in a new value, one of the table cell names.
  476.  
  477. function IconSelectorClass_update(newValue)
  478. {
  479.   document[this.value].bgColor = this.unselectedColor;  //unselect the prior table cell
  480.   document[this.value].innerHTML = document[this.value].innerHTML.replace(RegExp(this.selectedTextColor),this.unselectedTextColor); //unset the prior text color
  481.  
  482.   this.value = newValue;                                //change the value to the new value
  483.   document[this.value].bgColor = this.selectedColor;    //select the new table cell
  484.   document[this.value].innerHTML = document[this.value].innerHTML.replace(RegExp(this.unselectedTextColor),this.selectedTextColor); //set the new text color
  485. }
  486.  
  487. //----------- END IconSelectorClass --------------
  488.