forms-json-utils.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 (CheckMandatoryFields('wizStep'+iCurrentStep))
  55. {
  56. oCurrentStep.style.display = 'none';
  57. ActivateStep(iNextStep);
  58. }
  59. }
  60. function ActivateStep(iTargetStep)
  61. {
  62. UpdateObjectFromForm(aFieldsMap, oObj);
  63. var oNextStep = document.getElementById('wizStep'+(iTargetStep));
  64. window.location.href='#step'+iTargetStep;
  65. // If a handler for entering this step exists, call it
  66. if (typeof(this['OnEnterStep'+iTargetStep]) == 'function')
  67. {
  68. eval( 'OnEnterStep'+iTargetStep+'();');
  69. }
  70. oNextStep.style.display = '';
  71. G_iCurrentStep = iTargetStep;
  72. $('#wizStep'+(iTargetStep)).block({ message: null });
  73. }
  74. function AjaxGetValuesDef(oObj, sClass, sAttCode, iFieldId)
  75. {
  76. var oJSON = document.getElementById(sJsonFieldId);
  77. $.get('ajax.render.php?class=' + sClass + '&json_obj=' + oJSON.value + '&att_code=' + sAttCode,
  78. { operation: "allowed_values" },
  79. function(data){
  80. //$('#field_'+iFieldId).html(data);
  81. }
  82. );
  83. }
  84. function AjaxGetDefaultValue(oObj, sClass, sAttCode, iFieldId)
  85. {
  86. // Asynchronously call the server to provide a default value if the field is
  87. // empty
  88. if (oObj['m_aCurrValues'][sAttCode] == '')
  89. {
  90. var oJSON = document.getElementById(sJsonFieldId);
  91. $.get('ajax.render.php?class=' + sClass + '&json_obj=' + oJSON.value + '&att_code=' + sAttCode,
  92. { operation: "default_value" },
  93. function(json_data){
  94. var oObj = ReloadObjectFromServer(json_data);
  95. UpdateFieldFromObject(iFieldId, aFieldsMap, oObj)
  96. }
  97. );
  98. }
  99. }
  100. function CheckMandatoryFields(sFormId)
  101. {
  102. $('#'+sFormId+' :submit').attr('disable', 'disabled');
  103. $('#'+sFormId+' :button[type=submit]').attr('disable', 'disabled');
  104. firstErrorId = '';
  105. var iErrorsCount = 0;
  106. $('#'+sFormId+' :input.mandatory').each( function() {
  107. if (( this.value == '') || (this.value == 0))
  108. {
  109. this.style.background = '#fcc';
  110. iErrorsCount++;
  111. if (iErrorsCount == 1)
  112. {
  113. firstErrorId = this.id;
  114. }
  115. }
  116. else
  117. {
  118. this.style.background = '#fff';
  119. }
  120. }
  121. );
  122. if(iErrorsCount > 0)
  123. {
  124. alert('Please fill-in all mandatory fields before continuing.');
  125. $('#'+sFormId+' :submit').attr('disable', '');
  126. $('#'+sFormId+' :button[type=submit]').attr('disable', '');
  127. if (firstErrorId != '')
  128. {
  129. $('#'+firstErrorId).focus();
  130. }
  131. }
  132. return(iErrorsCount == 0);
  133. }