Move `doSearch` to `search.js`
This commit is contained in:
parent
35983df136
commit
be87b1e4cd
|
@ -57,7 +57,7 @@
|
|||
top: 100%;
|
||||
right: $search-box-offset + 20px; // keep space for scrollbar
|
||||
margin-top: $search-box-offset;
|
||||
z-index: 1;
|
||||
z-index: 2;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -246,17 +246,16 @@
|
|||
}
|
||||
}
|
||||
|
||||
function doSearch(doc, searchText) {
|
||||
return search(null, doc, searchText)
|
||||
}
|
||||
|
||||
// TODO: refactor the search solution and move it in a separate component
|
||||
// in: doc, searchText, activeSearchResultIndex
|
||||
// out: searchResultWithActive
|
||||
// callbacks: change searchText, change doc, change activeSearchResultIndex
|
||||
let searchResult
|
||||
let activeSearchResult = undefined
|
||||
let activeSearchResultIndex
|
||||
let flatSearchResult
|
||||
let searchResultWithActive
|
||||
$: searchResult = searchText ? doSearch(doc, searchText) : undefined
|
||||
$: searchResult = searchText ? search(doc, searchText) : undefined
|
||||
$: flatSearchResult = flattenSearch(searchResult)
|
||||
|
||||
$: {
|
||||
|
|
|
@ -2,30 +2,34 @@ import { isNumber } from 'lodash-es'
|
|||
import { STATE_SEARCH_PROPERTY, STATE_SEARCH_VALUE } from '../constants.js'
|
||||
import { valueType } from './typeUtils.js'
|
||||
|
||||
export function search (key, value, searchText) {
|
||||
export function search (doc, searchText) {
|
||||
return searchRecursive(null, doc, searchText)
|
||||
}
|
||||
|
||||
function searchRecursive (key, doc, searchText) {
|
||||
let results = undefined
|
||||
|
||||
if (typeof key === 'string' && containsCaseInsensitive(key, searchText)) {
|
||||
results = createOrAdd(results, STATE_SEARCH_PROPERTY, 'search')
|
||||
}
|
||||
|
||||
const type = valueType(value)
|
||||
const type = valueType(doc)
|
||||
if (type === 'array') {
|
||||
value.forEach((item, index) => {
|
||||
let childResults = search(index, item, searchText)
|
||||
doc.forEach((item, index) => {
|
||||
let childResults = searchRecursive(index, item, searchText)
|
||||
if (childResults) {
|
||||
results = createOrAdd(results, index, childResults)
|
||||
}
|
||||
})
|
||||
} else if (type === 'object') {
|
||||
Object.keys(value).forEach(prop => {
|
||||
let childResults = search(prop, value[prop], searchText)
|
||||
Object.keys(doc).forEach(prop => {
|
||||
let childResults = searchRecursive(prop, doc[prop], searchText)
|
||||
if (childResults) {
|
||||
results = createOrAdd(results, prop, childResults)
|
||||
}
|
||||
})
|
||||
} else { // type is a value
|
||||
if (containsCaseInsensitive(value, searchText)) {
|
||||
if (containsCaseInsensitive(doc, searchText)) {
|
||||
results = createOrAdd(results, STATE_SEARCH_VALUE, 'search')
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue