forms-json-utils.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. // ID of the (hidden) form field used to store the JSON representation of the
  2. // object being edited in this page
  3. var sJsonFieldId = 'json_object';
  4. // The memory representation of the object
  5. var oObj = {};
  6. // Mapping between the fields of the form and the attribute of the current object
  7. // If aFieldsMap[2] contains 'foo' it means that oObj.foo corresponds to the field
  8. // of Id 'att_2' in the form
  9. var aFieldsMap = new Array;
  10. window.bInSubmit = false; // For handling form cancellation via OnBeforeUnload events
  11. // Update the whole object from the form and also update its
  12. // JSON (serialized) representation in the (hidden) field
  13. function UpdateObjectFromForm(aFieldsMap, oObj)
  14. {
  15. for(i=0; i<aFieldsMap.length; i++)
  16. {
  17. var oElement = document.getElementById('att_'+i);
  18. var sFieldName = aFieldsMap[i];
  19. oObj['m_aCurrValues'][sFieldName] = oElement.value;
  20. sJSON = JSON.stringify(oObj);
  21. var oJSON = document.getElementById(sJsonFieldId);
  22. oJSON.value = sJSON;
  23. }
  24. return oObj;
  25. }
  26. // Update the specified field from the current object
  27. function UpdateFieldFromObject(idField, aFieldsMap, oObj)
  28. {
  29. var oElement = document.getElementById('att_'+idField);
  30. oElement.value = oObj['m_aCurrValues'][aFieldsMap[idField]];
  31. }
  32. // Update all the fields of the Form from the current object
  33. function UpdateFormFromObject(aFieldsMap, oObj)
  34. {
  35. for(i=0; i<aFieldsMap.length; i++)
  36. {
  37. UpdateFieldFromForm(i, aFieldsMap, oObj);
  38. }
  39. }
  40. // This function is meant to be called from the AJAX page
  41. // It reloads the object (oObj) from the JSON representation
  42. // and also updates the form field that contains the JSON
  43. // representation of the object
  44. function ReloadObjectFromServer(sJSON)
  45. {
  46. //console.log('JSON value:', sJSON);
  47. var oJSON = document.getElementById(sJsonFieldId);
  48. oJSON.value = sJSON;
  49. oObj = JSON.parse( '(' + sJSON + ')' );
  50. return oObj;
  51. }
  52. function GoToStep(iCurrentStep, iNextStep)
  53. {
  54. var oCurrentStep = document.getElementById('wizStep'+iCurrentStep);
  55. if (iNextStep > iCurrentStep)
  56. {
  57. // Check the values when moving forward
  58. if (CheckFields('wizStep'+iCurrentStep, true))
  59. {
  60. oCurrentStep.style.display = 'none';
  61. ActivateStep(iNextStep);
  62. }
  63. }
  64. else
  65. {
  66. oCurrentStep.style.display = 'none';
  67. ActivateStep(iNextStep);
  68. }
  69. }
  70. function ActivateStep(iTargetStep)
  71. {
  72. UpdateObjectFromForm(aFieldsMap, oObj);
  73. var oNextStep = document.getElementById('wizStep'+(iTargetStep));
  74. window.location.href='#step'+iTargetStep;
  75. // If a handler for entering this step exists, call it
  76. if (typeof(this['OnEnterStep'+iTargetStep]) == 'function')
  77. {
  78. eval( 'OnEnterStep'+iTargetStep+'();');
  79. }
  80. oNextStep.style.display = '';
  81. G_iCurrentStep = iTargetStep;
  82. //$('#wizStep'+(iTargetStep)).block({ message: null });
  83. }
  84. function OnUnload(sTransactionId)
  85. {
  86. if (!window.bInSubmit)
  87. {
  88. // If it's not a submit, then it's a "cancel" (Pressing the Cancel button, closing the window, using the back button...)
  89. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', {operation: 'on_form_cancel', transaction_id: sTransactionId }, function()
  90. {
  91. // Do nothing for now...
  92. });
  93. }
  94. }
  95. function OnSubmit(sFormId)
  96. {
  97. window.bInSubmit=true; // This is a submit, make sure that when the page gets unloaded we don't cancel the action
  98. var bResult = CheckFields(sFormId, true);
  99. if (!bResult)
  100. {
  101. window.bInSubmit = false; // Submit is/will be canceled
  102. }
  103. return bResult;
  104. }
  105. // Store the result of the form validation... there may be several forms per page, beware
  106. var oFormErrors = { err_form0: 0 };
  107. function CheckFields(sFormId, bDisplayAlert)
  108. {
  109. $('#'+sFormId+' :submit').attr('disable', 'disabled');
  110. $('#'+sFormId+' :button[type=submit]').attr('disable', 'disabled');
  111. firstErrorId = '';
  112. // The two 'fields' below will be updated when the 'validate' event is processed
  113. oFormErrors['err_'+sFormId] = 0; // Number of errors encountered when validating the form
  114. oFormErrors['input_'+sFormId] = null; // First 'input' with an error, to set the focus to it
  115. $('#'+sFormId+' :input').each( function()
  116. {
  117. validateEventResult = $(this).trigger('validate', sFormId);
  118. }
  119. );
  120. if(oFormErrors['err_'+sFormId] > 0)
  121. {
  122. if (bDisplayAlert)
  123. {
  124. alert('Please fill-in all mandatory fields before continuing.');
  125. }
  126. $('#'+sFormId+' :submit').attr('disable', '');
  127. $('#'+sFormId+' :button[type=submit]').attr('disable', '');
  128. if (oFormErrors['input_'+sFormId] != null)
  129. {
  130. $('#'+oFormErrors['input_'+sFormId]).focus();
  131. }
  132. }
  133. return (oFormErrors['err_'+sFormId] == 0); // If no error, submit the form
  134. }
  135. function ReportFieldValidationStatus(sFieldId, sFormId, bValid)
  136. {
  137. if (bValid)
  138. {
  139. // Visual feedback - none when it's Ok
  140. $('#v_'+sFieldId).html(''); //<img src="../images/validation_ok.png" />');
  141. }
  142. else
  143. {
  144. // Report the error...
  145. oFormErrors['err_'+sFormId]++;
  146. if (oFormErrors['input_'+sFormId] == null)
  147. {
  148. // Let's remember the first input with an error, so that we can put back the focus on it later
  149. oFormErrors['input_'+sFormId] = sFieldId;
  150. }
  151. // Visual feedback
  152. $('#v_'+sFieldId).html('<img src="../images/validation_error.png" style="vertical-align:middle"/>');
  153. }
  154. }
  155. function ValidateField(sFieldId, sPattern, bMandatory, sFormId, nullValue)
  156. {
  157. var bValid = true;
  158. if ($('#'+sFieldId).attr('disabled'))
  159. {
  160. bValid = true; // disabled fields are not checked
  161. }
  162. else
  163. {
  164. var currentVal = $('#'+sFieldId).val();
  165. if (currentVal == '$$NULL$$') // Convention to indicate a non-valid value since it may have to be passed as text
  166. {
  167. bValid = false;
  168. }
  169. else if (bMandatory && (currentVal == nullValue))
  170. {
  171. bValid = false;
  172. }
  173. else if (currentVal == nullValue)
  174. {
  175. // An empty field is Ok...
  176. bValid = true;
  177. }
  178. else if (sPattern != '')
  179. {
  180. re = new RegExp(sPattern);
  181. //console.log('Validating field: '+sFieldId + ' current value: '+currentVal + ' pattern: '+sPattern );
  182. bValid = re.test(currentVal);
  183. }
  184. }
  185. ReportFieldValidationStatus(sFieldId, sFormId, bValid);
  186. //console.log('Form: '+sFormId+' Validating field: '+sFieldId + ' current value: '+currentVal+' pattern: '+sPattern+' result: '+bValid );
  187. return true; // Do not stop propagation ??
  188. }
  189. function ValidateCKEditField(sFieldId, sPattern, bMandatory, sFormId, nullValue)
  190. {
  191. var bValid;
  192. var sTextContent;
  193. // Get the contents without the tags
  194. var oFormattedContents = $("#cke_"+sFieldId+" iframe");
  195. if (oFormattedContents.length == 0)
  196. {
  197. var oSourceContents = $("#cke_"+sFieldId+" textarea.cke_source");
  198. sTextContent = oSourceContents.val();
  199. }
  200. else
  201. {
  202. sTextContent = oFormattedContents.contents().find("body").text();
  203. }
  204. if (bMandatory && (sTextContent == ''))
  205. {
  206. bValid = false;
  207. }
  208. else
  209. {
  210. bValid = true;
  211. }
  212. ReportFieldValidationStatus(sFieldId, sFormId, bValid);
  213. setTimeout(function(){ValidateCKEditField(sFieldId, sPattern, bMandatory, sFormId, nullValue);}, 500);
  214. }
  215. /*
  216. function UpdateDependentFields(aFieldNames)
  217. {
  218. //console.log('UpdateDependentFields:');
  219. //console.log(aFieldNames);
  220. index = 0;
  221. oWizardHelper.ResetQuery();
  222. oWizardHelper.UpdateWizard();
  223. while(index < aFieldNames.length )
  224. {
  225. sAttCode = aFieldNames[index];
  226. sFieldId = oWizardHelper.GetFieldId(sAttCode);
  227. $('#v_'+sFieldId).html('<img src="../images/indicator.gif" />');
  228. oWizardHelper.RequestAllowedValues(sAttCode);
  229. index++;
  230. }
  231. oWizardHelper.AjaxQueryServer();
  232. }
  233. */
  234. function ResetPwd(id)
  235. {
  236. // Reset the values of the password fields
  237. $('#'+id).val('*****');
  238. $('#'+id+'_confirm').val('*****');
  239. // And reset the flag, to tell it that the password remains unchanged
  240. $('#'+id+'_changed').val(0);
  241. // Visual feedback, None when it's Ok
  242. $('#v_'+id).html('');
  243. }
  244. // Called whenever the content of a one way encrypted password changes
  245. function PasswordFieldChanged(id)
  246. {
  247. // Set the flag, to tell that the password changed
  248. $('#'+id+'_changed').val(1);
  249. }
  250. // Special validation function for one way encrypted password fields
  251. function ValidatePasswordField(id, sFormId)
  252. {
  253. var bChanged = $('#'+id+'_changed').val();
  254. if (bChanged)
  255. {
  256. if ($('#'+id).val() != $('#'+id+'_confirm').val())
  257. {
  258. oFormErrors['err_'+sFormId]++;
  259. if (oFormErrors['input_'+sFormId] == null)
  260. {
  261. // Let's remember the first input with an error, so that we can put back the focus on it later
  262. oFormErrors['input_'+sFormId] = id;
  263. }
  264. // Visual feedback
  265. $('#v_'+id).html('<img src="../images/validation_error.png" style="vertical-align:middle"/>');
  266. return false;
  267. }
  268. }
  269. $('#v_'+id).html(''); //<img src="../images/validation_ok.png" />');
  270. return true;
  271. }
  272. //Special validation function for case log fields, taking into account the history
  273. // to determine if the field is empty or not
  274. function ValidateCaseLogField(sFieldId, bMandatory, sFormId)
  275. {
  276. bValid = true;
  277. if ($('#'+sFieldId).attr('disabled'))
  278. {
  279. bValid = true; // disabled fields are not checked
  280. }
  281. else if (!bMandatory)
  282. {
  283. bValid = true;
  284. }
  285. else
  286. {
  287. if (bMandatory)
  288. {
  289. var count = $('#'+sFieldId+'_count').val();
  290. if ( (count == 0) && ($('#'+sFieldId).val() == '') )
  291. {
  292. // No previous entry and no content typed
  293. bValid = false;
  294. }
  295. }
  296. }
  297. ReportFieldValidationStatus(sFieldId, sFormId, bValid);
  298. return bValid;
  299. }
  300. // Manage a 'duration' field
  301. function UpdateDuration(iId)
  302. {
  303. var iDays = parseInt($('#'+iId+'_d').val(), 10);
  304. var iHours = parseInt($('#'+iId+'_h').val(), 10);
  305. var iMinutes = parseInt($('#'+iId+'_m').val(), 10);
  306. var iSeconds = parseInt($('#'+iId+'_s').val(), 10);
  307. var iDuration = (((iDays*24)+ iHours)*60+ iMinutes)*60 + iSeconds;
  308. $('#'+iId).val(iDuration);
  309. $('#'+iId).trigger('change');
  310. return true;
  311. }
  312. // Called when filling an autocomplete field
  313. function OnAutoComplete(id, event, data, formatted)
  314. {
  315. if (data)
  316. {
  317. // A valid match was found: data[0] => label, data[1] => value
  318. if (data[1] != $('#'+id).val())
  319. {
  320. $('#'+id).val(data[1]);
  321. $('#'+id).trigger('change');
  322. $('#'+id).trigger('extkeychange');
  323. }
  324. }
  325. else
  326. {
  327. if ($('#label_'+id).val() == '')
  328. {
  329. $('#'+id).val(''); // Empty value
  330. }
  331. else
  332. {
  333. $('#'+id).val('$$NULL$$'); // Convention: not a valid value
  334. }
  335. $('#'+id).trigger('change');
  336. }
  337. }