This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
cyber-narrator-cn-ui/src/components/advancedSearch/Index.vue

129 lines
3.6 KiB
Vue
Raw Normal View History

2022-01-23 23:34:51 +08:00
<template>
<div
class="advanced-search"
>
<text-mode
v-if="searchMode === 'text'"
2022-01-25 17:16:56 +08:00
ref="textMode"
:column-list="columnList"
:sql="sql"
2022-01-23 23:34:51 +08:00
@changeMode="changeMode"
@search="search"
></text-mode>
<tag-mode
v-if="searchMode === 'tag'"
2022-01-25 17:16:56 +08:00
ref="tagMode"
2022-01-23 23:34:51 +08:00
:column-list="columnList"
:operator-list="showOperatorList"
:connection-list="showConnectionList"
2022-01-25 17:16:56 +08:00
:convert-meta-list="metaList"
2022-01-23 23:34:51 +08:00
@changeMode="changeMode"
@search="search"
></tag-mode>
<!-- <div class="search-tip&#45;&#45;error">something error...</div>-->
2022-01-23 23:34:51 +08:00
</div>
</template>
<script>
import TagMode from '@/components/advancedSearch/TagMode'
import TextMode from '@/components/advancedSearch/TextMode'
import { defaultOperatorList, defaultConnectionList } from '@/components/advancedSearch/meta/meta'
import _ from 'lodash'
import { ref } from 'vue'
import SqlParser from '@/components/advancedSearch/meta/sql-parser'
import { ElMessage } from 'element-plus'
2022-01-23 23:34:51 +08:00
export default {
name: 'Index',
components: {
TagMode,
TextMode
},
2022-01-25 17:16:56 +08:00
data () {
return {
sql: null,
metaList: null
}
},
2022-01-23 23:34:51 +08:00
props: {
// 默认模式tag | text
defaultMode: String,
// 使用全文检索
fullText: {
type: Boolean,
default: true
},
// 条件字段列表
columnList: {
type: Array,
required: true
},
// 操作符列表
operatorList: Array,
// 连接符列表
connectionList: Array
},
methods: {
2022-01-25 17:16:56 +08:00
search (metaList, formatSql) {
this.$emit('search', metaList, formatSql)
2022-01-23 23:34:51 +08:00
},
2022-01-25 17:16:56 +08:00
changeMode (mode, data) {
2022-01-23 23:34:51 +08:00
this.searchMode = mode
2022-01-25 17:16:56 +08:00
if (mode === 'text') {
this.sql = data
} else if (mode === 'tag') {
this.metaList = data
}
},
2022-02-18 10:07:43 +08:00
// params: [{column, operator, value}, ...]
2022-01-25 17:16:56 +08:00
addParams (params) {
this.$refs.tagMode && this.$refs.tagMode.addParams(params)
this.$refs.textMode && this.$refs.textMode.addParams(params)
2022-01-26 17:44:24 +08:00
},
// params: [{column, operator, value}, ...]
2022-02-18 10:07:43 +08:00
removeParams (params) {
this.$refs.tagMode && this.$refs.tagMode.removeParams(params)
this.$refs.textMode && this.$refs.textMode.removeParams(params)
},
// params: [{ newParam: {column, operator, value }, oldParam: { column, operator, value }], ...]
changeParams (params) {
this.$refs.tagMode && this.$refs.tagMode.changeParams(params)
this.$refs.textMode && this.$refs.textMode.changeParams(params)
2022-02-18 10:07:43 +08:00
},
2022-01-26 17:44:24 +08:00
setSql (sql) {
if (this.searchMode === 'text') {
this.sql = sql
} else if (this.searchMode === 'tag') {
const parser = new SqlParser(sql, this.columnList)
const errorList = parser.validate()
if (this.$_.isEmpty(errorList)) {
const { metaList } = parser.formatSql()
this.metaList = metaList
} else {
ElMessage.error(this.$t('tip.invalidExpression'))
2022-01-26 17:44:24 +08:00
}
}
2022-01-23 23:34:51 +08:00
}
},
setup (props) {
// 默认为文本模式
let searchMode = ref('text')
2022-01-23 23:34:51 +08:00
if (props.defaultMode) {
switch (props.defaultMode) {
case 'tag': {
searchMode = 'tag'
break
}
}
}
// 如果参数中的operatorList、connectionList为空使用默认值
const showOperatorList = _.isEmpty(props.operatorList) ? defaultOperatorList : [...props.operatorList]
const showConnectionList = _.isEmpty(props.connectionList) ? defaultConnectionList : [...props.connectionList]
return {
searchMode,
showOperatorList,
showConnectionList
}
}
}
</script>