You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

85 lines
3.6 KiB

  1. /**
  2. * Handle display of dependent, i. e. optional fieldsets
  3. *
  4. * Fieldsets may be defined as dependent on the value of a certain input. In
  5. * this case they contain a p element with the CSS class “bureaucracy_depends”.
  6. * This p element holds a span with the class “bureaucracy_depends_fname”
  7. * and optionally another span with “bureaucracy_depends_fvalue”. They
  8. * specify the target input (fname) and the target value for which the fieldset
  9. * is to be shown.
  10. *
  11. * This function adds onchange handlers to the relevant inputs for showing and
  12. * heading the respective fieldsets.
  13. *
  14. * @author Adrian Lang <dokuwiki@cosmocode.de>
  15. **/
  16. jQuery(function () {
  17. jQuery('form.bureaucracy__plugin').each(function () {
  18. //show/hide fieldset and trigger depending children
  19. function updateFieldset(input) {
  20. jQuery.each(jQuery(input).data('dparray'), function (i, dp) {
  21. var showOrHide =
  22. input.parentNode.parentNode.style.display !== 'none' && // input/checkbox is displayed AND
  23. ((input.checked === dp.tval) || // ( checkbox is checked
  24. (input.type !== 'checkbox' && (dp.tval === true && input.value !== '')) || // OR no checkbox, but input is set
  25. input.value === dp.tval); // OR input === dp.tval )
  26. dp.fset.toggle(showOrHide);
  27. dp.fset.find('input,select')
  28. .each(function () {
  29. //toggle required attribute
  30. var $inputelem = jQuery(this);
  31. if($inputelem.hasClass('required')) {
  32. if(showOrHide) {
  33. $inputelem.attr('required', 'required');
  34. } else {
  35. $inputelem.removeAttr('required')
  36. }
  37. }
  38. //update dependencies
  39. if ($inputelem.data('dparray')) {
  40. $inputelem.change();
  41. }
  42. });
  43. });
  44. }
  45. //look for p (with info about controller) in depending fieldsets
  46. jQuery('p.bureaucracy_depends', this)
  47. .each(function () {
  48. //get controller info
  49. var fname = jQuery(this).find('span.bureaucracy_depends_fname').html(),
  50. fvalue = jQuery(this).find('span.bureaucracy_depends_fvalue');
  51. fvalue = (fvalue.length ? fvalue.html() : true);
  52. //get controller field and add info and change event to the input that controls depending fieldset
  53. var fieldsetinfo = {
  54. fset: jQuery(this).parent(),
  55. tval: fvalue
  56. };
  57. jQuery("label")
  58. .has(":first-child:contains('" + fname + "')").first()
  59. .find("select,input:last") //yesno field contains first a hidden input
  60. .each(function () {
  61. if (!jQuery(this).data('dparray')) {
  62. jQuery(this).data('dparray', [fieldsetinfo]);
  63. } else {
  64. jQuery(this).data('dparray').push(fieldsetinfo);
  65. }
  66. })
  67. .bind('change keyup', function () {
  68. updateFieldset(this);
  69. })
  70. .change();
  71. })
  72. .hide(); //hide p.bureaucracy_depends in fieldset
  73. });
  74. });