index.jsx 9.0 KB
Newer Older
徐立's avatar
徐立 committed
1 2 3 4 5
/**
 * 徐立
 * 2019年12月11日
 */
import React, { Component } from 'react';
钟是志's avatar
钟是志 committed
6
import { Button, Row, Col, Select } from 'antd';
徐立's avatar
徐立 committed
7
import funcList from './functionList';
8
import AceEditor from 'react-ace';
徐立's avatar
徐立 committed
9
import styles from './style.less';
徐立's avatar
徐立 committed
10
import Throttle from 'lodash-decorators/throttle';
钟是志's avatar
钟是志 committed
11
import { getModal } from '@/webPublic/one_stop_public/utils/utils';
徐立's avatar
徐立 committed
12
const { Option } = Select;
钟是志's avatar
钟是志 committed
13
const Modal = getModal();
徐立's avatar
徐立 committed
14
export default class index extends Component {
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 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
	state = {
		visible: false, // 模态框状态
		selectOne: [], // 系列函数初始数组
		selectTwo: [], // 子函数初始数组
		checkedList: [], // 用户选中的函数数据
		checkedFuc: '', // 用户选择的函数
		aceValue: '', // 编辑器输入内容
		callKey: '', // 选择函数调用名
		resultValue: '', // 计算结果
	};
	/**
	 * 展示模态框回调
	 */
	showModal = () => {
		this.setState({
			visible: true,
		});
	};
	/**
	 * 模态框确定函数回调
	 */
	handleOk = (e) => {
		const { checkedFuc, callKey } = this.state;
		this.setState({
			visible: false,
		});
		this.props.setPitchOff(`$.${callKey}()`);
	};
	/**
	 * 模态框取消函数回调
	 */
	handleCancel = (e) => {
		this.setState({
			visible: false,
		});
		this.props.setClosePitchOff();
	};
	/**
	 * 系列函数搜索框change
	 * @param {*} value
	 */
	selectOneChange = (value) => {
		// 筛选出用户选择
		let arr = funcList.filter((item) => item.name == value);
		let arrTwo = []; // 替换子函数搜索
		if (arr.length > 0) {
			arr[0].children.map((item) => {
				arrTwo.push({
					value: item.name,
					key: item.name,
					func: item.function,
					funcName: item.funcName,
					params: item.params,
					demo: item.demo,
					callKey: item.callKey,
				});
			});
			this.setState({
				selectTwo: arrTwo,
			});
		} else {
			const arrTwo = [];
			// 默认用户可查看全部数据
			funcList.map((item) => {
				item.children.map((item) => {
					arrTwo.push({
						funcName: item.funcName,
						value: item.name,
						key: item.name,
						func: item.function,
						params: item.params,
						demo: item.demo,
						callKey: item.callKey,
					});
				});
			});
			this.setState({
				selectTwo: arrTwo,
			});
		}
	};
	/**
	 * 子函数列表搜索框change
	 * @param {*} value
	 */
	selectTwoChange = (value) => {
		const { selectTwo } = this.state;
		let arr = selectTwo.filter((item) => item.key == value);
		this.setState(
			{
				checkedList: arr,
				checkedFuc: arr[0].func,
				aceValue: arr[0].func.toString(),
				callKey: arr[0].callKey,
			},
			() => {
				this.getListData(this.state.checkedList[0].demo);
			},
		);
	};
	/**
	 * 数据格式化等操作
	 */
	componentDidMount() {
		const arrOne = [],
			arrTwo = [];
		// 默认用户可查看全部数据
		funcList.map((item) => {
			arrOne.push({
				value: item.name,
				key: item.name,
			});
			item.children.map((item) => {
				arrTwo.push({
					funcName: item.funcName,
					value: item.name,
					key: item.name,
					func: item.function,
					params: item.params,
					demo: item.demo,
					callKey: item.callKey,
				});
			});
		});
		this.setState({
			selectOne: arrOne,
			selectTwo: arrTwo,
		});
	}
	/**
	 * 根据传入的数组长度返回对应的函数传入值
	 */
	@Throttle(1)
	getListData = (List) => {
		const { checkedFuc } = this.state;
		let ary = [];
		List.map((item) => {
			ary.push(item.value);
			return item;
		});
		let data = checkedFuc(...ary);
		this.setState({
			resultValue: data,
		});
	};
	componentWillReceiveProps(nextProps) {
		const { isPicth } = nextProps;
		const { visible } = this.state;
		if (visible !== isPicth) {
			this.setState({
				visible: isPicth,
			});
		}
	}
	// 编辑起状态函数
	onAceChange = (newValue) => {
		this.setState({
			aceValue: newValue,
		});
	};
	render() {
		const { selectOne, selectTwo, checkedList, checkedFuc, aceValue, resultValue } = this.state;
		const { isShowBtn, isPicth } = this.props;
		return (
			<>
				{isShowBtn ? (
					''
				) : (
					<Button type="primary" onClick={this.showModal}>
						选择公式
					</Button>
				)}
				<Modal
					title="选择已有公式"
					visible={this.state.visible}
					onOk={this.handleOk}
					onCancel={this.handleCancel}
					width={1200}>
					<Row>
						<div className={styles.title}>快捷查询函数</div>
						<Col style={{ padding: 12 }} span={12}>
							<span style={{ marginRight: 12 }}>函数类别</span>
							<Select
								allowClear
								showSearch
								style={{ width: '80%' }}
								placeholder="请选择函数类别"
								optionFilterProp="children"
								onChange={this.selectOneChange}
								filterOption={(input, option) =>
									option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
								}>
								{selectOne.map((item) => {
									return (
										<Option value={item.key} key={item.key}>
											{item.value}
										</Option>
									);
								})}
							</Select>
						</Col>
						<Col style={{ padding: 12 }} span={12}>
							<span style={{ marginRight: 12 }}>函数列表</span>
							<Select
								showSearch
								style={{ width: '80%' }}
								placeholder="请选择函数列表"
								optionFilterProp="children"
								onChange={this.selectTwoChange}
								filterOption={(input, option) =>
									option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
								}>
								{selectTwo.map((item) => {
									return (
										<Option value={item.key} key={item.key}>
											{item.value}
										</Option>
									);
								})}
							</Select>
						</Col>
					</Row>
					<Row>
						<div className={styles.title}>
							{checkedList.length > 0 ? checkedList[0].name : ''}
							函数详细内容
						</div>
						<Col span={24}>
							{checkedList.length > 0 ? (
								<div className={styles.yesList}>
									<Row gutter={16}>
										<Col span={5} className={styles.left_title}>
											函数名称:
										</Col>
										<Col span={16} className={styles.right_content}>
											{' '}
											{checkedList[0].value}
										</Col>
									</Row>
									<Row gutter={16}>
										<Col span={5} className={styles.left_title}>
											函数介绍:
										</Col>
										<Col span={16} className={styles.right_content}>
											{' '}
											{checkedList[0].funcName}
										</Col>
									</Row>
									{checkedList.length > 0
										? checkedList[0].params.map((item, index) => {
												return (
													<Row gutter={16}>
														<Col span={5} className={styles.left_title}>
															函数传入参数介绍(
															{index + 1}
															):
														</Col>
														<Col span={16} className={styles.right_content}>
															{' '}
															{item.text}
														</Col>
													</Row>
												);
										  })
										: ''}
									<Row gutter={16}>
										<Col span={5} className={styles.left_title}>
											函数内容:
										</Col>
										<Col span={16} className={styles.right_content}>
											<AceEditor
												height={450}
												width={462}
												placeholder="请输入json样式"
												mode={'json'}
												disabled
												theme={'textmate'}
												fontSize={12}
												readOnly
												value={aceValue}
												onChange={this.onAceChange}
												showPrintMargin={true}
												showGutter={true}
												highlightActiveLine={true}
												name="UNIQUE_ID_OF_DIV"
												// keyboardHandler="vim"
												setOptions={{
													enableBasicAutocompletion: true,
													enableLiveAutocompletion: true,
													enableSnippets: true,
													showLineNumbers: true,
													tabSize: 2,
												}}
												editorProps={{ $blockScrolling: true }}
											/>
											{/* <pre>{checkedList[0].func.toString()}</pre> */}
										</Col>
									</Row>
									<Row gutter={16}>
										<Col span={5} className={styles.left_title}>
											函数案例计算参数:
										</Col>
										<Col span={16} className={styles.right_content}>
											{checkedList[0].demo.map((item, index) => {
												return (
													<div>
														<span>
															案例传入参数(
															{index + 1}
															):
														</span>
														<span style={{ marginLeft: 12 }}>类型: {item.text}</span>
													</div>
												);
											})}
										</Col>
									</Row>
									<Row gutter={16}>
										<Col span={5} className={styles.left_title}>
											计算结果:
										</Col>
										<Col span={16} className={styles.right_content}>
											{resultValue
												? typeof resultValue === 'object' || typeof resultValue === 'boolean'
													? JSON.stringify(resultValue)
													: resultValue
												: typeof resultValue === 'boolean'
													? JSON.stringify(resultValue)
													: null}
										</Col>
									</Row>
								</div>
							) : (
								<div className={styles.noList}>请选择您需要执行的函数</div>
							)}
						</Col>
					</Row>
				</Modal>
			</>
		);
	}
徐立's avatar
徐立 committed
356
}