-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathgetChildren.ts
207 lines (184 loc) · 9.38 KB
/
getChildren.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import _ from 'lodash'
import ComparatorFunction from '../@types/ComparatorFunction'
import Path from '../@types/Path'
import SimplePath from '../@types/SimplePath'
import State from '../@types/State'
import Thought from '../@types/Thought'
import ThoughtContext from '../@types/ThoughtContext'
import ThoughtId from '../@types/ThoughtId'
import getSortPreference from '../selectors/getSortPreference'
import appendToPath from '../util/appendToPath'
import compareByRank from '../util/compareByRank'
import {
compareThought,
compareThoughtByCreated,
compareThoughtByUpdated,
compareThoughtDescending,
} from '../util/compareThought'
import head from '../util/head'
import isAbsolute from '../util/isAbsolute'
import isAttribute from '../util/isAttribute'
import isDescendantPath from '../util/isDescendantPath'
import sort from '../util/sort'
import unroot from '../util/unroot'
import childIdsToThoughts from './childIdsToThoughts'
import getThoughtById from './getThoughtById'
// use global instance of empty array so object reference doesn't change
const NO_CHILDREN: Thought[] = []
const NO_THOUGHT_IDS: ThoughtId[] = []
/** A selector that retrieves thoughts from a context and performs other functions like sorting or filtering. */
type GetThoughtsSelector = (state: State, id: ThoughtId) => Thought[]
/** Returns true if the child is not hidden due to being a function or having the =hidden attribute. */
export const isVisible = _.curry((state: State, child: Thought): boolean => {
// temporarily disable =hidden for performance
return !isAttribute(child.value) // && !findDescendant(state, child.id, '=hidden')
})
/** Returns the thoughts for the given thought id. If the children have not changed, returns the same object reference. If given null, returns an empty array. */
export const getAllChildren = (state: State, thoughtId: ThoughtId | null): ThoughtId[] => {
if (!thoughtId) return NO_THOUGHT_IDS
const childrenMap = getThoughtById(state, thoughtId)?.childrenMap
const children = childrenMap ? Object.values(childrenMap) : []
return children?.length > 0 ? children : NO_THOUGHT_IDS
}
/** Returns the subthoughts (as Thoughts) of the given ThoughtId unordered. May return a partial or empty list if any thoughts are missing. */
export const getAllChildrenAsThoughts = (state: State, id: ThoughtId | null): Thought[] => {
const children = childIdsToThoughts(state, getAllChildren(state, id))
return children.length === 0 ? NO_CHILDREN : children
}
/** Makes a function that only returns visible thoughts. */
const getVisibleThoughtsById = _.curry(
(getThoughtsFunction: GetThoughtsSelector, state: State, id: ThoughtId): Thought[] => {
const children = getThoughtsFunction(state, id)
return state.showHiddenThoughts ? children : children.filter(isVisible(state))
},
)
/** Gets all visible children of an id, unordered. */
export const getChildren = getVisibleThoughtsById(getAllChildrenAsThoughts)
/** Gets a list of all children of a context sorted by the given comparator function. */
const getChildrenSortedBy = (state: State, id: ThoughtId, compare: ComparatorFunction<Thought>): Thought[] =>
sort(getAllChildrenAsThoughts(state, id), compare)
/** Generates children sorted by their values. Sorts empty thoughts to their point of creation. */
const getChildrenSortedAlphabetical = (state: State, id: ThoughtId): Thought[] => {
const comparatorFunction =
getSortPreference(state, id).direction === 'Desc' ? compareThoughtDescending : compareThought
return getChildrenSortedBy(state, id, comparatorFunction)
}
/** Generates children sorted by their creation date. */
const getChildrenSortedCreated = (state: State, id: ThoughtId): Thought[] => {
const sortPreference = getSortPreference(state, id)
const sorted = getChildrenSortedBy(state, id, (a, b) =>
sortPreference.direction === 'Desc' ? compareThoughtByCreated(b, a) : compareThoughtByCreated(a, b),
)
return sorted
}
/** Generates children sorted by their last updated date. */
const getChildrenSortedUpdated = (state: State, id: ThoughtId): Thought[] => {
const sortPreference = getSortPreference(state, id)
const sorted = getChildrenSortedBy(state, id, (a, b) =>
sortPreference.direction === 'Desc' ? compareThoughtByUpdated(b, a) : compareThoughtByUpdated(a, b),
)
return sorted
}
/** Finds any child that matches the predicate. If there is more than one child that matches the predicate, which one is returned is non-deterministic. */
export const findAnyChild = (
state: State,
id: ThoughtId,
predicate: (child: Thought) => boolean,
): Thought | undefined => {
const childId = getAllChildren(state, id).find(childId => {
const child = getThoughtById(state, childId)
return child && predicate(child)
})
return childId ? getThoughtById(state, childId) : undefined
}
/** Returns true if the context has any visible children. */
export const hasChildren = (state: State, id: ThoughtId): boolean =>
!!findAnyChild(state, id, child => state.showHiddenThoughts || isVisible(state, child))
/** Gets all children of a thought sorted by rank. Returns a new object reference even if the children have not changed. */
export const getChildrenRanked = (state: State, thoughtId: ThoughtId | null): Thought[] => {
const allChildren = childIdsToThoughts(state, getAllChildren(state, thoughtId))
return sort(allChildren, compareByRank)
}
/** Returns any child of a thought. Only use on a thought with a single child. Also see: firstVisibleChild. */
export const anyChild = (state: State, id: ThoughtId | undefined | null): Thought | undefined => {
if (!id) return undefined
const children = getAllChildren(state, id)
return children.length > 0 ? getThoughtById(state, children[0]) : undefined
}
/** Returns all child that match the predicate (unordered). */
export const filterAllChildren = (state: State, id: ThoughtId, predicate: (child: Thought) => boolean): Thought[] => {
const childIds = getAllChildren(state, id).filter(childId => {
const child = getThoughtById(state, childId)
return child && predicate(child)
})
return childIdsToThoughts(state, childIds)
}
/** Checks if a child lies within the cursor path. */
const isChildInCursor = (state: State, path: Path, child: Thought): boolean => {
const childPath = unroot([...path, child.id])
return !!state.cursor && state.cursor[childPath.length - 1] === child.id
}
/** Check if the cursor is a meta attribute && the given Path is the descendant of the cursor. */
const isDescendantOfMetaCursor = (state: State, path: Path): boolean => {
if (!state.cursor) return false
const thought = getThoughtById(state, head(state.cursor))
if (!thought) return false
const { value: cursorValue } = thought
return isAttribute(cursorValue) && isDescendantPath(path, state.cursor)
}
/** Checks if the child is visible or if the child lies within the cursor or is descendant of the meta cursor. */
const isChildVisibleWithCursorCheck = _.curry(
(state: State, path: SimplePath, thought: Thought): boolean =>
state.showHiddenThoughts ||
isVisible(state, thought) ||
isChildInCursor(state, path, thought) ||
isDescendantOfMetaCursor(state, appendToPath(path, thought.id)),
3,
)
/** Checks if the child is created after latest absolute context toggle. */
const isCreatedAfterAbsoluteToggle = _.curry((state: State, child: ThoughtId | ThoughtContext): boolean => {
const thought = getThoughtById(state, child)
return (
!!thought && !!thought.lastUpdated && !!state.absoluteContextTime && thought.lastUpdated > state.absoluteContextTime
)
})
/**
* Children filter predicate used for rendering.
*
* 1. Checks if the child is visible.
* 2. Checks if child is within cursor.
* 3. Checks if child is created after latest absolute context toggle if starting context is absolute.
*/
export const childrenFilterPredicate = _.curry((state: State, parentPath: SimplePath, child: Thought): boolean => {
return (
isChildVisibleWithCursorCheck(state, parentPath, child) &&
(!isAbsolute(state.rootContext) || isCreatedAfterAbsoluteToggle(state, child.id))
)
}, 3)
/** Gets all children of a Context sorted by rank or sort preference. */
export const getAllChildrenSorted = (state: State, id: ThoughtId): Thought[] => {
const sortPreference = getSortPreference(state, id)
if (sortPreference.type === 'Alphabetical') {
return getChildrenSortedAlphabetical(state, id)
} else if (sortPreference.type === 'Created') {
return getChildrenSortedCreated(state, id)
} else if (sortPreference.type === 'Updated') {
return getChildrenSortedUpdated(state, id)
} else {
return getChildrenRanked(state, id)
}
}
/** Gets all visible children of a thought sorted by rank or sort preference.
* Note: It doesn't check if thought lies within the cursor path or is descendant of meta cursor.
*/
export const getChildrenSorted = (state: State, id: ThoughtId | null): Thought[] => {
return id ? getVisibleThoughtsById(getAllChildrenSorted, state, id) : NO_CHILDREN
}
/** Returns the first visible child of a sorted context. */
export const firstVisibleChild = (state: State, id: ThoughtId): Thought | undefined => getChildrenSorted(state, id)[0]
/** Returns the first visible child (with cursor check) of a context. */
export const firstVisibleChildWithCursorCheck = (state: State, path: SimplePath) => {
const children = getAllChildrenSorted(state, head(path))
return (state.showHiddenThoughts ? children : children.filter(isChildVisibleWithCursorCheck(state, path)))[0]
}
export default getChildren