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 / RadioGroup.js < prev    next >
Encoding:
JavaScript  |  2003-09-05  |  12.6 KB  |  474 lines

  1. // Copyright 2001, 2002, 2003 Macromedia, Inc. All rights reserved.
  2.  
  3. var HELP_DOC = MM.HELP_RadioGroup;
  4.  
  5. var GC_RADIOS     = new GridControl("LabelValuePairs");
  6. var TF_GROUP_NAME = new TextField("","GroupName");
  7. var RG_LAYOUT     = new RadioGroup("Layout");
  8.  
  9. var PREF_SECTION         = "Extensions\\Objects\\Radio Group";
  10. var PREF_KEY_LAYOUT      = "Layout Type Index";
  11. var PREF_DEFAULT_LAYOUT  = 0;
  12.  
  13. //--------------------------------------------------------------------
  14. // FUNCTION:
  15. //   displayHelp
  16. //
  17. // DESCRIPTION:
  18. //   called when the user clicks the Help button. in this implementation,
  19.  
  20. //
  21. // ARGUMENTS:
  22. //   none
  23. //
  24. // RETURNS:
  25. //   nothing
  26. //--------------------------------------------------------------------
  27. function displayHelp()
  28. {
  29.    // Replace the following call if you are modifying this file for your own use.
  30.    dwscripts.displayDWHelp(HELP_DOC);
  31. }
  32.  
  33. //--------------------------------------------------------------------
  34. // FUNCTION:
  35. //   commandButtons
  36. //
  37. // DESCRIPTION:
  38. //   dialog button control
  39. //
  40. // ARGUMENTS:
  41. //   none
  42. //
  43. // RETURNS:
  44. //   nothing
  45. //--------------------------------------------------------------------
  46. function commandButtons()
  47. {
  48.   return new Array(MM.BTN_OK,"clickedOK()",
  49.                    MM.BTN_Cancel,"window.close()",
  50.                    MM.BTN_Help,"displayHelp()");
  51. }
  52.  
  53.  
  54. //--------------------------------------------------------------------
  55. // FUNCTION:
  56. //   clickedOK
  57. //
  58. // DESCRIPTION:
  59. //   called when the user clicks OK, manages radio group insertion and error messages
  60. //
  61. // ARGUMENTS:
  62. //   none
  63. //
  64. // RETURNS:
  65. //   nothing
  66. //--------------------------------------------------------------------
  67. function clickedOK()
  68. {
  69.   var labelValueArr = GC_RADIOS.getAll();
  70.   var radioGroupName = TF_GROUP_NAME.getValue();
  71.   var canApplyMsg = "";
  72.   
  73.   canApplyMsg = checkForLabelsAndValues(labelValueArr);
  74.   
  75.   if (!canApplyMsg && !radioGroupName)
  76.     canApplyMsg = MM.MSG_NeedARadioGroupName;
  77.   else
  78.     radioGroupName = dwscripts.entityNameEncode(radioGroupName);
  79.   
  80.   if (canApplyMsg)
  81.   {
  82.     alert(canApplyMsg);
  83.     return;
  84.   }
  85.   
  86.   var isTable = RG_LAYOUT.getSelectedIndex() == 1;
  87.   
  88.   // Do not allow the checked attribute to be set from the form object version
  89.   //   of the radio group.
  90.   var selectValueEqualTo = ""; 
  91.   var insertionStr = createRadioGroupString(radioGroupName,isTable,selectValueEqualTo,labelValueArr);
  92.  
  93.   savePreferences(); // save layout choice, i.e. line break or table
  94.  
  95.   dw.getDocumentDOM().insertHTML(returnFormTag(insertionStr),false);
  96.   window.close();
  97.  
  98. }
  99.  
  100.  
  101. //--------------------------------------------------------------------
  102. // FUNCTION:
  103. //   initializeUI
  104. //
  105. // DESCRIPTION:
  106. //   prepare the dialog for user feedback
  107. //
  108. // ARGUMENTS:
  109. //   none
  110. //
  111. // RETURNS:
  112. //   nothing
  113. //--------------------------------------------------------------------
  114. function initializeUI()
  115. {
  116.   TF_GROUP_NAME.initializeUI();
  117.  
  118.   var radioLabel = MM.LABEL_Radio;
  119.   var radioValue = MM.LABEL_Radio.toLowerCase();
  120.  
  121.   var displayArr = new Array(new Array(radioLabel,radioValue),
  122.                              new Array(radioLabel,radioValue)
  123.                             );
  124.  
  125.   GC_RADIOS.setAll(displayArr);
  126.  
  127.   // generate unique radio group name, i.e.: "RadioGroup1"
  128.   TF_GROUP_NAME.setValue( generateRadioGroupName() );
  129.   
  130.   // set layout type to previous choice
  131.   RG_LAYOUT.setSelectedIndex( getPreference(PREF_KEY_LAYOUT) );
  132.  
  133.   TF_GROUP_NAME.textControl.focus();  // set focus to radio group name
  134.   TF_GROUP_NAME.textControl.select(); // select current group name
  135.  
  136. }
  137.  
  138.  
  139. //--------------------------------------------------------------------
  140. // FUNCTION:
  141. //   updateUI
  142. //
  143. // DESCRIPTION:
  144. //   update the UI based on user feedback.
  145. //
  146. // ARGUMENTS:
  147. //   theArg -- label for element or elements to update
  148. //
  149. // RETURNS:
  150. //   nothing
  151. //--------------------------------------------------------------------
  152. function updateUI(theArg)
  153. {
  154.   // if user clicks the "-" button, delete the currently selected item
  155.   if (theArg == "deleteButton")
  156.   {
  157.     GC_RADIOS.del();
  158.   }
  159. }
  160.  
  161.  
  162. //--------------------------------------------------------------------
  163. // FUNCTION:
  164. //   generateRadioGroupName
  165. //
  166. // DESCRIPTION:
  167. //   generate unique radio group name
  168. //
  169. // ARGUMENTS:
  170. //   none
  171. //
  172. // RETURNS:
  173. //   unique name
  174. //--------------------------------------------------------------------
  175. function generateRadioGroupName()
  176. {
  177.   var baseName = MM.LABEL_RadioGroupDefaultName;
  178.   return dwscripts.getUniqueNameForTag("INPUT",baseName);
  179. }
  180.  
  181.  
  182.  
  183. //--------------------------------------------------------------------
  184. // FUNCTION:
  185. //   createRadioGroupString
  186. //
  187. // DESCRIPTION:
  188. //   create the text string to insert into the document
  189. //
  190. // ARGUMENTS:
  191. //   groupName (string), isTable (boolean), selectValueEqualTo (string), labelValueArr(array)
  192. //
  193. // RETURNS:
  194. //   string to insert into the document
  195. //--------------------------------------------------------------------
  196. function createRadioGroupString(groupName,isTable,selectValueEqualTo,labelValueArr)
  197. {
  198.   // labelValueArr is n items long, where n equal the number of radio buttons
  199.   // Each nth item is an array in which [0] is the label and [1] is the value
  200.   
  201.   // Create a third item in the array that contains the checked string or
  202.   // an empty string if there is no checked string
  203.   addCheckedInformation(labelValueArr,selectValueEqualTo);
  204.  
  205.   var nItems = labelValueArr.length;
  206.   var i;
  207.   var insertionStr = "";
  208.   var paramObj = new Object();
  209.   paramObj.RadioName = groupName;
  210.   var radioButtonStr = "";
  211.  
  212.   for (i=0;i<nItems;i++)
  213.   {
  214.     paramObj.RadioButtonLabel = labelValueArr[i][0];
  215.     paramObj.RadioValue = labelValueArr[i][1];
  216.     paramObj.CheckedAttribute = labelValueArr[i][2];
  217.     paramObj.RadioButton = getRadioButtonString(paramObj);
  218.  
  219.     insertionStr += (isTable) ? getTableRowString(paramObj):
  220.                                 getLineBreakString(paramObj);
  221.   }
  222.  
  223.   return ( addOuterTag(insertionStr,isTable) );
  224. }
  225.  
  226.  
  227.  
  228. //--------------------------------------------------------------------
  229. // FUNCTION:
  230. //   addCheckedInformation
  231. //
  232. // DESCRIPTION:
  233. //   adds the checked attribute as a third item in the multi-dimensional array
  234. //   determines the correct type of checked attribute (static or dynamic), and
  235. //   builds up the correct string for either case
  236. //
  237. // ARGUMENTS:
  238. //   labelValueArr (multi-dimensional array),selectValueEqualTo (string)
  239. //
  240. // RETURNS:
  241. //   nothing -- the multi-dimensional array is passed by reference
  242. //--------------------------------------------------------------------
  243. function addCheckedInformation(labelValueArr,selectValueEqualTo)
  244. {
  245.   var nItems = labelValueArr.length;
  246.   var i;
  247.  
  248.   for (i=0; i<nItems; i++)
  249.   {
  250.     labelValueArr[i][2] = "";
  251.   }
  252.   
  253. /*
  254.   // if static value, then look for that value in the list
  255.   if (selectValueEqualTo && !dwscripts.hasServerMarkup(selectValueEqualTo))
  256.   {
  257.     for (i=0;i<nItems;i++)
  258.     {
  259.       labelValueArr[i][2] = (labelValueArr[i][1] == selectValueEqualTo)?"CHECKED":"";
  260.     }
  261.   }
  262.   
  263.   else if (selectValueEqualTo && dwscripts.hasServerModel()) // dynamic case
  264.   {
  265.     var paramObj = new Object();
  266.     var dynamicCheckedStr = "";
  267.     paramObj.DynValue = dwscripts.encodeDynamicExpression(selectValueEqualTo);
  268.  
  269.     for (i=0;i<nItems;i++)
  270.     {
  271.       paramObj.RadioButtonValue = labelValueArr[i][1];
  272.       labelValueArr[i][2] = extPart.getInsertString("","DynRadioGroup_checkedAttr",paramObj);
  273.     }
  274.   }
  275.   
  276.   else // there is no checked information to store
  277.   {
  278.     for (i=0;i<nItems;i++)
  279.       labelValueArr[i][2] = "";
  280.   }
  281. */
  282. }
  283.  
  284.  
  285. //--------------------------------------------------------------------
  286. // FUNCTION:
  287. //   addOuterTag
  288. //
  289. // DESCRIPTION:
  290. //   adds table or p tags around current string, based on user preference
  291. //
  292. // ARGUMENTS:
  293. //   theStr -- string to wrap outer tag around
  294. //   isTable -- boolean -- if false, it is assumed that layout is line breaks
  295. //
  296. // RETURNS:
  297. //   nothing
  298. //--------------------------------------------------------------------
  299. function addOuterTag(theStr,isTable)
  300. {
  301.   return (isTable)?'<table width="200">' + theStr + '</table>':
  302.                    '<p>'                 + theStr + '</p>';
  303.  
  304. }
  305.  
  306.  
  307. //--------------------------------------------------------------------
  308. // FUNCTION:
  309. //   getPreference
  310. //
  311. // DESCRIPTION:
  312. //   gets the layout type from the previous time the radio group was applied.
  313. //   the layout type is "sticky" and remembered from access to access
  314. //
  315. // ARGUMENTS:
  316. //   name of key to get
  317. //
  318. // RETURNS:
  319. //   the last chosen preference, uses default value if no preference on record
  320. //--------------------------------------------------------------------
  321. function getPreference(whichKey)
  322. {
  323.   var retVal = "";
  324.  
  325.   if (whichKey == PREF_KEY_LAYOUT)
  326.   {
  327.     retVal = dw.getPreferenceString(PREF_SECTION,PREF_KEY_LAYOUT,PREF_DEFAULT_LAYOUT);
  328.   }
  329.  
  330.   return retVal;
  331. }
  332.  
  333.  
  334. //--------------------------------------------------------------------
  335. // FUNCTION:
  336. //   savePreferences
  337. //
  338. // DESCRIPTION:
  339. //   save user layout preference
  340. //
  341. // ARGUMENTS:
  342. //   none
  343. //
  344. // RETURNS:
  345. //   nothing
  346. //--------------------------------------------------------------------
  347. function savePreferences()
  348. {
  349.   dw.setPreferenceString(PREF_SECTION,PREF_KEY_LAYOUT,RG_LAYOUT.getSelectedIndex());
  350. }
  351.  
  352.  
  353.  
  354. //--------------------------------------------------------------------
  355. // FUNCTION:
  356. //   getRadioButtonString
  357. //
  358. // DESCRIPTION:
  359. //   get the string which represents one radio button
  360. //
  361. // ARGUMENTS:
  362. //   paramObj -- object with name/value properties
  363. //
  364. // RETURNS:
  365. //   string which represents one html element radio button
  366. //--------------------------------------------------------------------
  367. function getRadioButtonString(paramObj)
  368. {
  369.   var radioStr = '<input type="radio" name="@@RadioName@@" value="@@RadioValue@@">';
  370.  
  371.  
  372.   radioStr = radioStr.replace(/@@RadioName@@/,paramObj.RadioName);
  373.   radioStr = radioStr.replace(/@@RadioValue@@/,paramObj.RadioValue);
  374.  
  375.   if (paramObj.CheckedAttribute)
  376.   {
  377.     radioStr = radioStr.substring(0,radioStr.length-1) + " " + paramObj.CheckedAttribute + ">";
  378.   }
  379.   return radioStr;
  380. }
  381.  
  382.  
  383. //--------------------------------------------------------------------
  384. // FUNCTION:
  385. //   getTableRowString
  386. //
  387. // DESCRIPTION:
  388. //   get the string which represents one table row
  389. //   used only when layout type is set to "Table"
  390. //
  391. // ARGUMENTS:
  392. //   paramObj -- object with name/value properties
  393. //
  394. // RETURNS:
  395. //   string which represents one html table row
  396. //--------------------------------------------------------------------
  397. function getTableRowString(paramObj)
  398. {
  399.   var rowStr =  "<tr><td><label>@@RadioButton@@@@RadioButtonLabel@@</label></td></tr>";
  400.  
  401.   rowStr = rowStr.replace(/@@RadioButton@@/,paramObj.RadioButton);
  402.   rowStr = rowStr.replace(/@@RadioButtonLabel@@/,paramObj.RadioButtonLabel);
  403.  
  404.   return rowStr;
  405. }
  406.  
  407.  
  408. //--------------------------------------------------------------------
  409. // FUNCTION:
  410. //   getLineBreakString
  411. //
  412. // DESCRIPTION:
  413. //   get the string which represents the radio button and radio button label
  414. //   used only when layout type is set to "Line Breaks"
  415. //
  416. // ARGUMENTS:
  417. //   paramObj -- object with name/value properties
  418. //
  419. // RETURNS:
  420. //   string which represents radio button and associated label
  421. //--------------------------------------------------------------------
  422. function getLineBreakString(paramObj)
  423. {
  424.   var textStr = "<label>@@RadioButton@@ @@RadioButtonLabel@@</label><br>";
  425.  
  426.   textStr = textStr.replace(/@@RadioButton@@/,paramObj.RadioButton);
  427.   textStr = textStr.replace(/@@RadioButtonLabel@@/,paramObj.RadioButtonLabel);
  428.  
  429.   return textStr;
  430.  
  431. }
  432.  
  433.  
  434. //--------------------------------------------------------------------
  435. // FUNCTION:
  436. //   checkForLabelsAndValues
  437. //
  438. // DESCRIPTION:
  439. //   checks a multi-dimensional array to verify its contents
  440. //   verifies that there is at least one item, and that all items are complete
  441. //
  442. // ARGUMENTS:
  443. //   theArr - multi-dimensional array
  444. //
  445. // RETURNS:
  446. //   empty string if everything is fine, and error message string if it is not
  447. //--------------------------------------------------------------------
  448. // returns empty string if valid, and error message if not valid
  449. function checkForLabelsAndValues(theArr)
  450. {
  451.   var retVal = "";
  452.   var nItems = theArr.length;
  453.   var i;
  454.   
  455.   if (!nItems || nItems == 0)
  456.   {
  457.     retVal = MM.MSG_NeedAtLeastOneButton;
  458.   }
  459.   
  460.   if (!retVal)
  461.   {
  462.     for (i=0;i<nItems;i++)
  463.     {
  464.       if (theArr[i][0] == "" || theArr[i][1] == "")
  465.       {
  466.         retVal = MM.MSG_EnterLabelAndValue;
  467.         break;
  468.       }
  469.     }
  470.   }
  471.   
  472.   return retVal;
  473. }
  474.