2019-11-02 21:57:59 +01:00
|
|
|
import { helper } from '@ember/component/helper';
|
2019-11-09 13:20:27 +01:00
|
|
|
import { next } from '@ember/runloop';
|
2019-11-02 21:57:59 +01:00
|
|
|
import { assert } from '@ember/debug';
|
|
|
|
|
|
|
|
function scrollFirstInvalidElementIntoViewPort() {
|
2019-11-09 13:20:27 +01:00
|
|
|
// `schedule('afterRender', function() {})` would be more approperiate but there seems to be a
|
|
|
|
// timing issue in Firefox causing the Browser not scrolling up far enough if doing so
|
|
|
|
// delaying to next runloop therefore
|
|
|
|
next(function() {
|
2019-11-15 23:24:54 +01:00
|
|
|
let invalidInput = document.querySelector('.form-control.is-invalid, .custom-control-input.is-invalid');
|
2019-11-02 21:57:59 +01:00
|
|
|
assert(
|
|
|
|
'Atleast one form control must be marked as invalid if form submission was rejected as invalid',
|
|
|
|
invalidInput
|
|
|
|
);
|
2019-11-09 13:20:27 +01:00
|
|
|
|
|
|
|
// focus first invalid control
|
2019-11-02 21:57:59 +01:00
|
|
|
invalidInput.focus({ preventScroll: true });
|
|
|
|
|
2019-11-15 23:24:54 +01:00
|
|
|
// scroll to label or legend of first invalid control
|
2019-11-02 21:57:59 +01:00
|
|
|
if (
|
|
|
|
invalidInput.getBoundingClientRect().top <= 0 ||
|
|
|
|
invalidInput.getBoundingClientRect().bottom >= window.innerHeight
|
|
|
|
) {
|
2019-11-15 23:24:54 +01:00
|
|
|
// Radio groups have a label and a legend. While the label is per input, the legend is for
|
|
|
|
// the whole group. Croodle should bring the legend into view in that case.
|
|
|
|
// Due to a bug in Ember Bootstrap it renders a `<label>` instead of a `<legend>`:
|
|
|
|
// https://github.com/kaliber5/ember-bootstrap/issues/931
|
|
|
|
// As a work-a-round we look the correct label up by a custom convention for the `id` of the
|
|
|
|
// inputs and the `for` of the input group `<label>` (which should be a `<legend>`).
|
|
|
|
let label =
|
|
|
|
document.querySelector(`label[for="${invalidInput.id.substr(0, invalidInput.id.indexOf('_'))}"`) ||
|
|
|
|
document.querySelector(`label[for="${invalidInput.id}"]`);
|
|
|
|
|
2019-11-02 21:57:59 +01:00
|
|
|
label.scrollIntoView({ behavior: 'smooth' });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export default helper(function() {
|
|
|
|
return scrollFirstInvalidElementIntoViewPort;
|
|
|
|
});
|