configureServicesUtils.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /////////////////// Render related functions /////////////////////////////
  2. var globalPasswordsArray = [];
  3. function generateDivForService (option, type, service, property, unit, displayAttributes) {
  4. var unitClass = (unit != null) ? 'unit' : '';
  5. var unitLabel = (unit != null && unit != 'int') ? unit : '';
  6. var readOnlyFlag= false;
  7. if (displayAttributes != null && displayAttributes.editable != null
  8. && !displayAttributes.editable) {
  9. readOnlyFlag = true;
  10. }
  11. var retString = '<div class="formElement">' +
  12. '<label for="' + service + '">' + option['displayName'] + '</label>' +
  13. //((unitString != '') ? '<div class="input-append">' : '') +
  14. '<input class="' + unitClass + '" type="' + type + '" id="' + property + '" name="' + service + '" value="' + option['value'] + '"';
  15. if (readOnlyFlag) {
  16. retString += ' readonly="readonly" ';
  17. }
  18. retString += '><label class="unit">' + unitLabel + '</label>' +
  19. '<div class="contextualHelp">' + option['description'] + '</div>' +
  20. //((unitString != '') ? '</div>' : '') +
  21. '<div class="formInputErrorReason" id="' + property + 'ErrorReason' + '"></div>' +
  22. '</div>';
  23. if (type == "password") {
  24. retString += '<div class="formElement">' +
  25. '<label for="' + service + '"> Retype ' + option['displayName'] + '</label>' +
  26. '<input type="' + type + '" id="' + property + 'SecretService" name="' + service + '" value="' + option['value'] + '">' +
  27. '<div class="contextualHelp">' + option['description'] + '</div>' +
  28. '<div class="formInputErrorReason" id="' + property + 'SecretServiceErrorReason' + '" ></div>' +
  29. '</div>';
  30. /// Put it in the global passwd array
  31. globalPasswordsArray[globalPasswordsArray.length] = {
  32. "passwordDivId" : property,
  33. "verificationDivId" : property + 'SecretService'
  34. };
  35. globalYui.log("Global Passwords Array: " + globalYui.Lang.dump(globalPasswordsArray));
  36. }
  37. return retString;
  38. }
  39. function constructDOM(optionsInfo) {
  40. /* Reset globalPasswordsArray at the beginning of each render cycle to
  41. * avoid using stale data from the last run - this isn't a problem on the
  42. * Configure Services page, but it bites us on the Manage Services page
  43. * there is re-use of this module of code within the same JS memory.
  44. */
  45. globalPasswordsArray = [];
  46. var optionsSummary = "";
  47. for (servicesKey in optionsInfo['services']) {
  48. if (optionsInfo['services'][servicesKey]["isEnabled"] == true) {
  49. var serviceNeedsRender = false;
  50. var propertiesRendering = "";
  51. for (property in optionsInfo['services'][servicesKey]["properties"]) {
  52. // service has configs, so needs render
  53. var type = convertDisplayType(optionsInfo['services'][servicesKey]['properties'][property]['type']);
  54. // globalYui.log("TYPE: " + type + "Property: " + property);
  55. if (type == "NODISPLAY") {
  56. continue;
  57. }
  58. serviceNeedsRender = true;
  59. var unit = optionsInfo['services'][servicesKey]['properties'][property]['unit'];
  60. var displayAttributes = null;
  61. if (optionsInfo['services'][servicesKey]['properties'][property]['displayAttributes']) {
  62. displayAttributes = optionsInfo['services'][servicesKey]['properties'][property]['displayAttributes'];
  63. }
  64. propertiesRendering += generateDivForService(optionsInfo['services'][servicesKey]["properties"][property], type, servicesKey, property, unit, displayAttributes);
  65. }
  66. if (serviceNeedsRender) {
  67. optionsSummary += "<fieldset> <legend>" + servicesKey + "</legend>";
  68. optionsSummary += '<div name=\"configureClusterAdvancedPerServiceDiv\" id=\"' + servicesKey + '\">';
  69. optionsSummary += propertiesRendering;
  70. optionsSummary += '</fieldset></div>';
  71. }
  72. }
  73. }
  74. return optionsSummary;
  75. }
  76. /////////////////// End of rendering related functions /////////////////////////////
  77. /////////////////// Submit related functions /////////////////////////////
  78. // use this function for cleaning up the formInputError class added
  79. // to the fields that failed to satisfy correctness
  80. function cleanupClassesForErrors (divId) {
  81. globalYui.one(divId).removeClass('formInputError');
  82. globalYui.one(divId + "ErrorReason").setContent('');
  83. }
  84. function cleanupClassesForPasswordErrors () {
  85. for (count = 0; count < globalPasswordsArray.length; count++) {
  86. divId = "#" + globalPasswordsArray[count]['verificationDivId'];
  87. cleanupClassesForErrors(divId);
  88. }
  89. }
  90. function clearErrorReasons(opts) {
  91. for(serviceName in opts) {
  92. globalYui.log('Clear errors for svc : ' + serviceName);
  93. globalYui.log(globalYui.Lang.dump(opts[serviceName]['properties']));
  94. for (propKey in opts[serviceName]['properties']) {
  95. globalYui.log('Clear errors for prop : ' + propKey);
  96. globalYui.one('#' + propKey).removeClass('formInputError');
  97. var elem = globalYui.one('#' + propKey + 'ErrorReason');
  98. elem.setContent('');
  99. //} else {
  100. // globalYui.log('Found invalid div for error reason for prop key : ' + propKey);
  101. //}
  102. }
  103. }
  104. }
  105. function addErrorStringToHiddenDiv (divId, errorReason) {
  106. errorDivId = divId + 'ErrorReason';
  107. globalYui.one(errorDivId).setContent(errorReason);
  108. }
  109. function checkPasswordCorrectness () {
  110. var count = 0;
  111. var focusId = "";
  112. var passwdMatch = true;
  113. var errCount = 0;
  114. var errString = "<ul>";
  115. for (count = 0; count < globalPasswordsArray.length; count++) {
  116. var divId = "#" + globalPasswordsArray[count]['passwordDivId'];
  117. var passwd = globalYui.one(divId).get('value');
  118. divId = "#" + globalPasswordsArray[count]['verificationDivId'];
  119. var verifyPasswd = globalYui.one(divId).get('value');
  120. if (passwd !== verifyPasswd) {
  121. errCount++;
  122. errString += "<li>Password does not match for " + globalYui.one(divId).get('name') + "</li>";
  123. globalYui.one(divId).addClass('formInputError');
  124. addErrorStringToHiddenDiv(divId, errString);
  125. if (focusId == '') {
  126. focusId = "formStatusDivId";
  127. }
  128. passwdMatch = false;
  129. } else {
  130. globalYui.one(divId).removeClass('formInputError');
  131. addErrorStringToHiddenDiv(divId, '');
  132. }
  133. }
  134. errString += "</ul>";
  135. retArray = {
  136. "passwdMatched" : passwdMatch,
  137. "focusOn" : focusId,
  138. "errorCount" : errCount,
  139. "errorString" : errString
  140. };
  141. return retArray;
  142. }
  143. function generateUserOpts () {
  144. var retval = checkPasswordCorrectness();
  145. if (retval.passwdMatched !== true) {
  146. setFormStatus(retval.errorString, true, true);
  147. document.getElementById(retval.focusOn).scrollIntoView();
  148. return {};
  149. }
  150. cleanupClassesForPasswordErrors();
  151. var desiredOptions = {};
  152. var temp = globalYui.all("#configureClusterAdvancedDynamicRenderDivId div[name=configureClusterAdvancedPerServiceDiv]");
  153. temp.each(function (selection) {
  154. var selectionStr = "#configureClusterAdvancedDynamicRenderDivId input[name=" + selection.get('id') + "]";
  155. var prop = globalYui.all(selectionStr);
  156. var properties = {};
  157. prop.each(function (proper) {
  158. var value = globalYui.Lang.trim(proper.get('value'));
  159. if ((proper.get('type') == "checkbox")) {
  160. value = proper.get('checked').toString();
  161. }
  162. var keyName = globalYui.Lang.trim(proper.get('id'));
  163. properties[keyName] = {
  164. "value" : value,
  165. };
  166. });
  167. desiredOptions[selection.get('id')] = {
  168. "properties" : properties,
  169. };
  170. });
  171. // globalYui.log("Final Options: " + globalYui.Lang.dump(desiredOptions));
  172. clearFormStatus();
  173. clearErrorReasons(desiredOptions);
  174. return desiredOptions;
  175. }
  176. function handleConfigureServiceErrors(errorResponse) {
  177. var message = errorResponse.error;
  178. setFormStatus(message, true, true);
  179. for (propKey in errorResponse['properties']) {
  180. errorReason = errorResponse['properties'][propKey]['error'];
  181. globalYui.one('#' + propKey).addClass('formInputError');
  182. var elemReason = propKey + 'ErrorReason';
  183. globalYui.log('Setting content ' + errorReason + ' for div ' + elemReason);
  184. globalYui.one('#' + elemReason).setContent(errorReason);
  185. }
  186. document.getElementById('formStatusDivId').scrollIntoView()
  187. }
  188. /////////////////// End of submitting related functions /////////////////////////////