2015-10-24 18:46:33 +05:30
|
|
|
# Quick Submit behavior
|
|
|
|
#
|
2016-06-02 11:05:42 +05:30
|
|
|
# When a child field of a form with a `js-quick-submit` class receives a
|
|
|
|
# "Meta+Enter" (Mac) or "Ctrl+Enter" (Linux/Windows) key combination, the form
|
|
|
|
# is submitted.
|
2015-10-24 18:46:33 +05:30
|
|
|
#
|
|
|
|
#= require extensions/jquery
|
|
|
|
#
|
|
|
|
# ### Example Markup
|
|
|
|
#
|
2016-06-02 11:05:42 +05:30
|
|
|
# <form action="/foo" class="js-quick-submit">
|
|
|
|
# <input type="text" />
|
|
|
|
# <textarea></textarea>
|
|
|
|
# <input type="submit" value="Submit" />
|
2015-10-24 18:46:33 +05:30
|
|
|
# </form>
|
|
|
|
#
|
2016-06-02 11:05:42 +05:30
|
|
|
isMac = ->
|
|
|
|
navigator.userAgent.match(/Macintosh/)
|
|
|
|
|
|
|
|
keyCodeIs = (e, keyCode) ->
|
|
|
|
return false if (e.originalEvent && e.originalEvent.repeat) || e.repeat
|
|
|
|
return e.keyCode == keyCode
|
|
|
|
|
2015-10-24 18:46:33 +05:30
|
|
|
$(document).on 'keydown.quick_submit', '.js-quick-submit', (e) ->
|
2016-06-02 11:05:42 +05:30
|
|
|
return unless keyCodeIs(e, 13) # Enter
|
2015-10-24 18:46:33 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
return unless (e.metaKey && !e.altKey && !e.ctrlKey && !e.shiftKey) || (e.ctrlKey && !e.altKey && !e.metaKey && !e.shiftKey)
|
2015-10-24 18:46:33 +05:30
|
|
|
|
|
|
|
e.preventDefault()
|
|
|
|
|
|
|
|
$form = $(e.target).closest('form')
|
2016-06-02 11:05:42 +05:30
|
|
|
$submit_button = $form.find('input[type=submit], button[type=submit]')
|
|
|
|
|
|
|
|
return if $submit_button.attr('disabled')
|
|
|
|
|
|
|
|
$submit_button.disable()
|
2015-10-24 18:46:33 +05:30
|
|
|
$form.submit()
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
# If the user tabs to a submit button on a `js-quick-submit` form, display a
|
|
|
|
# tooltip to let them know they could've used the hotkey
|
|
|
|
$(document).on 'keyup.quick_submit', '.js-quick-submit input[type=submit], .js-quick-submit button[type=submit]', (e) ->
|
|
|
|
return unless keyCodeIs(e, 9) # Tab
|
|
|
|
|
|
|
|
if isMac()
|
|
|
|
title = "You can also press ⌘-Enter"
|
|
|
|
else
|
|
|
|
title = "You can also press Ctrl-Enter"
|
|
|
|
|
|
|
|
$this = $(@)
|
|
|
|
$this.tooltip(
|
|
|
|
container: 'body'
|
|
|
|
html: 'true'
|
|
|
|
placement: 'auto top'
|
|
|
|
title: title
|
|
|
|
trigger: 'manual'
|
|
|
|
).tooltip('show').one('blur', -> $this.tooltip('hide'))
|