import React, { Component } from 'react'
|
import PropTypes from 'prop-types'
|
import { connect } from 'react-redux'
|
import { is, fromJS } from 'immutable'
|
import { Modal, notification, Popover, Icon } from 'antd'
|
import moment from 'moment'
|
|
import Api from '@/api'
|
import Utils from '@/utils/utils.js'
|
import zhCN from '@/locales/zh-CN/model.js'
|
import enUS from '@/locales/en-US/model.js'
|
import { getSearchForm } from '@/templates/zshare/formconfig'
|
|
import asyncIconComponent from '@/utils/asyncIconComponent'
|
import SearchForm from '@/templates/sharecomponent/searchcomponent/searchform'
|
import DragElement from './dragsearch'
|
import MKEmitter from '@/utils/events.js'
|
import './index.scss'
|
|
const { confirm } = Modal
|
|
const WrapComponent = asyncIconComponent(() => import('./wrapsetting'))
|
const CopyComponent = asyncIconComponent(() => import('@/menu/components/share/copycomponent'))
|
const PasteComponent = asyncIconComponent(() => import('@/menu/components/share/pastecomponent'))
|
|
class MainSearchComponent extends Component {
|
static propTpyes = {
|
card: PropTypes.object,
|
updateConfig: PropTypes.func,
|
deletecomponent: PropTypes.func
|
}
|
|
state = {
|
dict: localStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
|
searchlist: null, // 搜索条件集
|
sqlVerifing: false, // sql验证中
|
visible: false, // 模态框控制
|
editcard: null // 编辑中元素
|
}
|
|
/**
|
* @description 搜索条件初始化
|
*/
|
UNSAFE_componentWillMount () {
|
const { card } = this.props
|
|
if (card.isNew) {
|
let _card = {
|
uuid: card.uuid,
|
type: card.type,
|
floor: card.floor,
|
tabId: card.tabId || '',
|
parentId: card.parentId || '',
|
width: 24,
|
name: card.name,
|
subtype: card.subtype,
|
wrap: { name: card.name, width: 24, show: 'true', float: 'left' },
|
style: {
|
marginLeft: '8px', marginRight: '8px', marginTop: '8px', marginBottom: '8px'
|
},
|
search: []
|
}
|
this.setState({
|
card: _card
|
})
|
this.props.updateConfig(_card)
|
} else {
|
this.setState({
|
card: fromJS(card).toJS()
|
})
|
}
|
}
|
|
componentDidMount () {
|
MKEmitter.addListener('submitStyle', this.getStyle)
|
}
|
|
/**
|
* @description 组件销毁,清除state更新
|
*/
|
componentWillUnmount () {
|
this.setState = () => {
|
return
|
}
|
MKEmitter.removeListener('submitStyle', this.getStyle)
|
}
|
|
shouldComponentUpdate (nextProps, nextState) {
|
return !is(fromJS(this.state), fromJS(nextState))
|
}
|
|
getStyle = (comIds, style) => {
|
const { card } = this.state
|
|
if (comIds.length !== 1 || comIds[0] !== card.uuid) return
|
|
let _card = {...card, style}
|
|
this.setState({
|
card: _card
|
})
|
|
this.props.updateConfig(_card)
|
}
|
|
changeStyle = () => {
|
const { card } = this.state
|
|
MKEmitter.emit('changeStyle', [card.uuid], ['background', 'border', 'padding', 'margin'], card.style)
|
}
|
|
/**
|
* @description 卡片行外层信息更新(数据源,样式等)
|
*/
|
updateComponent = (component) => {
|
this.setState({
|
card: component
|
})
|
|
component.width = component.wrap.width
|
component.name = component.wrap.name
|
|
this.props.updateConfig(component)
|
}
|
|
/**
|
* @description 搜索条件顺序调整,或拖拽添加
|
*/
|
handleList = (list, newcell) => {
|
const { card } = this.state
|
let _card = {...card, search: list}
|
|
if (newcell) {
|
this.setState({card: _card})
|
this.handleSearch(newcell)
|
} else {
|
this.setState({card: _card}, ()=> {
|
this.props.updateConfig(_card)
|
})
|
}
|
}
|
|
/**
|
* @description 搜索条件编辑,获取搜索条件表单信息
|
*/
|
handleSearch = (cell) => {
|
const { card } = this.state
|
let linkableFields = []
|
|
card.search.forEach(item => {
|
if (item.uuid !== cell.uuid && (item.type === 'select' || item.type === 'link')) {
|
linkableFields.push({
|
value: item.field,
|
text: item.label
|
})
|
}
|
})
|
|
this.setState({
|
visible: true,
|
editcard: cell,
|
formlist: getSearchForm(cell, linkableFields)
|
})
|
}
|
|
/**
|
* @description 取消保存,如果元素为新添元素,则从序列中删除
|
*/
|
editModalCancel = () => {
|
const { card, editcard } = this.state
|
|
if (editcard.focus) {
|
card.search = card.search.filter(item => item.uuid !== editcard.uuid)
|
|
this.setState({
|
card: card,
|
editcard: null,
|
visible: false
|
})
|
} else {
|
this.setState({
|
editcard: null,
|
visible: false
|
})
|
}
|
}
|
|
/**
|
* @description 搜索修改后提交保存
|
* 1、字段及提示文字重复校验
|
* 2、下拉菜单数据源语法验证
|
*/
|
handleSubmit = () => {
|
let card = fromJS(this.state.card).toJS()
|
|
this.searchFormRef.handleConfirm().then(res => {
|
let fieldrepet = false // 字段重复
|
let labelrepet = false // 提示文字重复
|
|
card.search = card.search.map(item => { // 数据更新及重复检测
|
if (item.uuid !== res.uuid && res.field && item.field) {
|
let itemFields = []
|
if (item.type === 'text') {
|
itemFields = item.field.split(',')
|
} else if (item.type === 'group') {
|
itemFields = [item.field, item.datefield]
|
} else {
|
itemFields = [item.field]
|
}
|
|
let resFields = []
|
if (res.type === 'text') {
|
resFields = res.field.split(',')
|
} else if (res.type === 'group') {
|
resFields = [res.field, res.datefield]
|
} else {
|
resFields = [res.field]
|
}
|
|
let setFields = Array.from(new Set([...itemFields, ...resFields]))
|
|
if (setFields.length < itemFields.length + resFields.length && (res.type !== 'date' || item.type !== 'date')) {
|
fieldrepet = true
|
} else if (item.label === res.label) {
|
labelrepet = true
|
}
|
}
|
|
if (item.uuid === res.uuid) {
|
return res
|
} else {
|
return item
|
}
|
})
|
|
if (fieldrepet) {
|
notification.warning({
|
top: 92,
|
message: '字段已存在!',
|
duration: 5
|
})
|
return
|
} else if (labelrepet) {
|
notification.warning({
|
top: 92,
|
message: '名称已存在!',
|
duration: 5
|
})
|
return
|
}
|
|
if (['select', 'multiselect', 'link'].includes(res.type) && res.resourceType === '1' && /\s/.test(res.dataSource)) {
|
this.setState({
|
sqlVerifing: true
|
})
|
|
let param = {
|
func: 's_debug_sql',
|
LText: res.dataSource
|
}
|
|
param.LText = param.LText.replace(/@\$|\$@/ig, '')
|
|
param.LText = Utils.formatOptions(param.LText)
|
param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
|
param.secretkey = Utils.encrypt(param.LText, param.timestamp)
|
|
if (window.GLOB.mainSystemApi && res.database === 'sso') {
|
param.rduri = window.GLOB.mainSystemApi
|
}
|
|
Api.getLocalConfig(param).then(result => {
|
if (result.status) {
|
this.setState({
|
card: card,
|
sqlVerifing: false,
|
visible: false
|
}, ()=> {
|
this.props.updateConfig(card)
|
})
|
} else {
|
this.setState({sqlVerifing: false})
|
|
Modal.error({
|
title: result.message
|
})
|
}
|
})
|
} else {
|
this.setState({
|
card: card,
|
visible: false
|
}, ()=> {
|
this.props.updateConfig(card)
|
})
|
}
|
})
|
}
|
|
/**
|
* @description 搜索条件删除
|
*/
|
deleteElement = (cell) => {
|
const { dict } = this.state
|
let _this = this
|
|
confirm({
|
content: dict['model.confirm'] + dict['model.delete'] + ` - ${cell.label} ?`,
|
onOk() {
|
let _card = fromJS(_this.state.card).toJS()
|
_card.search = _card.search.filter(item => item.uuid !== cell.uuid)
|
|
_this.setState({
|
card: _card
|
}, () => {
|
_this.props.updateConfig(_card)
|
})
|
},
|
onCancel() {}
|
})
|
}
|
|
addSearch = () => {
|
let card = fromJS(this.state.card).toJS()
|
let item = {
|
uuid: Utils.getuuid(),
|
label: '搜索',
|
field: '',
|
initval: '',
|
type: 'text',
|
resourceType: '0',
|
match: 'like',
|
focus: true
|
}
|
card.search.push(item)
|
|
this.setState({card}, () => {
|
this.handleSearch(item)
|
})
|
}
|
|
clickComponent = (e) => {
|
if (sessionStorage.getItem('style-control') === 'true') {
|
e.stopPropagation()
|
MKEmitter.emit('clickComponent', this.state.card)
|
}
|
}
|
|
render() {
|
const { dict, card, visible, sqlVerifing } = this.state
|
|
return (
|
<div className={`main-search-edit-list ${card.wrap.float} ${card.wrap.show || ''}`} onClick={this.clickComponent} id={card.uuid} style={card.style}>
|
<DragElement
|
list={card.search}
|
handleList={this.handleList}
|
handleMenu={this.handleSearch}
|
deleteMenu={this.deleteElement}
|
placeholder={dict['header.form.search.placeholder']}
|
/>
|
<Popover overlayClassName="mk-popover-control-wrap" mouseLeaveDelay={0.2} mouseEnterDelay={0.2} content={
|
<div className="mk-popover-control">
|
<Icon className="plus" title="添加" onClick={this.addSearch} type="plus" />
|
<WrapComponent config={card} updateConfig={this.updateComponent}/>
|
<CopyComponent type="mainsearch" card={card}/>
|
<PasteComponent config={card} options={['search', 'form']} updateConfig={this.updateComponent} />
|
<Icon className="style" title="调整样式" onClick={this.changeStyle} type="font-colors" />
|
<Icon className="close" title="delete" type="delete" onClick={() => this.props.deletecomponent(card.uuid)} />
|
</div>
|
} trigger="hover">
|
<Icon type="tool" />
|
</Popover>
|
{/* 编辑搜索条件 */}
|
<Modal
|
title="搜索条件-编辑"
|
visible={visible}
|
width={850}
|
maskClosable={false}
|
onOk={this.handleSubmit}
|
confirmLoading={sqlVerifing}
|
onCancel={this.editModalCancel}
|
destroyOnClose
|
>
|
<SearchForm
|
dict={dict}
|
card={this.state.editcard}
|
formlist={this.state.formlist}
|
inputSubmit={this.handleSubmit}
|
wrappedComponentRef={(inst) => this.searchFormRef = inst}
|
/>
|
</Modal>
|
</div>
|
)
|
}
|
}
|
const mapStateToProps = (state) => {
|
return {
|
menu: state.customMenu
|
}
|
}
|
|
const mapDispatchToProps = () => {
|
return {}
|
}
|
|
export default connect(mapStateToProps, mapDispatchToProps)(MainSearchComponent)
|