home *** CD-ROM | disk | FTP | other *** search
/ PC User 2003 September / Australian PC User - September 2003 (CD1).iso / magstuff / web / files / dwmx61.exe / Disk1 / data1.cab / Configuration_En / Commands / Format Table.js < prev    next >
Encoding:
JavaScript  |  2002-11-25  |  17.7 KB  |  481 lines

  1. // Copyright 1998,2000-2002 Macromedia, Inc. All rights reserved.
  2.  
  3. // *************** GLOBAL VARS  *****************
  4.  
  5. var helpDoc = MM.HELP_cmdFormatTable;
  6. var DEFAULT_tableStyle = 7;
  7. //these constants used in tableFormats.js file when defining new table formats
  8. var NONE=0,LEFT=1,CENTER=2,RIGHT=3,BOLD=1,ITALIC=2,BOLD_ITALIC=3;
  9. var ORIGINAL_PREVIEW_TABLE = getPreviewTable().outerHTML;
  10.  
  11. //******************* API **********************
  12.  
  13. function commandButtons(){
  14.   var tableObj = findTable();
  15.   var Buttons=new Array(MM.BTN_OK,        "applyFormatToSelectedTable();window.close();",
  16.                         MM.BTN_Apply,     "applyFormatToSelectedTable()",
  17.                         MM.BTN_Cancel,    "window.close()",
  18.                                                 MM.BTN_Help,      "displayHelp()");
  19.   if (isUnrecognizedTable(tableObj) || hasCaption(tableObj))
  20.     Buttons=new Array(MM.BTN_OK,"window.close()",
  21.                       MM.BTN_Cancel,"window.close()");
  22.  
  23.   return Buttons;
  24. }
  25.  
  26. function canAcceptCommand(){
  27.   if (dw.getDocumentDOM() == null)
  28.       return false;
  29.   else if (dw.getDocumentDOM().getShowLayoutView())
  30.     return false;
  31.   else if (findTable())
  32.     return true;
  33.   else
  34.     return false;
  35. }
  36.  
  37.  
  38. //***************** LOCAL FUNCTIONS  ******************
  39.  
  40. function findTable(){
  41.   var tableObj="";
  42.   var selObj = dw.getDocumentDOM().getSelectedNode();
  43.  
  44.   while (tableObj=="" && selObj.parentNode){
  45.     if (selObj.nodeType == Node.ELEMENT_NODE && selObj.tagName=="TABLE")
  46.       tableObj=selObj;
  47.     else
  48.       selObj = selObj.parentNode;
  49.   }
  50.   return tableObj;
  51. }
  52.  
  53. function updatePreview(){
  54.   alternateRows(dwscripts.findDOMObject("presetNames").selectedIndex,getPreviewTable());
  55. }
  56.  
  57. function removeTag( theObj, Tag )
  58. {
  59.     var children   = theObj.childNodes;
  60.     var nChildren  = children.length;
  61.  
  62.     for( var i = 0; i < nChildren; i++ )
  63.     {
  64.          var currentChild = children.item(i);
  65.  
  66.          if ( currentChild.hasChildNodes() )
  67.             removeTag( currentChild, Tag );
  68.  
  69.          if ( currentChild.nodeType == Node.ELEMENT_NODE &&
  70.               currentChild.tagName == Tag )
  71.               currentChild.outerHTML = currentChild.innerHTML;
  72.      }
  73. }
  74.  
  75.  
  76. function applyFormatToSelectedTable(){
  77.  
  78.   var selObj,selArr;
  79.     var dom = dw.getDocumentDOM()
  80.   
  81.   // get current selection
  82.   selArr = dom.getSelection();
  83.   selObj = dom.offsetsToNode(selArr[0],selArr[1]);
  84.       
  85.   alternateRows(dwscripts.findDOMObject("presetNames").selectedIndex,findTable());
  86.   
  87.   // restore original selection, if it still exists; if not, just select the table.
  88.   if (dw.nodeExists(selObj)){
  89.     selArr = dom.nodeToOffsets(selObj);
  90.   }else{
  91.     selArr = dom.nodeToOffsets(findTable());
  92.   }
  93.   dom.setSelection(selArr[0],selArr[1]);
  94.   
  95.   savePreferences();
  96. }
  97.  
  98. function isUnrecognizedTable(tableObj) { //checks selected table
  99.   var counter=0;
  100.   var trIter = tableObj.childNodes;
  101.   var trNode=trIter.item(counter);
  102.   var retVal=false;
  103.   while (trNode && !retVal) {
  104.     if (trNode.tagName!="TR" || trNode.childNodes.length==0) 
  105.       retVal=true;
  106.     else trNode=trIter.item(++counter)
  107.   }
  108.   return retVal;
  109. }
  110.  
  111. function hasCaption(theObj){
  112.   if (theObj.childNodes.item(0).tagName && theObj.childNodes.item(0).tagName=="CAPTION")
  113.     return true;
  114.   return false;
  115. }
  116.  
  117. function initializeUI() { //fill presetNames list
  118.   var mainLayer=dwscripts.findDOMObject("mainLayer");
  119.   var presetNamesList,nSize,Names,i;
  120.   var tableObj=findTable();
  121.   var selectedFormat;
  122.  
  123.  if (hasCaption(tableObj))
  124.      mainLayer.innerHTML="<p> </p>" + MSG_CaptionIsPresent;
  125.  else if (isUltraDev() && hasServerBehaviorApplied(tableObj)) {
  126.      mainLayer.innerHTML = "<p> </p>" + MM.MSG_RegionServerBehaviorsNotAllowed;
  127.  } else if (isUnrecognizedTable(tableObj))
  128.     mainLayer.innerHTML="<p> </p>" + MSG_IsInvalidTable;
  129.  else {
  130.       //add select list options
  131.       mainLayer.visibility="hidden";
  132.       for (i=0;i<4;i++){  //populate select lists
  133.         dwscripts.findDOMObject("topRowAlign").options[i] = new Option(OPTIONS_Align[i]);
  134.         dwscripts.findDOMObject("topRowTextStyle").options[i]=new Option(OPTIONS_Text_Style[i]);
  135.         dwscripts.findDOMObject("leftColAlign").options[i] = new Option(OPTIONS_Align[i]);
  136.         dwscripts.findDOMObject("leftColTextStyle").options[i] = new Option(OPTIONS_Text_Style[i]);
  137.         dwscripts.findDOMObject("rowLimit").options[i] = new Option(OPTIONS_Row_Limit[i]);
  138.       }
  139.       dwscripts.findDOMObject("rowLimit").options[4] = new Option(OPTIONS_Row_Limit[4]);
  140.  
  141.     presetNamesList = dwscripts.findDOMObject("presetNames");
  142.     Names=tableFormats();  //get list of table format names from tableFormats.js file
  143.     nSize = Names.length;
  144.     for (i=0;i<nSize;i++) { 
  145.       presetNamesList.options[i] = new Option(Names[i].name); //populate form field
  146.     }
  147.     dwscripts.findDOMObject("mainLayer").visibility="visible";
  148.     
  149.     selectedFormat = getFormatPreference();
  150.     setUI(selectedFormat); //choose preferred format
  151.     alternateRows(selectedFormat,getPreviewTable()); //apply this format to preview table
  152.     presetNamesList.selectedIndex=selectedFormat; //select default format
  153.  } 
  154. }
  155.  
  156.  
  157. function alternateRowColors(trIter,firstRowColor,secondRowColor,rowLimit,useTD,topRowColor){
  158.   var rowLen=trIter.length;
  159.   var cellLen,tdNode,trNode,counter;
  160.   if (rowLimit==0) rowLimit=1000; //repeat first color if rowLimit is equal to 0 
  161.   var counter=2*rowLimit;
  162.   var startRow = (topRowColor)?1:0;
  163.   for (i=startRow;i<rowLen;i++){
  164.     trNode=trIter.item(i);
  165.     rowColor=(counter%(2*rowLimit)<rowLimit)?firstRowColor:secondRowColor;
  166.     if (!useTD){
  167.       // attach bgcolor attribute to tr tags
  168.       trNode.setAttribute("bgcolor",rowColor);
  169.       // remove bgcolor from td tags, so they don't override
  170.       tdIter=trNode.childNodes; cellLen=tdIter.length;
  171.       for (j=0;j<cellLen;j++){
  172.         if (tdIter.item(j).getAttribute("bgcolor") != null && tdIter.item(j).getAttribute("bgcolor") != undefined){   
  173.           tdIter.item(j).removeAttribute("bgcolor"); 
  174.         }
  175.       }
  176.     } 
  177.     else {
  178.       // attach bgcolor attribute to td tags
  179.       // remove tr settings, if any (they're useless now)
  180.       if (trNode.getAttribute("bgcolor") != null && trNode.getAttribute("bgcolor") != undefined){
  181.         trNode.removeAttribute("bgcolor");
  182.       }
  183.       tdIter=trNode.childNodes; cellLen=tdIter.length;
  184.       for (j=0;j<cellLen;j++) 
  185.         tdIter.item(j).setAttribute("bgcolor",rowColor);
  186.     }
  187.     counter++; 
  188.   }
  189. }
  190.  
  191. function formatTopRow(trIter,topRowColor,topRowTextColor,topRowTextStyle,topRowAlign,useTD){
  192.   var trNode=trIter.item(0), cellLen;
  193.  
  194.   //add bgcolor & align
  195.   if (!useTD) { 
  196.     // attach bgcolor & align to first row TR tag. make sure to
  197.     // remove settings from TDs to prevent them overriding the TR settings.
  198.     if (topRowColor){
  199.         trNode.setAttribute("bgcolor",topRowColor);
  200.       tdIter=trNode.childNodes; cellLen=tdIter.length;
  201.       for (i=0;i<cellLen;i++){
  202.         tdNode=tdIter.item(i);
  203.         if (tdNode.getAttribute("bgcolor") != null && tdNode.getAttribute("bgcolor") != undefined){
  204.           tdNode.removeAttribute("bgcolor");
  205.         }
  206.       }
  207.     }
  208.       if ( !topRowAlign || topRowAlign.toLowerCase() == "none" ){
  209.         trNode.removeAttribute("align");
  210.     }else{
  211.       trNode.setAttribute("align",topRowAlign);
  212.       tdIter=trNode.childNodes; cellLen=tdIter.length;
  213.       for (i=0;i<cellLen;i++){
  214.         tdNode=tdIter.item(i);
  215.         if (tdNode.getAttribute("align") != null && tdNode.getAttribute("align") != undefined){
  216.           tdNode.removeAttribute("align");
  217.         }
  218.       }
  219.     }
  220.   } else { 
  221.     // attach bgcolor & align to TD tags in first row
  222.     // remove settings from TR tag (they're useless now)
  223.     tdIter=trNode.childNodes; cellLen=tdIter.length;
  224.     for (i=0;i<cellLen;i++){
  225.       tdNode=tdIter.item(i);
  226.         if (topRowColor)
  227.         tdNode.setAttribute("bgcolor",topRowColor);
  228.       if ( !topRowAlign || topRowAlign.toLowerCase()=="none" )
  229.           tdNode.removeAttribute("align");
  230.       else
  231.           tdNode.setAttribute("align",topRowAlign);      
  232.     }
  233.     if (trNode.getAttribute("bgcolor") != null && trNode.getAttribute("bgcolor") != undefined){
  234.       trNode.removeAttribute("bgcolor");
  235.     }
  236.     if (trNode.getAttribute("align") != null && trNode.getAttribute("align") != undefined){
  237.       trNode.removeAttribute("align");
  238.     }
  239.   } 
  240.  
  241.   //add text color and text formatting
  242.   tdIter=trNode.childNodes; cellLen=tdIter.length;
  243.    for (i=0;i<cellLen;i++) {
  244.      tdNode=tdIter.item(i);
  245.      //set font color, if specified
  246.      if (topRowTextColor){
  247.        if (!findTag(tdNode,"FONT")){ //if the color attribute cannot be added to a "safe" font tag, add it.
  248.          tdNode.innerHTML='<font color="' + topRowTextColor + '">' + tdNode.innerHTML + '</font>';
  249.        }
  250.        else  { //font tag already exists, set color attribute on it
  251.          findTag(tdNode,"FONT").setAttribute("color",topRowTextColor);
  252.        }
  253.          //remove font color if NOT specified
  254.      }else{
  255.         var fontTag = findTag(tdNode,"FONT");
  256.                  if (fontTag && fontTag.getAttribute("color")){
  257.                     fontTag.removeAttribute("color");
  258.           if (fontTag.attributes.length == 0){
  259.             removeTag(tdNode,"FONT");
  260.           }
  261.                 }
  262.             }
  263.       //set text style
  264.      if (topRowTextStyle==0){//if text style set to none
  265.        removeTag(tdNode,"B"); //remove bold tags
  266.        removeTag(tdNode,"I"); //and remove italic tags
  267.      } else {
  268.        if (topRowTextStyle==1 || topRowTextStyle==3){ //if bold or bold italic
  269.          if (topRowTextStyle==1) //if bold
  270.            removeTag(tdNode,"I"); //remove all italic tags
  271.          if (!findTag(tdNode,"B")){ //if a B tag is not found around the table cell text
  272.            removeTag(tdNode,"B"); //remove any other B tags
  273.            tdNode.innerHTML="<b>" + tdNode.innerHTML + "</b>"; //make first child a B tag
  274.          }
  275.        }
  276.        if (topRowTextStyle==2 || topRowTextStyle==3){ //if italic or or bold italic
  277.          if (topRowTextStyle==2) //if italic
  278.            removeTag(tdNode,"B"); //remove all bold tags
  279.          if (!findTag(tdNode,"I")){ //if an I tag is not found around the table cell text 
  280.            removeTag(tdNode,"I"); //remove any other I tags
  281.            tdNode.innerHTML="<i>" + tdNode.innerHTML + "</i>"; //make first child an I tag
  282.          }
  283.        }
  284.      }
  285.    } 
  286. }
  287.  
  288. function formatLeftCol(trIter,leftColTextStyle,leftColAlign){
  289.   rowLen=trIter.length;
  290.    for (i=0;i<rowLen;i++){
  291.      trNode=trIter.item(i);
  292.      tdNode=trNode.childNodes.item(0);
  293.      //set text style
  294.      if (leftColTextStyle==0){ //if text style set to none
  295.        removeTag(tdNode,"B"); //remove bold tags
  296.        removeTag(tdNode,"I"); //and remove italic tags
  297.      }
  298.      else {
  299.        if (leftColTextStyle==1 || leftColTextStyle==3){ //if text style set to bold or bold italic
  300.          if (leftColTextStyle==1) //if bold
  301.            removeTag(tdNode,"I"); //remove all italic tags
  302.          if (!findTag(tdNode,"B")){ //if B tag is not found around text
  303.            removeTag(tdNode,"B"); //remove all other B tags
  304.            tdNode.innerHTML="<b>" + tdNode.innerHTML + "</b>"; //make first child a B tag
  305.          }
  306.     
  307.        }
  308.        if (leftColTextStyle==2 || leftColTextStyle==3){ //if text style is set to italic or bold italic
  309.          if (leftColTextStyle==2) //if italic
  310.            removeTag(tdNode,"B"); //remove all bold tags
  311.          if (!findTag(tdNode,"I")){ //if i tag is not found around text
  312.            removeTag(tdNode,"I"); //remove all other i tags
  313.            tdNode.innerHTML="<i>" + tdNode.innerHTML + "</i>"; //make first child tag i tag
  314.          }
  315.        }
  316.      }     
  317.      //set alignment
  318.      if ( !leftColAlign || leftColAlign.toLowerCase()=="none" )
  319.        tdNode.removeAttribute("align");
  320.      else
  321.        tdNode.setAttribute("align",leftColAlign);        
  322.    }
  323. }
  324.  
  325. function setUI(presetIndex){
  326.       var Names = tableFormats();
  327.       var thisFormat = Names[presetIndex];               
  328.       with (thisFormat){
  329.         //error check rowLimit value
  330.         if (rowLimit>4 || rowLimit<0)rowLimit=1;
  331.     
  332.         //align attributes are specified as "left","center","right", and "";
  333.         //following function gets correct selected index for alignment option
  334.         topRowAlign=getIndex(topRowAlign);
  335.         leftColAlign=getIndex(leftColAlign); 
  336.         //select the appropriate options
  337.         dwscripts.findDOMObject("rowLimit").selectedIndex=parseInt(rowLimit);
  338.         dwscripts.findDOMObject("topRowAlign").selectedIndex=topRowAlign;
  339.         dwscripts.findDOMObject("topRowTextStyle").selectedIndex=topRowTextStyle;
  340.         dwscripts.findDOMObject("leftColAlign").selectedIndex=leftColAlign;
  341.         dwscripts.findDOMObject("leftColTextStyle").selectedIndex=leftColTextStyle;
  342.       
  343.         //fill in textfields
  344.         dwscripts.findDOMObject("firstRowColor").value = firstRowColor;
  345.         dwscripts.findDOMObject("firstRow").value = firstRowColor;
  346.         dwscripts.findDOMObject("secondRowColor").value = secondRowColor;
  347.         dwscripts.findDOMObject("secondRow").value = secondRowColor;
  348.         dwscripts.findDOMObject("topRow").value = topRowColor;
  349.         dwscripts.findDOMObject("topRowColor").value = topRowColor;
  350.         dwscripts.findDOMObject("topRowTextColor").value = topRowTextColor;
  351.         dwscripts.findDOMObject("topRowText").value = topRowTextColor;
  352.         dwscripts.findDOMObject("borderSize").value = border;
  353.       }
  354.  
  355. }
  356. function alternateRows(presetChoiceIndex,tableObj){
  357.   var tableNode,trIter,trNode,tdIter,tdNode,counter;
  358.   var useTD,topRowColor,selInd,topRowAlign,topRowTextColor;
  359.   var topRowTextStyle,firstRowColor,secondRowColor;
  360.   var rowLimit,borderSize;
  361.  
  362.   tableNode=tableObj;
  363.   trIter=tableNode.childNodes;
  364.   useTD=dwscripts.findDOMObject('useTD').checked?true:false;
  365.   
  366.   //The rest of the function assigns values to below variables based on user interface.
  367.   //User interface is initially populated with argument values, but the user can change
  368.   //them to dynamically update the preview table.
  369.  
  370.   //set variables for the top row
  371.   topRowColor=dwscripts.findDOMObject('topRowColor').value;
  372.   selInd = dwscripts.findDOMObject('topRowAlign').selectedIndex;
  373.   topRowAlign=dwscripts.findDOMObject('topRowAlign').options[selInd].value;
  374.   topRowTextColor=dwscripts.findDOMObject('topRowTextColor').value;
  375.   topRowTextStyle=dwscripts.findDOMObject('topRowTextStyle').selectedIndex;
  376.   
  377.   //set variables for the left col
  378.   selInd = dwscripts.findDOMObject('leftColAlign').selectedIndex;
  379.   leftColAlign=dwscripts.findDOMObject('leftColAlign').options[selInd].value;
  380.   leftColTextStyle=dwscripts.findDOMObject('leftColTextStyle').selectedIndex;
  381.   
  382.   //set variables for the row Colors
  383.   firstRowColor=dwscripts.findDOMObject('firstRowColor').value;
  384.   secondRowColor=dwscripts.findDOMObject('secondRowColor').value;
  385.   rowLimit=dwscripts.findDOMObject("rowLimit").selectedIndex;
  386.   
  387.   //set border size
  388.   borderSize=dwscripts.findDOMObject("borderSize").value;
  389.  
  390.   //Now, use these values to format the table...
  391.   
  392.   //set table border
  393.   tableNode.setAttribute("border",borderSize); 
  394.  
  395.   //alternate row Colors  
  396.   alternateRowColors(trIter,firstRowColor,secondRowColor,rowLimit,useTD,topRowColor);
  397.  
  398.   //add left col formatting:text style & alignment
  399.   formatLeftCol(trIter,leftColTextStyle,leftColAlign);
  400.  
  401.   //add top row formatting:text style,alignent,row color, & text color
  402.   formatTopRow(trIter,topRowColor,topRowTextColor,topRowTextStyle,topRowAlign,useTD);
  403.   
  404.   //set border
  405.   tableNode.setAttribute("border",borderSize);
  406.  
  407.   //if there is a background color, remove it.
  408.   if (tableNode.getAttribute("bgColor")) {
  409.     tableNode.removeAttribute("bgColor");
  410.   }
  411. }
  412.  
  413. function updatePreviewTable(){
  414.   var presetIndex = dwscripts.findDOMObject("presetNames").selectedIndex;
  415.   var previewTable = getPreviewTable();
  416.   var mainLayer = dwscripts.findDOMObject("mainLayer");
  417.   
  418.   setUI(presetIndex);
  419.   alternateRows(dwscripts.findDOMObject("presetNames").selectedIndex,previewTable);//format table
  420. }
  421.  
  422. function getPreviewTable(){
  423.    //returns preview Table object
  424.    return document.getElementsByTagName("TABLE").item(1);
  425. }
  426.  
  427.  
  428. function getIndex(align){
  429.   //returns align index of UI that matches text of align argument i.e: "center"
  430.   //Note: I didn't use constants here because it is more intuitive to define
  431.   //the alignment attribute following the html syntax of align="attribute"
  432.     switch (align){
  433.      case "left": align=1; break;
  434.      case "center": align=2; break;
  435.      case "right": align=3; break;
  436.      default: align=0; break;
  437.    }
  438.  return align;
  439. }
  440.  
  441. //searches the child nodes of tableCellObj -
  442. //returns the innermost object of tagName that surrounds all of the text in
  443. //the cell. returns empty string if not found.
  444. function findTag(obj, tag) {
  445.   var retVal="";
  446.   while (obj.childNodes && obj.childNodes.length == 1) {
  447.     obj = obj.childNodes.item(0);
  448.    if (obj.nodeType == Node.ELEMENT_NODE && obj.tagName == tag)
  449.      retVal=obj;
  450.   }
  451.   return retVal;
  452.  
  453. }
  454.  
  455.  
  456. function getFormatPreference() {
  457.   var metaFile, savedVal, curVal = DEFAULT_tableStyle;
  458.   if (typeof MMNotes != 'undefined') { // Check for MMNotes extension.
  459.    metaFile = MMNotes.open(document.URL, false);
  460.    if (metaFile) {
  461.      // Form specific settings.
  462.      savedVal = MMNotes.get(metaFile, 'MM_pref_FormatTable');
  463.      MMNotes.close(metaFile);
  464.      if (curVal == parseInt(curVal).toString()) {
  465.        curVal = savedVal;
  466.      }
  467.    }
  468.   }
  469.   return parseInt(curVal);
  470. }
  471.  
  472. function savePreferences() {
  473.   if (typeof MMNotes == 'undefined') {return;} // Check for MMNotes extension.
  474.   var metaFile, curVal;
  475.   metaFile = MMNotes.open(document.URL, true);
  476.   if (metaFile) {
  477.     curVal = MMNotes.set(metaFile, 'MM_pref_FormatTable', findObject("presetNames").selectedIndex);
  478.     MMNotes.close(metaFile);
  479.   }
  480. }
  481.