import React, { Component } from 'react'
|
import PropTypes from 'prop-types'
|
import { is, fromJS } from 'immutable'
|
import { Modal, notification, Popover, Switch } from 'antd'
|
import { PlusOutlined, EditOutlined, ToolOutlined, DeleteOutlined, FontColorsOutlined } from '@ant-design/icons'
|
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 { resetStyle } 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 = {
|
dict: sessionStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
|
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,
|
floor: card.floor,
|
tabId: card.tabId || '',
|
parentId: card.parentId || '',
|
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('submitStyle', this.getStyle)
|
MKEmitter.addListener('plusSearch', this.plusSearch)
|
}
|
|
/**
|
* @description 组件销毁,清除state更新
|
*/
|
componentWillUnmount () {
|
this.setState = () => {
|
return
|
}
|
MKEmitter.removeListener('submitStyle', this.getStyle)
|
MKEmitter.removeListener('plusSearch', this.plusSearch)
|
}
|
|
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', 'shadow'], card.style)
|
}
|
|
/**
|
* @description 卡片行外层信息更新(数据源,样式等)
|
*/
|
updateComponent = (component) => {
|
this.setState({
|
card: component
|
})
|
|
component.width = component.wrap.width
|
component.name = component.wrap.name
|
|
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.setState({card: _card}, ()=> {
|
this.props.updateConfig(_card)
|
})
|
}
|
}
|
|
/**
|
* @description 搜索条件编辑,获取搜索条件表单信息
|
*/
|
handleSearch = (cell) => {
|
const { card } = this.state
|
let linkableFields = []
|
|
card.search.forEach(item => {
|
if (item.uuid === card.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 // 字段重复
|
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', 'checkcard'].includes(res.type) && res.resourceType === '1' && /\s/.test(res.dataSource)) {
|
this.setState({
|
sqlVerifing: true
|
})
|
|
let param = {
|
func: 's_debug_sql',
|
exec_type: 'y',
|
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.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)
|
})
|
}
|
|
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.setState({
|
card: _card,
|
}, () => {
|
this.props.updateConfig(_card)
|
})
|
} else if (type === 'multil') {
|
_card.search.push(...item)
|
this.setState({
|
card: _card,
|
}, () => {
|
this.props.updateConfig(_card)
|
})
|
}
|
}
|
|
getWrapForms = () => {
|
const { wrap, action } = this.state.card
|
|
return getWrapForm(wrap, action)
|
}
|
|
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)
|
}
|
}
|
|
render() {
|
const { dict, card, visible, sqlVerifing, showField } = this.state
|
let _style = resetStyle(card.style)
|
|
return (
|
<div className={`main-search-edit-list ${card.wrap.float} ${card.wrap.show || ''}`} onClick={this.clickComponent} id={card.uuid} style={_style}>
|
<FieldsComponent config={card} type="search" />
|
<Switch checkedChildren={dict['model.switch.open']} size="small" unCheckedChildren={dict['model.switch.close']} defaultChecked={showField} onChange={this.onFieldChange} />
|
<DragElement
|
list={card.search}
|
showField={showField}
|
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} />
|
<FontColorsOutlined className="style" title="调整样式" onClick={this.changeStyle}/>
|
<DeleteOutlined className="close" onClick={() => this.props.deletecomponent(card.uuid)} />
|
</div>
|
} trigger="hover">
|
<ToolOutlined />
|
</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>
|
)
|
}
|
}
|
|
export default MainSearchComponent
|