}
else if (actionData.data.actionName === 'com.woltlab.wcf.article.setCategory') {
try {
- dialog = UiDialog.getDialog('articleCategoryDialog');
+ UiDialog.getDialog('articleCategoryDialog');
UiDialog.openStatic('articleCategoryDialog');
}
catch (e) {
UiDialog.close('articleCategoryDialog');
}
else if (innerErrors.length === 0) {
- var innerError = elCreate('small');
- innerError.className = 'innerError';
- innerError.innerText = Language.get('wcf.global.form.error.empty');
-
- DomUtil.insertAfter(innerError, select);
+ elInnerError(select, Language.get('wcf.global.form.error.empty'));
}
},
*/
_ajaxSuccess: function(data) {
if (data.returnValues.errorMessage) {
- this._showPathError(data.returnValues.errorMessage);
+ elInnerError(_pathInput, data.returnValues.errorMessage);
_submitButton.disabled = false;
};
},
- /**
- * Returns the error element for the path input in the dialog or
- * `null` if no exists yet.
- *
- * @param {boolean} createPathError if `true` and inner error element does not exist, it will be created
- * @return {HTMLElement?} path error element
- */
- _getPathError: function(createPathError) {
- var innerError = DomTraverse.nextByClass(_pathInput, 'innerError');
-
- if (createPathError && innerError === null) {
- innerError = elCreate('small');
- innerError.className = 'innerError';
-
- DomUtil.insertAfter(innerError, _pathInput);
- }
-
- return innerError;
- },
-
/**
* Handles the `[ENTER]` key to submit the form.
*
_pathInput.focus();
// hide error
- var innerError = this._getPathError();
- if (innerError) {
- elHide(innerError);
- }
+ elInnerError(_pathInput, false);
},
/**
UiDialog.open(this);
},
- /**
- * Shows the path error message.
- *
- * @param {string} errorMessage path error emssage
- */
- _showPathError: function(errorMessage) {
- var innerError = this._getPathError(true);
- innerError.textContent = errorMessage;
-
- elShow(innerError);
- },
-
/**
* Is called if the dialog form is submitted.
*/
_submit: function() {
// check if path is empty
if (_pathInput.value === '') {
- this._showPathError(Language.get('wcf.global.form.error.empty'));
+ elInnerError(_pathInput, Language.get('wcf.global.form.error.empty'));
return;
}
_buttonRunTest.disabled = true;
_buttonRunTest.innerHTML = '<span class="icon icon16 fa-spinner"></span> ' + Language.get('wcf.global.loading');
- var innerError = _buttonRunTest.nextElementSibling;
- if (innerError && innerError.classList.contains('innerError')) elRemove(innerError);
+ elInnerError(_buttonRunTest, false);
window.setTimeout((function () {
Ajax.api(this, {
if (success) _buttonRunTest.innerHTML = '<span class="icon icon16 fa-check green"></span> ' + Language.get('wcf.acp.email.smtp.test.run.success');
else _buttonRunTest.innerHTML = Language.get('wcf.acp.email.smtp.test.run');
- if (errorMessage) {
- var innerError = elCreate('small');
- innerError.className = 'innerError';
- innerError.textContent = errorMessage;
- _buttonRunTest.parentNode.insertBefore(innerError, _buttonRunTest.nextElementSibling);
- }
+ if (errorMessage) elInnerError(_buttonRunTest, errorMessage);
},
_ajaxSetup: function () {
* @see WoltLabSuite/Core/Upload#_success
*/
_success: function(uploadId, data) {
- var error = DomTraverse.childByClass(this._button.parentNode, 'innerError');
+ var errorMessage = '';
if (data.returnValues.url) {
elAttr(this._target, 'src', data.returnValues.url + '?timestamp=' + Date.now());
- if (error) {
- elRemove(error);
- }
-
UiNotification.show();
}
else if (data.returnValues.errorType) {
- if (!error) {
- error = elCreate('small');
- error.className = 'innerError';
-
- this._button.parentNode.appendChild(error);
- }
-
- error.textContent = Language.get('wcf.acp.style.favicon.error.' + data.returnValues.errorType);
+ errorMessage = Language.get('wcf.acp.style.favicon.error.' + data.returnValues.errorType);
}
+
+ elInnerError(this._button, errorMessage);
}
});
* @see WoltLabSuite/Core/Upload#_success
*/
_success: function(uploadId, data) {
- var error = DomTraverse.childByClass(this._button.parentNode, 'innerError');
+ var errorMessage = '';
if (data.returnValues.url) {
elAttr(this._target, 'src', data.returnValues.url + '?timestamp=' + Date.now());
- if (error) {
- elRemove(error);
- }
-
UiNotification.show();
}
else if (data.returnValues.errorType) {
- if (!error) {
- error = elCreate('small');
- error.className = 'innerError';
-
- this._button.parentNode.appendChild(error);
- }
-
- error.textContent = Language.get('wcf.acp.style.image.error.' + data.returnValues.errorType);
+ errorMessage = Language.get('wcf.acp.style.image.error.' + data.returnValues.errorType);
}
+
+ elInnerError(this._button, errorMessage);
}
});
* @see WoltLabSuite/Core/Upload#_success
*/
_success: function(uploadId, data) {
- var error = DomTraverse.childByClass(this._button.parentNode, 'innerError');
+ elInnerError(this._button, false);
this._target.innerHTML = "<img src=\"" + data.returnValues.url + "?timestamp=" + Date.now() + "\" />";
- if (error) {
- elRemove(error);
- }
-
UINotification.show();
},
* @see WoltLabSuite/Core/Upload#_failure
*/
_failure: function(uploadId, data, responseText, xhr, requestOptions) {
- var error = DomTraverse.childByClass(this._button.parentNode, 'innerError');
- if (!error) {
- error = elCreate('small');
- error.className = 'innerError';
-
- this._button.parentNode.appendChild(error);
- }
-
- error.textContent = Language.get('wcf.acp.trophy.imageUpload.error.' + data.returnValues.errorType);
+ elInnerError(this._button, Language.get('wcf.acp.trophy.imageUpload.error.' + data.returnValues.errorType)=;
// remove previous images
this._target.innerHTML = "";
_setRoute: function(result, status) {
AjaxStatus.hide();
- if (status == 'OK') {
+ if (status === 'OK') {
elShow(this._map.getDiv().parentNode);
google.maps.event.trigger(this._map, 'resize');
elShow(DomTraverse.parentByTag(this._travelMode, 'DL'));
elShow(this._googleLink);
- if (this._errorMessage) {
- elHide(this._errorMessage);
- }
+ elInnerError(this._originInput, false);
}
else {
// map irrelevant errors to not found error
status = 'NOT_FOUND';
}
- if (this._errorMessage === undefined) {
- this._errorMessage = elCreate('small');
- this._errorMessage.className = 'innerError';
-
- DomUtil.insertAfter(this._errorMessage, this._originInput);
- }
- else {
- elShow(this._errorMessage);
- }
-
- this._errorMessage.textContent = Language.get('wcf.map.route.error.' + status.toLowerCase());
+ elInnerError(this._originInput, Language.get('wcf.map.route.error.' + status.toLowerCase()));
}
},
file.classList.add('uploadFailed');
- var small = elCreate('small');
- small.classList.add('innerError');
- small.textContent = Language.get('wcf.media.upload.error.' + error.errorType, {
- filename: error.filename
- });
-
var p = elBySelAll('.columnFilename .box48 > div > p', file)[1];
- DomUtil.insertAfter(small, p);
+ elInnerError(p, Language.get('wcf.media.upload.error.' + error.errorType, {
+ filename: error.filename
+ }));
+
elRemove(p);
}
}
var usernameInput = elBySel('input[name=username]', event.currentTarget.closest('.dialogContent'));
if (usernameInput.value === '') {
- var error = DomTraverse.nextByClass(usernameInput, 'innerError');
- if (!error) {
- error = elCreate('small');
- error.className = 'innerError';
- error.innerText = Language.get('wcf.global.form.error.empty');
-
- DomUtil.insertAfter(error, usernameInput);
-
- usernameInput.closest('dl').classList.add('formError');
- }
+ elInnerError(usernameInput, Language.get('wcf.global.form.error.empty'));
+ usernameInput.closest('dl').classList.add('formError');
return;
}
*/
_validate: function() {
// remove all existing error elements
- var errorMessages = elByClass('innerError', this._container);
- while (errorMessages.length) {
- elRemove(errorMessages[0]);
- }
+ elBySelAll('.innerError', this._container, elRemove);
// check if editor contains actual content
if (this._getEditor().utils.isEmpty()) {
* @param {string} message error message
*/
throwError: function(element, message) {
- var error = elCreate('small');
- error.className = 'innerError';
- error.textContent = (message === 'empty' ? Language.get('wcf.global.form.error.empty') : message);
-
- DomUtil.insertAfter(error, element);
+ elInnerError(element, (message === 'empty' ? Language.get('wcf.global.form.error.empty') : message));
},
/**
*/
_validate: function(parameters) {
// remove all existing error elements
- var errorMessages = elByClass('innerError', this._activeElement);
- while (errorMessages.length) {
- elRemove(errorMessages[0]);
- }
+ elBySelAll('.innerError', this._activeElement, elRemove);
var data = {
api: this,
* @param {string} message error message
*/
throwError: function(element, message) {
- var error = elCreate('small');
- error.className = 'innerError';
- error.textContent = message;
-
- DomUtil.insertAfter(error, element);
+ elInnerError(element, message);
},
/**
return true;
}
- var innerError = elBySel('.innerError', this._editorContainer);
- if (innerError === null) {
- innerError = elCreate('small');
- innerError.className = 'innerError';
-
- DomUtil.insertAfter(innerError, editor);
- }
-
//noinspection JSUnresolvedVariable
- innerError.textContent = data.returnValues.errorType;
+ elInnerError(editor, data.returnValues.errorType);
return false;
},
window.jQuery(this._textarea).redactor('code.set', html);
window.jQuery(this._textarea).redactor('WoltLabCaret.endOfEditor');
+ // the error message can appear anywhere in the container, not exclusively after the textarea
var innerError = elBySel('.innerError', this._textarea.parentNode);
if (innerError !== null) elRemove(innerError);
},
this._container.insertBefore(this._fragment.firstChild, this._container.firstChild);
this._value = value;
- var innerError = this._container.nextElementSibling;
- if (innerError && !innerError.classList.contains('innerError')) innerError = null;
-
- if (hasVisibleItems) {
- if (innerError) {
- elRemove(innerError);
- }
- }
- else {
- if (!innerError) {
- innerError = elCreate('small');
- innerError.className = 'innerError';
- innerError.textContent = Language.get('wcf.global.filter.error.noMatches');
- DomUtil.insertAfter(innerError, this._container);
- }
- }
+ elInnerError(this._container, (hasVisibleItems) ? false : Language.get('wcf.global.filter.error.noMatches'));
},
/**
*/
_validate: function(parameters) {
// remove all existing error elements
- var errorMessages = elByClass('innerError', this._activeElement);
- while (errorMessages.length) {
- elRemove(errorMessages[0]);
- }
+ elBySelAll('.innerError', this._activeElement, elRemove);
var data = {
api: this,
* @param {string} message error message
*/
throwError: function(element, message) {
- var error = elCreate('small');
- error.className = 'innerError';
- error.textContent = message;
-
- DomUtil.insertAfter(error, element);
+ elInnerError(element, message);
},
/**
return true;
}
- var innerError = elBySel('.innerError', elementData.messageBodyEditor);
- if (innerError === null) {
- innerError = elCreate('small');
- innerError.className = 'innerError';
-
- DomUtil.insertAfter(innerError, editor);
- }
-
//noinspection JSUnresolvedVariable
- innerError.textContent = data.returnValues.realErrorMessage;
+ elInnerError(editor, data.returnValues.realErrorMessage);
return false;
},
var usernameInput = elBySel('input[name=username]', event.currentTarget.closest('.dialogContent'));
if (usernameInput.value === '') {
- var error = DomTraverse.nextByClass(usernameInput, 'innerError');
- if (!error) {
- error = elCreate('small');
- error.className = 'innerError';
- error.innerText = Language.get('wcf.global.form.error.empty');
-
- DomUtil.insertAfter(error, usernameInput);
-
- usernameInput.closest('dl').classList.add('formError');
- }
+ elInnerError(usernameInput, Language.get('wcf.global.form.error.empty'));
+ usernameInput.closest('dl').classList.add('formError');
return;
}
*/
_validate: function() {
// remove all existing error elements
- var errorMessages = elByClass('innerError', this._container);
- while (errorMessages.length) {
- elRemove(errorMessages[0]);
- }
+ elBySelAll('.innerError', this._container, elRemove);
// check if editor contains actual content
if (this._getEditor().utils.isEmpty()) {
* @param {string} message error message
*/
throwError: function(element, message) {
- var error = elCreate('small');
- error.className = 'innerError';
- error.textContent = (message === 'empty' ? Language.get('wcf.global.form.error.empty') : message);
-
- DomUtil.insertAfter(error, element);
+ elInnerError(element, (message === 'empty' ? Language.get('wcf.global.form.error.empty') : message));
},
/**
event.preventDefault();
var inputContainer = _searchInput.parentNode;
- var innerError = inputContainer.nextSibling;
- if (innerError && innerError.nodeName === 'SMALL') elRemove(innerError);
var value = _searchInput.value.trim();
if (value.length < 3) {
- innerError = elCreate('small');
- innerError.className = 'innerError';
- innerError.textContent = Language.get('wcf.page.search.error.tooShort');
- DomUtil.insertAfter(innerError, inputContainer);
+ elInnerError(inputContainer, Language.get('wcf.page.search.error.tooShort'));
return;
}
+ else {
+ elInnerError(inputContainer, false);
+ }
Ajax.api(this, {
parameters: {
}).bind(this));
}
else {
- var innerError = elCreate('small');
- innerError.className = 'innerError';
- innerError.textContent = Language.get('wcf.page.search.error.noResults');
- DomUtil.insertAfter(innerError, _searchInput.parentNode);
+ elInnerError(_searchInput.parentNode, Language.get('wcf.page.search.error.noResults'));
}
},
// no matches
if (!Array.isArray(data.returnValues) || data.returnValues.length === 0) {
- var innerError = elCreate('small');
- innerError.className = 'innerError';
- innerError.textContent = Language.get('wcf.page.pageObjectID.search.noResults');
- DomUtil.insertAfter(innerError, _searchInput);
+ elInnerError(_searchInput, Language.get('wcf.page.pageObjectID.search.noResults'));
return;
}
* @protected
*/
_resetList: function() {
- var innerError = _searchInput.nextElementSibling;
- if (innerError && innerError.classList.contains('innerError')) elRemove(innerError);
+ elInnerError(_searchInput, false);
_resultList.innerHTML = '';
}
else {
var url = elById('redactor-link-url');
- var small = (url.nextElementSibling && url.nextElementSibling.nodeName === 'SMALL') ? url.nextElementSibling : null;
-
- if (small === null) {
- small = elCreate('small');
- small.className = 'innerError';
- url.parentNode.appendChild(small);
- }
-
- small.textContent = Language.get((url.value.trim() === '' ? 'wcf.global.form.error.empty' : 'wcf.editor.link.error.invalid'));
+ elInnerError(url, Language.get((url.value.trim() === '' ? 'wcf.global.form.error.empty' : 'wcf.editor.link.error.invalid')));
}
},
var id = 'redactor-quote-' + this._elementId;
var urlInput = elById(id + '-url');
- var innerError = elBySel('.innerError', urlInput.parentNode);
- if (innerError !== null) elRemove(innerError);
var url = urlInput.value.replace(/\u200B/g, '').trim();
// simple test to check if it at least looks like it could be a valid url
if (url.length && !/^https?:\/\/[^\/]+/.test(url)) {
- innerError = elCreate('small');
- innerError.className = 'innerError';
- innerError.textContent = Language.get('wcf.editor.quote.url.error.invalid');
- urlInput.parentNode.insertBefore(innerError, urlInput.nextElementSibling);
+ elInnerError(urlInput, Language.get('wcf.editor.quote.url.error.invalid'));
return;
}
+ else {
+ elInnerError(urlInput, false);
+ }
// set author
elData(this._quote, 'author', elById(id + '-author').value);
event.preventDefault();
var label = elById('wcfUiUserEditorExpiresLabel');
- var innerError = label.previousElementSibling;
- if (innerError.classList.contains('innerError')) elRemove(innerError);
var expires = '';
+ var errorMessage = '';
if (!elById('wcfUiUserEditorNeverExpires').checked) {
expires = elById('wcfUiUserEditorExpiresDatePicker').value;
if (expires === '') {
- innerError = elCreate('small');
- innerError.className = 'innerError';
- innerError.textContent = Language.get('wcf.global.form.error.empty');
- label.parentNode.insertBefore(innerError, label);
+ errorMessage = Language.get('wcf.global.form.error.empty');
}
}
+ elInnerError(label, errorMessage);
+
var parameters = {};
parameters[_actionName + 'Expires'] = expires;
parameters[_actionName + 'Reason'] = elById('wcfUiUserEditorReason').value.trim();
element.style.setProperty('display', 'none', '');
};
+ /**
+ * Displays or removes an error message below the provided element.
+ *
+ * @param {Element} element DOM element
+ * @param {string?} errorMessage error message; `false`, `null` and `undefined` are treated as an empty string
+ * @param {boolean?} isHtml defaults to false, causes `errorMessage` to be treated as text only
+ * @return {?Element} the inner error element or null if it was removed
+ */
+ window.elInnerError = function (element, errorMessage, isHtml) {
+ var parent = element.parentNode;
+ if (parent === null) {
+ throw new Error('Only elements that have a parent element or document are valid.');
+ }
+
+ if (typeof errorMessage !== 'string') {
+ if (errorMessage === undefined || errorMessage === null || errorMessage === false) {
+ errorMessage = '';
+ }
+ else {
+ throw new TypeError('The error message must be a string; `false`, `null` or `undefined` can be used as a substitute for an empty string.');
+ }
+ }
+
+ var innerError = element.nextElementSibling;
+ if (innerError === null || innerError.nodeName !== 'SMALL' || !innerError.classList.contains('innerError')) {
+ if (errorMessage === '') {
+ innerError = null;
+ }
+ else {
+ innerError = elCreate('small');
+ innerError.className = 'innerError';
+ parent.insertBefore(innerError, element.nextSibling);
+ }
+ }
+
+ if (errorMessage === '') {
+ if (innerError !== null) {
+ parent.removeChild(innerError);
+ innerError = null;
+ }
+ }
+ else {
+ innerError[(isHtml ? 'innerHTML' : 'textContent')] = errorMessage;
+ }
+
+ return innerError;
+ };
+
/**
* Shorthand function to remove an element.
*