forms-json-utils.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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))
  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)
  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. alert('Please fill-in all mandatory fields before continuing.');
  128. $('#'+sFormId+' :submit').attr('disable', '');
  129. $('#'+sFormId+' :button[type=submit]').attr('disable', '');
  130. if (oFormErrors['input_'+sFormId] != null)
  131. {
  132. $('#'+oFormErrors['input_'+sFormId]).focus();
  133. }
  134. }
  135. return (oFormErrors['err_'+sFormId] == 0); // If no error, submit the form
  136. }
  137. function ValidateField(sFieldId, sPattern, bMandatory, sFormId)
  138. {
  139. var bValid = true;
  140. var currentVal = $('#'+sFieldId).val();
  141. if (bMandatory && ((currentVal == '') || (currentVal == 0)))
  142. {
  143. bValid = false;
  144. }
  145. else if ((currentVal == '') || (currentVal == 0))
  146. {
  147. // An empty field is Ok...
  148. bValid = true;
  149. }
  150. else if (sPattern != '')
  151. {
  152. re = new RegExp(sPattern);
  153. //console.log('Validating field: '+sFieldId + ' current value: '+currentVal + ' pattern: '+sPattern );
  154. bValid = re.test(currentVal);
  155. }
  156. if (bValid)
  157. {
  158. // Visual feedback
  159. $('#v_'+sFieldId).html('<img src="../images/validation_ok.png" />');
  160. }
  161. else
  162. {
  163. // Report the error...
  164. oFormErrors['err_'+sFormId]++;
  165. if (oFormErrors['input_'+sFormId] == null)
  166. {
  167. // Let's remember the first input with an error, so that we can put back the focus on it later
  168. oFormErrors['input_'+sFormId] = sFieldId;
  169. }
  170. // Visual feedback
  171. $('#v_'+sFieldId).html('<img src="../images/validation_error.png" />');
  172. }
  173. //console.log('Form: '+sFormId+' Validating field: '+sFieldId + ' current value: '+currentVal+' pattern: '+sPattern+' result: '+bValid );
  174. return bValid;
  175. }
  176. function UpdateDependentFields(aFieldNames)
  177. {
  178. //console.log('UpdateDependentFields:');
  179. //console.log(aFieldNames);
  180. index = 0;
  181. oWizardHelper.ResetQuery();
  182. oWizardHelper.UpdateWizard();
  183. while(index < aFieldNames.length )
  184. {
  185. sAttCode = aFieldNames[index];
  186. oWizardHelper.RequestAllowedValues(sAttCode);
  187. index++;
  188. }
  189. oWizardHelper.AjaxQueryServer();
  190. }