wizardhelper.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Wizard Helper JavaScript class to communicate with the WizardHelper PHP class
  2. function WizardHelper(sClass, sFormPrefix)
  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. 'm_sFormPrefix' : sFormPrefix
  13. };
  14. this.m_oData.m_sClass = sClass;
  15. // Methods
  16. this.SetFieldsMap = function (oFieldsMap)
  17. {
  18. this.m_oData.m_oFieldsMap = oFieldsMap;
  19. }
  20. this.SetFieldsCount = function (count)
  21. {
  22. this.m_oData.m_iFieldsCount = count;
  23. }
  24. this.GetFieldId = function(sFieldName)
  25. {
  26. id = this.m_oData.m_oFieldsMap[sFieldName];
  27. return id;
  28. }
  29. this.RequestDefaultValue = function (sFieldName)
  30. {
  31. currentValue = this.UpdateCurrentValue(sFieldName);
  32. if (currentValue == null)
  33. {
  34. this.m_oData.m_aDefaultValueRequested.push(sFieldName);
  35. }
  36. }
  37. this.RequestAllowedValues = function (sFieldName)
  38. {
  39. this.m_oData.m_aAllowedValuesRequested.push(sFieldName);
  40. }
  41. this.SetCurrentValue = function (sFieldName, currentValue)
  42. {
  43. this.m_oData.m_oCurrentValues[sFieldName] = currentValue;
  44. }
  45. this.ToJSON = function ()
  46. {
  47. return JSON.stringify(this.m_oData);
  48. }
  49. this.FromJSON = function (sJSON)
  50. {
  51. //console.log('Parsing JSON:'+sJSON);
  52. this.m_oData = JSON.parse(sJSON);
  53. }
  54. this.ResetQuery = function ()
  55. {
  56. this.m_oData.m_aDefaultValueRequested = [];
  57. this.m_oData.m_oDefaultValue = {};
  58. this.m_oData.m_aAllowedValuesRequested = [];
  59. this.m_oData.m_oAllowedValues = {};
  60. }
  61. this.UpdateFields = function ()
  62. {
  63. //console.log('** UpdateFields **');
  64. // Set the full HTML for the input field
  65. for(i=0; i<this.m_oData.m_aAllowedValuesRequested.length; i++)
  66. {
  67. sAttCode = this.m_oData.m_aAllowedValuesRequested[i];
  68. sFieldId = this.m_oData.m_oFieldsMap[sAttCode];
  69. //console.log('Setting #field_'+sFieldId+' to: '+this.m_oData.m_oAllowedValues[sAttCode]);
  70. $('#field_'+sFieldId).html(this.m_oData.m_oAllowedValues[sAttCode]);
  71. }
  72. // Set the actual value of the input
  73. for(i=0; i<this.m_oData.m_aDefaultValueRequested.length; i++)
  74. {
  75. sAttCode = this.m_oData.m_aDefaultValueRequested[i];
  76. defaultValue = this.m_oData.m_oDefaultValue[sAttCode];
  77. sFieldId = this.m_oData.m_oFieldsMap[sAttCode];
  78. $('#'+sFieldId).val(defaultValue);
  79. }
  80. }
  81. this.UpdateWizard = function ()
  82. {
  83. //console.log('** UpdateWizard **')
  84. for(sFieldCode in this.m_oData.m_oFieldsMap)
  85. {
  86. sCleanFieldCode = sFieldCode.replace('"', '');
  87. //console.log(sFieldCode);
  88. this.UpdateCurrentValue(sCleanFieldCode);
  89. }
  90. }
  91. this.AjaxQueryServer = function ()
  92. {
  93. //console.log('data sent:', this.ToJSON());
  94. //console.log('oWizard:', this);
  95. $.post('ajax.render.php',
  96. { operation: 'wizard_helper', json_obj: this.ToJSON() },
  97. function(html){
  98. $('#ajax_content').html(html);
  99. //console.log('data received:', oWizardHelper);
  100. //oWizardHelper.FromJSON(json_data);
  101. //oWizardHelper.UpdateFields(); // Is done directly in the html provided by ajax.render.php
  102. //console.log(oWizardHelper);
  103. //$('#wizStep'+ G_iCurrentStep).unblock( {fadeOut: 0} );
  104. });
  105. }
  106. this.Preview = function (divId)
  107. {
  108. //console.log('data sent:', this.ToJSON());
  109. //console.log('oWizard:', this);
  110. $('#'+divId).load('ajax.render.php?operation=wizard_helper_preview',
  111. {'json_obj': this.ToJSON()},
  112. function(responseText, textStatus, XMLHttpRequest){
  113. $('#wizStep'+ G_iCurrentStep).unblock( {fadeOut: 0} );
  114. });
  115. }
  116. this.UpdateCurrentValue = function (sFieldCode)
  117. {
  118. value = $('#'+this.m_oData.m_oFieldsMap[sFieldCode]).val();
  119. if (value == '')
  120. {
  121. value = null;
  122. }
  123. this.m_oData.m_oCurrentValues[sFieldCode] = value;
  124. return value;
  125. }
  126. this.UpdateDependentFields = function(aFieldNames)
  127. {
  128. index = 0;
  129. this.ResetQuery();
  130. this.UpdateWizard();
  131. while(index < aFieldNames.length )
  132. {
  133. sAttCode = aFieldNames[index];
  134. sFieldId = this.GetFieldId(sAttCode);
  135. $('#v_'+sFieldId).html('<img src="../images/indicator.gif" />');
  136. this.RequestAllowedValues(sAttCode);
  137. index++;
  138. }
  139. this.AjaxQueryServer();
  140. }
  141. }