configureServicesUtils.js 9.1 KB

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