MDL-85041 core: Make methods calling announceChanges async as well

This commit is contained in:
Jun Pataleta
2025-06-04 17:33:18 +08:00
parent 25f8dd9d63
commit a92df9e0ee
3 changed files with 36 additions and 56 deletions
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+34 -54
View File
@@ -221,7 +221,7 @@ define([
* @param {Element} originalSelect The original select list.
* @return {Promise}
*/
var deselectItem = function(options, state, item, originalSelect) {
const deselectItem = async(options, state, item, originalSelect) => {
var selectedItemValue = $(item).attr('data-value');
// Preprend an empty option to the select list to avoid having a default selected option.
@@ -241,16 +241,13 @@ define([
});
const selectedItemText = item[0].childNodes[2].textContent?.trim();
announceChanges(state.selectionId, selectedItemText, 'removed');
await announceChanges(state.selectionId, selectedItemText, 'removed');
// Rerender the selection list.
return updateSelectionList(options, state, originalSelect)
.then(function() {
// Notify that the selection changed.
notifyChange(originalSelect);
await updateSelectionList(options, state, originalSelect);
return;
});
// Notify that the selection changed.
notifyChange(originalSelect);
};
/**
@@ -593,7 +590,7 @@ define([
* @param {JQuery} originalSelect The JQuery object matching the hidden select list.
* @return {Promise}
*/
var createItem = function(options, state, originalSelect) {
const createItem = async(options, state, originalSelect) => {
// Find the element in the DOM.
var inputElement = $(document.getElementById(state.inputId));
// Get the current text in the input field.
@@ -628,25 +625,17 @@ define([
}
});
announceChanges(state.selectionId, query.trim(), 'added');
// Announce the changes to the assistive technology.
await announceChanges(state.selectionId, query.trim(), 'added');
return updateSelectionList(options, state, originalSelect)
.then(function() {
// Notify that the selection changed.
notifyChange(originalSelect);
await updateSelectionList(options, state, originalSelect);
// Notify that the selection changed.
notifyChange(originalSelect);
return;
})
.then(function() {
// Clear the input field.
inputElement.val('');
return;
})
.then(function() {
// Close the suggestions list.
return closeSuggestions(state);
});
// Clear the input field.
inputElement.val('');
// Close the suggestions list.
await closeSuggestions(state);
};
/**
@@ -660,7 +649,7 @@ define([
* @param {string|null} selectedItem The item to be selected.
* @return {Promise}
*/
var selectCurrentItem = function(options, state, originalSelect, selectedItem) {
const selectCurrentItem = async(options, state, originalSelect, selectedItem) => {
// Find the elements in the page.
var inputElement = $(document.getElementById(state.inputId));
var suggestionsElement = $(document.getElementById(state.suggestionsId));
@@ -675,34 +664,30 @@ define([
originalSelect.children('option').prop('selected', false);
}
// Look for a match, and toggle the selected property if there is a match.
originalSelect.children('option').each(function(index, ele) {
originalSelect.children('option').each(function (index, ele) {
if ($(ele).attr('value') == selectedItemValue) {
$(ele).prop('selected', true);
}
});
announceChanges(state.selectionId, selectedItem, 'added');
await announceChanges(state.selectionId, selectedItem, 'added');
return updateSelectionList(options, state, originalSelect)
.then(function() {
// Notify that the selection changed.
notifyChange(originalSelect);
await updateSelectionList(options, state, originalSelect);
return;
})
.then(function() {
if (options.closeSuggestionsOnSelect) {
// Clear the input element.
inputElement.val('');
// Close the list of suggestions.
return closeSuggestions(state);
} else {
// Focus on the input element so the suggestions does not auto-close.
inputElement.focus();
// Remove the last selected item from the suggestions list.
return updateSuggestions(options, state, inputElement.val(), originalSelect);
}
});
// Notify that the selection changed.
notifyChange(originalSelect);
if (options.closeSuggestionsOnSelect) {
// Clear the input element.
inputElement.val('');
// Close the list of suggestions.
await closeSuggestions(state);
} else {
// Focus on the input element so the suggestions does not auto-close.
inputElement.focus();
// Remove the last selected item from the suggestions list.
await updateSuggestions(options, state, inputElement.val(), originalSelect);
}
};
/**
@@ -1329,12 +1314,7 @@ define([
return;
}
try {
const result = await str.get_string(action, 'core', tagname);
status.textContent = result;
} catch (error) {
notification.exception(error);
}
status.textContent = await str.get_string(action, 'core', tagname);
// Remove the status message after 4 seconds to prevent screen readers from announcing it.
setTimeout(() => {