Limit search results to max 1000 matches

This commit is contained in:
jos 2018-05-21 20:35:49 +02:00
parent 916c657e28
commit d9babab497
3 changed files with 31 additions and 14 deletions

View File

@ -8,6 +8,7 @@ https://github.com/josdejong/jsoneditor
- Better handling of large JSON documents: - Better handling of large JSON documents:
- Only displays the first 100 items of large arrays, with buttons - Only displays the first 100 items of large arrays, with buttons
"show more" and "show all" to render more items. "show more" and "show all" to render more items.
- Search results are now limited to max 1000 matches.
- Search does no longer expand the paths to all matches, instead - Search does no longer expand the paths to all matches, instead
it only expands the path of the current search result. it only expands the path of the current search result.
- Fixed index numbers of Array items not being updated after sorting. - Fixed index numbers of Array items not being updated after sorting.

View File

@ -40,6 +40,9 @@ function Node (editor, params) {
// debounce interval for keyboard input in milliseconds // debounce interval for keyboard input in milliseconds
Node.prototype.DEBOUNCE_INTERVAL = 150; Node.prototype.DEBOUNCE_INTERVAL = 150;
// search will stop iterating as soon as the max is reached
Node.prototype.MAX_SEARCH_RESULTS = 999;
// number of visible childs rendered initially in large arrays/objects (with a "show more" button to show more) // number of visible childs rendered initially in large arrays/objects (with a "show more" button to show more)
Node.prototype.MAX_VISIBLE_CHILDS = 100; Node.prototype.MAX_VISIBLE_CHILDS = 100;
@ -826,10 +829,14 @@ Node.prototype.insertAfter = function(node, afterNode) {
* Search in this node * Search in this node
* Searches are case insensitive. * Searches are case insensitive.
* @param {String} text * @param {String} text
* @param {Node[]} [results] Array where search results will be added
* used to count and limit the results whilst iterating
* @return {Node[]} results Array with nodes containing the search text * @return {Node[]} results Array with nodes containing the search text
*/ */
Node.prototype.search = function(text) { Node.prototype.search = function(text, results) {
var results = []; if (!Array.isArray(results)) {
results = [];
}
var index; var index;
var search = text ? text.toLowerCase() : undefined; var search = text ? text.toLowerCase() : undefined;
@ -838,7 +845,7 @@ Node.prototype.search = function(text) {
delete this.searchValue; delete this.searchValue;
// search in field // search in field
if (this.field !== undefined) { if (this.field !== undefined && results.length <= this.MAX_SEARCH_RESULTS) {
var field = String(this.field).toLowerCase(); var field = String(this.field).toLowerCase();
index = field.indexOf(search); index = field.indexOf(search);
if (index !== -1) { if (index !== -1) {
@ -859,16 +866,14 @@ Node.prototype.search = function(text) {
// search the nodes childs // search the nodes childs
if (this.childs) { if (this.childs) {
var childResults = [];
this.childs.forEach(function (child) { this.childs.forEach(function (child) {
childResults = childResults.concat(child.search(text)); child.search(text, results);
}); });
results = results.concat(childResults);
} }
} }
else { else {
// string, auto // string, auto
if (this.value !== undefined ) { if (this.value !== undefined && results.length <= this.MAX_SEARCH_RESULTS) {
var value = String(this.value).toLowerCase(); var value = String(this.value).toLowerCase();
index = value.indexOf(search); index = value.indexOf(search);
if (index !== -1) { if (index !== -1) {
@ -878,10 +883,10 @@ Node.prototype.search = function(text) {
'elem': 'value' 'elem': 'value'
}); });
} }
}
// update dom // update dom
this._updateDomValue(); this._updateDomValue();
}
} }
return results; return results;

View File

@ -230,15 +230,26 @@ SearchBox.prototype._onSearch = function (forceSearch) {
// only search again when changed // only search again when changed
this.lastText = text; this.lastText = text;
this.results = this.editor.search(text); this.results = this.editor.search(text);
var MAX_SEARCH_RESULTS = this.results[0]
? this.results[0].node.MAX_SEARCH_RESULTS
: Infinity;
this._setActiveResult(0, false); this._setActiveResult(0, false);
// display search results // display search results
if (text !== undefined) { if (text !== undefined) {
var resultCount = this.results.length; var resultCount = this.results.length;
switch (resultCount) { if (resultCount === 0) {
case 0: this.dom.results.innerHTML = 'no&nbsp;results'; break; this.dom.results.innerHTML = 'no&nbsp;results';
case 1: this.dom.results.innerHTML = '1&nbsp;result'; break; }
default: this.dom.results.innerHTML = resultCount + '&nbsp;results'; break; else if (resultCount === 1) {
this.dom.results.innerHTML = '1&nbsp;result';
}
else if (resultCount > MAX_SEARCH_RESULTS) {
this.dom.results.innerHTML = MAX_SEARCH_RESULTS + '+&nbsp;results';
}
else {
this.dom.results.innerHTML = resultCount + '&nbsp;results';
} }
} }
else { else {