wizardhelper.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_oCurrentValues': {},
  7. 'm_aDefaultValueRequested': [],
  8. 'm_aAllowedValuesRequested': [],
  9. 'm_oDefaultValue': {},
  10. 'm_oAllowedValues': {},
  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.GetFieldId = function(sFieldName)
  24. {
  25. id = this.m_oData.m_oFieldsMap[sFieldName];
  26. return id;
  27. }
  28. this.RequestDefaultValue = function (sFieldName)
  29. {
  30. currentValue = this.UpdateCurrentValue(sFieldName);
  31. if (currentValue == null)
  32. {
  33. this.m_oData.m_aDefaultValueRequested.push(sFieldName);
  34. }
  35. }
  36. this.RequestAllowedValues = function (sFieldName)
  37. {
  38. this.m_oData.m_aAllowedValuesRequested.push(sFieldName);
  39. }
  40. this.SetCurrentValue = function (sFieldName, currentValue)
  41. {
  42. this.m_oData.m_oCurrentValues[sFieldName] = currentValue;
  43. }
  44. this.ToJSON = function ()
  45. {
  46. return JSON.stringify(this.m_oData);
  47. }
  48. this.FromJSON = function (sJSON)
  49. {
  50. //console.log('Parsing JSON:'+sJSON);
  51. this.m_oData = JSON.parse(sJSON);
  52. }
  53. this.ResetQuery = function ()
  54. {
  55. this.m_oData.m_aDefaultValueRequested = [];
  56. this.m_oData.m_oDefaultValue = {};
  57. this.m_oData.m_aAllowedValuesRequested = [];
  58. this.m_oData.m_oAllowedValues = {};
  59. }
  60. this.UpdateFields = function ()
  61. {
  62. //console.log('** UpdateFields **');
  63. // Set the full HTML for the input field
  64. for(i=0; i<this.m_oData.m_aAllowedValuesRequested.length; i++)
  65. {
  66. sAttCode = this.m_oData.m_aAllowedValuesRequested[i];
  67. sFieldId = this.m_oData.m_oFieldsMap[sAttCode];
  68. //console.log('Setting #field_'+sFieldId+' to: '+this.m_oData.m_oAllowedValues[sAttCode]);
  69. $('#field_'+sFieldId).html(this.m_oData.m_oAllowedValues[sAttCode]);
  70. }
  71. // Set the actual value of the input
  72. for(i=0; i<this.m_oData.m_aDefaultValueRequested.length; i++)
  73. {
  74. sAttCode = this.m_oData.m_aDefaultValueRequested[i];
  75. defaultValue = this.m_oData.m_oDefaultValue[sAttCode];
  76. sFieldId = this.m_oData.m_oFieldsMap[sAttCode];
  77. $('#'+sFieldId).val(defaultValue);
  78. }
  79. }
  80. this.UpdateWizard = function ()
  81. {
  82. //console.log('** UpdateWizard **')
  83. for(sFieldCode in this.m_oData.m_oFieldsMap)
  84. {
  85. sCleanFieldCode = sFieldCode.replace('"', '');
  86. //console.log(sFieldCode);
  87. this.UpdateCurrentValue(sCleanFieldCode);
  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(); // Is done directly in the html provided by ajax.render.php
  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 (sFieldCode)
  116. {
  117. value = $('#'+this.m_oData.m_oFieldsMap[sFieldCode]).val();
  118. if (value == '')
  119. {
  120. value = null;
  121. }
  122. this.m_oData.m_oCurrentValues[sFieldCode] = value;
  123. return value;
  124. }
  125. }