| Línea 20... |
Línea 20... |
| 20 |
* @module theme_universe/form-display-errors
|
20 |
* @module theme_universe/form-display-errors
|
| 21 |
* @copyright 2016 Damyon Wiese <damyon@moodle.com>
|
21 |
* @copyright 2016 Damyon Wiese <damyon@moodle.com>
|
| 22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
| 23 |
*/
|
23 |
*/
|
| 24 |
define(['jquery', 'core_form/events'], function($, FormEvent) {
|
24 |
define(['jquery', 'core_form/events'], function($, FormEvent) {
|
| - |
|
25 |
let focusedAlready = false;
|
| 25 |
return {
|
26 |
return {
|
| 26 |
/**
|
27 |
/**
|
| 27 |
* Enhance the supplied element to handle form field errors.
|
28 |
* Enhance the supplied element to handle form field errors.
|
| 28 |
*
|
29 |
*
|
| 29 |
* @method
|
30 |
* @method
|
| Línea 40... |
Línea 41... |
| 40 |
|
41 |
|
| 41 |
element.addEventListener(FormEvent.eventTypes.formFieldValidationFailed, e => {
|
42 |
element.addEventListener(FormEvent.eventTypes.formFieldValidationFailed, e => {
|
| 42 |
const msg = e.detail.message;
|
43 |
const msg = e.detail.message;
|
| Línea 43... |
Línea 44... |
| 43 |
e.preventDefault();
|
44 |
e.preventDefault();
|
| 44 |
|
45 |
|
| 45 |
var parent = $(element).closest('.form-group');
|
46 |
var parent = $(element).closest('.fitem');
|
| Línea 46... |
Línea 47... |
| 46 |
var feedback = parent.find('.form-control-feedback');
|
47 |
var feedback = parent.find('.form-control-feedback');
|
| 47 |
const feedbackId = feedback.attr('id');
|
48 |
const feedbackId = feedback.attr('id');
|
| Línea 57... |
Línea 58... |
| 57 |
describedByIds = describedBy.split(" ");
|
58 |
describedByIds = describedBy.split(" ");
|
| 58 |
}
|
59 |
}
|
| 59 |
// Find the the feedback container in the aria-describedby attribute.
|
60 |
// Find the the feedback container in the aria-describedby attribute.
|
| 60 |
const feedbackIndex = describedByIds.indexOf(feedbackId);
|
61 |
const feedbackIndex = describedByIds.indexOf(feedbackId);
|
| Línea -... |
Línea 62... |
| - |
|
62 |
|
| 61 |
|
63 |
if (element.tagName === 'TEXTAREA') {
|
| 62 |
// Sometimes (atto) we have a hidden textarea backed by a real contenteditable div.
|
64 |
// Check if the textarea is backed by a contenteditable div.
|
| - |
|
65 |
const contentEditable = parent.find('[contenteditable]');
|
| - |
|
66 |
if (contentEditable.length > 0) {
|
| 63 |
if (($(element).prop("tagName") == 'TEXTAREA') && parent.find('[contenteditable]').length > 0) {
|
67 |
// Use the contenteditable div as the target element.
|
| - |
|
68 |
element = contentEditable[0];
|
| - |
|
69 |
} else {
|
| - |
|
70 |
// Use the TinyMCE iframe as the target element if it exists.
|
| - |
|
71 |
element = document.getElementById(`${element.id}_ifr`) || element;
|
| 64 |
element = parent.find('[contenteditable]');
|
72 |
}
|
| - |
|
73 |
}
|
| 65 |
}
|
74 |
|
| 66 |
if (msg !== '') {
|
75 |
if (msg !== '') {
|
| 67 |
parent.addClass('has-danger');
|
76 |
parent.addClass('has-danger');
|
| 68 |
parent.data('client-validation-error', true);
|
77 |
parent.data('client-validation-error', true);
|
| 69 |
$(element).addClass('is-invalid');
|
78 |
$(element).addClass('is-invalid');
|
| 70 |
// Append the feedback ID to the aria-describedby attribute if it doesn't exist yet.
|
79 |
// Append the feedback ID to the aria-describedby attribute if it doesn't exist yet.
|
| 71 |
if (feedbackIndex === -1) {
|
80 |
if (feedbackIndex === -1) {
|
| 72 |
describedByIds.push(feedbackId);
|
81 |
describedByIds.push(feedbackId);
|
| 73 |
$(element).attr('aria-describedby', describedByIds.join(" "));
|
82 |
$(element).attr('aria-describedby', describedByIds.join(" "));
|
| 74 |
}
|
83 |
}
|
| 75 |
$(element).attr('aria-invalid', true);
|
- |
|
| 76 |
feedback.attr('tabindex', 0);
|
84 |
$(element).attr('aria-invalid', true);
|
| - |
|
85 |
feedback.html(msg);
|
| Línea 77... |
Línea 86... |
| 77 |
feedback.html(msg);
|
86 |
feedback.show();
|
| - |
|
87 |
|
| 78 |
|
88 |
// If we haven't focused anything yet, focus this one.
|
| 79 |
// Only display and focus when the error was not already visible.
|
89 |
if (!focusedAlready) {
|
| 80 |
// This is so that, when tabbing around the form, you don't get stuck.
|
90 |
element.scrollIntoView({behavior: "smooth", block: "center"});
|
| - |
|
91 |
focusedAlready = true;
|
| - |
|
92 |
setTimeout(()=> {
|
| - |
|
93 |
// Actual focus happens later in case we need to do this in response to
|
| - |
|
94 |
// a change event which happens in the middle of changing focus.
|
| - |
|
95 |
element.focus({preventScroll: true});
|
| 81 |
if (!feedback.is(':visible')) {
|
96 |
// Let it focus again next time they submit the form.
|
| 82 |
feedback.show();
|
97 |
focusedAlready = false;
|
| Línea 83... |
Línea 98... |
| 83 |
feedback.focus();
|
98 |
}, 0);
|
| 84 |
}
|
99 |
}
|
| 85 |
|
100 |
|