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 / RepeatingTable.js < prev    next >
Encoding:
JavaScript  |  2003-09-05  |  7.0 KB  |  263 lines

  1. // Copyright 2002, 2003 Macromedia, Inc. All rights reserved.
  2.  
  3. //---------------   GLOBAL VARIABLES   ---------------
  4.  
  5. var helpDoc = MM.HELP_objRepeatingTable;
  6. var gDialogShown = false;
  7. var gReturnTag;
  8.  
  9. var nextEditRegionInd = -1; 
  10. var haveCalledCanInsert = false; 
  11.  
  12. //---------------     API FUNCTIONS    ---------------
  13.  
  14. function isDOMRequired() {
  15.     return false;
  16. }
  17.     
  18. function commandButtons(){
  19.    return new Array(MM.BTN_OK,         "onOK()",
  20.                     MM.BTN_Cancel,     "window.close()",
  21.                     MM.BTN_Help,       "dwscripts.displayDWHelp(helpDoc);" );
  22. }
  23.  
  24. function onOK()
  25.     {
  26.     //Make sure the rows make sense. 
  27.     var Rows = document.theForm.Rows.value;
  28.     var repeatStart = document.theForm.startingRow.value; 
  29.     var repeatEnd = document.theForm.endingRow.value;
  30.     
  31.     if (repeatStart > Rows || repeatEnd < repeatStart || repeatEnd > Rows)
  32.         {
  33.         alert(MM.RepeatTableRowsWarning);
  34.         return;
  35.         }
  36.     
  37.      var regionName = document.theForm.repeatName.value; 
  38.      if (findNamedRepeatingRegion(regionName, dw.getDocumentDOM("document")) != null)    
  39.         {
  40.         alert(MSG_alreadyExists_Repeat);
  41.         return; 
  42.         }
  43.     
  44.     if (!checkLegalTemplateName(document.theForm.repeatName.value))
  45.         return; 
  46.     
  47.     setTableStr();
  48.         
  49.     simpleBlockInsert( gReturnTag, "", dw.getDocumentDOM("document"), false );
  50.     gReturnTag = "";
  51.     
  52.     window.close();
  53.     }
  54.  
  55.  
  56.  
  57. //Get a row of a table that has edit regions in each cell. 
  58. function getEditTableRow(columnCount)
  59.     {
  60.     var tableRow = ""; 
  61.       for (var i=0; i< columnCount; i++)
  62.           {
  63.           var curCellName = MM.EditAutonamePreamble + nextEditRegionInd;
  64.           nextEditRegionInd++; 
  65.           
  66.           var curCell; 
  67.           
  68.           
  69.           if (dw.generateTemplateTagSyntax())
  70.               curCell = "<TD><MMTemplate:Editable name=\"" + curCellName + "\">" + dw.getDocumentDOM("document").getNBSPChar() + "</MMTemplate:Editable></TD>";
  71.         else    
  72.               curCell = "<TD><!-- TemplateBeginEditable name=\"" + curCellName + "\" -->" + dw.getDocumentDOM("document").getNBSPChar() + "<!-- TemplateEndEditable --></TD>";
  73.         
  74.         tableRow += curCell;
  75.            }
  76.            
  77.      return tableRow = "<"+"TR>" + tableRow + "<"+"/TR>";
  78.     }
  79.                     
  80. function canInsert()
  81.     {    
  82.     haveCalledCanInsert = true; 
  83.     
  84.     targetDom = arguments[0];
  85.     if (targetDom == null)
  86.         targetDom = dw.getDocumentDOM();
  87.             
  88.     if (!CheckWarnNoTemplate(targetDom))
  89.         return false;
  90.     
  91.     var result = new Object();
  92.     if (!canMakeTemplateContent("repeatingTable", targetDom, result))
  93.         {        
  94.         if (result.status == "contained in DW4 edit")
  95.             alert(MM.MSG_SaveDW4First);
  96.         else if (result.status == "locked")
  97.             alert(MSG_cantInsertRepeatHere)
  98.         else
  99.             alert(MM.MSG_AlreadyEdit);
  100.             
  101.         return false; 
  102.         }            
  103.             
  104.     return true; 
  105.     } //canInsert
  106.  
  107.  
  108.  
  109. function setTableStr(){
  110.  
  111.   var Columns = document.theForm.Cols.value;
  112.   var Rows = document.theForm.Rows.value;
  113.   var Border = document.theForm.Border.value;
  114.   var Width = document.theForm.Width.value;
  115.   var cellSpacing = document.theForm.Cellspace.value;
  116.   var cellPadding = document.theForm.Cellpad.value;
  117.   var unitChoice = document.forms[0].Units.selectedIndex;
  118.  
  119.   var repeatEnd = document.theForm.endingRow.value;
  120.   var repeatStart = document.theForm.startingRow.value;
  121.  
  122.   var regionName = document.theForm.repeatName.value; 
  123.   
  124.   if (regionName == "")
  125.       regionName = getUniqueRegionName(MM.RepeatAutonamePreamble, "MMTemplate:Repeat", dw.getDocumentDOM("document"));
  126.  
  127.   var repeatStartContent;
  128.   var repeatEndContent; 
  129.  
  130.   if (dw.generateTemplateTagSyntax())
  131.       {
  132.       repeatStartContent = "<MMTemplate:Repeat name=\"" + regionName + "\">"; 
  133.       repeatEndContent = "</MMTemplate:Repeat>";
  134.       }
  135.   else
  136.       {
  137.      repeatStartContent = "<!-- TemplateBeginRepeat name=\"" + regionName + "\" -->";
  138.      repeatEndContent = "<!-- TemplateEndRepeat -->";
  139.       }
  140.       
  141.   var tableCells='<'+'TD>' + dw.getDocumentDOM("document").getNBSPChar() + '<'+'/TD>\n';
  142.   var tableRow='',tableContent='';
  143.   var openTag= '<' + 'table ', spaceIndex;
  144.   var widthAttr;
  145.  
  146.   //change any negative or non-numeric row or column value into 1
  147.   if (Columns <= 0)
  148.       Columns = 1; 
  149.   if (Rows <= 0)
  150.       Rows = 1; 
  151.   if (repeatEnd <= 0)
  152.       repeatEnd = 1; 
  153.   if (repeatStart <= 0)
  154.       repeatStart = 1; 
  155.  
  156.   //CREATE TABLE
  157.   //determine contents of 1 table row
  158.   for (i=0; i< Columns; i++)
  159.     tableRow+=tableCells;
  160.   tableRow = "<"+"TR>\n" + tableRow + "<"+"/TR>\n";
  161.   
  162.   nextEditRegionInd = getUniqueRegionCount(MM.EditAutonamePreamble, "MMTemplate:Editable", dw.getDocumentDOM("document"));
  163.  
  164.   //determine number of table rows & concatanate rows together
  165.   for (var i=0; i< Rows; i++)
  166.       {     
  167.     if (i == repeatStart-1)
  168.         tableContent += repeatStartContent; 
  169.         
  170.     if (i >= repeatStart-1 && i <= repeatEnd -1)
  171.         tableContent += getEditTableRow(Columns); 
  172.     else
  173.         tableContent += tableRow;
  174.     
  175.     if (i == repeatEnd - 1)
  176.         tableContent += repeatEndContent; 
  177.     
  178.     }
  179.     
  180.     
  181.   //add percent or pixel values to opening tag, if applicable
  182.   if (Width)
  183.     openTag += 'width="'+Width + ((unitChoice == 0)? '%" ' : '" ');
  184.   //add border value, if applicable
  185.   if (Border)
  186.     openTag+='border="' + Border + '" ';
  187.   //add cellspacing value, if applicable
  188.   if (cellSpacing)
  189.     openTag+='cellspacing="' + cellSpacing + '" ';
  190.   //add cellpadding value, if applicable
  191.   if (cellPadding)
  192.     openTag+='cellpadding="' + cellPadding + '" ';
  193.  
  194.   //strip extra space from openTag, if it exists
  195.   spaceIndex = openTag.length-1
  196.   if (escape(openTag.charAt(spaceIndex))=='%20')
  197.     openTag = openTag.substring(0,spaceIndex);
  198.  
  199.   openTag += '>' + tableContent;
  200.  
  201.   if (gDialogShown) {
  202.     saveExtension(document)
  203.   }
  204.   gDialogShown = false; // Reset show dialog global.
  205.   
  206.   openTag += '<'+'/table'+'>';
  207.   gReturnTag= openTag;    
  208.    dw.getDocumentDOM("document").disableLocking();
  209. }
  210.  
  211.  
  212. function objectTag(){
  213.     
  214.     alert(gReturnTag);
  215.     return gReturnTag; 
  216.     }
  217.     
  218.  
  219.  
  220. function createTableStr() {
  221.  
  222.     return gReturnTag;
  223.  
  224. }
  225. //---------------    LOCAL FUNCTIONS   ---------------
  226.  
  227. function saveExtension(curDOM) {
  228.   var curHTML = DWfile.read(curDOM.URL);
  229.   var tempFilename = dw.getConfigurationPath() + '/Shared/MM/Cache/empty.htm';
  230.   if (DWfile.exists(tempFilename)) {
  231.     var tempDOM = dw.getDocumentDOM(tempFilename);
  232.     tempDOM.documentElement.outerHTML = curHTML;
  233.     var atrStr = DWfile.getAttributes(curDOM.URL);
  234.     if (tempDOM.body.outerHTML != curDOM.body.outerHTML && (atrStr.indexOf('R') == -1)){
  235.       tempDOM.body.outerHTML = curDOM.body.outerHTML;
  236.       DWfile.write(curDOM.URL, tempDOM.documentElement.outerHTML);
  237.     }
  238.   }
  239. }
  240.  
  241. function initializeUI()
  242. {
  243. if (!haveCalledCanInsert && !canInsert())
  244.     {
  245.     haveCalledCanInsert = false; 
  246.     window.close();
  247.     return;
  248.     }
  249.     
  250.   document.theForm.repeatName.value =  getUniqueRegionName(MM.RepeatAutonamePreamble, "MMTemplate:Repeat", dw.getDocumentDOM("document"));
  251.  
  252.   document.theForm.Rows.focus(); //set focus on textbox
  253.   document.theForm.Rows.select(); //set insertion point into textbox
  254.   gDialogShown = true;
  255.   gReturnTag = '';
  256.   haveCalledCanInsert = false; 
  257. }
  258.  
  259.  
  260.  
  261.  
  262.     
  263.