-
Notifications
You must be signed in to change notification settings - Fork 319
Expand file tree
/
Copy pathListHelper.js
More file actions
167 lines (140 loc) · 3.75 KB
/
ListHelper.js
File metadata and controls
167 lines (140 loc) · 3.75 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
// helper to handle showing lists of things
// parent must implement methods
// required: getListItem, getItemProps
// optional: isListChange, reloadList
//
// and give props
// required: store, currentRoute
// optional: listProps, segment
var React = require('react-native');
var {
View,
StyleSheet,
ListView
} = React;
var cssVar = require('../Lib/cssVar');
var CurrentUserStore = require('../Stores/CurrentUserStore');
var NavigationListener = require('../Mixins/NavigationListener');
var NavBarHelper = require('../Mixins/NavBarHelper');
var Loading = require('../Screens/Loading');
var Text = require('../Components/Text');
var SegmentedControl = require('../Components/SegmentedControl');
var SimpleList = require('../Components/SimpleList');
var AppActions = require('../Actions/AppActions');
var ListHelper = {
mixins: [NavigationListener, NavBarHelper],
getInitialState: function() {
return this.getListState();
},
getListState: function() {
return {
items: this.getListItems()
};
},
onListChange: function(arg) {
if (!this.isListChange || this.isListChange(arg)) {
this.setState(this.getListState());
}
},
onDidFocusNavigation: function() {
// items may have changed
this.setState(this.getListState());
},
componentDidMount: function() {
this.props.store.addChangeListener(this.onListChange);
if (this.reloadList) {
this.reloadList();
}
},
componentWillUnmount: function() {
this.props.store.removeChangeListener(this.onListChange);
},
getNavBarState: function() {
var title = this.props.username ? this.props.username : "Dashboard";
return { title: title };
},
getUsername: function() {
if (!this.username) {
this.username = this.props.username || CurrentUserStore.get().data.username;
}
return this.username;
},
getParseTitle: function() {
if (this.props.listProps.parse === true) {
return [
{type: 'url', style: styles.url, onPress: (url) => AppActions.launchExternalURL(url) },
{pattern: /@(\w+)/, style: styles.mention, onPress: (mention) => AppActions.launchRelativeItem(this.props.currentRoute, {replacePath: `follows/${mention.substring(1).toLowerCase()}/posts`}) },
{pattern: /#(\w+)/, style: styles.hashtag},
]
}
return null;
},
renderItems: function() {
return (
<SimpleList
style={styles.flex}
currentRoute={this.props.currentRoute}
getItemProps={this.getItemProps}
items={this.state.items}
reloadList={this.reloadList}
parseTitle={this.getParseTitle()}
{...this.props.listProps}
/>
);
},
renderEmpty: function() {
return(
<Text>
No Items
</Text>
);
},
renderHeader: function() {
if (!this.props.segment) return null;
return (
<SegmentedControl currentRoute={this.props.currentRoute} appendTestId={this.getUsername()} {...this.props.segment} />
);
},
renderContent: function() {
var header = this.renderHeader();
var content;
if (this.state.items.length === 0) {
content = this.renderEmpty();
}
else {
content = this.renderItems();
}
return (
<View style={styles.flex}>
{header}
{content}
</View>
);
},
render: function() {
if (!this.state.items) {
// TODO: load error?
return <Loading />;
}
else {
return this.renderContent();
}
}
};
var styles = StyleSheet.create({
flex: {
flex: 1
},
hashtag: {
color: cssVar('blue50'),
fontStyle: 'italic',
},
mention: {
color: cssVar('blue50'),
},
url: {
color: cssVar('blue50'),
textDecorationLine: 'underline',
}
});
module.exports = ListHelper;