|
| 1 | +import {DazzlerHtmlProps, UpdateAspectFunc} from './types'; |
| 2 | +import {assoc, omit, pipe, pick} from 'ramda'; |
| 3 | + |
| 4 | +const omittedProps = [ |
| 5 | + 'class_name', |
| 6 | + 'identity', |
| 7 | + 'updateAspects', |
| 8 | + 'handle_clicks', |
| 9 | + 'clicks', |
| 10 | + 'handle_hover', |
| 11 | + 'is_hovered', |
| 12 | + 'handle_load', |
| 13 | + 'is_loaded', |
| 14 | + 'handle_keys', |
| 15 | + 'last_key', |
| 16 | + 'handle_focus', |
| 17 | + 'is_focused', |
| 18 | +]; |
| 19 | + |
| 20 | +const clickKeys = [ |
| 21 | + 'altKey', |
| 22 | + 'button', |
| 23 | + 'buttons', |
| 24 | + 'clientX', |
| 25 | + 'clientY', |
| 26 | + 'ctrlKey', |
| 27 | + 'metaKey', |
| 28 | + 'movementX', |
| 29 | + 'movementY', |
| 30 | + 'offsetX', |
| 31 | + 'offsetY', |
| 32 | + 'pageX', |
| 33 | + 'pageY', |
| 34 | + 'screenX', |
| 35 | + 'screenY', |
| 36 | + 'shiftKey', |
| 37 | + 'x', |
| 38 | + 'y', |
| 39 | +]; |
| 40 | + |
| 41 | +const onClick = |
| 42 | + (clicks: number, updateAspects: UpdateAspectFunc) => (event: MouseEvent) => |
| 43 | + updateAspects({ |
| 44 | + clicks: (clicks || 0) + 1, |
| 45 | + click_event: pick(clickKeys, event), |
| 46 | + }); |
| 47 | + |
| 48 | +const updateFactory = |
| 49 | + <T>(key: string, value: T, updateAspects: UpdateAspectFunc) => |
| 50 | + () => |
| 51 | + updateAspects({[key]: value}); |
| 52 | + |
| 53 | +export function enhanceProps(props: DazzlerHtmlProps) { |
| 54 | + let newProps: any = { |
| 55 | + ...omit(omittedProps, props), |
| 56 | + className: props.class_name, |
| 57 | + id: props.id || props.identity, |
| 58 | + }; |
| 59 | + if (props.handle_clicks) { |
| 60 | + newProps = assoc( |
| 61 | + 'onClick', |
| 62 | + onClick(props.clicks, props.updateAspects), |
| 63 | + newProps |
| 64 | + ); |
| 65 | + } |
| 66 | + if (props.handle_hover) { |
| 67 | + newProps = pipe( |
| 68 | + assoc( |
| 69 | + 'onMouseEnter', |
| 70 | + updateFactory('is_hovered', true, props.updateAspects) |
| 71 | + ), |
| 72 | + assoc( |
| 73 | + 'onMouseLeave', |
| 74 | + updateFactory('is_hovered', false, props.updateAspects) |
| 75 | + ) |
| 76 | + )(newProps); |
| 77 | + } |
| 78 | + if (props.handle_focus) { |
| 79 | + newProps = pipe( |
| 80 | + assoc( |
| 81 | + 'onFocus', |
| 82 | + updateFactory('is_focused', true, props.updateAspects) |
| 83 | + ), |
| 84 | + assoc( |
| 85 | + 'onBlur', |
| 86 | + updateFactory('is_focused', false, props.updateAspects) |
| 87 | + ) |
| 88 | + )(newProps); |
| 89 | + } |
| 90 | + return newProps; |
| 91 | +} |
0 commit comments