color.js 5.3 KB

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