forms-json-utils.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. if ($('#'+sFieldId).attr('disabled'))
  164. {
  165. bValid = true; // disabled fields are not checked
  166. }
  167. else
  168. {
  169. var currentVal = $('#'+sFieldId).val();
  170. if (currentVal == '$$NULL$$') // Convention to indicate a non-valid value since it may have to be passed as text
  171. {
  172. bValid = false;
  173. }
  174. else if (bMandatory && (currentVal == nullValue))
  175. {
  176. bValid = false;
  177. }
  178. else if (currentVal == nullValue)
  179. {
  180. // An empty field is Ok...
  181. bValid = true;
  182. }
  183. else if (sPattern != '')
  184. {
  185. re = new RegExp(sPattern);
  186. //console.log('Validating field: '+sFieldId + ' current value: '+currentVal + ' pattern: '+sPattern );
  187. bValid = re.test(currentVal);
  188. }
  189. }
  190. ReportFieldValidationStatus(sFieldId, sFormId, bValid);
  191. //console.log('Form: '+sFormId+' Validating field: '+sFieldId + ' current value: '+currentVal+' pattern: '+sPattern+' result: '+bValid );
  192. return true; // Do not stop propagation ??
  193. }
  194. function ValidateCKEditField(sFieldId, sPattern, bMandatory, sFormId, nullValue)
  195. {
  196. var bValid;
  197. var sTextContent;
  198. // Get the contents without the tags
  199. var oFormattedContents = $("#cke_"+sFieldId+" iframe");
  200. if (oFormattedContents.length == 0)
  201. {
  202. var oSourceContents = $("#cke_"+sFieldId+" textarea.cke_source");
  203. sTextContent = oSourceContents.val();
  204. }
  205. else
  206. {
  207. sTextContent = oFormattedContents.contents().find("body").text();
  208. }
  209. if (bMandatory && (sTextContent == ''))
  210. {
  211. bValid = false;
  212. }
  213. else
  214. {
  215. bValid = true;
  216. }
  217. ReportFieldValidationStatus(sFieldId, sFormId, bValid);
  218. setTimeout(function(){ValidateCKEditField(sFieldId, sPattern, bMandatory, sFormId, nullValue);}, 500);
  219. }
  220. /*
  221. function UpdateDependentFields(aFieldNames)
  222. {
  223. //console.log('UpdateDependentFields:');
  224. //console.log(aFieldNames);
  225. index = 0;
  226. oWizardHelper.ResetQuery();
  227. oWizardHelper.UpdateWizard();
  228. while(index < aFieldNames.length )
  229. {
  230. sAttCode = aFieldNames[index];
  231. sFieldId = oWizardHelper.GetFieldId(sAttCode);
  232. $('#v_'+sFieldId).html('<img src="../images/indicator.gif" />');
  233. oWizardHelper.RequestAllowedValues(sAttCode);
  234. index++;
  235. }
  236. oWizardHelper.AjaxQueryServer();
  237. }
  238. */
  239. function ResetPwd(id)
  240. {
  241. // Reset the values of the password fields
  242. $('#'+id).val('*****');
  243. $('#'+id+'_confirm').val('*****');
  244. // And reset the flag, to tell it that the password remains unchanged
  245. $('#'+id+'_changed').val(0);
  246. // Visual feedback, None when it's Ok
  247. $('#v_'+id).html('');
  248. }
  249. // Called whenever the content of a one way encrypted password changes
  250. function PasswordFieldChanged(id)
  251. {
  252. // Set the flag, to tell that the password changed
  253. $('#'+id+'_changed').val(1);
  254. }
  255. // Special validation function for one way encrypted password fields
  256. function ValidatePasswordField(id, sFormId)
  257. {
  258. var bChanged = $('#'+id+'_changed').val();
  259. if (bChanged)
  260. {
  261. if ($('#'+id).val() != $('#'+id+'_confirm').val())
  262. {
  263. oFormErrors['err_'+sFormId]++;
  264. if (oFormErrors['input_'+sFormId] == null)
  265. {
  266. // Let's remember the first input with an error, so that we can put back the focus on it later
  267. oFormErrors['input_'+sFormId] = id;
  268. }
  269. // Visual feedback
  270. $('#v_'+id).html('<img src="../images/validation_error.png" style="vertical-align:middle"/>');
  271. return false;
  272. }
  273. }
  274. $('#v_'+id).html(''); //<img src="../images/validation_ok.png" />');
  275. return true;
  276. }
  277. // Manage a 'duration' field
  278. function UpdateDuration(iId)
  279. {
  280. var iDays = parseInt($('#'+iId+'_d').val(), 10);
  281. var iHours = parseInt($('#'+iId+'_h').val(), 10);
  282. var iMinutes = parseInt($('#'+iId+'_m').val(), 10);
  283. var iSeconds = parseInt($('#'+iId+'_s').val(), 10);
  284. var iDuration = (((iDays*24)+ iHours)*60+ iMinutes)*60 + iSeconds;
  285. $('#'+iId).val(iDuration);
  286. $('#'+iId).trigger('change');
  287. return true;
  288. }
  289. // Called when filling an autocomplete field
  290. function OnAutoComplete(id, event, data, formatted)
  291. {
  292. if (data)
  293. {
  294. // A valid match was found: data[0] => label, data[1] => value
  295. $('#'+id).val(data[1]);
  296. $('#'+id).trigger('change');
  297. }
  298. else
  299. {
  300. if ($('#label_'+id).val() == '')
  301. {
  302. $('#'+id).val(''); // Empty value
  303. }
  304. else
  305. {
  306. $('#'+id).val('$$NULL$$'); // Convention: not a valid value
  307. }
  308. $('#'+id).trigger('change');
  309. }
  310. }