diff options
author | Robert Speicher <rspeicher@gmail.com> | 2015-10-06 01:19:11 +0300 |
---|---|---|
committer | Robert Speicher <rspeicher@gmail.com> | 2015-10-07 22:23:41 +0300 |
commit | 01d0926bc16bb82ef676c35a5a61b8e90a82654c (patch) | |
tree | f30316edb40bb2afedf433e7a53e41e2ad556d5c /app/assets/javascripts/behaviors | |
parent | 38fbfb9fe6d52e1a36b16ccc2a9da9ae01561cca (diff) |
Add "Quick Submit" JS behavior
Diffstat (limited to 'app/assets/javascripts/behaviors')
-rw-r--r-- | app/assets/javascripts/behaviors/quick_submit.js.coffee | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/app/assets/javascripts/behaviors/quick_submit.js.coffee b/app/assets/javascripts/behaviors/quick_submit.js.coffee new file mode 100644 index 00000000000..dc9bd4d94cc --- /dev/null +++ b/app/assets/javascripts/behaviors/quick_submit.js.coffee @@ -0,0 +1,29 @@ +# Quick Submit behavior +# +# When an input field with the `js-quick-submit` class receives a "Meta+Enter" +# (Mac) or "Ctrl+Enter" (Linux/Windows) key combination, its parent form is +# submitted. +# +#= require extensions/jquery +# +# ### Example Markup +# +# <form action="/foo"> +# <input type="text" class="js-quick-submit" /> +# <textarea class="js-quick-submit"></textarea> +# </form> +# +$(document).on 'keydown.quick_submit', '.js-quick-submit', (e) -> + return if e.repeat + return unless e.keyCode == 13 # Enter + + if navigator.userAgent.match(/Macintosh/) + return unless (e.metaKey && !e.altKey && !e.ctrlKey && !e.shiftKey) + else + return unless (e.ctrlKey && !e.altKey && !e.metaKey && !e.shiftKey) + + e.preventDefault() + + $form = $(e.target).closest('form') + $form.find('input[type=submit], button[type=submit]').disable() + $form.submit() |