Move parse error icon to `ErrorTable` too

This commit is contained in:
jos 2019-07-24 11:20:01 +02:00
parent e82ef3ed31
commit 21b3cca592
2 changed files with 27 additions and 17 deletions

View File

@ -35,6 +35,10 @@ function ErrorTable (config) {
validationErrorCount.className = 'jsoneditor-validation-error-count';
validationErrorCount.style.display = 'none';
this.dom.validationErrorCount = validationErrorCount;
this.dom.parseErrorIndication = document.createElement('span');
this.dom.parseErrorIndication.className = 'jsoneditor-parse-error-icon';
this.dom.parseErrorIndication.style.display = 'none';
}
ErrorTable.prototype.getErrorTable = function () {
@ -45,10 +49,14 @@ ErrorTable.prototype.getErrorCounter = function () {
return this.dom.validationErrorCount;
};
ErrorTable.prototype.getErrorIcon = function () {
ErrorTable.prototype.getWarningIcon = function () {
return this.dom.validationErrorIcon;
};
ErrorTable.prototype.getErrorIcon = function () {
return this.dom.parseErrorIndication;
};
ErrorTable.prototype.toggleTableVisibility = function () {
this.errorTableVisible = !this.errorTableVisible;
this.onToggleVisibility(this.errorTableVisible);
@ -148,6 +156,21 @@ ErrorTable.prototype.setErrors = function (errors, errorLocations) {
this.dom.validationErrorCount.style.display = 'none';
this.dom.validationErrorIcon.style.display = 'none';
}
// update the parse error icon
var hasParseErrors = errors.some(function (error) {
return error.type === 'error'
});
if (hasParseErrors) {
var line = errors[0].line
this.dom.parseErrorIndication.style.display = 'block';
this.dom.parseErrorIndication.title = !isNaN(line)
? ('parse error on line ' + line)
: 'parse error - check that the json is valid';
}
else {
this.dom.parseErrorIndication.style.display = 'none';
}
};
module.exports = ErrorTable;

View File

@ -355,12 +355,8 @@ textmode.create = function (container, options) {
statusBar.appendChild(countLabel);
statusBar.appendChild(this.errorTable.getErrorCounter());
statusBar.appendChild(this.errorTable.getWarningIcon());
statusBar.appendChild(this.errorTable.getErrorIcon());
this.parseErrorIndication = document.createElement('span');
this.parseErrorIndication.className = 'jsoneditor-parse-error-icon';
this.parseErrorIndication.style.display = 'none';
statusBar.appendChild(this.parseErrorIndication);
}
this.setSchema(this.options.schema, this.options.schemaRefs);
@ -796,25 +792,16 @@ textmode.validate = function () {
var json;
try {
json = this.get(); // this can fail when there is no valid json
if (this.parseErrorIndication) {
this.parseErrorIndication.style.display = 'none';
}
doValidate = true;
}
catch (err) {
if (this.getText()) {
if (this.parseErrorIndication) {
this.parseErrorIndication.style.display = 'block';
}
// try to extract the line number from the jsonlint error message
var match = /\w*line\s*(\d+)\w*/g.exec(err.message);
var line;
if (match) {
line = +match[1];
}
if (this.parseErrorIndication) {
this.parseErrorIndication.title = !isNaN(line) ? ('parse error on line ' + line) : 'parse error - check that the json is valid';
}
parseErrors.push({
type: 'error',
message: err.message.replace(/\n/g, '<br>'),
@ -845,7 +832,7 @@ textmode.validate = function () {
.then(function (customValidationErrors) {
// only apply when there was no other validation started whilst resolving async results
if (seq === me.validationSequence) {
var errors = schemaErrors.concat(parseErrors || []).concat(customValidationErrors || []);
var errors = schemaErrors.concat(parseErrors).concat(customValidationErrors || []);
me._renderErrors(errors);
}
})
@ -858,7 +845,7 @@ textmode.validate = function () {
}
}
else {
this._renderErrors(parseErrors || []);
this._renderErrors(parseErrors);
}
};