wizardhelper.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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, sState)
  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. 'm_sState': sState
  29. };
  30. this.m_oData.m_sClass = sClass;
  31. // Methods
  32. this.SetFieldsMap = function (oFieldsMap)
  33. {
  34. this.m_oData.m_oFieldsMap = oFieldsMap;
  35. }
  36. this.SetFieldsCount = function (count)
  37. {
  38. this.m_oData.m_iFieldsCount = count;
  39. }
  40. this.GetFieldId = function(sFieldName)
  41. {
  42. id = this.m_oData.m_oFieldsMap[sFieldName];
  43. return id;
  44. }
  45. this.RequestDefaultValue = function (sFieldName)
  46. {
  47. currentValue = this.UpdateCurrentValue(sFieldName);
  48. if (currentValue == null)
  49. {
  50. this.m_oData.m_aDefaultValueRequested.push(sFieldName);
  51. }
  52. }
  53. this.RequestAllowedValues = function (sFieldName)
  54. {
  55. this.m_oData.m_aAllowedValuesRequested.push(sFieldName);
  56. }
  57. this.SetCurrentValue = function (sFieldName, currentValue)
  58. {
  59. this.m_oData.m_oCurrentValues[sFieldName] = currentValue;
  60. }
  61. this.ToJSON = function ()
  62. {
  63. return JSON.stringify(this.m_oData);
  64. }
  65. this.FromJSON = function (sJSON)
  66. {
  67. //console.log('Parsing JSON:'+sJSON);
  68. this.m_oData = JSON.parse(sJSON);
  69. }
  70. this.ResetQuery = function ()
  71. {
  72. this.m_oData.m_aDefaultValueRequested = [];
  73. this.m_oData.m_oDefaultValue = {};
  74. this.m_oData.m_aAllowedValuesRequested = [];
  75. this.m_oData.m_oAllowedValues = {};
  76. }
  77. this.UpdateFields = function ()
  78. {
  79. var aRefreshed = [];
  80. //console.log('** UpdateFields **');
  81. // Set the full HTML for the input field
  82. for(i=0; i<this.m_oData.m_aAllowedValuesRequested.length; i++)
  83. {
  84. var sAttCode = this.m_oData.m_aAllowedValuesRequested[i];
  85. var sFieldId = this.m_oData.m_oFieldsMap[sAttCode];
  86. var bDisabled = $('#'+sFieldId).attr('disabled');
  87. //console.log('Setting #field_'+sFieldId+' to: '+this.m_oData.m_oAllowedValues[sAttCode]);
  88. $('#field_'+sFieldId).html(this.m_oData.m_oAllowedValues[sAttCode]);
  89. if (bDisabled)
  90. {
  91. $('#'+sFieldId).attr('disabled', 'disabled');
  92. //$('#'+sFieldId).trigger('update'); // Propagate the disable
  93. }
  94. aRefreshed.push(sFieldId);
  95. }
  96. // Set the actual value of the input
  97. for(i=0; i<this.m_oData.m_aDefaultValueRequested.length; i++)
  98. {
  99. sAttCode = this.m_oData.m_aDefaultValueRequested[i];
  100. defaultValue = this.m_oData.m_oDefaultValue[sAttCode];
  101. sFieldId = this.m_oData.m_oFieldsMap[sAttCode];
  102. $('#'+sFieldId).val(defaultValue);
  103. if (!aRefreshed.indexOf(sFieldId))
  104. {
  105. aRefreshed.push(sFieldId);
  106. }
  107. }
  108. // For each "refreshed" field, asynchronously trigger a change in case there are dependent fields to update
  109. for(i=0; i<aRefreshed.length; i++)
  110. {
  111. var sString = "$('#"+aRefreshed[i]+"').trigger('change').trigger('update');"
  112. window.setTimeout(sString, 1); // Synchronous 'trigger' does nothing, call it asynchronously
  113. }
  114. }
  115. this.UpdateWizard = function ()
  116. {
  117. //console.log('** UpdateWizard **')
  118. for(sFieldCode in this.m_oData.m_oFieldsMap)
  119. {
  120. sCleanFieldCode = sFieldCode.replace('"', '');
  121. //console.log(sFieldCode);
  122. this.UpdateCurrentValue(sCleanFieldCode);
  123. }
  124. // Remove unnecessary stuff
  125. this.m_oData.m_oDefaultValue = {};
  126. this.m_oData.m_oAllowedValues = {};
  127. }
  128. this.UpdateWizardToJSON = function ()
  129. {
  130. this.UpdateWizard();
  131. return this.ToJSON()
  132. }
  133. this.AjaxQueryServer = function ()
  134. {
  135. //console.log('data sent:', this.ToJSON());
  136. //console.log('oWizard:', this);
  137. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php',
  138. { operation: 'wizard_helper', json_obj: this.ToJSON() },
  139. function(html){
  140. $('#ajax_content').html(html);
  141. //console.log('data received:', oWizardHelper);
  142. //oWizardHelper.FromJSON(json_data);
  143. //oWizardHelper.UpdateFields(); // Is done directly in the html provided by ajax.render.php
  144. //console.log(oWizardHelper);
  145. //$('#wizStep'+ G_iCurrentStep).unblock( {fadeOut: 0} );
  146. });
  147. }
  148. this.Preview = function (divId)
  149. {
  150. //console.log('data sent:', this.ToJSON());
  151. //console.log('oWizard:', this);
  152. $('#'+divId).load(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php?operation=wizard_helper_preview',
  153. {'json_obj': this.ToJSON()},
  154. function(responseText, textStatus, XMLHttpRequest){
  155. $('#wizStep'+ G_iCurrentStep).unblock( {fadeOut: 0} );
  156. });
  157. }
  158. this.UpdateCurrentValue = function (sFieldCode)
  159. {
  160. value = $('#'+this.m_oData.m_oFieldsMap[sFieldCode]).val();
  161. if (value == '')
  162. {
  163. value = null;
  164. }
  165. this.m_oData.m_oCurrentValues[sFieldCode] = value;
  166. return value;
  167. }
  168. this.UpdateDependentFields = function(aFieldNames)
  169. {
  170. index = 0;
  171. this.ResetQuery();
  172. this.UpdateWizard();
  173. while(index < aFieldNames.length )
  174. {
  175. sAttCode = aFieldNames[index];
  176. sFieldId = this.GetFieldId(sAttCode);
  177. $('#v_'+sFieldId).html('<img src="../images/indicator.gif" />');
  178. this.RequestAllowedValues(sAttCode);
  179. index++;
  180. }
  181. this.AjaxQueryServer();
  182. }
  183. this.ReloadObjectCreationForm = function(sFormId, sTargetState)
  184. {
  185. $('#'+sFormId).block();
  186. this.UpdateWizard();
  187. this.ResetQuery();
  188. var sTransactionId = $('input[name=transaction_id]').val();
  189. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php',
  190. { json_obj: this.ToJSON(), operation: 'obj_creation_form', target_state: sTargetState, transaction_id: sTransactionId },
  191. function(data)
  192. {
  193. // Delete any previous instances of CKEditor
  194. $('#'+sFormId).find('.htmlEditor').each(function() {
  195. var sId = $(this).attr('id');
  196. var editorInst = CKEDITOR.instances[sId];
  197. if (editorInst)
  198. {
  199. editorInst.destroy(true);
  200. }
  201. });
  202. $('#'+sFormId).html(data);
  203. onDelayedReady();
  204. $('#'+sFormId).unblock();
  205. }
  206. );
  207. }
  208. }