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-UpdRecASPNet.js < prev    next >
Encoding:
JavaScript  |  2003-09-05  |  16.8 KB  |  624 lines

  1. // Copyright 2002, 2003 Macromedia, Inc. All rights reserved.
  2.  
  3.     
  4. //**********************GLOBAL VARS********************
  5.  
  6. var helpDoc = MM.HELP_soASPNetUpdateForm;
  7. var SB_FILENAME = "ServerObject-UpdRecASPNet.htm";
  8.  
  9. // stores separate file dom needed for dynamic UI
  10.  
  11. var domUIPieces = dw.getDocumentDOM("ServerObject-FormEditHTML.htm"); 
  12.  
  13. var _ConnectionName = new ConnectionMenu(SB_FILENAME, "ConnectionName");
  14. var _TableName = new ConnectionTableMenu(SB_FILENAME, "TableName");
  15. var _RecordsetName  = new RecordsetMenu("UpdateRecord.htm", "RecordsetName", "");
  16. var _ColumnNames = null;
  17. var _ElemUp = null;
  18. var _ElemDown = null;
  19. var _ElemAdd = null;
  20. var _ElemDel = null;
  21. var _ElementLabel = null;
  22. var _DisplayAs = null;
  23. var _UseWebFormCtrl = new CheckBox(SB_FILENAME, "UseWebFormControl");
  24. var _SubmitAs = null;
  25. var _SuccessURL = new TextField(SB_FILENAME, "SuccessURL");
  26. var _FailureURL = new TextField(SB_FILENAME, "FailureURL");
  27. var _DebugInfo = new CheckBox(SB_FILENAME, "Debug");
  28. var _UniqueKeyColumn = null;
  29. var _UniqueKeySubmitAs = null;
  30.  
  31. var ColumnsToAdd = new Array();
  32. var ColumnTypes = new Array();
  33. var recordsetName = "";
  34. var uniqueKeyColumnName = "";
  35.  
  36. //********************API FUNCTIONS******************************
  37.  
  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.  
  53. function commandButtons()
  54. {
  55.   return new Array(MM.BTN_OK,     "clickedOK()", 
  56.                    MM.BTN_Cancel, "clickedCancel()", 
  57.                    MM.BTN_Help,   "displayHelp()"); 
  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.  
  79. function canInsertObject()
  80. {
  81.   return true;
  82. }
  83.  
  84. //********************LOCAL FUNCTIONS****************************** 
  85.  
  86. //--------------------------------------------------------------------
  87. // FUNCTION:
  88. //   clickedOK
  89. //
  90. // DESCRIPTION:
  91. //   This function is called when the user clicks OK
  92. //
  93. // ARGUMENTS:
  94. //   none
  95. //
  96. // RETURNS:
  97. //   nothing
  98. //--------------------------------------------------------------------
  99.  
  100. function clickedOK()
  101. {
  102.   var conn = _ConnectionName.getValue();
  103.   var databaseType = MMDB.getDatabaseType(conn);
  104.   var table = _TableName.getValue();
  105.   var successURL = (_SuccessURL.getValue()) ? _SuccessURL.getValue() : "";
  106.   var failureURL = (_FailureURL.getValue()) ? _FailureURL.getValue() : "";
  107.   var debug = _DebugInfo.getCheckedState();
  108.   var uniqueCol = _UniqueKeyColumn.getValue();
  109.  
  110.   recordsetName = _RecordsetName.getValue();
  111.  
  112.   var i;
  113.   var currRowInfoObj;
  114.   var fieldInfoObj;
  115.   var sqlObj = new SQLStatement("");
  116.   var columnInfoList = new Array();
  117.   var ElementStrArr = new Array();
  118.   var hiddenFieldNamesArr = new Array();
  119.   var hiddenFieldValuesArr = new Array();
  120.   var rowInfoArr = _ColumnNames.valueList;
  121.   var nRows = rowInfoArr.length;
  122.  
  123.   // check for error conditions
  124.  
  125.   var errMsgStr = "";
  126.  
  127.   if (!conn)
  128.   {
  129.     errMsgStr = MM.MSG_NoDataSource;
  130.   }
  131.   else if (!table)
  132.   {
  133.     errMsgStr = MM.MSG_NoTablesInDS;
  134.   }
  135.   else if (!recordsetName)
  136.   {
  137.     errMsgStr = dwscripts.sprintf(MM.MSG_NoRecordsets, dwscripts.getRecordsetDisplayName());
  138.   }
  139.   else if (!nRows)
  140.   {
  141.     errMsgStr = MM.Msg_NoColumnsInTable;
  142.   }   
  143.  
  144.   if (!errMsgStr) 
  145.   {
  146.     // check that the selected column exists in the recordset
  147.     
  148.     var colList = dwscripts.getFieldNames(recordsetName);
  149.     var found = false;
  150.     
  151.     for (var j = 0; j < colList.length; j++)
  152.     {
  153.       if (colList[j].toLowerCase() == uniqueCol.toLowerCase())
  154.       {
  155.         found = true;
  156.         break;
  157.       }
  158.     }
  159.  
  160.     if (!found)
  161.     {
  162.       errMsgStr = dwscripts.sprintf(MM.MSG_NoColumnInRS, dwscripts.getRecordsetDisplayName());
  163.     }
  164.   }
  165.  
  166.   // if error condition, alert it and return
  167.  
  168.   if (errMsgStr)
  169.   {
  170.     alert (errMsgStr);
  171.     return;
  172.   }
  173.     
  174.   // if no error conditions, build the edits to apply to the document
  175.  
  176.   MM.setBusyCursor();
  177.  
  178.   // create parameter object used to provide variables for this edit op
  179.   // server behavior
  180.  
  181.   var paramObj = new Object(); 
  182.  
  183.   paramObj.TableAlign   = "center";
  184.  
  185.   // Get a unique form name...
  186.  
  187.   var formName = dwscripts.getUniqueNameForTag("FORM", "form");
  188.  
  189.   paramObj.FormName = formName;
  190.   paramObj.Language = dw.getDocumentDOM().serverModel.getServerLanguage();
  191.  
  192.   var addedPrimary = false;
  193.     
  194.   for (i = 0; i < nRows; i++)
  195.   {
  196.     currRowInfoObj = rowInfoArr[i];
  197.     
  198.       fieldInfoObj = currRowInfoObj.displayAs; 
  199.     fieldInfoObj.fieldName = currRowInfoObj.fieldName;
  200.  
  201.     var columnName = currRowInfoObj.column;
  202.  
  203.     if (columnName)
  204.     {
  205.       var columnInfoNode = dwscripts.getColumnValueNode();
  206.             
  207.       columnInfoNode.setColumnName(columnName);
  208.       columnInfoNode.setColumnType(fieldInfoObj.type);
  209.       columnInfoNode.setVariableName(fieldInfoObj.fieldName);
  210.       columnInfoNode.setRuntimeValue((databaseType.toLowerCase() == "oledb") ? "?" : ("@" + columnName));
  211.  
  212.       // Check if this field is the unique primary key field
  213.       // if so push another node for the primary key...
  214.  
  215.       if (columnName == uniqueCol)
  216.       {
  217.         addedPrimary = true;
  218.  
  219.         columnInfoNode.setIsPrimaryKey(true);
  220.         columnInfoNode.setSubmitAs(_UniqueKeySubmitAs.getValue());
  221.                                                                 
  222.         // push the Primary Key column into the hidden field
  223.         // array if it's not already slated for that
  224.  
  225.         if (fieldInfoObj.type != "hiddenField")
  226.         {
  227.           hiddenFieldNamesArr.push(fieldInfoObj.fieldName);
  228.           hiddenFieldValuesArr.push(fieldInfoObj.text);
  229.         }                                                                   
  230.       }
  231.       else
  232.       {      
  233.         columnInfoNode.setSubmitAs(currRowInfoObj.submitAs);                  
  234.       }    
  235.  
  236.       columnInfoList.push(columnInfoNode);
  237.     }
  238.         
  239.     // handle the hidden fields
  240.  
  241.     if (fieldInfoObj.type == "hiddenField")
  242.     {
  243.       hiddenFieldNamesArr.push(fieldInfoObj.fieldName);
  244.       hiddenFieldValuesArr.push(fieldInfoObj.value);
  245.     }  
  246.   }
  247.   
  248.   // Also push the Update hidden field into the array...
  249.   
  250.   hiddenFieldNamesArr.push("MM_update");
  251.   hiddenFieldValuesArr.push(formName);
  252.   
  253.   // Handle the primary key if not already done
  254.  
  255.   if (!addedPrimary)
  256.   {
  257.     columnInfoNode = dwscripts.getColumnValueNode();
  258.     
  259.     var uniqueColFieldName = getElementNameFromColumnName(uniqueCol);
  260.     
  261.     columnInfoNode.setColumnName(uniqueCol);
  262.     columnInfoNode.setColumnType("");
  263.     columnInfoNode.setVariableName(uniqueColFieldName);                  
  264.     columnInfoNode.setIsPrimaryKey(true);
  265.     columnInfoNode.setSubmitAs(_UniqueKeySubmitAs.getValue());
  266.     columnInfoNode.setRuntimeValue((databaseType.toLowerCase() == "oledb") ? "?" : ("@" + uniqueCol));
  267.  
  268.     columnInfoList.push(columnInfoNode);
  269.     
  270.     // push the Primary Key column hidden field into the hidden field array                                                                
  271.     hiddenFieldNamesArr.push(uniqueColFieldName);
  272.     hiddenFieldValuesArr.push(createDynamicData(recordsetName, uniqueCol));       
  273.   }
  274.  
  275.   paramObj.HiddenFieldName = hiddenFieldNamesArr;
  276.   paramObj.HiddenFieldValue = hiddenFieldValuesArr;
  277.   
  278.   // Parameters need to be in the same order as they
  279.   // are referenced in the sql. For an update statement,
  280.   // the primary keys are at the end. So, put them last.
  281.  
  282.   columnInfoList.sort(sortByPrimaryKeyCB);
  283.  
  284.   // this will go through all the form elements and returns a string array...
  285.   
  286.   ElementStrArr = createFormElementStrings(rowInfoArr);
  287.   
  288.   paramObj.ElementString = ElementStrArr;
  289.   paramObj.ButtonText = MM.BTN_UpdateRecord;
  290.   paramObj.ConnectionPath = dwscripts.getConnectionURL(conn);
  291.  
  292.   sqlObj.createUpdateStatement(table, columnInfoList);    
  293.   
  294.   paramObj.FormName = formName;
  295.   paramObj.CommandText = dwscripts.escSQLQuotes(sqlObj.getStatement(true));
  296.   paramObj.ConnectionName = conn;  
  297.   paramObj.SuccessURL = SBDatabaseCallASPNET.transformURL(successURL, true);
  298.   paramObj.FailureURL = SBDatabaseCallASPNET.transformURL(failureURL, true);
  299.   paramObj.Debug = debug;
  300.    
  301.   // Parmeters
  302.   
  303.   var paramNames = new Array();
  304.   var paramRuntimes = new Array(); 
  305.   var paramTypes = new Array(); 
  306.  
  307.   for (var z = 0; z < columnInfoList.length; ++z)
  308.   {
  309.     if (columnInfoList[z].getVariableName())
  310.     {
  311.       var columnName = columnInfoList[z].getColumnName();
  312.       var paramName = ("@" + columnName);
  313.       var defaultValue = (columnInfoList[z].getDefaultValue() != "") ? 
  314.                           columnInfoList[z].getDefaultValue() : "\"\"";
  315.         var submitAs = columnInfoList[z].getSubmitAs();
  316.       var runtime = dwscripts.getParameterCodeFromType(MM.LABEL_ASPNET_Param_Types[1],
  317.                                                        columnInfoList[z].getVariableName(),
  318.                                                          defaultValue,
  319.                                                        submitAs);
  320.  
  321.       paramNames.push(paramName);
  322.       paramRuntimes.push(runtime);     
  323.       paramTypes.push(submitAs);
  324.     }
  325.   }
  326.   
  327.   paramObj.SqlVarName = paramNames;
  328.   paramObj.SqlVarRuntime = paramRuntimes;
  329.   paramObj.SqlVarType = paramTypes;
  330.   
  331.   // correct the selection
  332.   checkThatCursorIsNotInsideOfAForm();
  333.   dwscripts.setCursorOutsideParagraph();
  334.   dwscripts.fixUpSelection(dw.getDocumentDOM(), false, true);
  335.   
  336.   dwscripts.applyGroup("RecordUpdateForm", paramObj);
  337.  
  338.   MM.clearBusyCursor();
  339.   window.close();
  340. }
  341.  
  342. //--------------------------------------------------------------------
  343. // FUNCTION:
  344. //   clickedCancel
  345. //
  346. // DESCRIPTION:
  347. //   This function is called when CANCEL is clicked
  348. //
  349. // ARGUMENTS:
  350. //   none
  351. //
  352. // RETURNS:
  353. //   nothing
  354. //--------------------------------------------------------------------
  355.  
  356. function clickedCancel()
  357. {
  358.   MM.commandReturnValue = "";
  359.   window.close();
  360. }
  361.  
  362. //--------------------------------------------------------------------
  363. // FUNCTION:
  364. //   displayHelp
  365. //
  366. // DESCRIPTION:
  367. //   Displays the built-in Dreamweaver help.
  368. //
  369. // ARGUMENTS:
  370. //   none
  371. //
  372. // RETURNS:
  373. //   nothing
  374. //--------------------------------------------------------------------
  375.  
  376. function displayHelp()
  377. {
  378.   dwscripts.displayDWHelp(helpDoc);
  379. }
  380.  
  381. //--------------------------------------------------------------------
  382. // FUNCTION:
  383. //   initializeUI
  384. //
  385. // DESCRIPTION:
  386. //   This function is called in the onLoad event.  It is responsible
  387. //   for initializing the UI.
  388. //
  389. // ARGUMENTS:
  390. //   none
  391. //
  392. // RETURNS:
  393. //   nothing
  394. //--------------------------------------------------------------------
  395.  
  396. function initializeUI()
  397.   // define global form elements
  398.  
  399.   _DisplayAs = new ListControl("DisplayAs");
  400.   _SubmitAs = new ListControl("SubmitAs");
  401.   _ElementLabel = dwscripts.findDOMObject("ElementLabel");
  402.   _UniqueKeyColumn = new ListControl("UniqueKeyColumn");
  403.   _UniqueKeySubmitAs = new ListControl("UniqueKeySubmitAs");
  404.   _ColumnNames = new TreeControlWithNavControls("ColumnNames");
  405.   _ElemUp = dwscripts.findDOMObject("elemUp");
  406.   _ElemDown = dwscripts.findDOMObject("elemDown");
  407.   _ElemAdd = dwscripts.findDOMObject("elemAdd");
  408.   _ElemDel = dwscripts.findDOMObject("elemDel");
  409.   _SuccessURL.initializeUI();
  410.   _FailureURL.initializeUI();
  411.   _DebugInfo.initializeUI();  
  412.   _DebugInfo.setCheckedState(true);
  413.   _UseWebFormCtrl.initializeUI();
  414.  
  415.   // initialize the form elements
  416.  
  417.   var setConnectionSuccess = _ConnectionName.initializeUI();
  418.   
  419.   if (!setConnectionSuccess)
  420.   {
  421.     clickedCancel(); 
  422.   }   
  423.  
  424.   _RecordsetName.initializeUI();
  425.   _TableName.initializeUI();
  426.   _ColumnNames.setColumnNames(MM.LABEL_ColGrid);
  427.   
  428.   var displayAsArr = new Array(TEXTFIELD,
  429.                                TEXTAREA,
  430.                                MENU,
  431.                                HIDDENFIELD,
  432.                                CHECKBOX,
  433.                                RADIOGROUP,
  434.                                PASSWORDFIELD,
  435.                                STATICTEXT);
  436.   
  437.   _DisplayAs.setAll(displayAsArr, displayAsArr);
  438.   
  439.   recordsetName = _RecordsetName.getValue();
  440.  
  441.   updateUI("Debug");
  442.  
  443.   elts = document.forms[0].elements;
  444.   if (elts && elts.length)
  445.   {
  446.     elts[0].focus();
  447.   }
  448. }
  449.  
  450. //--------------------------------------------------------------------
  451. // FUNCTION:
  452. //   updateUI
  453. //
  454. // DESCRIPTION:
  455. //   This function is called by the UI controls to handle UI updates
  456. //
  457. // ARGUMENTS:
  458. //   control - string - the name of the control sending the event
  459. //   event - string - the event which is being sent
  460. //
  461. // RETURNS:
  462. //   nothing
  463. //--------------------------------------------------------------------
  464.  
  465. function updateUI(control, event)
  466. {
  467.   if (control == "ConnectionName")
  468.   {
  469.     var databaseType = MMDB.getDatabaseType(_ConnectionName.getValue());
  470.     var theTypes = SBDatabaseCallASPNET.getParamTypeList(databaseType);
  471.   
  472.     _UniqueKeySubmitAs.setAll(theTypes, theTypes);
  473.     _SubmitAs.setAll(theTypes, theTypes);
  474.  
  475.     _TableName.updateUI(_ConnectionName, event); // this will trigger a recursive call to this function
  476.   }
  477.   else if (control == "Define")
  478.   {
  479.     _ConnectionName.launchConnectionDialog();
  480.   }  
  481.   else if (control == "TableName")
  482.   {
  483.     // clear additional column list
  484.     // it lists columns that don't get populated in the grid, and needs to be cleared
  485.     
  486.     updateAdditionalColumnList('clear'); 
  487.  
  488.     // populate grid
  489.     
  490.     populateColumnGrid();
  491.     
  492.     // clear global field names array (used to check for dupe field names)
  493.     
  494.     STR_ELEMENT_NAMES = STR_DIVIDER;
  495.     
  496.     // populate UI according to first grid item
  497.     
  498.     displayGridFieldValues();
  499.  
  500.     // populate uniqueKey control
  501.  
  502.     var colArr = new Array();
  503.     var uniqueKeyCol = null;
  504.  
  505.     for (i in ColumnTypes)
  506.     {
  507.       colArr.push(i);
  508.     }
  509.     
  510.     if (colArr.length > 0)
  511.     {
  512.       _UniqueKeyColumn.setAll(colArr, colArr);
  513.     }
  514.     else
  515.     {
  516.       _UniqueKeyColumn.setAll(new Array(MM.LABEL_NoColumns), new Array()); 
  517.     }
  518.     
  519.     if (uniqueKeyColumnName)
  520.     {
  521.       _UniqueKeyColumn.pickValue(uniqueKeyColumnName);
  522.     }
  523.  
  524.     updateUI("UniqueKeyColumn");
  525.   }
  526.   else if (control == "RecordsetName")
  527.   {
  528.     if (event == 'onChange')
  529.     {
  530.       if (recordsetName != "")
  531.       {
  532.         populateColumnGrid(); 
  533.         checkForUnsupportedColumnTypes(false);         
  534.         updateDefaultFormFieldValues(recordsetName, _RecordsetName.getValue());
  535.         displayGridFieldValues();
  536.         
  537.         recordsetName = _RecordsetName.getValue();
  538.       }
  539.     }
  540.   }
  541.   else if (control == "UniqueKeyColumn")
  542.   {        
  543.     var column = _UniqueKeyColumn.get();
  544.     
  545.     // populate the column grid again after the
  546.     // unique key column is selected
  547.     
  548.     populateColumnGrid();
  549.     
  550.     checkForUnsupportedColumnTypes(false);
  551.     
  552.     // refresh the grid field values
  553.     
  554.     displayGridFieldValues();
  555.         
  556.     if (ColumnTypes[column] != null) 
  557.     {
  558.       var connectionName = _ConnectionName.getValue();
  559.       var databaseType = MMDB.getDatabaseType(connectionName);
  560.  
  561.       _UniqueKeySubmitAs.pickValue(dwscripts.getDBColumnTypeAsString(ColumnTypes[column], databaseType));
  562.     }
  563.   }
  564.   else if (control == "AddRow")
  565.   {
  566.     addGridRow();
  567.   }
  568.   else if (control == "DeleteRow")
  569.   {
  570.     deleteGridRow();
  571.   }
  572.   else if (control == "MoveRowUp")
  573.   {
  574.     _ColumnNames.moveRowUp();
  575.   }
  576.   else if (control == "MoveRowDown")
  577.   {
  578.     _ColumnNames.moveRowDown();
  579.   }
  580.   else if (control == "ColumnNames")
  581.   {
  582.     displayGridFieldValues();
  583.   }
  584.   else if (control == "ElementLabel")
  585.   {
  586.     updateGridRow('label');
  587.   }
  588.   else if (control == "SubmitAs")
  589.   {
  590.     updateGridRow('submitAs');
  591.   }
  592.   else if (control == "DisplayAs")
  593.   {
  594.     showDifferentParams(true);
  595.     updateGridRow('displayAs')
  596.   }
  597.   else if (control == "UseWebFormControl")
  598.   {
  599.     updateGridRow('useWebFormControl');
  600.   }
  601.   else if (control == "SuccessURL")
  602.   {
  603.     var theSuccessURL = dw.browseForFileURL("select", MM.LABEL_FileRedirect,0,0); 
  604.     
  605.     if (theSuccessURL.length > 0)
  606.     {
  607.       _SuccessURL.setValue(theSuccessURL); 
  608.     }    
  609.   }
  610.   else if (control == "FailureURL")
  611.   {
  612.     var theFailureURL = dw.browseForFileURL("select", MM.LABEL_FileRedirect,0,0); 
  613.     
  614.     if (theFailureURL.length > 0)
  615.     {
  616.       _FailureURL.setValue(theFailureURL); 
  617.     }    
  618.   }  
  619.   else if (control == "Debug")
  620.   {
  621.     _FailureURL.setDisabled(_DebugInfo.getCheckedState());
  622.   }
  623. }