index.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <template>
  2. <el-select
  3. :key="config.code"
  4. v-model="value"
  5. :popper-class="'basic-component-select select-popper-' + config.code"
  6. :class="['basic-component-select', `select-${config.code}`]"
  7. :placeholder="`请选择${placeholder || newPlaceholder}`"
  8. filterable
  9. clearable
  10. @visible-change="visibleChange"
  11. @change="selectChange"
  12. @mouseenter.native="mouseenter"
  13. >
  14. <el-option
  15. v-for="(option, key) in optionData"
  16. :key="key"
  17. :label="option[config.dataSource.dimensionField]"
  18. :value="option[config.dataSource.metricField]"
  19. />
  20. </el-select>
  21. </template>
  22. <script>
  23. import { EventBus } from 'data-room-ui/js/utils/eventBus'
  24. import commonMixins from 'data-room-ui/js/mixins/commonMixins'
  25. import linkageMixins from 'data-room-ui/js/mixins/linkageMixins'
  26. import { getDataSetDetails } from 'data-room-ui/js/api/bigScreenApi'
  27. import { settingToTheme } from 'data-room-ui/js/utils/themeFormatting'
  28. import cloneDeep from 'lodash/cloneDeep'
  29. window.dataSetFields = []
  30. export default {
  31. name: 'BasicComponentSelect',
  32. components: {},
  33. mixins: [commonMixins, linkageMixins],
  34. props: {
  35. // 组件配置
  36. config: {
  37. type: Object,
  38. default: () => ({})
  39. }
  40. },
  41. data () {
  42. return {
  43. value: '',
  44. innerConfig: {},
  45. optionData: [],
  46. newPlaceholder: ''
  47. }
  48. },
  49. computed: {
  50. isPreview () {
  51. return (this.$route.path === window?.BS_CONFIG?.routers?.previewUrl) || (this.$route.path === '/big-screen/preview')
  52. },
  53. placeholder: {
  54. get () {
  55. return window.dataSetFields.find(field => field.value === this.config.dataSource.dimensionField)?.label || ''
  56. },
  57. set (val) {
  58. this.newPlaceholder = val
  59. }
  60. }
  61. },
  62. watch: { },
  63. created () { },
  64. mounted () {
  65. window.dataSetFields = []
  66. this.changeStyle(this.config)
  67. EventBus.$on('changeBusinessKey', () => {
  68. window.dataSetFields = []
  69. })
  70. if (this.isPreview) {
  71. if (window.dataSetFields.length === 0) {
  72. getDataSetDetails(this.config.dataSource.businessKey).then(res => {
  73. window.dataSetFields = res.fields.map(field => {
  74. return {
  75. label: field.comment || field.fieldDesc,
  76. value: field.name || field.fieldName
  77. }
  78. })
  79. this.placeholder = window.dataSetFields.find(field => field.value === this.config.dataSource.dimensionField)?.label || ''
  80. })
  81. }
  82. }
  83. },
  84. beforeDestroy () {
  85. EventBus.$off('changeBusinessKey')
  86. },
  87. methods: {
  88. dataFormatting (config, data) {
  89. // 数据返回成功则赋值
  90. if (data.success) {
  91. data = data.data
  92. // 获取到后端返回的数据,有则赋值
  93. if (config.dataHandler) {
  94. try {
  95. // 此处函数处理data
  96. eval(config.dataHandler)
  97. } catch (e) {
  98. console.info(e)
  99. }
  100. }
  101. config.option.data = data
  102. this.optionData = data
  103. config.customize.title = config.option.data[config.dataSource.dimensionField] || config.customize.title
  104. if (window.dataSetFields.length === 0) {
  105. getDataSetDetails(this.config.dataSource.businessKey).then(res => {
  106. window.dataSetFields = res.fields.map(field => {
  107. return {
  108. label: field.comment || field.fieldDesc,
  109. value: field.name || field.fieldName
  110. }
  111. })
  112. this.placeholder = window.dataSetFields.find(field => field.value === this.config.dataSource.dimensionField)?.label || ''
  113. })
  114. }
  115. // 语音播报
  116. } else {
  117. // 数据返回失败则赋前端的模拟数据
  118. config.option.data = []
  119. this.optionData = []
  120. }
  121. return config
  122. },
  123. changeStyle (config) {
  124. // 样式改变时更新主题配置
  125. config.theme = settingToTheme(cloneDeep(config), this.customTheme)
  126. this.changeChartConfig(config)
  127. this.innerConfig = config
  128. // 选择器元素
  129. const selectInputEl = document.querySelector(`.select-${config.code} .el-input__inner`)
  130. // 背景颜色
  131. selectInputEl.style.backgroundColor = config.customize.backgroundColor
  132. // 字体大小
  133. selectInputEl.style.fontSize = config.customize.fontSize + 'px'
  134. // 字体颜色
  135. selectInputEl.style.color = config.customize.fontColor
  136. // 下拉图标
  137. const selectDropdownIcon = document.querySelector(`.select-${config.code} .el-icon-arrow-up`)
  138. selectDropdownIcon.style.fontSize = config.customize.fontSize + 'px'
  139. // 选择器下拉框元素
  140. const selectDropdownEl = document.querySelector(`.select-${config.code} .el-select-dropdown`)
  141. // 箭头背景颜色和下拉框背景颜色一致
  142. if (selectDropdownEl) {
  143. // 下拉框无边框
  144. selectDropdownEl.style.border = 'none'
  145. // 背景颜色
  146. selectDropdownEl.style.backgroundColor = config.customize.dropDownBackgroundColor
  147. }
  148. },
  149. // 组件联动
  150. selectChange (val) {
  151. this.linkage(this.config.option.data.find(item => item[this.config.dataSource.metricField] === val))
  152. },
  153. visibleChange (val) {
  154. if (val) {
  155. // 修改下拉框背景颜色,让下拉框背景颜色和箭头背景颜色一致
  156. const selectDropdownEl = document.querySelector(`.select-popper-${this.innerConfig.code}`)
  157. selectDropdownEl.style.color = this.innerConfig.customize.dropDownBackgroundColor
  158. // 空状态
  159. const selectDropdownEmptyEl = document.querySelector(`.select-popper-${this.innerConfig.code} .el-select-dropdown__empty`)
  160. if (selectDropdownEmptyEl) {
  161. selectDropdownEmptyEl.style.backgroundColor = this.innerConfig.customize.dropDownBackgroundColor
  162. }
  163. // 下拉项hover颜色
  164. const selectDropdownItemEl = document.querySelectorAll(`.select-popper-${this.innerConfig.code} .el-select-dropdown__item`)
  165. // 给--dropDownHoverFontColor 和 --dropDownHoverBackgroundColor 赋值
  166. selectDropdownItemEl.forEach(item => {
  167. item.style.setProperty('--dropDownHoverFontColor', this.innerConfig.customize.dropDownHoverFontColor)
  168. item.style.setProperty('--dropDownHoverBackgroundColor', this.innerConfig.customize.dropDownHoverBackgroundColor)
  169. })
  170. // 激活项
  171. const selectDropdownItemSelectedEl = document.querySelectorAll(`.select-popper-${this.innerConfig.code} .el-select-dropdown__item.selected`)
  172. selectDropdownItemSelectedEl.forEach(item => {
  173. item.style.color = this.innerConfig.customize.activeFontColor
  174. item.style.backgroundColor = this.innerConfig.customize.activeBackgroundColor
  175. })
  176. }
  177. // 不是激活项的还是使用背景颜色
  178. const selectDropdownItemEl = document.querySelectorAll(`.select-popper-${this.innerConfig.code} .el-select-dropdown__item`)
  179. selectDropdownItemEl.forEach(item => {
  180. // 检查是否是激活项,不是则使用背景颜色
  181. if (!item.classList.contains('selected')) {
  182. item.style.color = this.innerConfig.customize.dropDownFontColor
  183. item.style.backgroundColor = this.innerConfig.customize.dropDownBackgroundColor
  184. }
  185. })
  186. },
  187. mouseenter () {
  188. if (this.value) {
  189. setTimeout(() => {
  190. // 清空图标
  191. const selectDropdownCloseIcon = document.querySelector(`.select-${this.innerConfig.code} .el-icon-circle-close`)
  192. if (selectDropdownCloseIcon) {
  193. selectDropdownCloseIcon.style.fontSize = this.innerConfig.customize.fontSize + 'px'
  194. }
  195. }, 30)
  196. }
  197. }
  198. }
  199. }
  200. </script>
  201. <style lang="scss">
  202. .basic-component-select{
  203. color: '';
  204. .el-select-dropdown__wrap {
  205. margin-bottom: 0px !important;
  206. }
  207. .el-select-group__wrap:not(:last-of-type)::after {
  208. background-color: transparent !important;
  209. }
  210. .popper__arrow{
  211. border-bottom-color:var(--color) !important;
  212. &::after{
  213. border-bottom-color:var(--color) !important;
  214. }
  215. }
  216. }
  217. </style>
  218. <style lang="scss" scoped>
  219. .basic-component-select {
  220. width: 100%;
  221. height: 100%;
  222. ::v-deep .el-input {
  223. height: 100% !important;
  224. .el-select__caret{
  225. width: 100%;
  226. height: 100%;
  227. display: flex;
  228. align-items: center;
  229. }
  230. // 选择器输入框样式
  231. .el-input__inner {
  232. height: 100% !important;
  233. border-color: var(--bs-el-border) !important;
  234. }
  235. }
  236. .el-select-dropdown__item.hover,
  237. .el-select-dropdown__item:hover {
  238. color: var(--dropDownHoverFontColor) !important;
  239. background-color: var(--dropDownHoverBackgroundColor) !important;
  240. }
  241. .el-tag.el-tag--info {
  242. color: var(--bs-el-text) !important;
  243. }
  244. .popper__arrow {
  245. bottom: 0 !important;
  246. &:after {
  247. bottom: 0 !important;
  248. }
  249. }
  250. }
  251. </style>