|
2 | 2 | define(function (require, exports, module) { |
3 | 3 | const Global = require("./global"); |
4 | 4 |
|
| 5 | + /** |
| 6 | + * this is a generic function to show error messages for input fields |
| 7 | + * |
| 8 | + * @param {string} inputId - input field id |
| 9 | + * @param {string} wrapperId - wrapper element id |
| 10 | + * @param {string} errorMessage - error message to display |
| 11 | + * @param {string} errorId - Unique ID for the error message element |
| 12 | + */ |
| 13 | + function showError(inputId, wrapperId, errorMessage, errorId) { |
| 14 | + // First, clear any existing error messages for this input field |
| 15 | + const $inputField = $(`#${inputId}`); |
| 16 | + const $wrapper = $(`#${wrapperId}`); |
| 17 | + |
| 18 | + // Remove any existing error messages in this wrapper |
| 19 | + $wrapper.find('.error-message').remove(); |
| 20 | + |
| 21 | + // Remove error styling from the input field |
| 22 | + $inputField.removeClass("error-input"); |
| 23 | + |
| 24 | + // Now show the new error message |
| 25 | + const $errorMessage = $("<div>") |
| 26 | + .attr("id", errorId) |
| 27 | + .addClass("error-message") |
| 28 | + .text(errorMessage); |
| 29 | + |
| 30 | + $wrapper.append($errorMessage); |
| 31 | + |
| 32 | + // highlight the input field with error |
| 33 | + $inputField.addClass("error-input"); |
| 34 | + |
| 35 | + // to automatically remove it after 5 seconds |
| 36 | + setTimeout(function () { |
| 37 | + $(`#${errorId}`).fadeOut(function () { |
| 38 | + $(this).remove(); |
| 39 | + }); |
| 40 | + $inputField.removeClass("error-input"); |
| 41 | + }, 5000); |
| 42 | + |
| 43 | + $inputField.one("input", function () { |
| 44 | + $(`#${errorId}`).remove(); |
| 45 | + $(this).removeClass("error-input"); |
| 46 | + }); |
| 47 | + } |
| 48 | + |
5 | 49 | /** |
6 | 50 | * This function is called when there are no available snippets to display |
7 | 51 | * this is called inside the 'showSnippetsList' function inside the snippetsList.js file |
@@ -119,33 +163,19 @@ define(function (require, exports, module) { |
119 | 163 | * Shows an error message when a snippet with the same abbreviation already exists |
120 | 164 | * and user is trying to add a new one |
121 | 165 | * @param {string} abbreviation - The abbreviation that's duplicated |
| 166 | + * @param {boolean} isEditForm - Whether this is for the edit form (optional, defaults to false) |
122 | 167 | */ |
123 | | - function showDuplicateAbbreviationError(abbreviation) { |
124 | | - // just make sure that the error message is not already displaying |
125 | | - if ($("#abbreviation-error-message").length === 0) { |
126 | | - const $errorMessage = $("<div>") |
127 | | - .attr("id", "abbreviation-error-message") |
128 | | - .addClass("error-message") |
129 | | - .text(`A snippet with abbreviation "${abbreviation}" already exists.`); |
130 | | - |
131 | | - $("#abbr-box-wrapper").append($errorMessage); |
132 | | - |
133 | | - // highlight the abbreviation input with error |
134 | | - $("#abbr-box").addClass("error-input"); |
135 | | - |
136 | | - // automatically remove it after 5 seconds |
137 | | - setTimeout(function () { |
138 | | - $("#abbreviation-error-message").fadeOut(function () { |
139 | | - $(this).remove(); |
140 | | - }); |
141 | | - $("#abbr-box").removeClass("error-input"); |
142 | | - }, 5000); |
143 | | - |
144 | | - $("#abbr-box").one("input", function () { |
145 | | - $("#abbreviation-error-message").remove(); |
146 | | - $(this).removeClass("error-input"); |
147 | | - }); |
148 | | - } |
| 168 | + function showDuplicateAbbreviationError(abbreviation, isEditForm = false) { |
| 169 | + const inputId = isEditForm ? 'edit-abbr-box' : 'abbr-box'; |
| 170 | + const wrapperId = isEditForm ? 'edit-abbr-box-wrapper' : 'abbr-box-wrapper'; |
| 171 | + const errorId = isEditForm ? 'edit-abbreviation-duplicate-error' : 'abbreviation-duplicate-error'; |
| 172 | + |
| 173 | + showError( |
| 174 | + inputId, |
| 175 | + wrapperId, |
| 176 | + `A snippet with abbreviation "${abbreviation}" already exists.`, |
| 177 | + errorId |
| 178 | + ); |
149 | 179 | } |
150 | 180 |
|
151 | 181 | /** |
@@ -187,4 +217,5 @@ define(function (require, exports, module) { |
187 | 217 | exports.showSnippetsListHeader = showSnippetsListHeader; |
188 | 218 | exports.hideSnippetsListHeader = hideSnippetsListHeader; |
189 | 219 | exports.initializeListViewToolbarTitle = initializeListViewToolbarTitle; |
| 220 | + exports.showError = showError; |
190 | 221 | }); |
0 commit comments