wizardhelper.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. 'm_iFieldsCount' : 0,
  12. };
  13. this.m_oData.m_sClass = sClass;
  14. // Methods
  15. this.SetFieldsMap = function (oFieldsMap)
  16. {
  17. this.m_oData.m_oFieldsMap = oFieldsMap;
  18. }
  19. this.SetFieldsCount = function (count)
  20. {
  21. this.m_oData.m_iFieldsCount = count;
  22. }
  23. this.RequestDefaultValue = function (sFieldName)
  24. {
  25. currentValue = this.UpdateCurrentValue(sFieldName);
  26. sFieldId = this.m_oData.m_oFieldsMap[sFieldName];
  27. if (currentValue == null)
  28. {
  29. this.m_oData.m_aDefaultValueRequested.push(sFieldName);
  30. }
  31. }
  32. this.RequestAllowedValues = function (sFieldName)
  33. {
  34. this.m_oData.m_aAllowedValuesRequested.push(sFieldName);
  35. }
  36. this.SetCurrentValue = function (sFieldName, currentValue)
  37. {
  38. this.m_oData.m_aCurrentValues[this.m_oData.m_oFieldsMap[sFieldName]] = currentValue;
  39. }
  40. this.ToJSON = function ()
  41. {
  42. return JSON.stringify(this.m_oData);
  43. }
  44. this.FromJSON = function (sJSON)
  45. {
  46. //console.log('Parsing JSON:'+sJSON);
  47. this.m_oData = JSON.parse(sJSON);
  48. }
  49. this.ResetQuery = function ()
  50. {
  51. this.m_oData.m_aDefaultValueRequested = [];
  52. this.m_oData.m_aDefaultValue = [];
  53. this.m_oData.m_aAllowedValuesRequested = [];
  54. this.m_oData.m_aAllowedValues = [];
  55. }
  56. this.UpdateFields = function ()
  57. {
  58. //console.log('** UpdateFields **');
  59. //console.log(this.m_oData);
  60. for(i=0; i< this.m_oData.m_aAllowedValuesRequested.length; i++)
  61. {
  62. sAttCode = this.m_oData.m_aAllowedValuesRequested[i];
  63. sFieldId = this.m_oData.m_oFieldsMap[sAttCode];
  64. $('#field_'+sFieldId).html(this.m_oData.m_aAllowedValues[sFieldId]);
  65. }
  66. for(i=0; i< this.m_oData.m_aDefaultValueRequested.length; i++)
  67. {
  68. sAttCode = this.m_oData.m_aDefaultValueRequested[i];
  69. sFieldId = this.m_oData.m_oFieldsMap[sAttCode];
  70. defaultValue = this.m_oData.m_aDefaultValue[sFieldId];
  71. //console.log('Setting field:'+sFieldId+' ('+sAttCode+') to: '+defaultValue);
  72. var oElement = document.getElementById('att_'+sFieldId);
  73. oElement.value = defaultValue;
  74. //console.log('att_'+sFieldId+', set to '+defaultValue);
  75. }
  76. }
  77. this.UpdateWizard = function ()
  78. {
  79. //console.log('** UpdateWizard **')
  80. for(i=0; i< this.m_oData.m_iFieldsCount; i++)
  81. {
  82. value = $('#att_'+i).val();
  83. if (value == '')
  84. {
  85. value = null;
  86. }
  87. this.m_oData.m_aCurrentValues[i] = value;
  88. }
  89. }
  90. this.AjaxQueryServer = function ()
  91. {
  92. //console.log('data sent:', this.ToJSON());
  93. //console.log('oWizard:', this);
  94. $.get('ajax.render.php?json_obj=' + this.ToJSON(),
  95. { operation: 'wizard_helper' },
  96. function(html){
  97. $('body').append(html);
  98. //console.log('data received:', oWizardHelper);
  99. //oWizardHelper.FromJSON(json_data);
  100. oWizardHelper.UpdateFields();
  101. //console.log(oWizardHelper);
  102. $('#wizStep'+ G_iCurrentStep).unblock( {fadeOut: 0} );
  103. });
  104. }
  105. this.Preview = function (divId)
  106. {
  107. //console.log('data sent:', this.ToJSON());
  108. //console.log('oWizard:', this);
  109. $('#'+divId).load('ajax.render.php?operation=wizard_helper_preview',
  110. {'json_obj': this.ToJSON()},
  111. function(responseText, textStatus, XMLHttpRequest){
  112. $('#wizStep'+ G_iCurrentStep).unblock( {fadeOut: 0} );
  113. });
  114. }
  115. this.UpdateCurrentValue = function (sFieldName)
  116. {
  117. sFieldId = this.m_oData.m_oFieldsMap[sFieldName];
  118. var oElement = document.getElementById('att_'+sFieldId);
  119. value = oElement.value;
  120. if (value == '')
  121. {
  122. value = null;
  123. }
  124. this.m_oData.m_aCurrentValues[sFieldId] = value;
  125. return value;
  126. }
  127. }