index.vue 8.4 KB

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