forms-json-utils.js 9.5 KB

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