-
-
Notifications
You must be signed in to change notification settings - Fork 391
/
DisplayOptionsToFilters.ts
83 lines (69 loc) · 2.7 KB
/
DisplayOptionsToFilters.ts
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { ContentType } from '@standardnotes/common'
import { DecryptedItem } from '../../Abstract/Item'
import { SNTag } from '../../Syncable/Tag'
import { CompoundPredicate } from '../Predicate/CompoundPredicate'
import { ItemWithTags } from './Search/ItemWithTags'
import { itemMatchesQuery, itemPassesFilters } from './Search/SearchUtilities'
import { ItemFilter, ReferenceLookupCollection, SearchableDecryptedItem } from './Search/Types'
import { FilterDisplayOptions } from './DisplayOptions'
export function computeUnifiedFilterForDisplayOptions(
options: FilterDisplayOptions,
collection: ReferenceLookupCollection,
additionalFilters: ItemFilter[] = [],
): ItemFilter {
const filters = computeFiltersForDisplayOptions(options, collection).concat(additionalFilters)
return (item: SearchableDecryptedItem) => {
return itemPassesFilters(item, filters)
}
}
export function computeFiltersForDisplayOptions(
options: FilterDisplayOptions,
collection: ReferenceLookupCollection,
): ItemFilter[] {
const filters: ItemFilter[] = []
let viewsPredicate: CompoundPredicate<DecryptedItem> | undefined = undefined
if (options.views && options.views.length > 0) {
const compoundPredicate = new CompoundPredicate(
'and',
options.views.map((t) => t.predicate),
)
viewsPredicate = compoundPredicate
filters.push((item) => {
if (compoundPredicate.keypathIncludesString('tags')) {
const noteWithTags = ItemWithTags.Create(
item.payload,
item,
collection.elementsReferencingElement(item, ContentType.Tag) as SNTag[],
)
return compoundPredicate.matchesItem(noteWithTags)
} else {
return compoundPredicate.matchesItem(item)
}
})
}
if (options.tags && options.tags.length > 0) {
for (const tag of options.tags) {
filters.push((item) => tag.isReferencingItem(item))
}
}
if (options.includePinned === false && !viewsPredicate?.keypathIncludesString('pinned')) {
filters.push((item) => !item.pinned)
}
if (options.includeProtected === false && !viewsPredicate?.keypathIncludesString('protected')) {
filters.push((item) => !item.protected)
}
if (options.includeTrashed === false && !viewsPredicate?.keypathIncludesString('trashed')) {
filters.push((item) => !item.trashed)
}
if (options.includeArchived === false && !viewsPredicate?.keypathIncludesString('archived')) {
filters.push((item) => !item.archived)
}
if (options.searchQuery) {
const query = options.searchQuery
filters.push((item) => itemMatchesQuery(item, query, collection))
}
if (!viewsPredicate?.keypathIncludesString('conflict_of')) {
filters.push((item) => !item.conflictOf)
}
return filters
}