index.vue 7.7 KB

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