auto-submit.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. Drupal.behaviors.CToolsAutoSubmit = {
  35. attach: function(context) {
  36. // 'this' references the form element
  37. function triggerSubmit (e) {
  38. var $this = $(this);
  39. if (!$this.hasClass('ctools-ajaxing')) {
  40. $this.find('.ctools-auto-submit-click').click();
  41. }
  42. }
  43. // the change event bubbles so we only need to bind it to the outer form
  44. $('form.ctools-auto-submit-full-form', context)
  45. .add('.ctools-auto-submit', context)
  46. .filter('form, select, input:not(:text, :submit)')
  47. .once('ctools-auto-submit')
  48. .change(function (e) {
  49. // don't trigger on text change for full-form
  50. if ($(e.target).is(':not(:text, :submit, .ctools-auto-submit-exclude)')) {
  51. triggerSubmit.call(e.target.form);
  52. }
  53. });
  54. // e.keyCode: key
  55. var discardKeyCode = [
  56. 16, // shift
  57. 17, // ctrl
  58. 18, // alt
  59. 20, // caps lock
  60. 33, // page up
  61. 34, // page down
  62. 35, // end
  63. 36, // home
  64. 37, // left arrow
  65. 38, // up arrow
  66. 39, // right arrow
  67. 40, // down arrow
  68. 9, // tab
  69. 13, // enter
  70. 27 // esc
  71. ];
  72. // Don't wait for change event on textfields
  73. $('.ctools-auto-submit-full-form input:text, input:text.ctools-auto-submit', context)
  74. .filter(':not(.ctools-auto-submit-exclude)')
  75. .once('ctools-auto-submit', function () {
  76. // each textinput element has his own timeout
  77. var timeoutID = 0;
  78. $(this)
  79. .bind('keydown keyup', function (e) {
  80. if ($.inArray(e.keyCode, discardKeyCode) === -1) {
  81. timeoutID && clearTimeout(timeoutID);
  82. }
  83. })
  84. .keyup(function(e) {
  85. if ($.inArray(e.keyCode, discardKeyCode) === -1) {
  86. timeoutID = setTimeout($.proxy(triggerSubmit, this.form), 500);
  87. }
  88. })
  89. .bind('change', function (e) {
  90. if ($.inArray(e.keyCode, discardKeyCode) === -1) {
  91. timeoutID = setTimeout($.proxy(triggerSubmit, this.form), 500);
  92. }
  93. });
  94. });
  95. }
  96. }
  97. })(jQuery);