-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTagNodeView.js
More file actions
222 lines (197 loc) · 5.91 KB
/
Copy pathTagNodeView.js
File metadata and controls
222 lines (197 loc) · 5.91 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import React, { useState } from 'react';
import { LayoutAnimation } from 'react-native';
import {
StyleSheet,
View
} from 'react-native';
import PropTypes from 'prop-types';
// Paper
import {
Surface,
Paragraph,
Title,
TouchableRipple,
IconButton,
Portal,
Dialog,
Button,
withTheme,
} from 'react-native-paper';
'use strict';
const styles = StyleSheet.create({
container: {
borderRadius: 8,
borderWidth: 1,
borderColor: '#ddd',
},
titleBar: {
width: '100%',
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
paddingRight: 8,
paddingLeft: 8,
},
titleBarText: {
flex: 1.0,
color: 'black', // Paper themeing doesn't separate the text styles.
},
titleBarExpand: {
fontSize: 24,
},
tagsText: {
margin: 8,
},
utilityButton: {
width: 32,
height: 32,
margin: 0,
}
});
// TagNodeView acts as a tree-view visual node, showing each node's
// data, and a list of children underneath, each of which can be
// expanded, edited, and selected.
// TODO: Serialization
const TagNodeView = (props) => {
const [expanded, setExpanded] = useState(false);
const [deleteNodeDialogVisible, setDeleteNodeDialogVisible] = useState(false);
const path = Array.prototype.concat(props.parentPath, props.nodeData.path);
showEditModal = () => {
props.nagivation.push("Edit", {})
}
getContentView = () => {
if (props.nodeData.children && expanded) {
return (
<View style={{ flexDirection: 'column', flex: 1.0, margin: props.style.margin }}>
{
props.nodeData.children.map((nodeData, idx) =>
<TagNodeView
{...props}
key={idx}
nodeData={nodeData}
parentPath={path} />)
}
</View>
);
}
}
const selectedPredicate = (path) => {
const pathStr = path.toString();
return !!props.selectionState.find((x) => x.toString() == pathStr)
}
const childrenSelectedPredicate = (path) => {
const pathStr = path.toString();
return !!props.selectionState.find((x) => x.length > path.length && x.slice(0, path.length).toString() === pathStr)
}
const onEditNode = () => {
props.onEditNode(path);
}
const onExpandPress = () => {
LayoutAnimation.easeInEaseOut();
setExpanded(!expanded);
}
const onLongPress = () => {
props.onNodeLongPress(path);
}
const onNodePress = () => {
props.onNodePress(path);
}
const onAddNode = () => {
props.onAddNode(path);
setExpanded(true);
}
const hideDeleteNodeDialog = () => {
setDeleteNodeDialogVisible(false);
}
const onDeleteNode = () => {
hideDeleteNodeDialog();
props.onDeleteNode(path);
}
const onHighPriority = () => {
props.onHighPriority(path);
}
const getUtilityButtons = (selected) => {
switch (props.mode) {
case 'selection': {
const color = props.nodeData.highPriority ? 'red' : null;
return (
selected && <IconButton style={styles.utilityButton} iconColor={color} onPress={onHighPriority} icon="priority-high" />
);
}
case 'edit':
return ([
<IconButton style={styles.utilityButton} key="4" onPress={() => setDeleteNodeDialogVisible(true)} icon="delete-sweep" />,
<IconButton style={styles.utilityButton} key="5" onPress={onAddNode} icon="plus" />,
]);
default:
return ([
<IconButton style={styles.utilityButton} key="6" onPress={onEditNode} icon="pencil" />,
]);
};
}
const hasData = props.nodeData.data && props.nodeData.data.length > 0;
const tagsText = hasData && props.nodeData.data.map((x) => "#" + x).join(" ");
const selected = selectedPredicate(path);
const childrenSelected = childrenSelectedPredicate(path);
const selectedStyle = selected ? props.selectedStyle : null;
const expandable = props.nodeData.children && props.nodeData.children.length > 0;
let defaultAction = () => { };
if (props.mode == 'selection') {
defaultAction = onNodePress;
} else if (expandable) {
defaultAction = onExpandPress
}
return (
<Surface style={[styles.container, props.style, selectedStyle]}>
<TouchableRipple
borderless
onPress={defaultAction}
onLongPress={onLongPress}>
<View>
<Portal>
<Dialog
visible={deleteNodeDialogVisible}
onDismiss={hideDeleteNodeDialog}>
<Dialog.Title>Warning</Dialog.Title>
<Dialog.Content>
<Paragraph>Are you sure you want to delete {props.nodeData.title}?</Paragraph>
</Dialog.Content>
<Dialog.Actions>
<Button onPress={hideDeleteNodeDialog}>Cancel</Button>
<Button onPress={onDeleteNode}>Delete</Button>
</Dialog.Actions>
</Dialog>
</Portal>
<View style={styles.titleBar}>
{
expandable &&
<IconButton
disabled={childrenSelected}
style={styles.utilityButton}
onPress={onExpandPress}
icon={expanded ? "chevron-down" : "chevron-right"} />
}
<Title style={[styles.titleBarText, { opacity: props.nodeData.title ? 1.0 : 0.5 }]}>
{props.nodeData.title || "Untitled"}
{
expandable &&
<Title style={{ fontSize: 12, color: 'lightgray' }} opacity={0.2}> +{props.nodeData.children.length}</Title>
}
</Title>
{getUtilityButtons(selected)}
</View>
{hasData && <Paragraph style={styles.tagsText}>{tagsText}</Paragraph>}
<View>
{
getContentView(expanded)
}
</View>
</View>
</TouchableRipple>
</Surface>
);
}
TagNodeView.propTypes = {
selectedColor: PropTypes.string,
};
export default withTheme(TagNodeView);