-
Notifications
You must be signed in to change notification settings - Fork 1k
/
SearchBarAutocompletions.js
47 lines (41 loc) · 1.39 KB
/
SearchBarAutocompletions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import Immutable from 'immutable';
import { FieldTypesStore } from 'enterprise/stores/FieldTypesStore';
import { ViewMetadataStore } from '../../stores/ViewMetadataStore';
const _fieldResult = (field, score = 1) => {
const { name, type } = field;
return {
name: name,
value: name,
score,
meta: type.type,
};
};
const _matchesFieldName = (prefix) => {
return field => (field.name.indexOf(prefix) >= 0);
};
export default class SearchBarAutoCompletions {
constructor() {
this.state = FieldTypesStore.getInitialState();
FieldTypesStore.listen((newState) => {
this.state = newState;
});
this.onViewMetadataStoreUpdate(ViewMetadataStore.getInitialState());
ViewMetadataStore.listen(this.onViewMetadataStoreUpdate);
}
onViewMetadataStoreUpdate = (newState) => {
const { activeQuery } = newState;
this.activeQuery = activeQuery;
};
getCompletions = (editor, session, pos, prefix, callback) => {
const { all, queryFields } = this.state;
const matchesFieldName = _matchesFieldName(prefix);
const currentQueryFields = queryFields.get(this.activeQuery, Immutable.Set());
const results = Immutable.List([
currentQueryFields.filter(matchesFieldName)
.map(field => _fieldResult(field, 2)),
all.filter(matchesFieldName)
.map(field => _fieldResult(field, 1)),
]).flatten().toJS();
callback(null, results);
}
}