root / drupal7 / sites / all / modules / ctools / js / states-show.js @ fc2c1c7a
1 | 560c3060 | Julien Enselme | /**
|
---|---|---|---|
2 | * @file
|
||
3 | * Custom state for handling visibility
|
||
4 | */
|
||
5 | |||
6 | /**
|
||
7 | * Add a new state to Drupal #states. We use this to toggle element-invisible
|
||
8 | * to show/hidden #states elements. This allows elements to be visible to
|
||
9 | * screen readers.
|
||
10 | *
|
||
11 | * To use:
|
||
12 | * $form['my_form_field'] = array(
|
||
13 | * ..
|
||
14 | * // Only show this field if 'some_other_field' is checked.
|
||
15 | * '#states => array(
|
||
16 | * 'show' => array(
|
||
17 | * 'some-other-field' => array('checked' => TRUE),
|
||
18 | * ),
|
||
19 | * ),
|
||
20 | * ..
|
||
21 | * // Required to load the 'show' state handler.
|
||
22 | * '#attached' => array(
|
||
23 | * 'js' => array(ctools_attach_js('states-show')),
|
||
24 | * ),
|
||
25 | * );
|
||
26 | */
|
||
27 | |||
28 | (function ($) { |
||
29 | 'use strict';
|
||
30 | |||
31 | Drupal.states.State.aliases.hidden = '!show';
|
||
32 | |||
33 | // Show/hide form items by toggling the 'element-invisible' class. This is a
|
||
34 | // more accessible option than the core 'visible' state.
|
||
35 | $(document).bind('state:show', function(e) { |
||
36 | if (e.trigger) {
|
||
37 | var element = $(e.target).closest('.form-item, .form-submit, .form-wrapper'); |
||
38 | element.toggle(e.value); |
||
39 | e.value === true ? element.removeClass('element-invisible') : element.addClass('element-invisible'); |
||
40 | } |
||
41 | }); |
||
42 | |||
43 | })(jQuery); |