Fixing form submit focusing issues

This commit is contained in:
Bruno Alves de Faria 2017-05-31 11:17:59 -03:00 committed by Erik Ziegler
parent d9475db1ff
commit 56c3d0f1dd

View File

@ -1,4 +1,5 @@
import { Template } from 'meteor/templating'; import { Template } from 'meteor/templating';
import { $ } from 'meteor/jquery';
import { OHIF } from 'meteor/ohif:core'; import { OHIF } from 'meteor/ohif:core';
/* /*
@ -24,39 +25,47 @@ OHIF.mixins.action = new OHIF.Mixin({
const { action } = instance.data; const { action } = instance.data;
const params = instance.data.params ? instance.data.params : event; const params = instance.data.params ? instance.data.params : event;
// Set the focus back to the input that triggered the click with Enter key
const $focused = $(':focus');
const applyFocus = () => {
if ($focused[0] && event.currentTarget !== $focused[0]) {
setTimeout(() => $focused.focus());
}
};
// Stop here if the component is disabled // Stop here if the component is disabled
if (component.$element.hasClass('disabled')) return; if (component.$element.hasClass('disabled')) return;
// Get the current component's API // Get the current component's API
const api = component.getApi(); const api = component.getApi();
// Stop here calling the action if it's a function if (!api || !action || typeof api[action] !== 'function') {
if (typeof action === 'function') { // Stop here if no API or action was defined
return true;
} else if (typeof action === 'function') {
// Call the action if it's a function
component.actionResult = action.call(event.currentTarget, params); component.actionResult = action.call(event.currentTarget, params);
return component.actionResult; } else {
// Call the defined action function
component.actionResult = api[action].call(event.currentTarget, params);
} }
// Stop here if no API or action was defined
if (!api || !action || typeof api[action] !== 'function') return;
// Call the defined action function
component.actionResult = api[action].call(event.currentTarget, params);
// Prepend a spinner into the action element content if it's a promise // Prepend a spinner into the action element content if it's a promise
if (component.actionResult instanceof Promise) { if (component.actionResult instanceof Promise) {
const form = component.getForm(); const form = component.getForm();
form.disable(true); form.disable(true);
const $spinner = $('<i class="fa fa-spin fa-circle-o-notch fa-fw m-r"></i>'); const $spinner = $('<i class="fa fa-spin fa-circle-o-notch fa-fw m-r"></i>');
component.$element.prepend($spinner); component.$element.prepend($spinner);
const dismissSpinner = () => { const finishAction = () => {
$spinner.remove(); $spinner.remove();
form.disable(false); form.disable(false);
applyFocus();
}; };
component.actionResult.then(dismissSpinner).catch(dismissSpinner); component.actionResult.then(finishAction).catch(finishAction);
} else {
applyFocus();
} }
return component.actionResult;
} }
} }
} }