2018-03-09 20:18:59 +00:00
|
|
|
import $ from 'jquery';
|
2017-03-27 01:33:47 +00:00
|
|
|
import '../commons/bootstrap';
|
2019-05-01 15:52:55 +00:00
|
|
|
import { __ } from '~/locale';
|
2020-10-21 21:09:00 +00:00
|
|
|
import { add, show, hide } from '~/tooltips';
|
2021-02-01 15:08:56 +00:00
|
|
|
import { isInIssuePage } from '../lib/utils/common_utils';
|
2016-12-14 05:26:26 +00:00
|
|
|
|
2016-07-27 03:32:10 +00: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 20:45:11 +00:00
|
|
|
|
2017-03-27 01:33:47 +00:00
|
|
|
function isMac() {
|
|
|
|
return navigator.userAgent.match(/Macintosh/);
|
|
|
|
}
|
2016-07-24 20:45:11 +00:00
|
|
|
|
2017-03-27 01:33:47 +00:00
|
|
|
function keyCodeIs(e, keyCode) {
|
|
|
|
if ((e.originalEvent && e.originalEvent.repeat) || e.repeat) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return e.keyCode === keyCode;
|
|
|
|
}
|
2016-07-24 20:45:11 +00:00
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
$(document).on('keydown.quick_submit', '.js-quick-submit', (e) => {
|
2017-03-27 01:33:47 +00:00
|
|
|
// 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 16:06:38 +00:00
|
|
|
const $submitButton = $form.find('input[type=submit], button[type=submit]').first();
|
2017-03-27 01:33:47 +00:00
|
|
|
|
2018-02-20 22:20:48 +00:00
|
|
|
if (!$submitButton.prop('disabled')) {
|
2017-05-05 10:57:29 +00:00
|
|
|
$submitButton.trigger('click', [e]);
|
2017-08-21 18:48:09 +00:00
|
|
|
|
2017-09-06 08:01:01 +00:00
|
|
|
if (!isInIssuePage()) {
|
2017-08-21 18:48:09 +00:00
|
|
|
$submitButton.disable();
|
|
|
|
}
|
2017-03-27 01:33:47 +00: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
|
2018-10-10 06:09:55 +00:00
|
|
|
$(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;
|
|
|
|
}
|
2017-03-27 01:33:47 +00:00
|
|
|
|
2020-10-21 21:09:00 +00:00
|
|
|
const $el = $(this);
|
2019-05-01 15:52:55 +00:00
|
|
|
const title = isMac()
|
2020-10-21 21:09:00 +00:00
|
|
|
? __('You can also press \u{2318}-Enter')
|
2019-05-01 15:52:55 +00:00
|
|
|
: __('You can also press Ctrl-Enter');
|
2016-07-24 20:45:11 +00:00
|
|
|
|
2020-10-21 21:09:00 +00:00
|
|
|
add($el, {
|
|
|
|
triggers: 'manual',
|
|
|
|
show: true,
|
2018-10-10 06:09:55 +00:00
|
|
|
title,
|
|
|
|
});
|
2020-10-21 21:09:00 +00:00
|
|
|
$el.one('blur click', () => hide($el));
|
|
|
|
show($el);
|
2018-10-10 06:09:55 +00:00
|
|
|
},
|
|
|
|
);
|