Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | 12x 2x 6x 2x 4x 12x 12x 9x 9x 11x 12x 3x 12x 11x 2x 1x 15x 15x 12x 5x 20x 9x 11x 12x 12x 11x 11x 8x 7x 1x 3x 2x 1x 12x 8x 1x 1x 1x 12x 13x 5x 13x 5x 5x 1x 1x 1x 5x 5x 5x 8x 8x 8x 8x 16x | /* eslint react/prop-types: 0 */ import React from 'react'; import * as R from 'ramda'; import { getNodePath } from '../utils'; //--------------------------------------------------------------------------------------------Select export const getCurrentIds = (getValue, value, items) => R.reduce((acc, item) => { if (R.equals(getValue(item), value)) { return [...acc, R.prop('id')(item)]; } return acc; }, [])(items); export const getSelection = ({ items, option, removedValue }) => { const value = R.isNil(option) ? removedValue : option; switch (R.prop('type')(value)) { case 'level': return getCurrentIds(R.prop('level'), R.prop('level')(value), items); case 'allCurrent': return getCurrentIds(R.prop('parentId'), R.prop('parentId')(value), items); default: return []; } }; export const getOptionsSelect = (items, currentItem, translation = []) => { const levelIds = R.pipe(R.indexBy(R.prop('level')), R.keys, R.length)(items); return [ { label: R.head(translation), value: 'allCurrent', type: 'allCurrent', parentId: R.prop('id')(currentItem), }, ...R.times(id => ({ label: `${id}`, value: `level${id}`, level: id, type: 'level' }))(levelIds), ]; }; //-----------------------------------------------------------------------------------------Spotlight const getSpotlightedList = ({ spotlight, term = '', items, itemsById, noPath, hasPath }) => spotlight.engine({ ...spotlight, term, items, itemsById, noPath, hasPath, }); export const getPlaceholder = ({ placeholder = '', mainPlaceholder, secondaryPlaceholder, currentItem, }) => { if (R.and(R.isNil(mainPlaceholder), R.isNil(secondaryPlaceholder))) return placeholder; if (R.isNil(currentItem)) return mainPlaceholder; return secondaryPlaceholder; }; //----------------------------------------------------------------------------------------------List export const withHasChild = itemsByParentId => item => R.assoc('hasChild', R.pipe(R.prop(item.id), R.isNil, R.not)(itemsByParentId))(item); export const getCurrentList = ({ currentItemId, itemsByParentId, items }) => R.reduce((acc, item) => { if (R.equals(currentItemId, item.parentId)) { return [...acc, withHasChild(itemsByParentId)(item)]; } return acc; }, [])(items); export const getMainList = R.filter(R.pipe(R.prop('parentId'), R.isNil)); export const getList = ({ items, itemsByParentId, itemsById, spotlight, currentItem, term, hasPath, }) => { const currentItemId = currentItem ? R.prop('id')(currentItem) : null; if (R.isEmpty(term)) { if (R.isNil(currentItemId)) return R.pipe(getMainList, R.map(item => withHasChild(itemsByParentId)(item)))(items); return getCurrentList({ currentItemId, itemsByParentId, items }); } if (R.isNil(currentItemId)) return getSpotlightedList({ spotlight, term, items, itemsById, hasPath }); return getSpotlightedList({ spotlight, term, items: getCurrentList({ currentItemId, itemsByParentId, items }), noPath: true, hasPath, }); }; //----------------------------------------------------------------------------------------------Node export const getEnhancedNode = (currentItem, items, itemsById, hasPath) => { if (R.isNil(currentItem)) return currentItem; const id = R.prop('id')(currentItem); const item = R.find(R.propEq('id', id))(items); return { ...currentItem, isSelected: R.prop('isSelected')(item), nodePath: hasPath ? R.join(' > ', R.propOr([], 'path')(item)) : getNodePath(currentItem, itemsById), }; }; //-----------------------------------------------------------------------------------------------Hoc export const withScopeList = Component => class extends React.Component { state = { term: R.propOr('', 'term')(this.props), }; static getDerivedStateFromProps = (props, state) => ({ ...state, itemsByParentId: R.indexBy(R.prop('parentId'), props.items), // only use for hasChild, same keys are overide itemsById: R.indexBy(R.prop('id'), props.items), }); changeList = id => this.setState({ currentItem: R.prop(id)(this.state.itemsById), term: '' }); changeSelection = panelId => id => event => { event.preventDefault(); Eif (R.is(Function, this.props.changeSelection)) return this.props.changeSelection(panelId, id); return null; }; changeSelect = panelId => (selected, { action, option, removedValue }) => { const valueIds = getSelection({ items: this.props.items, option, removedValue }); switch (action) { case 'select-option': return this.props.onSelect(panelId, valueIds); case 'remove-value': return this.props.onUnselect(panelId, valueIds); case 'clear': return this.props.onUnselect(panelId, []); default: return null; } }; changeSpotlight = spotlight => this.setState({ term: R.propOr('', 'term')(spotlight) }); render = () => { // "items", "itemsByParentId", "itemsById" are the same collection, // but only "items" is updated by the selection (on hold the selection): { isSelected: true/false } const { items, spotlight, mainPlaceholder, secondaryPlaceholder, activePanelId, id, translationOptions, hasPath, } = this.props; const { currentItem, term, itemsByParentId, itemsById } = this.state; const isActivePanel = R.equals(activePanelId, id); return ( <Component {...this.props} items={items} tagValue={`${R.length(R.keys(R.filter(item => item.isSelected)(items)))}/${R.length( R.keys(items), )}`} list={ isActivePanel ? getList({ items, itemsByParentId, itemsById, spotlight, currentItem, term, hasPath, }) : [] } changeList={this.changeList} changeSelection={this.changeSelection} currentItem={ isActivePanel ? getEnhancedNode(currentItem, items, itemsById, hasPath) : null } changeSpotlight={this.changeSpotlight} term={term} placeholder={getPlaceholder({ ...spotlight, mainPlaceholder, secondaryPlaceholder, currentItem, })} changeSelect={this.changeSelect} options={getOptionsSelect(items, currentItem, translationOptions)} /> ); }; }; |