auto-submit.js 3.3 KB

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