dataVMixins.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { mapMutations, mapState } from 'vuex'
  2. import { settingToTheme } from 'data-room-ui/js/utils/themeFormatting'
  3. import cloneDeep from 'lodash/cloneDeep'
  4. const dataVMixins = {
  5. props: {
  6. // 卡片的属性
  7. config: {
  8. type: Object,
  9. default: () => ({})
  10. }
  11. },
  12. data () {
  13. return {
  14. updateKey: 0,
  15. borderBgId: null
  16. }
  17. },
  18. computed: {
  19. ...mapState({
  20. customTheme: state => state.bigScreen.pageInfo.pageConfig.customTheme,
  21. activeCode: state => state.bigScreen.activeCode
  22. }),
  23. code () {
  24. return this.config.code
  25. },
  26. color () {
  27. return this.config.customize.borderMainColor ||
  28. this.config.customize.borderSecondaryColor
  29. ? [
  30. this.config.customize.borderMainColor,
  31. this.config.customize.borderSecondaryColor
  32. ]
  33. : null
  34. },
  35. backgroundColor () {
  36. return this.config.customize.backgroundColor
  37. ? this.config.customize.backgroundColor
  38. : 'transparent'
  39. },
  40. colorType () {
  41. return this.config.customize.colorType
  42. },
  43. Data () {
  44. return JSON.parse(JSON.stringify(this.config))
  45. }
  46. },
  47. watch: {
  48. config: {
  49. handler: function (val) {
  50. if (val && val.customize && val.customize.colorType) {
  51. this.changeBorderStyle(this.config)
  52. if (val.customize.colorType === 'single') {
  53. this.borderBgId = null
  54. this.$nextTick(() => {
  55. this.updateKey = new Date().getTime()
  56. })
  57. } else {
  58. this.borderBgId = `borderBg${this.config.code}`
  59. }
  60. }
  61. },
  62. deep: true
  63. },
  64. Data: {
  65. handler (newVal, oldVal) {
  66. this.$nextTick(() => {
  67. if ((newVal.w !== oldVal.w) || (newVal.h !== oldVal.h)) {
  68. this.$nextTick(() => {
  69. this.updateKey = new Date().getTime()
  70. })
  71. }
  72. })
  73. },
  74. deep: true
  75. }
  76. },
  77. mounted () {
  78. this.changeBorderStyle(this.config)
  79. },
  80. methods: {
  81. ...mapMutations({
  82. changeChartConfig: 'bigScreen/changeChartConfig',
  83. changeActiveItemConfig: 'bigScreen/changeActiveItemConfig'
  84. }),
  85. changeStyle (config) {
  86. config = { ...this.config, ...config }
  87. // 样式改变时更新主题配置
  88. config.theme = settingToTheme(cloneDeep(config), this.customTheme)
  89. this.changeChartConfig(config)
  90. if (config.code === this.activeCode) {
  91. this.changeActiveItemConfig(config)
  92. }
  93. this.changeBorderStyle(config)
  94. },
  95. changeBorderStyle (config) {
  96. this.borderBgId = `borderBg${config.code}`
  97. if (document.querySelector(`#dataV${config.code}`)) {
  98. const borderElement = document.querySelector(`#dataV${config.code}`).querySelector('.border') || document.querySelector(`#dataV${config.code}`)?.querySelector('.dv-border-svg-container')
  99. if (borderElement) {
  100. let isBorder7 = false
  101. borderElement.childNodes.forEach(e => {
  102. if (e._prevClass === 'dv-bb7-line-width-2') {
  103. isBorder7 = true
  104. }
  105. })
  106. borderElement.style.opacity = (config.customize.opacity / 100)
  107. if (this.colorType === 'gradient') {
  108. if (!isBorder7) {
  109. let gradientDirection = ''
  110. switch (config.customize.gradientDirection) {
  111. case 'to right':
  112. gradientDirection = 'x1="0%" y1="0%" x2="100%" y2="0%"'
  113. break
  114. case 'to left':
  115. gradientDirection = 'x1="100%" y1="0%" x2="0%" y2="0%"'
  116. break
  117. case 'to bottom':
  118. gradientDirection = 'x1="0%" y1="0%" x2="0%" y2="100%"'
  119. break
  120. case 'to top':
  121. gradientDirection = 'x1="0%" y1="100%" x2="0%" y2="0%"'
  122. break
  123. case 'to bottom right':
  124. gradientDirection = 'x1="0%" y1="0%" x2="100%" y2="100%"'
  125. break
  126. case 'to bottom left':
  127. gradientDirection = 'x1="100%" y1="0%" x2="0%" y2="100%"'
  128. break
  129. case 'to top right':
  130. gradientDirection = 'x1="0%" y1="100%" x2="100%" y2="0%"'
  131. break
  132. case 'to top left':
  133. gradientDirection = 'x1="100%" y1="100%" x2="0%" y2="0%"'
  134. break
  135. default:
  136. gradientDirection = 'x1="0%" y1="0%" x2="100%" y2="0%"'
  137. break
  138. }
  139. // 在目标元素内的第一个位置插入 <defs> 和其中的内容
  140. borderElement.insertAdjacentHTML(
  141. 'afterbegin',
  142. `<defs>
  143. <linearGradient id="${this.borderBgId}" ${gradientDirection}>
  144. <stop offset="0%" stop-color="${config.customize.gradientColor0}" />
  145. <stop offset="100%" stop-color="${config.customize.gradientColor1}" />
  146. </linearGradient>
  147. </defs>`
  148. )
  149. } else {
  150. borderElement.style.background = `linear-gradient(${config.customize.gradientDirection},${config.customize.gradientColor0}, ${config.customize.gradientColor1})`
  151. isBorder7 = false
  152. }
  153. }
  154. }
  155. }
  156. }
  157. }
  158. }
  159. export { dataVMixins }