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 17jQuery(function () { 18 19 jQuery('form.bureaucracy__plugin').each(function () { 20 21 //show/hide fieldset and trigger depending children 22 function updateFieldset(input) { 23 jQuery.each(jQuery(input).data('dparray'), function (i, dp) { 24 var showOrHide = 25 input.parentNode.parentNode.style.display !== 'none' && // input/checkbox is displayed AND 26 ((input.checked === dp.tval) || // ( checkbox is checked 27 (input.type !== 'checkbox' && (dp.tval === true && input.value !== '')) || // OR no checkbox, but input is set 28 input.value === dp.tval); // OR input === dp.tval ) 29 30 dp.fset.toggle(showOrHide); 31 32 dp.fset.find('input,select') 33 .each(function () { 34 //toggle required attribute 35 var $inputelem = jQuery(this); 36 if($inputelem.hasClass('required')) { 37 if(showOrHide) { 38 $inputelem.attr('required', 'required'); 39 } else { 40 $inputelem.removeAttr('required') 41 } 42 } 43 //update dependencies 44 if ($inputelem.data('dparray')) { 45 $inputelem.change(); 46 } 47 }); 48 }); 49 } 50 51 //look for p (with info about controller) in depending fieldsets 52 jQuery('p.bureaucracy_depends', this) 53 .each(function () { 54 //get controller info 55 var fname = jQuery(this).find('span.bureaucracy_depends_fname').html(), 56 fvalue = jQuery(this).find('span.bureaucracy_depends_fvalue'); 57 fvalue = (fvalue.length ? fvalue.html() : true); 58 59 //get controller field and add info and change event to the input that controls depending fieldset 60 var fieldsetinfo = { 61 fset: jQuery(this).parent(), 62 tval: fvalue 63 }; 64 65 jQuery("label") 66 .has(":first-child:contains('" + fname + "')").first() 67 .find("select,input:last") //yesno field contains first a hidden input 68 .each(function () { 69 if (!jQuery(this).data('dparray')) { 70 jQuery(this).data('dparray', [fieldsetinfo]); 71 } else { 72 jQuery(this).data('dparray').push(fieldsetinfo); 73 } 74 }) 75 .bind('change keyup', function () { 76 updateFieldset(this); 77 }) 78 .change(); 79 80 }) 81 .hide(); //hide p.bureaucracy_depends in fieldset 82 83 }); 84}); 85