2017-03-26 21:33:47 -04:00
|
|
|
import '../commons/bootstrap';
|
2016-12-14 00:26:26 -05:00
|
|
|
|
2016-07-26 23:32:10 -04:00
|
|
|
// Quick Submit behavior
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// ### Example Markup
|
|
|
|
//
|
|
|
|
// <form action="/foo" class="js-quick-submit">
|
|
|
|
// <input type="text" />
|
|
|
|
// <textarea></textarea>
|
|
|
|
// <input type="submit" value="Submit" />
|
|
|
|
// </form>
|
|
|
|
//
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-26 21:33:47 -04:00
|
|
|
function isMac() {
|
|
|
|
return navigator.userAgent.match(/Macintosh/);
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-26 21:33:47 -04:00
|
|
|
function keyCodeIs(e, keyCode) {
|
|
|
|
if ((e.originalEvent && e.originalEvent.repeat) || e.repeat) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return e.keyCode === keyCode;
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-26 21:33:47 -04:00
|
|
|
$(document).on('keydown.quick_submit', '.js-quick-submit', (e) => {
|
|
|
|
// Enter
|
|
|
|
if (!keyCodeIs(e, 13)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const onlyMeta = e.metaKey && !e.altKey && !e.ctrlKey && !e.shiftKey;
|
|
|
|
const onlyCtrl = e.ctrlKey && !e.altKey && !e.metaKey && !e.shiftKey;
|
|
|
|
if (!onlyMeta && !onlyCtrl) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
const $form = $(e.target).closest('form');
|
2017-06-27 12:06:38 -04:00
|
|
|
const $submitButton = $form.find('input[type=submit], button[type=submit]').first();
|
2017-03-26 21:33:47 -04:00
|
|
|
|
|
|
|
if (!$submitButton.attr('disabled')) {
|
2017-05-05 06:57:29 -04:00
|
|
|
$submitButton.trigger('click', [e]);
|
2017-08-21 14:48:09 -04:00
|
|
|
|
2017-08-24 20:41:35 -04:00
|
|
|
if (!gl.utils.isInIssuePage()) {
|
2017-08-21 14:48:09 -04:00
|
|
|
$submitButton.disable();
|
|
|
|
}
|
2017-03-26 21:33:47 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// 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]', function displayTooltip(e) {
|
|
|
|
// Tab
|
|
|
|
if (!keyCodeIs(e, 9)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const $this = $(this);
|
|
|
|
const title = isMac() ?
|
|
|
|
'You can also press ⌘-Enter' :
|
|
|
|
'You can also press Ctrl-Enter';
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-26 21:33:47 -04:00
|
|
|
$this.tooltip({
|
|
|
|
container: 'body',
|
|
|
|
html: 'true',
|
|
|
|
placement: 'auto top',
|
|
|
|
title,
|
|
|
|
trigger: 'manual',
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
2017-03-26 21:33:47 -04:00
|
|
|
$this.tooltip('show').one('blur', () => $this.tooltip('hide'));
|
|
|
|
});
|