wizardhelper.js 7.4 KB

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