diff --git webform_conditional.js webform_conditional.js index a04e28c..843834b 100755 --- webform_conditional.js +++ webform_conditional.js @@ -1,106 +1,132 @@ -$(document).ready(function() { +// $Id$ - $.each(webform_conditional, function(dependentField, dependentInfo) { - var formItemWrapper = webform_condtional_wrapper(dependentField,dependentInfo); - - formItemWrapper.css("display", "none"); - // Add onclick handler to Parent field - webform_conditional_add_onchange(dependentField, dependentInfo); +/** + * Attaches the behavior to all required fields + */ +Drupal.behaviors.webform_conditional = function(context) { + $.each(Drupal.settings.webform_conditional.objects, function(dependentField, dependentInfo) { + Drupal.webform_conditional(dependentField, dependentInfo); + }); +}; +/** + * Init + */ +Drupal.webform_conditional = function(dependentField, dependentInfo) { + var formItemWrapper = Drupal.webform_conditional.wrapper(dependentField,dependentInfo); - }); - return; - -}); -function webform_condtional_wrapper(dependentField,dependentInfo){ - if(dependentInfo['type']=='fieldset' || dependentInfo['type']=='markup'){ - return $('#webform-component-' + dependentField); - } - var cssName = dependentField.replace(/_/g,"-"); - if(dependentInfo['fieldsetId']!=''){ - cssName = dependentInfo['fieldsetId'].replace(/_/g,"-") + "-" + cssName; - } - cssId = "#edit-submitted-" + cssName + "-wrapper"; - if($(cssId).length==0){ - cssId = "#edit-submitted-" + cssName + "-1-wrapper"; - return $(cssId).parent().parent(); - }else{ - return $(cssId); - } - -} -function webform_conditional_add_onchange(dependentField, dependentInfo) { - var field_name = "["+ dependentInfo['monitor_field_key'] + "]"; - $('[name$="'+field_name+'"]') - .change(function() { - // If checked - - var currentValue = webform_conditional_field_value(field_name); - - /* - * if((dependentInfo['monitor_field_trigger'] = 'empty' && - * currrentValue == '' ) || - * (dependentInfo['monitor_field_trigger'] = 'not empty' && - * currrentValue != '' ) || - * currentValue==dependentInfo['monitor_field_value'] ){ - */ - if(webform_coditional_matches(currentValue,dependentInfo['monitor_field_value'])){ - // show the hidden div - webform_condtional_wrapper(dependentField,dependentInfo).show( - "slow"); - }else { - // otherwise, hide it - webform_condtional_wrapper(dependentField,dependentInfo).hide( - "slow"); - // and clear data (using different selector: want the - // textarea to be selected, not the parent div) - webform_conditional_clear_dependents(dependentField,dependentInfo); - - - // $("#edit-submitted-" + - // dependentField.replace(/_/g,"-")).val(''); - } - }); + formItemWrapper.css("display", "none"); + // Add onclick handler to Parent field + Drupal.webform_conditional.add_change(dependentField, dependentInfo); +}; +/** + * Adds the change handler + */ +Drupal.webform_conditional.add_change = function(dependentField, dependentInfo) { + var field_name = "["+ dependentInfo['monitor_field_key'] + "]"; + $('[name$="'+field_name+'"]') + .change(function() { + // If checked + + var currentValue = Drupal.webform_conditional.field_value(field_name); + + /* + * if((dependentInfo['monitor_field_trigger'] = 'empty' && + * currrentValue == '' ) || + * (dependentInfo['monitor_field_trigger'] = 'not empty' && + * currrentValue != '' ) || + * currentValue==dependentInfo['monitor_field_value'] ){ + */ + if(Drupal.webform_conditional.matches(currentValue,dependentInfo['monitor_field_value'])){ + // show the hidden div + Drupal.webform_conditional.wrapper(dependentField,dependentInfo).show( + "slow"); + }else { + Drupal.webform_conditional.hide_dependent(dependentField, dependentInfo); + + // $("#edit-submitted-" + + // dependentField.replace(/_/g,"-")).val(''); + } + }); +}; +/** + * Recursively hides dependents + */ +Drupal.webform_conditional.hide_dependent = function(dependentField, dependentInfo) { + Drupal.webform_conditional.wrapper(dependentField,dependentInfo).hide("slow"); + // and clear data (using different selector: want the + // textarea to be selected, not the parent div) + Drupal.webform_conditional.clear_dependents(dependentField,dependentInfo); + $.each(Drupal.settings.webform_conditional.objects, function(fieldName, fieldObject) { + if (dependentField == fieldObject.monitor_field_key) { + Drupal.webform_conditional.hide_dependent(fieldName, fieldObject); + } + }); } -function webform_conditional_clear_dependents(dependentField,dependentInfo){ - - - if(dependentInfo['type']=='fieldset'){ - //when hidding a fieldset clear all components inside it - $('#webform-component-' +dependentField+ ' input' - + ',#webform-component-' +dependentField+ ' select' - + ',#webform-component-' +dependentField+ ' textarea').val(''); - - $('#webform-component-' +dependentField+ ' input[type=checkbox]' - +',#webform-component-' +dependentField+ ' input[type=radio]').attr('checked', false); +/** + * Adds a wrapper and returns the id + */ +Drupal.webform_conditional.wrapper = function(dependentField, dependentInfo) { + if(dependentInfo['type']=='fieldset' || dependentInfo['type']=='markup'){ + return $('#webform-component-' + dependentField); + } + var cssName = dependentField.replace(/_/g,"-"); + if(dependentInfo['fieldsetId']!=''){ + cssName = dependentInfo['fieldsetId'].replace(/_/g,"-") + "-" + cssName; + } + var cssId = "#edit-submitted-" + cssName + "-wrapper"; + if($(cssId).length==0){ + cssId = "#edit-submitted-" + cssName + "-1-wrapper"; + return $(cssId).parent().parent(); + }else{ + return $(cssId); + } +}; +/** + * + */ +Drupal.webform_conditional.clear_dependents = function(dependentField,dependentInfo){ + if(dependentInfo['type']=='fieldset'){ + //when hidding a fieldset clear all components inside it + $('#webform-component-' +dependentField+ ' input' + + ',#webform-component-' +dependentField+ ' select' + + ',#webform-component-' +dependentField+ ' textarea').val(''); + + $('#webform-component-' +dependentField+ ' input[type=checkbox]' + +',#webform-component-' +dependentField+ ' input[type=radio]').attr('checked', false); - return; - } - field_name_dependent = "["+ dependentField + "]"; - var component = $('[name*="'+field_name_dependent+'"]'); - if((component[0].nodeName == 'INPUT' && component.attr('type') == 'text' ) - || component[0].nodeName == 'SELECT' - || component[0].nodeName == 'TEXTAREA'){ - component.val(''); - }else if(component[0].nodeName == 'INPUT' && - (component.attr('type') == 'radio' || component.attr('type') == 'checkbox' ) ){ - component.attr('checked', false); - } - -} -function webform_conditional_field_value(field_name){ - if($('input[name$="'+field_name+'"]:checked').length == 1){ - return $('input[name$="'+field_name+'"]:checked').val(); - }else if($('select[name$="'+field_name+'"] option:selected').length == 1){ - return $('select[name$="'+field_name+'"] option:selected').val(); - } + return; + } + field_name_dependent = "["+ dependentField + "]"; + var component = $('[name*="'+field_name_dependent+'"]'); + if((component[0].nodeName == 'INPUT' && component.attr('type') == 'text' ) + || component[0].nodeName == 'SELECT' + || component[0].nodeName == 'TEXTAREA'){ + component.val(''); + }else if(component[0].nodeName == 'INPUT' && + (component.attr('type') == 'radio' || component.attr('type') == 'checkbox' ) ){ + component.attr('checked', false); + } +}; +/** + * + */ +Drupal.webform_conditional.field_value = function(field_name) { + if($('input[name$="'+field_name+'"]:checked').length == 1){ + return $('input[name$="'+field_name+'"]:checked').val(); + }else if($('select[name$="'+field_name+'"] option:selected').length == 1){ + return $('select[name$="'+field_name+'"] option:selected').val(); + } +}; +/** + * + */ +Drupal.webform_conditional.matches = function(currentValue,triggerValues){ + var found = false; + $.each(triggerValues, function(index, value) { + if(currentValue==value){ + found = true; + return false; + } + }); + return found; } -function webform_coditional_matches(currentValue,triggerValues){ - found = false; - $.each(triggerValues, function(index, value) { - if(currentValue==value){ - found = true; - return false; - } - }); - return found; -} \ No newline at end of file diff --git webform_conditional.module webform_conditional.module index 3f19315..164f861 100755 --- webform_conditional.module +++ webform_conditional.module @@ -108,7 +108,7 @@ function webform_conditional_form_alter(&$form, $form_state, $form_id){ 'type' => $row['type'], 'fieldsetId' => $field['fieldset_name'], 'monitor_field_key' => $extra['webform_conditional_field_key'], - 'monitor_field_value' => explode('|',$extra['webform_conditional_field_value']), + 'monitor_field_value' => explode('|',$extra['webform_conditional_field_value']), 'monitor_field_trigger' => $extra['webform_conditional_trigger'], ); } @@ -118,17 +118,22 @@ function webform_conditional_form_alter(&$form, $form_state, $form_id){ } - - $jsScript = 'var webform_conditional ='.drupal_to_js($jsObject).";"; + + $settings = array( + 'webform_conditional' => array( + 'objects' => $jsObject, + 'nid' => $nid, + ), + ); if(!empty($mandatoryFields)){ $form['webform_conditional_mandatory']=array( '#type' => 'hidden', '#value' => implode('|',$mandatoryFields), ); - $jsScript .= "var webform_conditional_mandatory=".drupal_to_js($mandatoryFields).";"; + $settings['webform_conditional']['mandatory'] = $mandatoryFields; } - drupal_add_js($jsScript,'inline'); + drupal_add_js($settings, 'setting'); drupal_add_js(drupal_get_path('module','webform_conditional') . '/webform_conditional.js'); } @@ -189,4 +194,4 @@ function _webform_conditional_get_field($fields,$fieldKey,$fieldsetName=''){ return null; } } -?> \ No newline at end of file +?>