Rap 原分销系统代码Web
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

c96fa2f6f85108831ffca762beee7fad95bb2dbe.svn-base 4.6KB

5 달 전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <template>
  2. <el-color-picker
  3. v-model="theme"
  4. :predefine="['#409EFF', '#1890ff', '#304156','#212121','#11a983', '#13c2c2', '#6959CD', '#f5222d']"
  5. class="theme-picker"
  6. popper-class="theme-picker-dropdown"
  7. />
  8. </template>
  9. <script>
  10. const version = require('element-ui/package.json').version // element-ui version from node_modules
  11. const ORIGINAL_THEME = '#409EFF' // default color
  12. import Cookies from 'js-cookie'
  13. export default {
  14. data() {
  15. return {
  16. chalk: '', // content of theme-chalk css
  17. theme: ''
  18. }
  19. },
  20. computed: {
  21. defaultTheme() {
  22. return this.$store.state.settings.theme
  23. }
  24. },
  25. watch: {
  26. defaultTheme: {
  27. handler: function(val, oldVal) {
  28. this.theme = val
  29. },
  30. immediate: true
  31. },
  32. async theme(val) {
  33. Cookies.set('theme', val, { expires: 365 })
  34. const oldVal = this.chalk ? this.theme : Cookies.get('theme') ? Cookies.get('theme') : ORIGINAL_THEME
  35. if (typeof val !== 'string') return
  36. const themeCluster = this.getThemeCluster(val.replace('#', ''))
  37. const originalCluster = this.getThemeCluster(oldVal.replace('#', ''))
  38. const getHandler = (variable, id) => {
  39. return () => {
  40. const originalCluster = this.getThemeCluster(ORIGINAL_THEME.replace('#', ''))
  41. const newStyle = this.updateStyle(this[variable], originalCluster, themeCluster)
  42. let styleTag = document.getElementById(id)
  43. if (!styleTag) {
  44. styleTag = document.createElement('style')
  45. styleTag.setAttribute('id', id)
  46. document.head.appendChild(styleTag)
  47. }
  48. styleTag.innerText = newStyle
  49. }
  50. }
  51. if (!this.chalk) {
  52. const url = `https://unpkg.com/element-ui@${version}/lib/theme-chalk/index.css`
  53. await this.getCSSString(url, 'chalk')
  54. }
  55. const chalkHandler = getHandler('chalk', 'chalk-style')
  56. chalkHandler()
  57. const styles = [].slice.call(document.querySelectorAll('style'))
  58. .filter(style => {
  59. const text = style.innerText
  60. return new RegExp(oldVal, 'i').test(text) && !/Chalk Variables/.test(text)
  61. })
  62. styles.forEach(style => {
  63. const { innerText } = style
  64. if (typeof innerText !== 'string') return
  65. style.innerText = this.updateStyle(innerText, originalCluster, themeCluster)
  66. })
  67. this.$emit('change', val)
  68. }
  69. },
  70. methods: {
  71. updateStyle(style, oldCluster, newCluster) {
  72. let newStyle = style
  73. oldCluster.forEach((color, index) => {
  74. newStyle = newStyle.replace(new RegExp(color, 'ig'), newCluster[index])
  75. })
  76. return newStyle
  77. },
  78. getCSSString(url, variable) {
  79. return new Promise(resolve => {
  80. const xhr = new XMLHttpRequest()
  81. xhr.onreadystatechange = () => {
  82. if (xhr.readyState === 4 && xhr.status === 200) {
  83. this[variable] = xhr.responseText.replace(/@font-face{[^}]+}/, '')
  84. resolve()
  85. }
  86. }
  87. xhr.open('GET', url)
  88. xhr.send()
  89. })
  90. },
  91. getThemeCluster(theme) {
  92. const tintColor = (color, tint) => {
  93. let red = parseInt(color.slice(0, 2), 16)
  94. let green = parseInt(color.slice(2, 4), 16)
  95. let blue = parseInt(color.slice(4, 6), 16)
  96. if (tint === 0) { // when primary color is in its rgb space
  97. return [red, green, blue].join(',')
  98. } else {
  99. red += Math.round(tint * (255 - red))
  100. green += Math.round(tint * (255 - green))
  101. blue += Math.round(tint * (255 - blue))
  102. red = red.toString(16)
  103. green = green.toString(16)
  104. blue = blue.toString(16)
  105. return `#${red}${green}${blue}`
  106. }
  107. }
  108. const shadeColor = (color, shade) => {
  109. let red = parseInt(color.slice(0, 2), 16)
  110. let green = parseInt(color.slice(2, 4), 16)
  111. let blue = parseInt(color.slice(4, 6), 16)
  112. red = Math.round((1 - shade) * red)
  113. green = Math.round((1 - shade) * green)
  114. blue = Math.round((1 - shade) * blue)
  115. red = red.toString(16)
  116. green = green.toString(16)
  117. blue = blue.toString(16)
  118. return `#${red}${green}${blue}`
  119. }
  120. const clusters = [theme]
  121. for (let i = 0; i <= 9; i++) {
  122. clusters.push(tintColor(theme, Number((i / 10).toFixed(2))))
  123. }
  124. clusters.push(shadeColor(theme, 0.1))
  125. return clusters
  126. }
  127. }
  128. }
  129. </script>
  130. <style>
  131. .theme-message,
  132. .theme-picker-dropdown {
  133. z-index: 99999 !important;
  134. }
  135. .theme-picker .el-color-picker__trigger {
  136. height: 26px !important;
  137. width: 26px !important;
  138. padding: 2px;
  139. }
  140. .theme-picker-dropdown .el-color-dropdown__link-btn {
  141. display: none;
  142. }
  143. </style>