wizardhelper.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // Copyright (C) 2010-2016 Combodo SARL
  2. //
  3. // This file is part of iTop.
  4. //
  5. // iTop is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // iTop is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  17. // Wizard Helper JavaScript class to communicate with the WizardHelper PHP class
  18. if (!Array.prototype.indexOf) // Emulation of the indexOf function for IE and old browsers
  19. {
  20. Array.prototype.indexOf = function(elt /*, from*/)
  21. {
  22. var len = this.length;
  23. var from = Number(arguments[1]) || 0;
  24. from = (from < 0) ? Math.ceil(from) : Math.floor(from);
  25. if (from < 0) from += len;
  26. for (; from < len; from++)
  27. {
  28. if (from in this && this[from] === elt) return from;
  29. }
  30. return -1;
  31. };
  32. }
  33. function WizardHelper(sClass, sFormPrefix, sState, sInitialState, sStimulus)
  34. {
  35. this.m_oData = { 'm_sClass' : '',
  36. 'm_oFieldsMap': {},
  37. 'm_oCurrentValues': {},
  38. 'm_aDefaultValueRequested': [],
  39. 'm_aAllowedValuesRequested': [],
  40. 'm_oDefaultValue': {},
  41. 'm_oAllowedValues': {},
  42. 'm_iFieldsCount' : 0,
  43. 'm_sFormPrefix' : sFormPrefix,
  44. 'm_sState': sState
  45. };
  46. this.m_oData.m_sClass = sClass;
  47. // Setting optional transition data
  48. if(sInitialState !== undefined)
  49. {
  50. this.m_oData.m_sInitialState = sInitialState;
  51. }
  52. if(sStimulus !== undefined)
  53. {
  54. this.m_oData.m_sStimulus = sStimulus;
  55. }
  56. // Methods
  57. this.SetFieldsMap = function (oFieldsMap)
  58. {
  59. this.m_oData.m_oFieldsMap = oFieldsMap;
  60. };
  61. this.SetFieldsCount = function (count)
  62. {
  63. this.m_oData.m_iFieldsCount = count;
  64. };
  65. this.GetFieldId = function(sFieldName)
  66. {
  67. id = this.m_oData.m_oFieldsMap[sFieldName];
  68. return id;
  69. };
  70. this.RequestDefaultValue = function (sFieldName)
  71. {
  72. currentValue = this.UpdateCurrentValue(sFieldName);
  73. if (currentValue == null)
  74. {
  75. this.m_oData.m_aDefaultValueRequested.push(sFieldName);
  76. }
  77. };
  78. this.RequestAllowedValues = function (sFieldName)
  79. {
  80. this.m_oData.m_aAllowedValuesRequested.push(sFieldName);
  81. };
  82. this.SetCurrentValue = function (sFieldName, currentValue)
  83. {
  84. this.m_oData.m_oCurrentValues[sFieldName] = currentValue;
  85. };
  86. this.ToJSON = function ()
  87. {
  88. return JSON.stringify(this.m_oData);
  89. };
  90. this.FromJSON = function (sJSON)
  91. {
  92. //console.log('Parsing JSON:'+sJSON);
  93. this.m_oData = JSON.parse(sJSON);
  94. };
  95. this.ResetQuery = function ()
  96. {
  97. this.m_oData.m_aDefaultValueRequested = [];
  98. this.m_oData.m_oDefaultValue = {};
  99. this.m_oData.m_aAllowedValuesRequested = [];
  100. this.m_oData.m_oAllowedValues = {};
  101. };
  102. this.UpdateFields = function ()
  103. {
  104. var aRefreshed = [];
  105. //console.log('** UpdateFields **');
  106. // Set the full HTML for the input field
  107. for(i=0; i<this.m_oData.m_aAllowedValuesRequested.length; i++)
  108. {
  109. var sAttCode = this.m_oData.m_aAllowedValuesRequested[i];
  110. var sFieldId = this.m_oData.m_oFieldsMap[sAttCode];
  111. var bDisabled = $('#'+sFieldId).attr('disabled');
  112. //console.log('Setting #field_'+sFieldId+' to: '+this.m_oData.m_oAllowedValues[sAttCode]);
  113. $('#field_'+sFieldId).html(this.m_oData.m_oAllowedValues[sAttCode]);
  114. if (bDisabled)
  115. {
  116. $('#'+sFieldId).attr('disabled', 'disabled');
  117. //$('#'+sFieldId).trigger('update'); // Propagate the disable
  118. }
  119. aRefreshed.push(sFieldId);
  120. }
  121. // Set the actual value of the input
  122. for(i=0; i<this.m_oData.m_aDefaultValueRequested.length; i++)
  123. {
  124. sAttCode = this.m_oData.m_aDefaultValueRequested[i];
  125. defaultValue = this.m_oData.m_oDefaultValue[sAttCode];
  126. sFieldId = this.m_oData.m_oFieldsMap[sAttCode];
  127. $('#'+sFieldId).val(defaultValue);
  128. if (!aRefreshed.indexOf(sFieldId))
  129. {
  130. aRefreshed.push(sFieldId);
  131. }
  132. }
  133. // For each "refreshed" field, asynchronously trigger a change in case there are dependent fields to update
  134. for(i=0; i<aRefreshed.length; i++)
  135. {
  136. var sString = "$('#"+aRefreshed[i]+"').trigger('change').trigger('update');";
  137. window.setTimeout(sString, 1); // Synchronous 'trigger' does nothing, call it asynchronously
  138. }
  139. };
  140. this.UpdateWizard = function ()
  141. {
  142. //console.log('** UpdateWizard **')
  143. for(sFieldCode in this.m_oData.m_oFieldsMap)
  144. {
  145. sCleanFieldCode = sFieldCode.replace('"', '');
  146. //console.log(sFieldCode);
  147. this.UpdateCurrentValue(sCleanFieldCode);
  148. }
  149. };
  150. this.UpdateWizardToJSON = function ()
  151. {
  152. this.UpdateWizard();
  153. return this.ToJSON();
  154. };
  155. this.AjaxQueryServer = function ()
  156. {
  157. //console.log('data sent:', this.ToJSON());
  158. //console.log('oWizard:', this);
  159. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php',
  160. { operation: 'wizard_helper', json_obj: this.ToJSON() },
  161. function(html){
  162. $('#ajax_content').html(html);
  163. $('.blockUI').parent().unblock();
  164. //console.log('data received:', oWizardHelper);
  165. //oWizardHelper.FromJSON(json_data);
  166. //oWizardHelper.UpdateFields(); // Is done directly in the html provided by ajax.render.php
  167. //console.log(oWizardHelper);
  168. //$('#wizStep'+ G_iCurrentStep).unblock( {fadeOut: 0} );
  169. });
  170. };
  171. this.Preview = function (divId)
  172. {
  173. //console.log('data sent:', this.ToJSON());
  174. //console.log('oWizard:', this);
  175. $('#'+divId).load(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php?operation=wizard_helper_preview',
  176. {'json_obj': this.ToJSON()},
  177. function(responseText, textStatus, XMLHttpRequest){
  178. $('#wizStep'+ G_iCurrentStep).unblock( {fadeOut: 0} );
  179. });
  180. };
  181. this.UpdateCurrentValue = function (sFieldCode)
  182. {
  183. $('#'+this.m_oData.m_oFieldsMap[sFieldCode]).trigger('update_value'); // Give the widget a chance to update its value (if it is aware of this event)
  184. value = $('#'+this.m_oData.m_oFieldsMap[sFieldCode]).val();
  185. if (value == '')
  186. {
  187. value = null;
  188. }
  189. this.m_oData.m_oCurrentValues[sFieldCode] = value;
  190. return value;
  191. };
  192. this.UpdateDependentFields = function(aFieldNames)
  193. {
  194. index = 0;
  195. this.ResetQuery();
  196. this.UpdateWizard();
  197. while(index < aFieldNames.length )
  198. {
  199. sAttCode = aFieldNames[index];
  200. sFieldId = this.GetFieldId(sAttCode);
  201. if (sFieldId !== undefined) {
  202. $('#fstatus_' + sFieldId).html('<img src="../images/indicator.gif" />');
  203. $('#field_' + sFieldId).find('div').block({
  204. message: '',
  205. overlayCSS: {backgroundColor: '#f1f1f1', opacity: 0.3}
  206. });
  207. this.RequestAllowedValues(sAttCode);
  208. }
  209. index++;
  210. }
  211. this.AjaxQueryServer();
  212. };
  213. this.ReloadObjectCreationForm = function(sFormId, sTargetState)
  214. {
  215. $('#'+sFormId).block();
  216. this.UpdateWizard();
  217. this.ResetQuery();
  218. var sTransactionId = $('input[name=transaction_id]').val();
  219. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php',
  220. { json_obj: this.ToJSON(), operation: 'obj_creation_form', target_state: sTargetState, transaction_id: sTransactionId },
  221. function(data)
  222. {
  223. // Delete any previous instances of CKEditor
  224. $('#'+sFormId).find('.htmlEditor').each(function() {
  225. var sId = $(this).attr('id');
  226. var editorInst = CKEDITOR.instances[sId];
  227. if (editorInst.status == 'ready')
  228. {
  229. editorInst.destroy(true);
  230. }
  231. });
  232. $('#'+sFormId).html(data);
  233. onDelayedReady();
  234. $('#'+sFormId).unblock();
  235. }
  236. );
  237. };
  238. }