wizardhelper.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Wizard Helper JavaScript class to communicate with the WizardHelper PHP class
  2. if (!Array.prototype.indexOf) // Emulation of the indexOf function for IE and old browsers
  3. {
  4. Array.prototype.indexOf = function(elt /*, from*/)
  5. {
  6. var len = this.length;
  7. var from = Number(arguments[1]) || 0;
  8. from = (from < 0) ? Math.ceil(from) : Math.floor(from);
  9. if (from < 0) from += len;
  10. for (; from < len; from++)
  11. {
  12. if (from in this && this[from] === elt) return from;
  13. }
  14. return -1;
  15. };
  16. }
  17. function WizardHelper(sClass, sFormPrefix)
  18. {
  19. this.m_oData = { 'm_sClass' : '',
  20. 'm_oFieldsMap': {},
  21. 'm_oCurrentValues': {},
  22. 'm_aDefaultValueRequested': [],
  23. 'm_aAllowedValuesRequested': [],
  24. 'm_oDefaultValue': {},
  25. 'm_oAllowedValues': {},
  26. 'm_iFieldsCount' : 0,
  27. 'm_sFormPrefix' : sFormPrefix
  28. };
  29. this.m_oData.m_sClass = sClass;
  30. // Methods
  31. this.SetFieldsMap = function (oFieldsMap)
  32. {
  33. this.m_oData.m_oFieldsMap = oFieldsMap;
  34. }
  35. this.SetFieldsCount = function (count)
  36. {
  37. this.m_oData.m_iFieldsCount = count;
  38. }
  39. this.GetFieldId = function(sFieldName)
  40. {
  41. id = this.m_oData.m_oFieldsMap[sFieldName];
  42. return id;
  43. }
  44. this.RequestDefaultValue = function (sFieldName)
  45. {
  46. currentValue = this.UpdateCurrentValue(sFieldName);
  47. if (currentValue == null)
  48. {
  49. this.m_oData.m_aDefaultValueRequested.push(sFieldName);
  50. }
  51. }
  52. this.RequestAllowedValues = function (sFieldName)
  53. {
  54. this.m_oData.m_aAllowedValuesRequested.push(sFieldName);
  55. }
  56. this.SetCurrentValue = function (sFieldName, currentValue)
  57. {
  58. this.m_oData.m_oCurrentValues[sFieldName] = currentValue;
  59. }
  60. this.ToJSON = function ()
  61. {
  62. return JSON.stringify(this.m_oData);
  63. }
  64. this.FromJSON = function (sJSON)
  65. {
  66. //console.log('Parsing JSON:'+sJSON);
  67. this.m_oData = JSON.parse(sJSON);
  68. }
  69. this.ResetQuery = function ()
  70. {
  71. this.m_oData.m_aDefaultValueRequested = [];
  72. this.m_oData.m_oDefaultValue = {};
  73. this.m_oData.m_aAllowedValuesRequested = [];
  74. this.m_oData.m_oAllowedValues = {};
  75. }
  76. this.UpdateFields = function ()
  77. {
  78. var aRefreshed = [];
  79. //console.log('** UpdateFields **');
  80. // Set the full HTML for the input field
  81. for(i=0; i<this.m_oData.m_aAllowedValuesRequested.length; i++)
  82. {
  83. sAttCode = this.m_oData.m_aAllowedValuesRequested[i];
  84. sFieldId = this.m_oData.m_oFieldsMap[sAttCode];
  85. //console.log('Setting #field_'+sFieldId+' to: '+this.m_oData.m_oAllowedValues[sAttCode]);
  86. $('#field_'+sFieldId).html(this.m_oData.m_oAllowedValues[sAttCode]);
  87. aRefreshed.push(sFieldId);
  88. }
  89. // Set the actual value of the input
  90. for(i=0; i<this.m_oData.m_aDefaultValueRequested.length; i++)
  91. {
  92. sAttCode = this.m_oData.m_aDefaultValueRequested[i];
  93. defaultValue = this.m_oData.m_oDefaultValue[sAttCode];
  94. sFieldId = this.m_oData.m_oFieldsMap[sAttCode];
  95. $('#'+sFieldId).val(defaultValue);
  96. if (!aRefreshed.indexOf(sFieldId))
  97. {
  98. aRefreshed.push(sFieldId);
  99. }
  100. }
  101. // For each "refreshed" field, asynchronously trigger a change in case there are dependent fields to update
  102. for(i=0; i<aRefreshed.length; i++)
  103. {
  104. var sString = "$('#"+aRefreshed[i]+"').trigger('change');"
  105. window.setTimeout(sString, 1); // Synchronous 'trigger' does nothing, call it asynchronously
  106. }
  107. }
  108. this.UpdateWizard = function ()
  109. {
  110. //console.log('** UpdateWizard **')
  111. for(sFieldCode in this.m_oData.m_oFieldsMap)
  112. {
  113. sCleanFieldCode = sFieldCode.replace('"', '');
  114. //console.log(sFieldCode);
  115. this.UpdateCurrentValue(sCleanFieldCode);
  116. }
  117. }
  118. this.AjaxQueryServer = function ()
  119. {
  120. //console.log('data sent:', this.ToJSON());
  121. //console.log('oWizard:', this);
  122. $.post('ajax.render.php',
  123. { operation: 'wizard_helper', json_obj: this.ToJSON() },
  124. function(html){
  125. $('#ajax_content').html(html);
  126. //console.log('data received:', oWizardHelper);
  127. //oWizardHelper.FromJSON(json_data);
  128. //oWizardHelper.UpdateFields(); // Is done directly in the html provided by ajax.render.php
  129. //console.log(oWizardHelper);
  130. //$('#wizStep'+ G_iCurrentStep).unblock( {fadeOut: 0} );
  131. });
  132. }
  133. this.Preview = function (divId)
  134. {
  135. //console.log('data sent:', this.ToJSON());
  136. //console.log('oWizard:', this);
  137. $('#'+divId).load('ajax.render.php?operation=wizard_helper_preview',
  138. {'json_obj': this.ToJSON()},
  139. function(responseText, textStatus, XMLHttpRequest){
  140. $('#wizStep'+ G_iCurrentStep).unblock( {fadeOut: 0} );
  141. });
  142. }
  143. this.UpdateCurrentValue = function (sFieldCode)
  144. {
  145. value = $('#'+this.m_oData.m_oFieldsMap[sFieldCode]).val();
  146. if (value == '')
  147. {
  148. value = null;
  149. }
  150. this.m_oData.m_oCurrentValues[sFieldCode] = value;
  151. return value;
  152. }
  153. this.UpdateDependentFields = function(aFieldNames)
  154. {
  155. index = 0;
  156. this.ResetQuery();
  157. this.UpdateWizard();
  158. while(index < aFieldNames.length )
  159. {
  160. sAttCode = aFieldNames[index];
  161. sFieldId = this.GetFieldId(sAttCode);
  162. $('#v_'+sFieldId).html('<img src="../images/indicator.gif" />');
  163. this.RequestAllowedValues(sAttCode);
  164. index++;
  165. }
  166. this.AjaxQueryServer();
  167. }
  168. }