forms-json-utils.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // ID of the (hidden) form field used to store the JSON representation of the
  2. // object being edited in this page
  3. var sJsonFieldId = 'json_object';
  4. // The memory representation of the object
  5. var oObj = {};
  6. // Mapping between the fields of the form and the attribute of the current object
  7. // If aFieldsMap[2] contains 'foo' it means that oObj.foo corresponds to the field
  8. // of Id 'att_2' in the form
  9. var aFieldsMap = new Array;
  10. // Update the whole object from the form and also update its
  11. // JSON (serialized) representation in the (hidden) field
  12. function UpdateObjectFromForm(aFieldsMap, oObj)
  13. {
  14. for(i=0; i<aFieldsMap.length; i++)
  15. {
  16. var oElement = document.getElementById('att_'+i);
  17. var sFieldName = aFieldsMap[i];
  18. oObj['m_aCurrValues'][sFieldName] = oElement.value;
  19. sJSON = JSON.stringify(oObj);
  20. var oJSON = document.getElementById(sJsonFieldId);
  21. oJSON.value = sJSON;
  22. }
  23. return oObj;
  24. }
  25. // Update the specified field from the current object
  26. function UpdateFieldFromObject(idField, aFieldsMap, oObj)
  27. {
  28. var oElement = document.getElementById('att_'+idField);
  29. oElement.value = oObj['m_aCurrValues'][aFieldsMap[idField]];
  30. }
  31. // Update all the fields of the Form from the current object
  32. function UpdateFormFromObject(aFieldsMap, oObj)
  33. {
  34. for(i=0; i<aFieldsMap.length; i++)
  35. {
  36. UpdateFieldFromForm(i, aFieldsMap, oObj);
  37. }
  38. }
  39. // This function is meant to be called from the AJAX page
  40. // It reloads the object (oObj) from the JSON representation
  41. // and also updates the form field that contains the JSON
  42. // representation of the object
  43. function ReloadObjectFromServer(sJSON)
  44. {
  45. //console.log('JSON value:', sJSON);
  46. var oJSON = document.getElementById(sJsonFieldId);
  47. oJSON.value = sJSON;
  48. oObj = JSON.parse( '(' + sJSON + ')' );
  49. return oObj;
  50. }
  51. function GoToStep(iCurrentStep, iNextStep)
  52. {
  53. var oCurrentStep = document.getElementById('wizStep'+iCurrentStep);
  54. if (iNextStep > iCurrentStep)
  55. {
  56. // Check the values when moving forward
  57. if (CheckFields('wizStep'+iCurrentStep, true))
  58. {
  59. oCurrentStep.style.display = 'none';
  60. ActivateStep(iNextStep);
  61. }
  62. }
  63. else
  64. {
  65. oCurrentStep.style.display = 'none';
  66. ActivateStep(iNextStep);
  67. }
  68. }
  69. function ActivateStep(iTargetStep)
  70. {
  71. UpdateObjectFromForm(aFieldsMap, oObj);
  72. var oNextStep = document.getElementById('wizStep'+(iTargetStep));
  73. window.location.href='#step'+iTargetStep;
  74. // If a handler for entering this step exists, call it
  75. if (typeof(this['OnEnterStep'+iTargetStep]) == 'function')
  76. {
  77. eval( 'OnEnterStep'+iTargetStep+'();');
  78. }
  79. oNextStep.style.display = '';
  80. G_iCurrentStep = iTargetStep;
  81. //$('#wizStep'+(iTargetStep)).block({ message: null });
  82. }
  83. //function AjaxGetValuesDef(oObj, sClass, sAttCode, iFieldId)
  84. //{
  85. // var oJSON = document.getElementById(sJsonFieldId);
  86. // $.get('ajax.render.php?class=' + sClass + '&json_obj=' + oJSON.value + '&att_code=' + sAttCode,
  87. // { operation: "allowed_values" },
  88. // function(data){
  89. // //$('#field_'+iFieldId).html(data);
  90. // }
  91. // );
  92. //}
  93. //
  94. //function AjaxGetDefaultValue(oObj, sClass, sAttCode, iFieldId)
  95. //{
  96. // // Asynchronously call the server to provide a default value if the field is
  97. // // empty
  98. // if (oObj['m_aCurrValues'][sAttCode] == '')
  99. // {
  100. // var oJSON = document.getElementById(sJsonFieldId);
  101. // $.get('ajax.render.php?class=' + sClass + '&json_obj=' + oJSON.value + '&att_code=' + sAttCode,
  102. // { operation: "default_value" },
  103. // function(json_data){
  104. // var oObj = ReloadObjectFromServer(json_data);
  105. // UpdateFieldFromObject(iFieldId, aFieldsMap, oObj)
  106. // }
  107. // );
  108. // }
  109. //}
  110. // Store the result of the form validation... there may be several forms per page, beware
  111. var oFormErrors = { err_form0: 0 };
  112. function CheckFields(sFormId, bDisplayAlert)
  113. {
  114. $('#'+sFormId+' :submit').attr('disable', 'disabled');
  115. $('#'+sFormId+' :button[type=submit]').attr('disable', 'disabled');
  116. firstErrorId = '';
  117. // The two 'fields' below will be updated when the 'validate' event is processed
  118. oFormErrors['err_'+sFormId] = 0; // Number of errors encountered when validating the form
  119. oFormErrors['input_'+sFormId] = null; // First 'input' with an error, to set the focus to it
  120. $('#'+sFormId+' :input').each( function()
  121. {
  122. validateEventResult = $(this).trigger('validate', sFormId);
  123. }
  124. );
  125. if(oFormErrors['err_'+sFormId] > 0)
  126. {
  127. if (bDisplayAlert)
  128. {
  129. alert('Please fill-in all mandatory fields before continuing.');
  130. }
  131. $('#'+sFormId+' :submit').attr('disable', '');
  132. $('#'+sFormId+' :button[type=submit]').attr('disable', '');
  133. if (oFormErrors['input_'+sFormId] != null)
  134. {
  135. $('#'+oFormErrors['input_'+sFormId]).focus();
  136. }
  137. }
  138. return (oFormErrors['err_'+sFormId] == 0); // If no error, submit the form
  139. }
  140. function ValidateField(sFieldId, sPattern, bMandatory, sFormId)
  141. {
  142. var bValid = true;
  143. var currentVal = $('#'+sFieldId).val();
  144. if (bMandatory && ((currentVal == '') || (currentVal == 0) || (currentVal == '[]')))
  145. {
  146. bValid = false;
  147. }
  148. else if ((currentVal == '') || (currentVal == 0) || (currentVal == '[]'))
  149. {
  150. // An empty field is Ok...
  151. bValid = true;
  152. }
  153. else if (sPattern != '')
  154. {
  155. re = new RegExp(sPattern);
  156. //console.log('Validating field: '+sFieldId + ' current value: '+currentVal + ' pattern: '+sPattern );
  157. bValid = re.test(currentVal);
  158. }
  159. if (bValid)
  160. {
  161. // Visual feedback - none when it's Ok
  162. $('#v_'+sFieldId).html(''); //<img src="../images/validation_ok.png" />');
  163. }
  164. else
  165. {
  166. // Report the error...
  167. oFormErrors['err_'+sFormId]++;
  168. if (oFormErrors['input_'+sFormId] == null)
  169. {
  170. // Let's remember the first input with an error, so that we can put back the focus on it later
  171. oFormErrors['input_'+sFormId] = sFieldId;
  172. }
  173. // Visual feedback
  174. $('#v_'+sFieldId).html('<img src="../images/validation_error.png" />');
  175. }
  176. //console.log('Form: '+sFormId+' Validating field: '+sFieldId + ' current value: '+currentVal+' pattern: '+sPattern+' result: '+bValid );
  177. return true; // Do not stop propagation ??
  178. }
  179. function UpdateDependentFields(aFieldNames)
  180. {
  181. //console.log('UpdateDependentFields:');
  182. //console.log(aFieldNames);
  183. index = 0;
  184. oWizardHelper.ResetQuery();
  185. oWizardHelper.UpdateWizard();
  186. while(index < aFieldNames.length )
  187. {
  188. sAttCode = aFieldNames[index];
  189. sFieldId = oWizardHelper.GetFieldId(sAttCode);
  190. $('#v_'+sFieldId).html('<img src="../images/indicator.gif" />');
  191. oWizardHelper.RequestAllowedValues(sAttCode);
  192. index++;
  193. }
  194. oWizardHelper.AjaxQueryServer();
  195. }
  196. function ResetPwd(id)
  197. {
  198. // Reset the values of the password fields
  199. $('#'+id).val('*****');
  200. $('#'+id+'_confirm').val('*****');
  201. // And reset the flag, to tell it that the password remains unchanged
  202. $('#'+id+'_changed').val(0);
  203. // Visual feedback, None when it's Ok
  204. $('#v_'+id).html('');
  205. }
  206. // Called whenever the content of a one way encrypted password changes
  207. function PasswordFieldChanged(id)
  208. {
  209. // Set the flag, to tell that the password changed
  210. $('#'+id+'_changed').val(1);
  211. }
  212. // Special validation function for one way encrypted password fields
  213. function ValidatePasswordField(id, sFormId)
  214. {
  215. var bChanged = $('#'+id+'_changed').val();
  216. if (bChanged)
  217. {
  218. if ($('#'+id).val() != $('#'+id+'_confirm').val())
  219. {
  220. oFormErrors['err_'+sFormId]++;
  221. if (oFormErrors['input_'+sFormId] == null)
  222. {
  223. // Let's remember the first input with an error, so that we can put back the focus on it later
  224. oFormErrors['input_'+sFormId] = id;
  225. }
  226. // Visual feedback
  227. $('#v_'+id).html('<img src="../images/validation_error.png" />');
  228. return false;
  229. }
  230. }
  231. $('#v_'+id).html(''); //<img src="../images/validation_ok.png" />');
  232. return true;
  233. }