wizardhelper.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.RequestDefaultValue = function (sFieldName)
  24. {
  25. currentValue = this.UpdateCurrentValue(sFieldName);
  26. if (currentValue == null)
  27. {
  28. this.m_oData.m_aDefaultValueRequested.push(sFieldName);
  29. }
  30. }
  31. this.RequestAllowedValues = function (sFieldName)
  32. {
  33. this.m_oData.m_aAllowedValuesRequested.push(sFieldName);
  34. }
  35. this.SetCurrentValue = function (sFieldName, currentValue)
  36. {
  37. this.m_oData.m_oCurrentValues[sFieldName] = currentValue;
  38. }
  39. this.ToJSON = function ()
  40. {
  41. return JSON.stringify(this.m_oData);
  42. }
  43. this.FromJSON = function (sJSON)
  44. {
  45. //console.log('Parsing JSON:'+sJSON);
  46. this.m_oData = JSON.parse(sJSON);
  47. }
  48. this.ResetQuery = function ()
  49. {
  50. this.m_oData.m_aDefaultValueRequested = [];
  51. this.m_oData.m_oDefaultValue = {};
  52. this.m_oData.m_aAllowedValuesRequested = [];
  53. this.m_oData.m_oAllowedValues = {};
  54. }
  55. this.UpdateFields = function ()
  56. {
  57. //console.log('** UpdateFields **');
  58. // Set the full HTML for the input field
  59. for(i=0; i<this.m_oData.m_aAllowedValuesRequested.length; i++)
  60. {
  61. sAttCode = this.m_oData.m_aAllowedValuesRequested[i];
  62. sFieldId = this.m_oData.m_oFieldsMap[sAttCode];
  63. //console.log('Setting #field_'+sFieldId+' to: '+this.m_oData.m_oAllowedValues[sAttCode]);
  64. $('#field_'+sFieldId).html(this.m_oData.m_oAllowedValues[sAttCode]);
  65. }
  66. // Set the actual value of the input
  67. for(i=0; i<this.m_oData.m_aDefaultValueRequested.length; i++)
  68. {
  69. sAttCode = this.m_oData.m_aDefaultValueRequested[i];
  70. defaultValue = this.m_oData.m_oDefaultValue[sAttCode];
  71. sFieldId = this.m_oData.m_oFieldsMap[sAttCode];
  72. $('#'+sFieldId).val(defaultValue);
  73. }
  74. }
  75. this.UpdateWizard = function ()
  76. {
  77. //console.log('** UpdateWizard **')
  78. for(sFieldCode in this.m_oData.m_oFieldsMap)
  79. {
  80. sCleanFieldCode = sFieldCode.replace('"', '');
  81. //console.log(sFieldCode);
  82. this.UpdateCurrentValue(sCleanFieldCode);
  83. }
  84. }
  85. this.AjaxQueryServer = function ()
  86. {
  87. //console.log('data sent:', this.ToJSON());
  88. //console.log('oWizard:', this);
  89. $.get('ajax.render.php?json_obj=' + this.ToJSON(),
  90. { operation: 'wizard_helper' },
  91. function(html){
  92. $('body').append(html);
  93. //console.log('data received:', oWizardHelper);
  94. //oWizardHelper.FromJSON(json_data);
  95. //oWizardHelper.UpdateFields(); // Is done directly in the html provided by ajax.render.php
  96. //console.log(oWizardHelper);
  97. //$('#wizStep'+ G_iCurrentStep).unblock( {fadeOut: 0} );
  98. });
  99. }
  100. this.Preview = function (divId)
  101. {
  102. //console.log('data sent:', this.ToJSON());
  103. //console.log('oWizard:', this);
  104. $('#'+divId).load('ajax.render.php?operation=wizard_helper_preview',
  105. {'json_obj': this.ToJSON()},
  106. function(responseText, textStatus, XMLHttpRequest){
  107. $('#wizStep'+ G_iCurrentStep).unblock( {fadeOut: 0} );
  108. });
  109. }
  110. this.UpdateCurrentValue = function (sFieldCode)
  111. {
  112. value = $('#'+this.m_oData.m_oFieldsMap[sFieldCode]).val();
  113. if (value == '')
  114. {
  115. value = null;
  116. }
  117. this.m_oData.m_oCurrentValues[sFieldCode] = value;
  118. return value;
  119. }
  120. }