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 | 3x 3x 3x 3x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 5x 5x 5x 1x 1x 4x 2x 1x 1x 1x 1x 2x 2x 2x 2x 19x 9x 14x 31x 31x 14x 21x 21x 14x | // @flow import * as React from 'react'; import {Button, Icon, Spin} from 'antd'; import styled from 'styled-components'; import type {HOCProps} from './types'; const RENDER_CHILDREN = 0; const RENDER_COMPONENT = 1; const RENDER_NULL = 2; const ButtonWrapper = styled.div` text-align: right; ` type State = { loading: boolean, loadingTip: string } export default function withRoute(Com: React.ComponentType<*>) { return class ComWithRoute extends React.Component<HOCProps, State> { state = { loading: false, loadingTip: 'loading...' }; deploy = () => { const {refId, deploy} = this.props; this.setState({ loading: true, loadingTip: 'deploying...', }); deploy(refId.getPathArr()[0]) .then(this.success) .catch(this.fail); } reset = () => { const {refId, reset} = this.props; this.setState({ loading: true, loadingTip: 'reseting...', }); reset(refId.getPathArr()[0]) .then(this.success) .catch(this.fail); } success = () => { const {goTo, routes} = this.props; setTimeout(() => { this.setState({ loading: false }, () => { goTo({pathname: routes[0]}); }); }, 400); } fail = () => { this.setState({ loading: false }); } discard = () => { const {goTo, routes, routerParams, reset, refId} = this.props; if (routerParams.operator === 'create') { reset(refId.getPathArr()[0]).then(() => goTo({pathname: routes.join('/')})); } else { reset(refId.getPathArr()[0]).then(() => goTo({pathname: routes.slice(0, -1).join('/')})); } } render() { const {loading, loadingTip} = this.state; let {routes, pattern, path, routerParams, refId, renderChildren, hideButtons, uirouterParams} = this.props; const renderType = getRenderType({ pattern, routes, path, routerParams }); const pathArrLength = refId.getPathArr().length; const routesLength = routes.length; const {operator} = routerParams; let renderKeys = uirouterParams && uirouterParams.updateKeys; // render all Iif (operator === 'create') { renderKeys = uirouterParams && uirouterParams.createKeys; } return <Spin tip={loadingTip} spinning={loading}> { // quick fix for route array's children // need to find a stable way to control route (renderType === RENDER_CHILDREN && pattern === 'array' && (routesLength === pathArrLength || (routesLength + 1 === pathArrLength && operator === 'create'))) && <Button onClick={this.discard} style={{marginBottom: 16}}> <Icon type="arrow-left" /> Back </Button> } { renderType === RENDER_CHILDREN && renderChildren(node => { return { hidden: renderKeys && renderKeys.indexOf(node.keyName) === -1, refId }; }) } { // quick fix for route array's children // need to find a stable way to control route (renderType === RENDER_CHILDREN && pattern === 'array' && !hideButtons && (routesLength === pathArrLength || (routesLength + 1 === pathArrLength && operator === 'create'))) && <ButtonWrapper> <Button style={{marginRight: 16}} type="primary" onClick={this.deploy}>Confirm</Button> <Button onClick={this.reset}>Reset</Button> </ButtonWrapper> } { renderType === RENDER_COMPONENT && <Com {...this.props} /> } </Spin> } } } function getRenderType({ routes, path, pattern, routerParams }) { const paths = genPaths(path, pattern); const pathsLength = paths.length; const routesLength = routes.length; if (routes[0] !== paths[0]) { return RENDER_NULL; } const type = pattern.split('.').slice(-1)[0]; const {operator} = routerParams; if (type === 'object') { Eif (routesLength === pathsLength && isCompleteContain(paths, routes)) { return RENDER_COMPONENT; } if (routesLength < pathsLength) { return RENDER_COMPONENT; } if (routesLength > pathsLength) { return RENDER_CHILDREN; } return RENDER_NULL; } else if (type === 'array') { if (routesLength === pathsLength && isCompleteContain(paths, routes)) { return RENDER_CHILDREN; } Eif (routesLength < pathsLength) { Iif (routesLength === pathsLength - 1 && operator === 'create') { return RENDER_CHILDREN; } return RENDER_COMPONENT; } if (routesLength > pathsLength) { return RENDER_COMPONENT; } return RENDER_NULL; } else { Iif (routesLength === pathsLength && isCompleteContain(paths, routes)) { return RENDER_NULL; } Iif (routesLength < pathsLength) { return RENDER_COMPONENT; } Iif (routesLength > pathsLength) { return RENDER_COMPONENT; } return RENDER_NULL; } } export function isCompleteContain(paths: Array<string>, routes: Array<string>) { return paths.map((key, i) => routes[i] === key || key === '__ARRAY_INDEX__') .reduce((result: any, curr: any) => result && curr); } export function genPaths(path: string, pattern: string): Array<string> { const patterns = pattern.split('.'); const indexs = patterns.map((type, i) => type === 'array' ? i : -1) .filter(index => index !== -1); let paths = path.split('/') .map((route, i) => indexs.indexOf(i) === -1 ? route : [].concat(route, '__ARRAY_INDEX__')) .reduce((result: any, curr: any) => result.concat(curr), []); return paths; } |