color.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import {
  2. mapGetters
  3. } from 'vuex'
  4. const version = require('element-ui/package.json').version // element-ui version from node_modules
  5. const ORIGINAL_THEME = '#409EFF' // default color
  6. export default function () {
  7. return {
  8. data () {
  9. return {
  10. themeVal: ORIGINAL_THEME
  11. }
  12. },
  13. created () {
  14. this.themeVal = this.theme
  15. },
  16. watch: {
  17. themeVal (val, oldVal) {
  18. this.$store.commit('SET_THEME', val)
  19. this.updateTheme(val, oldVal)
  20. }
  21. },
  22. computed: {
  23. ...mapGetters(['theme'])
  24. },
  25. methods: {
  26. updateTheme (val, oldVal) {
  27. if (typeof val !== 'string') return
  28. const head = document.getElementsByTagName('head')[0]
  29. const themeCluster = this.getThemeCluster(val.replace('#', ''))
  30. const originalCluster = this.getThemeCluster(oldVal.replace('#', ''))
  31. const getHandler = (variable, id) => {
  32. return () => {
  33. const originalCluster = this.getThemeCluster(
  34. ORIGINAL_THEME.replace('#', '')
  35. )
  36. const newStyle = this.updateStyle(
  37. this[variable],
  38. originalCluster,
  39. themeCluster
  40. )
  41. let styleTag = document.getElementById(id)
  42. if (!styleTag) {
  43. styleTag = document.createElement('style')
  44. styleTag.setAttribute('id', id)
  45. head.appendChild(styleTag)
  46. }
  47. styleTag.innerText = newStyle
  48. }
  49. }
  50. const chalkHandler = getHandler('chalk', 'chalk-style')
  51. if (!this.chalk) {
  52. const url = `https://unpkg.com/element-ui@${version}/lib/theme-chalk/index.css`
  53. this.getCSSString(url, chalkHandler, 'chalk')
  54. } else {
  55. chalkHandler()
  56. }
  57. const link = [].slice.call(
  58. document.getElementsByTagName('head')[0].getElementsByTagName('link')
  59. )
  60. for (let i = link.length - 3; i < link.length; i++) {
  61. const style = link[i]
  62. if (style.href.indexOf('app') != -1) {
  63. this.getCSSString(style.href, innerText => {
  64. const originalCluster = this.getThemeCluster(
  65. ORIGINAL_THEME.replace('#', '')
  66. )
  67. const newStyle = this.updateStyle(
  68. innerText,
  69. originalCluster,
  70. themeCluster
  71. )
  72. let styleTag = document.getElementById(i)
  73. if (!styleTag) {
  74. styleTag = document.createElement('style')
  75. styleTag.id = i
  76. styleTag.innerText = newStyle
  77. head.appendChild(styleTag)
  78. }
  79. })
  80. }
  81. }
  82. const styles = [].slice
  83. .call(document.querySelectorAll('style'))
  84. .filter(style => {
  85. const text = style.innerText
  86. return (
  87. new RegExp(oldVal, 'i').test(text) && !/Chalk Variables/.test(text)
  88. )
  89. })
  90. styles.forEach(style => {
  91. const {
  92. innerText
  93. } = style
  94. if (typeof innerText !== 'string') return
  95. style.innerText = this.updateStyle(
  96. innerText,
  97. originalCluster,
  98. themeCluster
  99. )
  100. })
  101. },
  102. updateStyle (style, oldCluster, newCluster) {
  103. let newStyle = style
  104. oldCluster.forEach((color, index) => {
  105. newStyle = newStyle.replace(new RegExp(color, 'ig'), newCluster[index])
  106. })
  107. return newStyle
  108. },
  109. getCSSString (url, callback, variable) {
  110. const xhr = new XMLHttpRequest()
  111. xhr.onreadystatechange = () => {
  112. if (xhr.readyState === 4 && xhr.status === 200) {
  113. if (variable) {
  114. this[variable] = xhr.responseText.replace(/@font-face{[^}]+}/, '')
  115. }
  116. callback(xhr.responseText)
  117. }
  118. }
  119. xhr.open('GET', url)
  120. xhr.send()
  121. },
  122. getThemeCluster (theme) {
  123. const tintColor = (color, tint) => {
  124. let red = parseInt(color.slice(0, 2), 16)
  125. let green = parseInt(color.slice(2, 4), 16)
  126. let blue = parseInt(color.slice(4, 6), 16)
  127. if (tint === 0) {
  128. // when primary color is in its rgb space
  129. return [red, green, blue].join(',')
  130. } else {
  131. red += Math.round(tint * (255 - red))
  132. green += Math.round(tint * (255 - green))
  133. blue += Math.round(tint * (255 - blue))
  134. red = red.toString(16)
  135. green = green.toString(16)
  136. blue = blue.toString(16)
  137. return `#${red}${green}${blue}`
  138. }
  139. }
  140. const shadeColor = (color, shade) => {
  141. let red = parseInt(color.slice(0, 2), 16)
  142. let green = parseInt(color.slice(2, 4), 16)
  143. let blue = parseInt(color.slice(4, 6), 16)
  144. red = Math.round((1 - shade) * red)
  145. green = Math.round((1 - shade) * green)
  146. blue = Math.round((1 - shade) * blue)
  147. red = red.toString(16)
  148. green = green.toString(16)
  149. blue = blue.toString(16)
  150. return `#${red}${green}${blue}`
  151. }
  152. const clusters = [theme]
  153. for (let i = 0; i <= 9; i++) {
  154. clusters.push(tintColor(theme, Number((i / 10).toFixed(2))))
  155. }
  156. clusters.push(shadeColor(theme, 0.1))
  157. return clusters
  158. }
  159. }
  160. }
  161. }