UNPKG

@talend/react-containers

Version:

Provide connected components aka containers for @talend/react-cmf based on @talend/react-components.

124 lines (123 loc) 4.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.compare = compare; exports.configureGetFilteredItems = configureGetFilteredItems; exports.configureGetPagedItems = configureGetPagedItems; exports.configureGetPagination = configureGetPagination; exports.configureGetSortedItems = configureGetSortedItems; exports.getCollectionItems = getCollectionItems; exports.getSortedResults = getSortedResults; var _reactCmf = _interopRequireDefault(require("@talend/react-cmf")); var _reselect = require("reselect"); var _immutable = require("immutable"); var _lodash = require("lodash"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function contains(listItem, query, columns) { let item = listItem; if (_immutable.Map.isMap(listItem)) { item = listItem.toJS(); } return columns.some(column => typeof item[column.key] === 'string' && item[column.key].toLowerCase().indexOf(query.toLowerCase()) !== -1); } function getCollection(state, collectionId) { return state.cmf.collections.get(collectionId); } function getCollectionItems(state, collectionId) { const collection = getCollection(state, collectionId); if (_immutable.Map.isMap(collection)) { return collection.get('items'); } return collection; } function configureGetPagination(state, { collectionId }) { const collection = getCollection(state, collectionId); if (_immutable.Map.isMap(collection)) { return collection.get('pagination'); } return null; } function getComponentState(collectionId) { return state => state.cmf.components.getIn(['Container(List)', collectionId || 'default']); } function configureGetFilteredItems(configure) { const localConfig = configure; const getFilteredList = (0, _reselect.createSelector)(getComponentState(localConfig.collectionId), componentState => { let results = localConfig.items; if (componentState) { const searchQuery = componentState.get('searchQuery'); if (searchQuery && results) { results = results.filter(item => contains(item, searchQuery, localConfig.columns)); } } return results; }); return (0, _reselect.createSelector)([getFilteredList, getComponentState], items => items); } function compare(sortBy) { return (a, b) => { let aValue = a.get(sortBy); let bValue = b.get(sortBy); if (typeof aValue === 'string' && typeof bValue === 'string') { aValue = aValue.toLowerCase(); bValue = bValue.toLowerCase(); return aValue.localeCompare(bValue); } if (typeof aValue === 'number' && typeof bValue === 'number') { if (aValue < bValue) { return -1; } if (aValue > bValue) { return 1; } return 0; } if (!b[sortBy]) { return 0; } return -1; }; } function getSortedResults(componentState, config, listItems) { if (!_immutable.List.isList(listItems)) { return new _immutable.List(); } let results = listItems; if (!(0, _lodash.isEmpty)(componentState)) { const sortBy = componentState.get('sortOn'); const sortAsc = componentState.get('sortAsc'); const sortedColumn = (0, _lodash.get)(config, 'columns', []).find(column => column.key === sortBy); if ((0, _lodash.get)(sortedColumn, 'sortFunction')) { // Immutable sort method returns sorted array results = results.sort(_reactCmf.default.registry.getFromRegistry(sortedColumn.sortFunction)(sortBy, sortAsc)); } else { results = results.sort(compare(sortBy)); } if (!sortAsc) { results = results.reverse(); } } return results; } function configureGetSortedItems(config, listItems) { const getSortedList = (0, _reselect.createSelector)(getComponentState(config.collectionId), componentState => getSortedResults(componentState, config, listItems)); return (0, _reselect.createSelector)([getSortedList, getComponentState], items => items); } function configureGetPagedItems(configure, listItems) { const getPagedList = (0, _reselect.createSelector)(getComponentState(configure.collectionId), componentState => { let results = listItems; if (componentState) { const startIndex = componentState.get('startIndex'); const itemsPerPage = componentState.get('itemsPerPage'); if (itemsPerPage > 0 && startIndex > 0) { results = results.slice(startIndex - 1, Math.min(startIndex + itemsPerPage - 1, results.size)); } } return results; }); return (0, _reselect.createSelector)([getPagedList, getComponentState], items => items); } //# sourceMappingURL=selector.js.map