-
Notifications
You must be signed in to change notification settings - Fork 379
/
dataSelectors.js
310 lines (258 loc) · 9.58 KB
/
dataSelectors.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import Immutable from 'immutable';
import { createSelector, createSelectorCreator, defaultMemoize } from 'reselect';
import _ from 'lodash';
const createDeepEqualSelector = createSelectorCreator(
defaultMemoize,
_.isEqual,
)
import MAX_SAFE_INTEGER from 'max-safe-integer'
//import { createSelector } from 'reselect';
/** Gets the full dataset currently tracked by Griddle */
export const dataSelector = state => state.get('data');
export const dataLoadingSelector = createSelector(dataSelector, data => !data);
/** Gets the page size */
export const pageSizeSelector = state => state.getIn(['pageProperties', 'pageSize']);
/** Gets the current page */
export const currentPageSelector = state => state.getIn(['pageProperties', 'currentPage']);
/** Gets the record count */
export const recordCountSelector = state => state.getIn(['pageProperties', 'recordCount']);
/** Gets the render properties */
export const renderPropertiesSelector = state => (state.get('renderProperties'));
/** Determines if there are previous pages */
export const hasPreviousSelector = createSelector(
currentPageSelector,
(currentPage) => (currentPage > 1)
);
/** Gets the max page size
*/
export const maxPageSelector = createSelector(
pageSizeSelector,
recordCountSelector,
(pageSize, recordCount) => {
const calc = recordCount / pageSize;
const result = calc > Math.floor(calc) ? Math.floor(calc) + 1 : Math.floor(calc);
return _.isFinite(result) ? result : 1;
}
);
/** Determines if there are more pages available. Assumes pageProperties.maxPage is set by the container */
export const hasNextSelector = createSelector(
currentPageSelector,
maxPageSelector,
(currentPage, maxPage) => {
return currentPage < maxPage;
}
);
/** Gets current filter */
export const filterSelector = state => state.get('filter') || '';
/** Gets the current sortColumns */
export const sortColumnsSelector = state => state.get('sortColumns') || [];
/** Gets all the columns */
export const allColumnsSelector = createSelector(
dataSelector,
renderPropertiesSelector,
(data, renderProperties) => {
const dataColumns = !data || data.size === 0 ?
[] :
data.get(0).keySeq().toJSON();
const columnPropertyColumns = (renderProperties && renderProperties.size > 0) ?
// TODO: Make this not so ugly
Object.keys(renderProperties.get('columnProperties').toJSON()) :
[];
return _.union(dataColumns, columnPropertyColumns);
}
);
/** Gets the column properties objects sorted by order
*/
export const sortedColumnPropertiesSelector = createSelector(
renderPropertiesSelector,
(renderProperties) => (
renderProperties && renderProperties.get('columnProperties') && renderProperties.get('columnProperties').size !== 0 ?
renderProperties.get('columnProperties')
.sortBy(col => (col && col.get('order'))||MAX_SAFE_INTEGER) :
null
)
);
/** Gets metadata column ids
*/
export const metaDataColumnsSelector = createSelector(
sortedColumnPropertiesSelector,
(sortedColumnProperties) => (
sortedColumnProperties ? sortedColumnProperties
.filter(c => c.get('isMetadata'))
.keySeq()
.toJSON() :
[]
)
);
/** Gets the visible columns either obtaining the sorted column properties or all columns
*/
export const visibleColumnsSelector = createSelector(
sortedColumnPropertiesSelector,
allColumnsSelector,
(sortedColumnProperties, allColumns) => (
sortedColumnProperties ? sortedColumnProperties
.filter(c => {
const isVisible = c.get('visible') || c.get('visible') === undefined;
const isMetadata = c.get('isMetadata');
return isVisible && !isMetadata;
})
.keySeq()
.toJSON() :
allColumns
)
);
/** TODO: add tests and docs
*/
export const visibleColumnPropertiesSelector = createSelector(
visibleColumnsSelector,
renderPropertiesSelector,
(visibleColumns=[], renderProperties) => (
visibleColumns.map(c => {
const columnProperty = renderProperties.getIn(['columnProperties', c]);
return (columnProperty && columnProperty.toJSON()) || { id: c }
})
)
)
/** Gets the possible columns that are currently hidden */
export const hiddenColumnsSelector = createSelector(
visibleColumnsSelector,
allColumnsSelector,
metaDataColumnsSelector,
(visibleColumns, allColumns, metaDataColumns) => {
const removeColumns = [...visibleColumns, ...metaDataColumns];
return allColumns.filter(c => removeColumns.indexOf(c) === -1);
}
);
/** TODO: add tests and docs
*/
export const hiddenColumnPropertiesSelector = createSelector(
hiddenColumnsSelector,
renderPropertiesSelector,
(hiddenColumns=[], renderProperties) => (
hiddenColumns.map(c => {
const columnProperty = renderProperties.getIn(['columnProperties', c]);
return (columnProperty && columnProperty.toJSON()) || { id: c }
})
)
)
/** Gets the sort property for a given column */
export const sortPropertyByIdSelector = (state, { columnId }) => {
const sortProperties = state.get('sortProperties');
const individualProperty = sortProperties && sortProperties.size > 0 && sortProperties.find(r => r.get('id') === columnId);
return (individualProperty && individualProperty.toJSON()) || null;
}
/** Gets the icons property from styles */
export const iconByNameSelector = (state, { name }) => {
return state.getIn(['styleConfig', 'icons', name]);
}
/** Gets the icons for a component */
export const iconsForComponentSelector = (state, componentName) => {
const icons = state.getIn(['styleConfig', 'icons', componentName]);
return icons && icons.toJS ? icons.toJS() : icons;
}
/** Gets a style for a component */
export const stylesForComponentSelector = (state, componentName) => {
const style = state.getIn(['styleConfig', 'styles', componentName]);
return style && style.toJS ? style.toJS() : style;
}
/** Gets a classname for a component */
export const classNamesForComponentSelector = (state, componentName) => {
const classNames = state.getIn(['styleConfig', 'classNames', componentName]);
return classNames && classNames.toJS ? classNames.toJS() : classNames;
}
/** Gets a custom component for a given column
* TODO: Needs tests
*/
export const customComponentSelector = (state, { columnId }) => {
return state.getIn(['renderProperties', 'columnProperties', columnId, 'customComponent']);
}
/** Gets a custom heading component for a given column
* TODO: Needs tests
*/
export const customHeadingComponentSelector = (state, { columnId}) => {
return state.getIn(['renderProperties', 'columnProperties', columnId, 'customHeadingComponent']);
}
export const isSettingsEnabledSelector = (state) => {
const enableSettings = state.get('enableSettings');
return enableSettings === undefined ? true : enableSettings;
}
export const isSettingsVisibleSelector = (state) => state.get('showSettings');
export const textSelector = (state, { key}) => {
return state.getIn(['textProperties', key]);
}
/** Gets the column ids for the visible columns
*/
export const columnIdsSelector = createSelector(
renderPropertiesSelector,
visibleColumnsSelector,
(renderProperties, visibleColumns) => {
const offset = 1000;
// TODO: Make this better -- This is pretty inefficient
return visibleColumns
.map((k, index) => ({
id: renderProperties.getIn(['columnProperties', k, 'id']) || k,
order: renderProperties.getIn(['columnProperties', k, 'order']) || offset + index
}))
.sort((first, second) => first.order - second.order)
.map(item => item.id);
}
);
/** Gets the column titles for the visible columns
*/
export const columnTitlesSelector = createSelector(
columnIdsSelector,
renderPropertiesSelector,
(columnIds, renderProperties) => columnIds.map(k => renderProperties.getIn(['columnProperties', k, 'title']) || k)
);
/** Gets the griddleIds for the visible rows */
export const visibleRowIdsSelector = createSelector(
dataSelector,
currentPageData => currentPageData ? currentPageData.map(c => c.get('griddleKey')) : new Immutable.List()
);
/** Gets the count of visible rows */
export const visibleRowCountSelector = createSelector(
visibleRowIdsSelector,
(visibleRowIds) => visibleRowIds.size
);
// TODO: Needs tests and jsdoc
export const cellValueSelector = (state, props) => {
const { griddleKey, columnId } = props;
const cellProperties = cellPropertiesSelector(state, props);
//TODO: Make Griddle key a string in data utils
const lookup = state.getIn(['lookup', griddleKey.toString()]);
const value = state
.get('data').get(lookup)
.getIn(columnId.split('.'));
const type = !!cellProperties ? cellProperties.type : 'string';
switch (type) {
case 'date':
return value.toLocaleDateString();
case 'string':
default:
return value;
}
};
// TODO: Needs jsdoc
export const rowDataSelector = (state, { griddleKey }) => {
const rowIndex = state.getIn(['lookup', griddleKey.toString()]);
return state.get('data').get(rowIndex).toJSON();
};
/** Gets the row render properties
*/
export const rowPropertiesSelector = (state) => {
const row = state.getIn(['renderProperties', 'rowProperties']);
return (row && row.toJSON()) || {};
};
/** Gets the column render properties for the specified columnId
*/
export const cellPropertiesSelectorFactory = () => {
const immutableCellPropertiesSelector = (state, { columnId }) => {
const item = state.getIn(['renderProperties', 'columnProperties', columnId]);
return (item && item.toJSON()) || {};
};
return createDeepEqualSelector(
immutableCellPropertiesSelector,
item => item,
);
};
export const cellPropertiesSelector = cellPropertiesSelectorFactory();