summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/behaviors/quick_submit.js
diff options
context:
space:
mode:
Diffstat (limited to 'app/assets/javascripts/behaviors/quick_submit.js')
-rw-r--r--app/assets/javascripts/behaviors/quick_submit.js109
1 files changed, 52 insertions, 57 deletions
diff --git a/app/assets/javascripts/behaviors/quick_submit.js b/app/assets/javascripts/behaviors/quick_submit.js
index 626f3503c91..3d162b24413 100644
--- a/app/assets/javascripts/behaviors/quick_submit.js
+++ b/app/assets/javascripts/behaviors/quick_submit.js
@@ -1,4 +1,4 @@
-/* eslint-disable func-names, space-before-function-paren, one-var, no-var, one-var-declaration-per-line, prefer-arrow-callback, camelcase, consistent-return, quotes, object-shorthand, comma-dangle, max-len */
+import '../commons/bootstrap';
// Quick Submit behavior
//
@@ -6,9 +6,6 @@
// "Meta+Enter" (Mac) or "Ctrl+Enter" (Linux/Windows) key combination, the form
// is submitted.
//
-import '../commons/bootstrap';
-
-//
// ### Example Markup
//
// <form action="/foo" class="js-quick-submit">
@@ -17,61 +14,59 @@ import '../commons/bootstrap';
// <input type="submit" value="Submit" />
// </form>
//
-(function() {
- var isMac, keyCodeIs;
- isMac = function() {
- return navigator.userAgent.match(/Macintosh/);
- };
+function isMac() {
+ return navigator.userAgent.match(/Macintosh/);
+}
- keyCodeIs = function(e, keyCode) {
- if ((e.originalEvent && e.originalEvent.repeat) || e.repeat) {
- return false;
- }
- return e.keyCode === keyCode;
- };
+function keyCodeIs(e, keyCode) {
+ if ((e.originalEvent && e.originalEvent.repeat) || e.repeat) {
+ return false;
+ }
+ return e.keyCode === keyCode;
+}
- $(document).on('keydown.quick_submit', '.js-quick-submit', function(e) {
- var $form, $submit_button;
- // Enter
- if (!keyCodeIs(e, 13)) {
- return;
- }
- if (!((e.metaKey && !e.altKey && !e.ctrlKey && !e.shiftKey) || (e.ctrlKey && !e.altKey && !e.metaKey && !e.shiftKey))) {
- return;
- }
- e.preventDefault();
- $form = $(e.target).closest('form');
- $submit_button = $form.find('input[type=submit], button[type=submit]');
- if ($submit_button.attr('disabled')) {
- return;
- }
- $submit_button.disable();
- return $form.submit();
- });
+$(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');
+ const $submitButton = $form.find('input[type=submit], button[type=submit]');
+
+ if (!$submitButton.attr('disabled')) {
+ $submitButton.disable();
+ $form.submit();
+ }
+});
+
+// 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 &#8984;-Enter' :
+ 'You can also press Ctrl-Enter';
- // 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(e) {
- var $this, title;
- // Tab
- if (!keyCodeIs(e, 9)) {
- return;
- }
- if (isMac()) {
- title = "You can also press &#8984;-Enter";
- } else {
- title = "You can also press Ctrl-Enter";
- }
- $this = $(this);
- return $this.tooltip({
- container: 'body',
- html: 'true',
- placement: 'auto top',
- title: title,
- trigger: 'manual'
- }).tooltip('show').one('blur', function() {
- return $this.tooltip('hide');
- });
+ $this.tooltip({
+ container: 'body',
+ html: 'true',
+ placement: 'auto top',
+ title,
+ trigger: 'manual',
});
-}).call(window);
+ $this.tooltip('show').one('blur', () => $this.tooltip('hide'));
+});