configureServicesUtils.js 8.0 KB

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