wizardhelper.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Wizard Helper JavaScript class to communicate with the WizardHelper PHP class
  2. function WizardHelper(sClass)
  3. {
  4. this.m_oData = { 'm_sClass' : '',
  5. 'm_oFieldsMap': {},
  6. 'm_aCurrentValues': [],
  7. 'm_aDefaultValueRequested': [],
  8. 'm_aAllowedValuesRequested': [],
  9. 'm_aDefaultValue': [],
  10. 'm_aAllowedValues': [],
  11. };
  12. this.m_oData.m_sClass = sClass;
  13. // Methods
  14. this.SetFieldsMap = function (oFieldsMap)
  15. {
  16. this.m_oData.m_oFieldsMap = oFieldsMap;
  17. }
  18. this.RequestDefaultValue = function (sFieldName)
  19. {
  20. currentValue = this.UpdateCurrentValue(sFieldName);
  21. sFieldId = this.m_oData.m_oFieldsMap[sFieldName];
  22. if (currentValue == null)
  23. {
  24. this.m_oData.m_aDefaultValueRequested.push(sFieldName);
  25. }
  26. }
  27. this.RequestAllowedValues = function (sFieldName)
  28. {
  29. this.m_oData.m_aAllowedValuesRequested.push(sFieldName);
  30. }
  31. this.SetCurrentValue = function (sFieldName, currentValue)
  32. {
  33. this.m_oData.m_aCurrentValues[this.m_oData.m_oFieldsMap[sFieldName]] = currentValue;
  34. }
  35. this.ToJSON = function ()
  36. {
  37. return JSON.stringify(this.m_oData);
  38. }
  39. this.FromJSON = function (sJSON)
  40. {
  41. //console.log('Parsing JSON:'+sJSON);
  42. this.m_oData = JSON.parse(sJSON);
  43. }
  44. this.ResetQuery = function ()
  45. {
  46. this.m_oData.m_aDefaultValueRequested = [];
  47. this.m_oData.m_aAllowedValuesRequested = [];
  48. }
  49. this.UpdateFields = function ()
  50. {
  51. //console.log('** UpdateFields **')
  52. for(i=0; i< this.m_oData.m_aDefaultValueRequested.length; i++)
  53. {
  54. sAttCode = this.m_oData.m_aDefaultValueRequested[i];
  55. sFieldId = this.m_oData.m_oFieldsMap[sAttCode];
  56. defaultValue = this.m_oData.m_aDefaultValue[sFieldId];
  57. //console.log('Setting field:'+sFieldId+' ('+sAttCode+') to: '+defaultValue);
  58. var oElement = document.getElementById('att_'+sFieldId);
  59. oElement.value = defaultValue;
  60. }
  61. }
  62. this.AjaxQueryServer = function ()
  63. {
  64. //console.log('data sent:', this.ToJSON());
  65. //console.log('oWizard:', this);
  66. $.get('ajax.render.php?json_obj=' + this.ToJSON(),
  67. { operation: 'wizard_helper' },
  68. function(json_data){
  69. //console.log('data received:', json_data);
  70. oWizardHelper.FromJSON(json_data);
  71. oWizardHelper.UpdateFields();
  72. //console.log(oWizardHelper);
  73. $('#wizStep'+ G_iCurrentStep).unblock( {fadeOut: 0} );
  74. });
  75. }
  76. this.Preview = function (divId)
  77. {
  78. //console.log('data sent:', this.ToJSON());
  79. //console.log('oWizard:', this);
  80. $('#'+divId).load('ajax.render.php?operation=wizard_helper_preview',
  81. {'json_obj': this.ToJSON()},
  82. function(responseText, textStatus, XMLHttpRequest){
  83. $('#wizStep'+ G_iCurrentStep).unblock( {fadeOut: 0} );
  84. });
  85. }
  86. this.UpdateCurrentValue = function (sFieldName)
  87. {
  88. sFieldId = this.m_oData.m_oFieldsMap[sFieldName];
  89. var oElement = document.getElementById('att_'+sFieldId);
  90. value = oElement.value;
  91. if (value == '')
  92. {
  93. value = null;
  94. }
  95. this.m_oData.m_aCurrentValues[sFieldId] = value;
  96. return value;
  97. }
  98. }