dependent.js 8.3 KB

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