index.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. },
  65. mounted () {
  66. window.dataSetFields = []
  67. this.changeStyle(this.config)
  68. EventBus.$on('changeBusinessKey', () => {
  69. window.dataSetFields = []
  70. })
  71. if (this.isPreview) {
  72. if (window.dataSetFields.length === 0) {
  73. getDataSetDetails(this.config.dataSource.businessKey).then(res => {
  74. window.dataSetFields = res.fields.map(field => {
  75. return {
  76. label: field.comment || field.fieldDesc,
  77. value: field.name || field.fieldName
  78. }
  79. })
  80. this.placeholder = window.dataSetFields.find(field => field.value === this.config.dataSource.dimensionField)?.label || ''
  81. })
  82. }
  83. }
  84. },
  85. beforeDestroy () {
  86. EventBus.$off('changeBusinessKey')
  87. },
  88. methods: {
  89. dataFormatting (config, data) {
  90. // 数据返回成功则赋值
  91. if (data.success) {
  92. data = data.data
  93. // 获取到后端返回的数据,有则赋值
  94. if (config.dataHandler) {
  95. try {
  96. // 此处函数处理data
  97. eval(config.dataHandler)
  98. } catch (e) {
  99. console.info(e)
  100. }
  101. }
  102. config.option.data = data
  103. this.optionData = data
  104. config.customize.title = config.option.data[config.dataSource.dimensionField] || config.customize.title
  105. if (window.dataSetFields.length === 0) {
  106. getDataSetDetails(this.config.dataSource.businessKey).then(res => {
  107. window.dataSetFields = res.fields.map(field => {
  108. return {
  109. label: field.comment || field.fieldDesc,
  110. value: field.name || field.fieldName
  111. }
  112. })
  113. this.placeholder = window.dataSetFields.find(field => field.value === this.config.dataSource.dimensionField)?.label || ''
  114. })
  115. }
  116. // 语音播报
  117. } else {
  118. // 数据返回失败则赋前端的模拟数据
  119. config.option.data = []
  120. this.optionData = []
  121. }
  122. return config
  123. },
  124. changeStyle (config) {
  125. // 样式改变时更新主题配置
  126. config.theme = settingToTheme(cloneDeep(config), this.customTheme)
  127. this.changeChartConfig(config)
  128. this.innerConfig = config
  129. // 选择器元素
  130. const selectInputEl = document.querySelector(`.select-${config.code} .el-input__inner`)
  131. // 背景颜色
  132. selectInputEl.style.backgroundColor = config.customize.backgroundColor
  133. // 字体大小
  134. selectInputEl.style.fontSize = config.customize.fontSize + 'px'
  135. // 字体颜色
  136. selectInputEl.style.color = config.customize.fontColor
  137. // 下拉图标
  138. const selectDropdownIcon = document.querySelector(`.select-${config.code} .el-icon-arrow-up`)
  139. selectDropdownIcon.style.fontSize = config.customize.fontSize + 'px'
  140. // 选择器下拉框元素
  141. const selectDropdownEl = document.querySelector(`.select-${config.code} .el-select-dropdown`)
  142. // 箭头背景颜色和下拉框背景颜色一致
  143. if (selectDropdownEl) {
  144. // 下拉框无边框
  145. selectDropdownEl.style.border = 'none'
  146. // 背景颜色
  147. selectDropdownEl.style.backgroundColor = config.customize.dropDownBackgroundColor
  148. }
  149. },
  150. // 组件联动
  151. selectChange (val) {
  152. this.linkage(this.config.option.data.find(item => item[this.config.dataSource.metricField] === val))
  153. },
  154. visibleChange (val) {
  155. if (val) {
  156. // 修改下拉框背景颜色,让下拉框背景颜色和箭头背景颜色一致
  157. const selectDropdownEl = document.querySelector(`.select-popper-${this.innerConfig.code}`)
  158. selectDropdownEl.style.color = this.innerConfig.customize.dropDownBackgroundColor
  159. // 空状态
  160. const selectDropdownEmptyEl = document.querySelector(`.select-popper-${this.innerConfig.code} .el-select-dropdown__empty`)
  161. if (selectDropdownEmptyEl) {
  162. selectDropdownEmptyEl.style.backgroundColor = this.innerConfig.customize.dropDownBackgroundColor
  163. }
  164. // 下拉项hover颜色
  165. const selectDropdownItemEl = document.querySelectorAll(`.select-popper-${this.innerConfig.code} .el-select-dropdown__item`)
  166. // 给--dropDownHoverFontColor 和 --dropDownHoverBackgroundColor 赋值
  167. selectDropdownItemEl.forEach(item => {
  168. item.style.setProperty('--dropDownHoverFontColor', this.innerConfig.customize.dropDownHoverFontColor)
  169. item.style.setProperty('--dropDownHoverBackgroundColor', this.innerConfig.customize.dropDownHoverBackgroundColor)
  170. })
  171. // 激活项
  172. // const selectDropdownItemSelectedEl = document.querySelectorAll(`.select-popper-${this.innerConfig.code} .el-select-dropdown__item.selected`)
  173. // selectDropdownItemSelectedEl.forEach(item => {
  174. // item.style.color = this.innerConfig.customize.activeFontColor
  175. // item.style.backgroundColor = this.innerConfig.customize.activeBackgroundColor
  176. // })
  177. }
  178. // 不是激活项的还是使用背景颜色
  179. const selectDropdownItemEl = document.querySelectorAll(`.select-popper-${this.innerConfig.code} .el-select-dropdown__item`)
  180. selectDropdownItemEl.forEach(item => {
  181. // 检查是否是激活项,不是则使用背景颜色
  182. if (!item.classList.contains('selected')) {
  183. item.style.color = this.innerConfig.customize.dropDownFontColor
  184. item.style.backgroundColor = this.innerConfig.customize.dropDownBackgroundColor
  185. }
  186. })
  187. },
  188. mouseenter () {
  189. if (this.value) {
  190. setTimeout(() => {
  191. // 清空图标
  192. const selectDropdownCloseIcon = document.querySelector(`.select-${this.innerConfig.code} .el-icon-circle-close`)
  193. if (selectDropdownCloseIcon) {
  194. selectDropdownCloseIcon.style.fontSize = this.innerConfig.customize.fontSize + 'px'
  195. }
  196. }, 30)
  197. }
  198. }
  199. }
  200. }
  201. </script>
  202. <style lang="scss">
  203. .basic-component-select{
  204. color: '';
  205. .el-select-dropdown__wrap {
  206. margin-bottom: 0px !important;
  207. }
  208. .el-select-group__wrap:not(:last-of-type)::after {
  209. background-color: transparent !important;
  210. }
  211. .popper__arrow{
  212. border-bottom-color:var(--color) !important;
  213. &::after{
  214. border-bottom-color:var(--color) !important;
  215. }
  216. }
  217. }
  218. </style>
  219. <style lang="scss" scoped>
  220. .basic-component-select {
  221. width: 100%;
  222. height: 100%;
  223. ::v-deep .el-input {
  224. height: 100% !important;
  225. .el-select__caret{
  226. width: 100%;
  227. height: 100%;
  228. display: flex;
  229. align-items: center;
  230. }
  231. // 选择器输入框样式
  232. .el-input__inner {
  233. height: 100% !important;
  234. border-color: var(--bs-el-border) !important;
  235. }
  236. }
  237. .el-select-dropdown__item.hover,
  238. .el-select-dropdown__item:hover {
  239. color: var(--dropDownHoverFontColor) !important;
  240. background-color: var(--dropDownHoverBackgroundColor) !important;
  241. }
  242. .el-tag.el-tag--info {
  243. color: var(--bs-el-text) !important;
  244. }
  245. .popper__arrow {
  246. bottom: 0 !important;
  247. &:after {
  248. bottom: 0 !important;
  249. }
  250. }
  251. }
  252. </style>