dataVMixins.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. const dataVMixins = {
  2. props: {
  3. // 卡片的属性
  4. config: {
  5. type: Object,
  6. default: () => ({})
  7. }
  8. },
  9. data () {
  10. return {
  11. borderBgId: `borderBg${this.config.code}`
  12. }
  13. },
  14. computed: {
  15. code () {
  16. return this.config.code
  17. },
  18. color () {
  19. return this.config.customize.borderMainColor ||
  20. this.config.customize.borderSecondaryColor
  21. ? [
  22. this.config.customize.borderMainColor,
  23. this.config.customize.borderSecondaryColor
  24. ]
  25. : null
  26. },
  27. backgroundColor () {
  28. return this.config.customize.backgroundColor
  29. ? this.config.customize.backgroundColor
  30. : 'transparent'
  31. },
  32. colorType () {
  33. return this.config.customize.colorType
  34. }
  35. },
  36. mounted () {
  37. if (document.querySelector(`#dataV${this.config.code}`)) {
  38. const borderElement = document.querySelector(`#dataV${this.config.code}`).querySelector('.border') || document.querySelector(`#dataV${this.config.code}`)?.querySelector('.dv-border-svg-container')
  39. if (borderElement) {
  40. let isBorder7 = false
  41. borderElement.childNodes.forEach(e => {
  42. if (e._prevClass === 'dv-bb7-line-width-2') {
  43. isBorder7 = true
  44. }
  45. })
  46. borderElement.style.opacity = (this.config.customize.opacity / 100)
  47. if (this.colorType === 'gradient') {
  48. if (!isBorder7) {
  49. let gradientDirection = ''
  50. switch (this.config.customize.gradientDirection) {
  51. case 'to right':
  52. gradientDirection = 'x1="0%" y1="0%" x2="100%" y2="0%"'
  53. break
  54. case 'to left':
  55. gradientDirection = 'x1="100%" y1="0%" x2="0%" y2="0%"'
  56. break
  57. case 'to bottom':
  58. gradientDirection = 'x1="0%" y1="0%" x2="0%" y2="100%"'
  59. break
  60. case 'to top':
  61. gradientDirection = 'x1="0%" y1="100%" x2="0%" y2="0%"'
  62. break
  63. case 'to bottom right':
  64. gradientDirection = 'x1="0%" y1="0%" x2="100%" y2="100%"'
  65. break
  66. case 'to bottom left':
  67. gradientDirection = 'x1="100%" y1="0%" x2="0%" y2="100%"'
  68. break
  69. case 'to top right':
  70. gradientDirection = 'x1="0%" y1="100%" x2="100%" y2="0%"'
  71. break
  72. case 'to top left':
  73. gradientDirection = 'x1="100%" y1="100%" x2="0%" y2="0%"'
  74. break
  75. default:
  76. gradientDirection = 'x1="0%" y1="0%" x2="100%" y2="0%"'
  77. break
  78. }
  79. // 在目标元素内的第一个位置插入 <defs> 和其中的内容
  80. borderElement.insertAdjacentHTML(
  81. 'afterbegin',
  82. `<defs>
  83. <linearGradient id="${this.borderBgId}" ${gradientDirection}>
  84. <stop offset="0%" stop-color="${this.config.customize.gradientColor0}" />
  85. <stop offset="100%" stop-color="${this.config.customize.gradientColor1}" />
  86. </linearGradient>
  87. </defs>`
  88. )
  89. } else {
  90. borderElement.style.background = `linear-gradient(${this.config.customize.gradientDirection},${this.config.customize.gradientColor0}, ${this.config.customize.gradientColor1})`
  91. isBorder7 = false
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }
  98. export { dataVMixins }