import React, { Component } from 'react'
|
import PropTypes from 'prop-types'
|
import { is, fromJS } from 'immutable'
|
import { Modal, notification, Popover, Switch, message } from 'antd'
|
import { PlusOutlined, EditOutlined, ToolOutlined, DeleteOutlined, FontColorsOutlined } from '@ant-design/icons'
|
|
import Api from '@/api'
|
import Utils from '@/utils/utils.js'
|
import { getSearchForm } from '@/templates/zshare/formconfig'
|
import { resetStyle } from '@/utils/utils-custom.js'
|
import { getTables } from '@/utils/utils-custom.js'
|
import asyncComponent from '@/utils/asyncComponent'
|
import asyncIconComponent from '@/utils/asyncIconComponent'
|
import DragElement from './dragsearch'
|
import getWrapForm from './options'
|
import MKEmitter from '@/utils/events.js'
|
import './index.scss'
|
|
const { confirm } = Modal
|
|
const NormalForm = asyncIconComponent(() => import('@/components/normalform'))
|
const FieldsComponent = asyncComponent(() => import('@/templates/sharecomponent/fieldscomponent'))
|
const SearchForm = asyncIconComponent(() => import('@/templates/sharecomponent/searchcomponent/searchform'))
|
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 = {
|
appType: sessionStorage.getItem('appType'),
|
searchlist: null, // 搜索条件集
|
sqlVerifing: false, // sql验证中
|
visible: false, // 模态框控制
|
showField: false,
|
editcard: null // 编辑中元素
|
}
|
|
/**
|
* @description 搜索条件初始化
|
*/
|
UNSAFE_componentWillMount () {
|
const { card } = this.props
|
|
if (card.isNew) {
|
let _card = {
|
uuid: card.uuid,
|
type: card.type,
|
width: 24,
|
name: card.name,
|
subtype: card.subtype,
|
wrap: { name: card.name, width: 24, show: this.state.appType === 'mob' ? 'false' : '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('plusSearch', this.plusSearch)
|
}
|
|
/**
|
* @description 组件销毁,清除state更新
|
*/
|
componentWillUnmount () {
|
this.setState = () => {
|
return
|
}
|
MKEmitter.removeListener('plusSearch', this.plusSearch)
|
}
|
|
shouldComponentUpdate (nextProps, nextState) {
|
return !is(fromJS(this.state), fromJS(nextState))
|
}
|
|
getStyle = (style) => {
|
let _card = {...this.state.card, style}
|
|
this.setState({
|
card: _card
|
})
|
|
this.updateComponent(_card)
|
}
|
|
changeStyle = () => {
|
const { card } = this.state
|
|
MKEmitter.emit('changeStyle', ['background', 'border', 'padding', 'margin', 'shadow', 'clear'], card.style, this.getStyle)
|
}
|
|
/**
|
* @description 卡片行外层信息更新(数据源,样式等)
|
*/
|
updateComponent = (component) => {
|
component.$tables = getTables(component)
|
|
component.width = component.wrap.width
|
component.name = component.wrap.name
|
|
this.setState({
|
card: component
|
})
|
|
this.props.updateConfig(component)
|
}
|
|
// checkComponent = (component) => {
|
// this.updateComponent(component)
|
|
// let _item = null
|
// component.search.forEach(item => {
|
// if (!_item && item.focus) {
|
// _item = item
|
// }
|
// })
|
// if (_item) {
|
// this.handleSearch(_item)
|
// }
|
// }
|
|
/**
|
* @description 搜索条件顺序调整,或拖拽添加
|
*/
|
handleList = (list, newcell) => {
|
const { card } = this.state
|
let _card = {...card, search: list}
|
|
if (newcell) {
|
this.setState({card: _card})
|
this.handleSearch(newcell)
|
} else {
|
this.updateComponent(_card)
|
}
|
}
|
|
/**
|
* @description 搜索条件编辑,获取搜索条件表单信息
|
*/
|
handleSearch = (cell) => {
|
const { card } = this.state
|
let linkableFields = []
|
|
card.search.forEach(item => {
|
if (item.uuid === cell.uuid) return
|
if (!['select', 'link', 'checkcard'].includes(item.type)) return
|
if (item.type === 'checkcard' && item.multiple === 'true') return
|
|
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 // 字段重复
|
|
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
|
}
|
}
|
|
if (item.uuid === res.uuid) {
|
return res
|
} else {
|
return item
|
}
|
})
|
|
if (fieldrepet) {
|
notification.warning({
|
top: 92,
|
message: '字段已存在!',
|
duration: 5
|
})
|
return
|
}
|
|
if (['select', 'multiselect', 'link', 'checkcard'].includes(res.type) && res.resourceType === '1' && res.database !== 'sso' && /\s/.test(res.dataSource)) {
|
this.setState({
|
sqlVerifing: true
|
})
|
|
let sql = `declare @mk_departmentcode nvarchar(512),@mk_organization nvarchar(512),@mk_user_type nvarchar(20)
|
${res.dataSource}`
|
sql = sql.replace(/@\$|\$@/ig, '')
|
|
Api.sDebug(sql).then(result => {
|
if (result.status || result.ErrCode === '-2') {
|
this.setState({
|
sqlVerifing: false,
|
visible: false
|
}, ()=> {
|
this.updateComponent(card)
|
})
|
} else {
|
this.setState({sqlVerifing: false})
|
|
Modal.error({
|
title: result.message
|
})
|
}
|
})
|
} else {
|
this.setState({
|
visible: false
|
}, ()=> {
|
this.updateComponent(card)
|
})
|
}
|
})
|
}
|
|
/**
|
* @description 搜索条件删除
|
*/
|
deleteElement = (cell) => {
|
let that = this
|
|
confirm({
|
content: `确定删除 - ${cell.label} ?`,
|
onOk() {
|
let _card = fromJS(that.state.card).toJS()
|
_card.search = _card.search.filter(item => item.uuid !== cell.uuid)
|
|
that.updateComponent(_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
|
}
|
|
if (card.search.length) {
|
item.ratio = card.search[card.search.length - 1].ratio
|
}
|
|
card.search.push(item)
|
|
this.setState({card}, () => {
|
this.handleSearch(item)
|
})
|
}
|
|
onFieldChange = () => {
|
const { showField } = this.state
|
|
this.setState({
|
showField: !showField
|
})
|
}
|
|
plusSearch = (MenuId, item, type) => {
|
const { card } = this.state
|
|
if (MenuId !== card.uuid) return
|
|
let _card = fromJS(card).toJS()
|
|
if (type === 'simple') {
|
_card.search.push(item)
|
|
this.updateComponent(_card)
|
this.handleSearch(item)
|
} else if (type === 'replace') {
|
delete item.focus
|
_card.search = _card.search.map(cell => {
|
if (cell.field && cell.field.toLowerCase() === item.field.toLowerCase()) {
|
return item
|
}
|
return cell
|
})
|
|
this.updateComponent(_card)
|
this.handleSearch(item)
|
} else if (type === 'multil') {
|
_card.search.push(...item)
|
|
this.updateComponent(_card)
|
}
|
}
|
|
getWrapForms = () => {
|
const { card } = this.state
|
|
return getWrapForm(card.wrap, card.uuid)
|
}
|
|
updateWrap = (res) => {
|
this.updateComponent({...this.state.card, wrap: res})
|
}
|
|
clickComponent = (e) => {
|
if (sessionStorage.getItem('style-control') === 'true' || sessionStorage.getItem('style-control') === 'component') {
|
e.stopPropagation()
|
MKEmitter.emit('clickComponent', this.state.card.uuid, null, (style) => {
|
let _card = {...this.state.card}
|
_card.style = {..._card.style, ...style}
|
|
this.updateComponent(_card)
|
})
|
}
|
}
|
|
render() {
|
const { card, visible, sqlVerifing, showField } = this.state
|
let _style = resetStyle(card.style)
|
|
return (
|
<div className={`main-search-edit-list ${card.wrap.float} ${showField ? 'show-field' : ''} mk-order-${card.wrap.searchSize || ''}`} onClick={this.clickComponent} id={card.uuid} style={_style}>
|
<FieldsComponent config={card} type="search" />
|
<Switch checkedChildren="开" size="small" unCheckedChildren="关" defaultChecked={showField} onChange={this.onFieldChange} />
|
<DragElement
|
list={card.search}
|
setting={card.wrap}
|
handleList={this.handleList}
|
handleMenu={this.handleSearch}
|
deleteMenu={this.deleteElement}
|
/>
|
<Popover overlayClassName="mk-popover-control-wrap" mouseLeaveDelay={0.2} mouseEnterDelay={0.2} content={
|
<div className="mk-popover-control">
|
<PlusOutlined className="plus" title="添加" onClick={this.addSearch}/>
|
<NormalForm title="搜索设置" width={800} update={this.updateWrap} getForms={this.getWrapForms}>
|
<EditOutlined style={{color: '#1890ff'}} title="编辑"/>
|
</NormalForm>
|
<CopyComponent type="mainsearch" card={card}/>
|
{/* <PasteComponent config={card} options={['search', 'form']} updateConfig={this.checkComponent} /> */}
|
<PasteComponent config={card} options={['search', 'form']} />
|
<FontColorsOutlined className="style" title="调整样式" onClick={this.changeStyle}/>
|
<DeleteOutlined className="close" onClick={() => this.props.deletecomponent(card.uuid)} />
|
</div>
|
} trigger="hover">
|
<ToolOutlined />
|
</Popover>
|
<div className="component-name">
|
<div className="center" onDoubleClick={() => {
|
let oInput = document.createElement('input')
|
oInput.value = 'anchor' + card.uuid
|
document.body.appendChild(oInput)
|
oInput.select()
|
document.execCommand('Copy')
|
document.body.removeChild(oInput)
|
message.success('复制成功。')
|
}}>{card.name}</div>
|
</div>
|
{/* 编辑搜索条件 */}
|
<Modal
|
title="搜索条件-编辑"
|
wrapClassName="mk-scroll-modal"
|
visible={visible}
|
width={950}
|
maskClosable={false}
|
onOk={this.handleSubmit}
|
confirmLoading={sqlVerifing}
|
onCancel={this.editModalCancel}
|
destroyOnClose
|
>
|
<SearchForm
|
card={this.state.editcard}
|
formlist={this.state.formlist}
|
inputSubmit={this.handleSubmit}
|
wrappedComponentRef={(inst) => this.searchFormRef = inst}
|
/>
|
</Modal>
|
</div>
|
)
|
}
|
}
|
|
export default MainSearchComponent
|