| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 // $Id: dependent.js,v 1.9.2.1 2009/11/18 02:43:47 merlinofchaos Exp $ 2 /** 3 * @file dependent.js 4 * 5 * Written by dmitrig01 (Dmitri Gaskin) for Views; this provides dependent 6 * visibility for form items in Views' ajax forms. 7 * 8 * To your $form item definition add: 9 * - '#process' => array('views_process_dependency'), 10 * - Add '#dependency' => array('id-of-form-item' => array(list, of, values, that, 11 make, this, item, show), 12 * 13 * Special considerations: 14 * - radios are harder. Because Drupal doesn't give radio groups individual ids, 15 * use 'radio:name-of-radio' 16 * 17 * - Checkboxes don't have their own id, so you need to add one in a div 18 * around the checkboxes via #prefix and #suffix. You actually need to add TWO 19 * divs because it's the parent that gets hidden. Also be sure to retain the 20 * 'expand_checkboxes' in the #process array, because the views process will 21 * override it. 22 */ 23 24 Drupal.Views = Drupal.Views || {}; 25 26 Drupal.Views.dependent = { bindings: {}, activeBindings: {}, activeTriggers: [] }; 27 28 Drupal.Views.dependent.inArray = function(array, search_term) { 29 var i = array.length; 30 if (i > 0) { 31 do { 32 if (array[i] == search_term) { 33 return true; 34 } 35 } while (i--); 36 } 37 return false; 38 } 39 40 41 Drupal.Views.dependent.autoAttach = function() { 42 // Clear active bindings and triggers. 43 for (i in Drupal.Views.dependent.activeTriggers) { 44 jQuery(Drupal.Views.dependent.activeTriggers[i]).unbind('change'); 45 } 46 Drupal.Views.dependent.activeTriggers = []; 47 Drupal.Views.dependent.activeBindings = {}; 48 Drupal.Views.dependent.bindings = {}; 49 50 if (!Drupal.settings.viewsAjax) { 51 return; 52 } 53 54 // Iterate through all relationships 55 for (id in Drupal.settings.viewsAjax.formRelationships) { 56 57 // Drupal.Views.dependent.activeBindings[id] is a boolean, 58 // whether the binding is active or not. Defaults to no. 59 Drupal.Views.dependent.activeBindings[id] = 0; 60 // Iterate through all possible values 61 for(bind_id in Drupal.settings.viewsAjax.formRelationships[id].values) { 62 // This creates a backward relationship. The bind_id is the ID 63 // of the element which needs to change in order for the id to hide or become shown. 64 // The id is the ID of the item which will be conditionally hidden or shown. 65 // Here we're setting the bindings for the bind 66 // id to be an empty array if it doesn't already have bindings to it 67 if (!Drupal.Views.dependent.bindings[bind_id]) { 68 Drupal.Views.dependent.bindings[bind_id] = []; 69 } 70 // Add this ID 71 Drupal.Views.dependent.bindings[bind_id].push(id); 72 // Big long if statement. 73 // Drupal.settings.viewsAjax.formRelationships[id].values[bind_id] holds the possible values 74 75 if (bind_id.substring(0, 6) == 'radio:') { 76 var trigger_id = "input[name='" + bind_id.substring(6) + "']"; 77 } 78 else { 79 var trigger_id = '#' + bind_id; 80 } 81 82 Drupal.Views.dependent.activeTriggers.push(trigger_id); 83 84 if (jQuery(trigger_id).attr('type') == 'checkbox') { 85 $(trigger_id).parent().addClass('hidden-options'); 86 } 87 88 var getValue = function(item, trigger) { 89 if (item.substring(0, 6) == 'radio:') { 90 var val = jQuery(trigger + ':checked').val(); 91 } 92 else { 93 switch (jQuery(trigger).attr('type')) { 94 case 'checkbox': 95 var val = jQuery(trigger).attr('checked') || 0; 96 97 if (val) { 98 $(trigger).parent().removeClass('hidden-options').addClass('expanded-options'); 99 } 100 else { 101 $(trigger).parent().removeClass('expanded-options').addClass('hidden-options'); 102 } 103 104 break; 105 default: 106 var val = jQuery(trigger).val(); 107 } 108 } 109 return val; 110 } 111 112 var setChangeTrigger = function(trigger_id, bind_id) { 113 // Triggered when change() is clicked. 114 var changeTrigger = function() { 115 var val = getValue(bind_id, trigger_id); 116 117 for (i in Drupal.Views.dependent.bindings[bind_id]) { 118 var id = Drupal.Views.dependent.bindings[bind_id][i]; 119 120 // Fix numerous errors 121 if (typeof id != 'string') { 122 continue; 123 } 124 125 // This bit had to be rewritten a bit because two properties on the 126 // same set caused the counter to go up and up and up. 127 if (!Drupal.Views.dependent.activeBindings[id]) { 128 Drupal.Views.dependent.activeBindings[id] = {}; 129 } 130 131 if (Drupal.Views.dependent.inArray(Drupal.settings.viewsAjax.formRelationships[id].values[bind_id], val)) { 132 Drupal.Views.dependent.activeBindings[id][bind_id] = 'bind'; 133 } 134 else { 135 delete Drupal.Views.dependent.activeBindings[id][bind_id]; 136 } 137 138 var len = 0; 139 for (i in Drupal.Views.dependent.activeBindings[id]) { 140 len++; 141 } 142 143 var object = jQuery('#' + id + '-wrapper'); 144 if (!object.size()) { 145 object = jQuery('#' + id).parent(); 146 } 147 148 var rel_num = Drupal.settings.viewsAjax.formRelationships[id].num; 149 if (typeof rel_num === 'object') { 150 rel_num = Drupal.settings.viewsAjax.formRelationships[id].num[0]; 151 } 152 153 if (rel_num <= len) { 154 // Show if the element if criteria is matched 155 object.show(0); 156 object.addClass('dependent-options'); 157 } 158 else { 159 // Otherwise hide 160 object.hide(0); 161 } 162 } 163 } 164 165 jQuery(trigger_id).change(function() { 166 // Trigger the internal change function 167 // the attr('id') is used because closures are more confusing 168 changeTrigger(trigger_id, bind_id); 169 }); 170 // Trigger initial reaction 171 changeTrigger(trigger_id, bind_id); 172 } 173 setChangeTrigger(trigger_id, bind_id); 174 } 175 } 176 } 177 178 Drupal.behaviors.viewsDependent = function (context) { 179 Drupal.Views.dependent.autoAttach(); 180 181 // Really large sets of fields are too slow with the above method, so this 182 // is a sort of hacked one that's faster but much less flexible. 183 $("select.views-master-dependent:not(.views-processed)") 184 .addClass('views-processed') 185 .change(function() { 186 var val = $(this).val(); 187 if (val == 'all') { 188 $('.views-dependent-all').show(0); 189 } 190 else { 191 $('.views-dependent-all').hide(0); 192 $('.views-dependent-' + val).show(0); 193 } 194 }) 195 .trigger('change'); 196 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Mar 24 11:18:33 2011 | Cross-referenced by PHPXref 0.7 |