Projet

Général

Profil

Paste
Télécharger (3,33 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / js / auto-submit.js @ 6e3ce7c2

1
(function($){
2
/**
3
 * To make a form auto submit, all you have to do is 3 things:
4
 *
5
 * ctools_add_js('auto-submit');
6
 *
7
 * On gadgets you want to auto-submit when changed, add the ctools-auto-submit
8
 * class. With FAPI, add:
9
 * @code
10
 *  '#attributes' => array('class' => array('ctools-auto-submit')),
11
 * @endcode
12
 *
13
 * If you want to have auto-submit for every form element,
14
 * add the ctools-auto-submit-full-form to the form. With FAPI, add:
15
 * @code
16
 *   '#attributes' => array('class' => array('ctools-auto-submit-full-form')),
17
 * @endcode
18
 *
19
 * If you want to exclude a field from the ctool-auto-submit-full-form auto submission,
20
 * add the class ctools-auto-submit-exclude to the form element. With FAPI, add:
21
 * @code
22
 *   '#attributes' => array('class' => array('ctools-auto-submit-exclude')),
23
 * @endcode
24
 *
25
 * Finally, you have to identify which button you want clicked for autosubmit.
26
 * The behavior of this button will be honored if it's ajaxy or not:
27
 * @code
28
 *  '#attributes' => array('class' => array('ctools-use-ajax', 'ctools-auto-submit-click')),
29
 * @endcode
30
 *
31
 * Currently only 'select', 'radio', 'checkbox' and 'textfield' types are supported. We probably
32
 * could use additional support for HTML5 input types.
33
 */
34

    
35
Drupal.behaviors.CToolsAutoSubmit = {
36
  attach: function(context) {
37
    // 'this' references the form element
38
    function triggerSubmit (e) {
39
      if ($.contains(document.body, this)) {
40
        var $this = $(this);
41
        if (!$this.hasClass('ctools-ajaxing')) {
42
          $this.find('.ctools-auto-submit-click').click();
43
        }
44
      }
45
    }
46

    
47
    // the change event bubbles so we only need to bind it to the outer form
48
    $('form.ctools-auto-submit-full-form', context)
49
      .add('.ctools-auto-submit', context)
50
      .filter('form, select, input:not(:text, :submit)')
51
      .once('ctools-auto-submit')
52
      .change(function (e) {
53
        // don't trigger on text change for full-form
54
        if ($(e.target).is(':not(:text, :submit, .ctools-auto-submit-exclude)')) {
55
          triggerSubmit.call(e.target.form);
56
        }
57
      });
58

    
59
    // e.keyCode: key
60
    var discardKeyCode = [
61
      16, // shift
62
      17, // ctrl
63
      18, // alt
64
      20, // caps lock
65
      33, // page up
66
      34, // page down
67
      35, // end
68
      36, // home
69
      37, // left arrow
70
      38, // up arrow
71
      39, // right arrow
72
      40, // down arrow
73
       9, // tab
74
      13, // enter
75
      27  // esc
76
    ];
77
    // Don't wait for change event on textfields
78
    $('.ctools-auto-submit-full-form input:text, input:text.ctools-auto-submit', context)
79
      .filter(':not(.ctools-auto-submit-exclude)')
80
      .once('ctools-auto-submit', function () {
81
        // each textinput element has his own timeout
82
        var timeoutID = 0;
83
        $(this)
84
          .bind('keydown keyup', function (e) {
85
            if ($.inArray(e.keyCode, discardKeyCode) === -1) {
86
              timeoutID && clearTimeout(timeoutID);
87
            }
88
          })
89
          .keyup(function(e) {
90
            if ($.inArray(e.keyCode, discardKeyCode) === -1) {
91
              timeoutID = setTimeout($.proxy(triggerSubmit, this.form), 500);
92
            }
93
          })
94
          .bind('change', function (e) {
95
            if ($.inArray(e.keyCode, discardKeyCode) === -1) {
96
              timeoutID = setTimeout($.proxy(triggerSubmit, this.form), 500);
97
            }
98
          });
99
      });
100
  }
101
};
102
})(jQuery);