index.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. config = { ...this.config, ...config }
  126. // 样式改变时更新主题配置
  127. config.theme = settingToTheme(cloneDeep(config), this.customTheme)
  128. this.changeChartConfig(config)
  129. this.innerConfig = config
  130. // 选择器元素
  131. const selectInputEl = document.querySelector(`.select-${config.code} .el-input__inner`)
  132. // 背景颜色
  133. selectInputEl.style.backgroundColor = config.customize.backgroundColor
  134. // 字体大小
  135. selectInputEl.style.fontSize = config.customize.fontSize + 'px'
  136. // 字体颜色
  137. selectInputEl.style.color = config.customize.fontColor
  138. // 下拉图标
  139. const selectDropdownIcon = document.querySelector(`.select-${config.code} .el-icon-arrow-up`)
  140. selectDropdownIcon.style.fontSize = config.customize.fontSize + 'px'
  141. // 选择器下拉框元素
  142. const selectDropdownEl = document.querySelector(`.select-${config.code} .el-select-dropdown`)
  143. // 箭头背景颜色和下拉框背景颜色一致
  144. if (selectDropdownEl) {
  145. // 下拉框无边框
  146. selectDropdownEl.style.border = 'none'
  147. // 背景颜色
  148. selectDropdownEl.style.backgroundColor = config.customize.dropDownBackgroundColor
  149. }
  150. },
  151. // 组件联动
  152. selectChange (val) {
  153. this.linkage(this.config.option.data.find(item => item[this.config.dataSource.metricField] === val))
  154. },
  155. visibleChange (val) {
  156. if (val) {
  157. // 修改下拉框背景颜色,让下拉框背景颜色和箭头背景颜色一致
  158. const selectDropdownEl = document.querySelector(`.select-popper-${this.innerConfig.code}`)
  159. selectDropdownEl.style.color = this.innerConfig.customize.dropDownBackgroundColor
  160. // 空状态
  161. const selectDropdownEmptyEl = document.querySelector(`.select-popper-${this.innerConfig.code} .el-select-dropdown__empty`)
  162. if (selectDropdownEmptyEl) {
  163. selectDropdownEmptyEl.style.backgroundColor = this.innerConfig.customize.dropDownBackgroundColor
  164. }
  165. // 下拉项hover颜色
  166. const selectDropdownItemEl = document.querySelectorAll(`.select-popper-${this.innerConfig.code} .el-select-dropdown__item`)
  167. // 给--dropDownHoverFontColor 和 --dropDownHoverBackgroundColor 赋值
  168. selectDropdownItemEl.forEach(item => {
  169. item.style.setProperty('--dropDownHoverFontColor', this.innerConfig.customize.dropDownHoverFontColor)
  170. item.style.setProperty('--dropDownHoverBackgroundColor', this.innerConfig.customize.dropDownHoverBackgroundColor)
  171. })
  172. // 激活项
  173. // const selectDropdownItemSelectedEl = document.querySelectorAll(`.select-popper-${this.innerConfig.code} .el-select-dropdown__item.selected`)
  174. // selectDropdownItemSelectedEl.forEach(item => {
  175. // item.style.color = this.innerConfig.customize.activeFontColor
  176. // item.style.backgroundColor = this.innerConfig.customize.activeBackgroundColor
  177. // })
  178. }
  179. // 不是激活项的还是使用背景颜色
  180. const selectDropdownItemEl = document.querySelectorAll(`.select-popper-${this.innerConfig.code} .el-select-dropdown__item`)
  181. selectDropdownItemEl.forEach(item => {
  182. // 检查是否是激活项,不是则使用背景颜色
  183. if (!item.classList.contains('selected')) {
  184. item.style.color = this.innerConfig.customize.dropDownFontColor
  185. item.style.backgroundColor = this.innerConfig.customize.dropDownBackgroundColor
  186. }
  187. })
  188. },
  189. mouseenter () {
  190. if (this.value) {
  191. setTimeout(() => {
  192. // 清空图标
  193. const selectDropdownCloseIcon = document.querySelector(`.select-${this.innerConfig.code} .el-icon-circle-close`)
  194. if (selectDropdownCloseIcon) {
  195. selectDropdownCloseIcon.style.fontSize = this.innerConfig.customize.fontSize + 'px'
  196. }
  197. }, 30)
  198. }
  199. }
  200. }
  201. }
  202. </script>
  203. <style lang="scss">
  204. .basic-component-select{
  205. color: '';
  206. .el-select-dropdown__wrap {
  207. margin-bottom: 0px !important;
  208. }
  209. .el-select-group__wrap:not(:last-of-type)::after {
  210. background-color: transparent !important;
  211. }
  212. .popper__arrow{
  213. border-bottom-color:var(--color) !important;
  214. &::after{
  215. border-bottom-color:var(--color) !important;
  216. }
  217. }
  218. }
  219. </style>
  220. <style lang="scss" scoped>
  221. .basic-component-select {
  222. width: 100%;
  223. height: 100%;
  224. ::v-deep .el-input {
  225. height: 100% !important;
  226. .el-select__caret{
  227. width: 100%;
  228. height: 100%;
  229. display: flex;
  230. align-items: center;
  231. }
  232. // 选择器输入框样式
  233. .el-input__inner {
  234. height: 100% !important;
  235. border-color: var(--bs-el-border) !important;
  236. }
  237. }
  238. .el-select-dropdown__item.hover,
  239. .el-select-dropdown__item:hover {
  240. color: var(--dropDownHoverFontColor) !important;
  241. background-color: var(--dropDownHoverBackgroundColor) !important;
  242. }
  243. .el-tag.el-tag--info {
  244. color: var(--bs-el-text) !important;
  245. }
  246. .popper__arrow {
  247. bottom: 0 !important;
  248. &:after {
  249. bottom: 0 !important;
  250. }
  251. }
  252. }
  253. </style>