forms-json-utils.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. oCurrentStep.style.display = 'none';
  55. ActivateStep(iNextStep);
  56. }
  57. function ActivateStep(iTargetStep)
  58. {
  59. UpdateObjectFromForm(aFieldsMap, oObj);
  60. var oNextStep = document.getElementById('wizStep'+(iTargetStep));
  61. window.location.href='#step'+iTargetStep;
  62. // If a handler for entering this step exists, call it
  63. if (typeof(this['OnEnterStep'+iTargetStep]) == 'function')
  64. {
  65. eval( 'OnEnterStep'+iTargetStep+'();');
  66. }
  67. oNextStep.style.display = '';
  68. G_iCurrentStep = iTargetStep;
  69. $('#wizStep'+(iTargetStep)).block({ message: null });
  70. }
  71. function AjaxGetValuesDef(oObj, sClass, sAttCode, iFieldId)
  72. {
  73. var oJSON = document.getElementById(sJsonFieldId);
  74. $.get('ajax.render.php?class=' + sClass + '&json_obj=' + oJSON.value + '&att_code=' + sAttCode,
  75. { operation: "allowed_values" },
  76. function(data){
  77. //$('#field_'+iFieldId).html(data);
  78. }
  79. );
  80. }
  81. function AjaxGetDefaultValue(oObj, sClass, sAttCode, iFieldId)
  82. {
  83. // Asynchronously call the server to provide a default value if the field is
  84. // empty
  85. if (oObj['m_aCurrValues'][sAttCode] == '')
  86. {
  87. var oJSON = document.getElementById(sJsonFieldId);
  88. $.get('ajax.render.php?class=' + sClass + '&json_obj=' + oJSON.value + '&att_code=' + sAttCode,
  89. { operation: "default_value" },
  90. function(json_data){
  91. var oObj = ReloadObjectFromServer(json_data);
  92. UpdateFieldFromObject(iFieldId, aFieldsMap, oObj)
  93. }
  94. );
  95. }
  96. }