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 / ServerObject-UpdRecCF.js < prev    next >
Encoding:
JavaScript  |  2003-09-05  |  16.6 KB  |  557 lines

  1. // Copyright 2002, 2003 Macromedia, Inc. All rights reserved.
  2.  
  3.     
  4. //**********************GLOBAL VARS********************
  5.  
  6. var helpDoc = MM.HELP_objUpdateRecord;
  7.  
  8. var rsName = "";
  9.  
  10. // stores separate file dom needed for dynamic UI
  11. var domUIPieces = dw.getDocumentDOM("ServerObject-FormEditHTML.htm"); 
  12.  
  13.  
  14. // global form widget variables
  15. var _DisplayAs, _SubmitAs, _ElemUp, _ElemDown, _ElemAdd, _ElemDel;
  16. var _GoToURL, _ElementLabel;
  17.  
  18. var _ConnectionName = new CFDataSourceMenu("ServerObject-UpdateRecord.htm", "ConnectionName"); 
  19. var _UserName = new TextField("ServerObject-UpdateRecord.htm", "UserName");
  20. var _Password = new TextField("ServerObject-UpdateRecord.htm", "Password");
  21. var _TableName = new ConnectionTableMenu("ServerObject-UpdateRecord.htm", "TableName");
  22. var _RecordsetName  = new RecordsetMenu("UpdateRecord.htm", "RecordsetName", "");
  23. var _ColumnNames = new TreeControlWithNavControls("ColumnNames");
  24. var _UniqueKeyColumn = new ListControl("UniqueKeyColumn");
  25. var _IsNumeric = new CheckBox("", "IsNumeric"); 
  26.  
  27. var _includeQuery = null;
  28.  
  29. var ColumnsToAdd = new Array();  // the list of columns available for adding
  30. var ColumnTypes = new Array();
  31.  
  32. // stores the unique key column's SubmitAs
  33. // TODO: the submitAs types are platform specific
  34. var SUBMIT_AS_NUMERIC_KEY = "none,none,NULL";
  35. var SUBMIT_AS_TEXT_KEY = "',none,NULL";   
  36.  
  37. //********************API FUNCTIONS******************************    
  38. //--------------------------------------------------------------------
  39. // FUNCTION:
  40. //   commandButtons
  41. //
  42. // DESCRIPTION:
  43. //   Returns the list of buttons which should appear on the right hand
  44. //   side of the dialog
  45. //
  46. // ARGUMENTS:
  47. //   none
  48. //
  49. // RETURNS:
  50. //   Array - pairs of button name and function call
  51. //--------------------------------------------------------------------
  52. function commandButtons()
  53. {
  54.   return new Array(MM.BTN_OK,     "clickedOK()", 
  55.                    MM.BTN_Cancel, "clickedCancel()", 
  56.                    MM.BTN_Help,   "displayHelp()"); 
  57. }
  58.  
  59.  
  60. //--------------------------------------------------------------------
  61. // FUNCTION:
  62. //   canInsertObject
  63. //
  64. // DESCRIPTION:
  65. //   This function is called to determine if this object can be inserted
  66. //   into the current document.  It displays the relevant error messages,
  67. //   and then returns a boolean to indicate if insertion is possible.
  68. //
  69. //   NOTE: this function is called before initializeUI, so it should
  70. //         not rely on internal state.
  71. //
  72. // ARGUMENTS:
  73. //   none
  74. //
  75. // RETURNS:
  76. //   boolean
  77. //--------------------------------------------------------------------
  78. function canInsertObject()
  79. {
  80.   var retVal = true;
  81.  
  82.   var errMsgStr = "";
  83.   if (dwscripts.getRecordsetNames().length == 0) 
  84.   { 
  85.     errMsgStr = dwscripts.sprintf(MM.MSG_NeedRecordsetForObject, dwscripts.getRecordsetDisplayName());
  86.   }
  87.  
  88.   if (errMsgStr)
  89.   {
  90.     alert (errMsgStr);
  91.     retVal = false;
  92.   }
  93.  
  94.   return retVal;
  95. }
  96.  
  97.  
  98.  
  99. //********************LOCAL FUNCTIONS******************************    
  100. //--------------------------------------------------------------------
  101. // FUNCTION:
  102. //   clickedOK
  103. //
  104. // DESCRIPTION:
  105. //   This function is called when the user clicks OK
  106. //
  107. // ARGUMENTS:
  108. //   none
  109. //
  110. // RETURNS:
  111. //   nothing
  112. //--------------------------------------------------------------------
  113. function clickedOK()
  114. {
  115.   var table = _TableName.getValue();
  116.   var redirectURL   = (_GoToURL.value)?_GoToURL.value:"";
  117.   var nRows, i, currRowInfoObj, fieldInfoObj;  
  118.   var sqlObj = new SQLStatement("");
  119.   var columnInfoList = new Array(), columnInfoNode, ElementStrArr = new Array();
  120.   var hiddenFieldNamesArr = new Array(), hiddenFieldValuesArr = new Array();
  121.   
  122.   rs  = _RecordsetName.getValue();
  123.   col = _UniqueKeyColumn.getValue();
  124.   rowInfoArr = _ColumnNames.valueList;
  125.   nRows = rowInfoArr.length;
  126.     
  127.   // check for error conditions
  128.   var errMsgStr = "";
  129.   if (!_ConnectionName.getValue()) 
  130.   {
  131.     errMsgStr = MM.MSG_NoCFDataSource;
  132.   } 
  133.   else if (!table) 
  134.   {
  135.     errMsgStr = MM.MSG_NoTablesInDS;
  136.   }
  137.   else if (!nRows)
  138.   {
  139.     errMsgStr = MM.Msg_NoColumnsInTable;
  140.   } 
  141.   else if (!rs)
  142.   {
  143.     alert(dwscripts.sprintf(MM.MSG_NoRecordsets, dwscripts.getRecordsetDisplayName()));
  144.   }
  145.   else if (!col) 
  146.   {
  147.     errMsgStr = MM.MSG_NoColumn;
  148.   }
  149.   
  150.   if (!errMsgStr) 
  151.   {
  152.     var colList = dwscripts.getFieldNames(rs);
  153.     // check that the selected column exists in the recordset
  154.     var j, found=true;
  155.     for (j=0, found=false; !found && j < colList.length; j++)
  156.     {
  157.       found = (colList[j].toLowerCase() == col.toLowerCase());
  158.     }  
  159.     if (!found)
  160.     {
  161.       errMsgStr = dwscripts.sprintf(MM.MSG_NoColumnInRS, dwscripts.getRecordsetDisplayName());
  162.     }
  163.   }
  164.   
  165.   // if error condition, alert it and return
  166.   if (errMsgStr)
  167.   {
  168.     alert (errMsgStr);
  169.     return;
  170.   }
  171.   
  172.   // if no error conditions, build the edits to apply to the document
  173.   MM.setBusyCursor();
  174.   
  175.   // create parameter object used to provide variables for this edit op
  176.   // server behavior
  177.   var paramObj = new Object(); 
  178.  
  179.   paramObj.TableAlign   = "center";
  180.   // Get a unique form name...
  181.   var formName = dwscripts.getUniqueNameForTag("FORM","form");
  182.   paramObj.FormName = formName; 
  183.   
  184.   var addedPrimary = false;
  185.   
  186.   for (i = 0; i < nRows; i++)
  187.   {
  188.     currRowInfoObj = rowInfoArr[i];
  189.     fieldInfoObj = currRowInfoObj.displayAs; 
  190.     fieldInfoObj.fieldName = currRowInfoObj.fieldName;
  191.     var columnName = currRowInfoObj.column;
  192.     
  193.     if (columnName)
  194.     {
  195.       columnInfoNode = dwscripts.getColumnValueNode(); // get platform specific ColumnValueNode
  196.       columnInfoNode.setColumnName(columnName);    
  197.       columnInfoNode.setColumnType(fieldInfoObj.type);      
  198.       // TODO: The 'Form.' prefix is platform specific, how can we remove this?
  199.       columnInfoNode.setVariableName("FORM." + fieldInfoObj.fieldName);                  
  200.    
  201.       // Check if this field is the unique primary key field, if so
  202.       // push another node for the primary key...
  203.       if (columnName == col)
  204.       {
  205.         addedPrimary = true;
  206.         columnInfoNode.setIsPrimaryKey(true);
  207.         // Select between submit as strings for a text primary key or numeric primary key.
  208.         columnInfoNode.setSubmitAs(_IsNumeric.getCheckedState() ? SUBMIT_AS_NUMERIC_KEY 
  209.                                                                 : SUBMIT_AS_TEXT_KEY);
  210.                                                                 
  211.         // push the Primary Key column hidden field into the hidden field array                                                                
  212.         hiddenFieldNamesArr.push(fieldInfoObj.fieldName);
  213.         hiddenFieldValuesArr.push(fieldInfoObj.text);                                                                   
  214.       }
  215.       else
  216.       {      
  217.         columnInfoNode.setSubmitAs(currRowInfoObj.submitAs);                  
  218.       }    
  219.       columnInfoList.push(columnInfoNode);          
  220.     }
  221.             
  222.     // handle the hidden fields
  223.     if (fieldInfoObj.type == "hiddenField")
  224.     {
  225.       hiddenFieldNamesArr.push(fieldInfoObj.fieldName);
  226.       hiddenFieldValuesArr.push(fieldInfoObj.value);
  227.     }  
  228.   }  
  229.   
  230.   // Push the Update hidden field into the array...
  231.   hiddenFieldNamesArr.push("MM_UpdateRecord");
  232.   hiddenFieldValuesArr.push(formName);
  233.   
  234.   // TODO: the following code is platform specific, how can we remove this?
  235. //  hiddenFieldNamesArr.push(col);
  236. //  var primaryKeyValue = "<cfoutput>" + "#" + rs + "." + col + "#" + "</cfoutput>";
  237. //  hiddenFieldValuesArr.push(primaryKeyValue);
  238.   
  239.   if (!addedPrimary)
  240.   {
  241.     // add primary key columnInfoNode
  242.     var keyFieldName = getElementNameFromColumnName(col);
  243.     columnInfoNode = dwscripts.getColumnValueNode(); // get platform specific ColumnValueNode
  244.     columnInfoNode.setColumnName(col);
  245.     columnInfoNode.setColumnType("");
  246.     columnInfoNode.setVariableName("FORM." + keyFieldName);                  
  247.     columnInfoNode.setIsPrimaryKey(true);
  248.     // Select between submit as strings for a text primary key or numeric primary key.
  249.     columnInfoNode.setSubmitAs(_IsNumeric.getCheckedState() ? SUBMIT_AS_NUMERIC_KEY 
  250.                                                             : SUBMIT_AS_TEXT_KEY);
  251.     columnInfoList.push(columnInfoNode);
  252.     
  253.     // push the Primary Key column hidden field into the hidden field array                                                                
  254.     hiddenFieldNamesArr.push(keyFieldName);
  255.     hiddenFieldValuesArr.push(createDynamicData(rs, col));       
  256.   }
  257.   
  258.   paramObj.HiddenFieldName = hiddenFieldNamesArr;
  259.   paramObj.HiddenFieldValue = hiddenFieldValuesArr;
  260.     
  261.   // this will go through all the form elements and returns a string array...
  262.   ElementStrArr = createFormElementStrings(rowInfoArr);
  263.   
  264.   paramObj.ElementString = ElementStrArr;
  265.   paramObj.ButtonText = MM.BTN_UpdateRecord;
  266.   
  267.   paramObj.RedirectURL = redirectURL;
  268.   paramObj.ActionQueryString = (_includeQuery.checked) ? "?#CGI.QUERY_STRING#" : "";
  269.   
  270.   paramObj.ConnectionName = _ConnectionName.getName();
  271.   paramObj.UserName = _UserName.getValue();
  272.   paramObj.Password = _Password.getValue();
  273.   
  274.   paramObj.RecordsetName = rs;
  275.   paramObj.PrimaryKeyColumn = col; 
  276.  
  277.   sqlObj.createUpdateStatement(table, columnInfoList);    
  278.   var sqlString = sqlObj.getStatement();
  279.   paramObj.SQLStatement = sqlString;
  280.   
  281.   // correct the selection
  282.   checkThatCursorIsNotInsideOfAForm();
  283.   dwscripts.setCursorOutsideParagraph();
  284.   dwscripts.fixUpSelection(dw.getDocumentDOM(), false, true);
  285.   
  286.   dwscripts.applyGroup("RecordUpdateForm", paramObj);
  287.  
  288.   MM.clearBusyCursor();
  289.   window.close();
  290. }
  291.  
  292. //--------------------------------------------------------------------
  293. // FUNCTION:
  294. //   clickedCancel
  295. //
  296. // DESCRIPTION:
  297. //   This function is called when CANCEL is clicked
  298. //
  299. // ARGUMENTS:
  300. //   none
  301. //
  302. // RETURNS:
  303. //   nothing
  304. //--------------------------------------------------------------------
  305. function clickedCancel(){
  306.   MM.commandReturnValue = "";
  307.   window.close();
  308. }
  309.  
  310. //--------------------------------------------------------------------
  311. // FUNCTION:
  312. //   displayHelp
  313. //
  314. // DESCRIPTION:
  315. //   Displays the built-in Dreamweaver help.
  316. //
  317. // ARGUMENTS:
  318. //   none
  319. //
  320. // RETURNS:
  321. //   nothing
  322. //--------------------------------------------------------------------
  323. function displayHelp()
  324. {
  325.   // Replace the following call if you are modifying this file for your own use.
  326.   dwscripts.displayDWHelp(helpDoc);
  327. }
  328.  
  329. //--------------------------------------------------------------------
  330. // FUNCTION:
  331. //   initializeUI
  332. //
  333. // DESCRIPTION:
  334. //   This function is called in the onLoad event.  It is responsible
  335. //   for initializing the UI.
  336. //
  337. // ARGUMENTS:
  338. //   none
  339. //
  340. // RETURNS:
  341. //   nothing
  342. //--------------------------------------------------------------------
  343. function initializeUI()
  344.   _DisplayAs = new ListControl("DisplayAs");
  345.   _SubmitAs = new ListControl("SubmitAs");
  346.   _GoToURL  = dwscripts.findDOMObject("GoToURL");
  347.   _ElementLabel    = dwscripts.findDOMObject("ElementLabel");
  348.   _ElemUp = dwscripts.findDOMObject("elemUp");
  349.   _ElemDown = dwscripts.findDOMObject("elemDown");
  350.   _ElemAdd = dwscripts.findDOMObject("elemAdd");
  351.   _ElemDel = dwscripts.findDOMObject("elemDel");
  352.      
  353.   _includeQuery = dwscripts.findDOMObject("includeQuery");
  354.   
  355.   // initialize the form elements
  356.   var setConnectionSuccess = true;  // return value from connectionmenu's initializeUI() 
  357.   setConnectionSuccess = _ConnectionName.initializeUI();
  358.   
  359.   _IsNumeric.initializeUI(); 
  360.  
  361.    // populate menus
  362.    var displayAsArr = new Array(TEXTFIELD,TEXTAREA,MENU,HIDDENFIELD,CHECKBOX,RADIOGROUP,PASSWORDFIELD,STATICTEXT);
  363.    _DisplayAs.setAll(displayAsArr,displayAsArr);
  364.    
  365.    _SubmitAs.init();  // get the values from the HTML
  366.    
  367.   _RecordsetName.initializeUI();
  368.   _UserName.initializeUI();
  369.   _Password.initializeUI();
  370.   _TableName.initializeUI();   
  371.  
  372.   rsName = _RecordsetName.getValue();
  373.   
  374.   _ColumnNames.setColumnNames(MM.LABEL_ColGrid);
  375.   
  376.   //EnableDisableUpDownBtns();
  377.   //EnableDisableAddDelBtns();    
  378.  
  379.   elts = document.forms[0].elements;
  380.   if (elts && elts.length)
  381.   {
  382.     elts[0].focus();
  383.   }
  384. }
  385.  
  386. //--------------------------------------------------------------------
  387. // FUNCTION:
  388. //   updateUI
  389. //
  390. // DESCRIPTION:
  391. //   This function is called by the UI controls to handle UI updates
  392. //
  393. // ARGUMENTS:
  394. //   control - string - the name of the control sending the event
  395. //   event - string - the event which is being sent
  396. //
  397. // RETURNS:
  398. //   nothing
  399. //--------------------------------------------------------------------
  400. function updateUI(control, event)
  401. {
  402.   if (control == "ConnectionName")
  403.   {
  404.    // check the connection, and get a username and password if needed
  405.    _ConnectionName.ensureLogin();
  406.  
  407.    if (event == "onChange")
  408.    {
  409.      // set the username and password for this data source
  410.      _UserName.setValue(_ConnectionName.getUsername());
  411.      _Password.setValue(_ConnectionName.getPassword());
  412.    }
  413.  
  414.    _TableName.updateUI(_ConnectionName, event); // this will trigger a recursive call to this function
  415.   }
  416.   else if (control == "TableName")
  417.   {
  418.     var i, colArr = new Array(), EMPTY_LIST = new Array();
  419.     // clear additional column list
  420.     // it lists columns that don't get populated in the grid, and needs to be cleared
  421.     updateAdditionalColumnList('clear'); 
  422.  
  423.     // populate grid
  424.     populateColumnGrid();
  425.     
  426.     checkForUnsupportedColumnTypes(true);
  427.     
  428.     // clear global field names array (used to check for dupe field names)
  429.     STR_ELEMENT_NAMES = STR_DIVIDER;
  430.     
  431.     // populate UI according to first grid item
  432.     displayGridFieldValues();
  433.  
  434.     // populate uniqueID menu
  435.     for (i in ColumnTypes)
  436.     {
  437.       colArr.push(i);
  438.     }
  439.     
  440.     if (colArr.length > 0)
  441.       _UniqueKeyColumn.setAll(colArr, colArr);
  442.     else
  443.       _UniqueKeyColumn.setAll(new Array(MM.LABEL_NoColumns), EMPTY_LIST); 
  444.     updateUI("UniqueKeyColumn");
  445.   }
  446.   else if (control == "RecordsetName")
  447.   {
  448.     if(event == 'onChange')
  449.     {
  450.       if (rsName != "")
  451.       {
  452.         // populate grid
  453.         populateColumnGrid(); 
  454.         checkForUnsupportedColumnTypes(false);         
  455.         updateDefaultFormFieldValues(rsName, _RecordsetName.getValue());
  456.         displayGridFieldValues();
  457.         rsName = _RecordsetName.getValue();
  458.       }
  459.     }
  460.   }
  461.   else if (control == "UniqueKeyColumn")
  462.   {        
  463.     // check for checking numeric box here
  464.     var column = _UniqueKeyColumn.get();
  465.     
  466.     // populate the column grid again after the unique key column is selected
  467.     populateColumnGrid();
  468.     
  469.     checkForUnsupportedColumnTypes(false);
  470.     
  471.     // refresh the grid field values
  472.     displayGridFieldValues();
  473.         
  474.     if (ColumnTypes[column] != null) 
  475.     {
  476.       if (dwscripts.isNumericDBColumnType(ColumnTypes[column])) {
  477.         _IsNumeric.setCheckedState(true);
  478.       } else {
  479.         _IsNumeric.setCheckedState(false);
  480.       }
  481.     }
  482.   }
  483.   else if (control == "AddRow")
  484.   {
  485.     addGridRow();
  486.     //EnableDisableUpDownBtns();
  487.     //EnableDisableAddDelBtns();    
  488.   }
  489.   else if (control == "DeleteRow")
  490.   {
  491.     deleteGridRow();
  492.     //EnableDisableUpDownBtns();
  493.     //EnableDisableAddDelBtns();    
  494.   }
  495.   else if (control == "MoveRowUp")
  496.   {
  497.     _ColumnNames.moveRowUp();
  498.     //EnableDisableUpDownBtns();
  499.   }
  500.   else if (control == "MoveRowDown")
  501.   {
  502.     _ColumnNames.moveRowDown();
  503.     //EnableDisableUpDownBtns();
  504.   }
  505.   else if (control == "ColumnNames")
  506.   {
  507.     displayGridFieldValues();
  508.   }
  509.   else if (control == "ElementLabel")
  510.   {
  511.     updateGridRow('label');
  512.   }
  513.   else if (control == "SubmitAs")
  514.   {
  515.     updateGridRow('submitAs');
  516.   }
  517.   else if (control == "DisplayAs")
  518.   {
  519.     showDifferentParams(true);
  520.  
  521.     var newSubmitType = getSubmitTypeBasedOnElementType
  522.       (_SubmitAs.getValue(),_DisplayAs.get(),ColumnTypes[ _ColumnNames.getRowValue().column ]);
  523.     _SubmitAs.pickValue(newSubmitType);
  524.  
  525.     updateGridRow('displayAs')
  526.   }  
  527.   else if (control == "GoToURL")
  528.   {
  529.     if (event == "onClick")
  530.     {
  531.       var theRedirectURL = dw.browseForFileURL("select", MM.LABEL_FileRedirect,0,0); 
  532.  
  533.       if (theRedirectURL.length > 0)
  534.       {
  535.         theRedirectURL = dwscripts.stripCFOutputTags(theRedirectURL);
  536.  
  537.         _GoToURL.value = theRedirectURL; 
  538.         _GoToURL.focus();
  539.  
  540.         _includeQuery.checked = hasQueryString(theRedirectURL);
  541.       } 
  542.     }
  543.     else if (event == "onBlur")
  544.     {
  545.       _includeQuery.checked = hasQueryString(_GoToURL.value);
  546.     }
  547.   }
  548.   else if (control == "includeQuery")
  549.   {
  550.     var redirectURL = getRedirectURL(_GoToURL.value, _includeQuery.checked);
  551.     
  552.     _GoToURL.value = redirectURL;
  553.   }
  554.  
  555. }
  556.