index.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <script>
  2. import bus from '../../../bus.js';
  3. import Loading from './loading';
  4. import DocStyle from './docStyle';
  5. import { updateVars } from './api.js';
  6. import { updateDomHeadStyle } from '../utils.js';
  7. import {
  8. ACTION_APPLY_THEME,
  9. ACTION_DOWNLOAD_THEME,
  10. ACTION_USER_CONFIG_UPDATE
  11. } from '../constant.js';
  12. import {
  13. loadUserThemeFromLocal,
  14. loadPreviewFromLocal
  15. } from '../localstorage.js';
  16. import { getActionDisplayName } from '../../theme-configurator/utils/utils';
  17. export default {
  18. mixins: [Loading, DocStyle],
  19. mounted() {
  20. this.checkLocalThemeConfig();
  21. bus.$on(ACTION_APPLY_THEME, val => {
  22. this.userConfig = val;
  23. this.onAction();
  24. });
  25. bus.$on(ACTION_DOWNLOAD_THEME, val => {
  26. this.onDownload(val);
  27. });
  28. },
  29. data() {
  30. return {
  31. userConfig: {},
  32. lastApply: 0
  33. };
  34. },
  35. methods: {
  36. applyStyle(res, time) {
  37. if (time < this.lastApply) return;
  38. this.updateDocs(() => {
  39. updateDomHeadStyle('chalk-style', res);
  40. });
  41. this.lastApply = time;
  42. },
  43. onDownload(themeConfig) {
  44. this.triggertProgressBar(true);
  45. updateVars(
  46. Object.assign({}, themeConfig, { download: true }),
  47. xhr => {
  48. xhr.responseType = 'blob';
  49. }
  50. ).then()
  51. .catch((err) => {
  52. this.onError(err);
  53. })
  54. .then(() => {
  55. this.triggertProgressBar(false);
  56. });
  57. ga('send', 'event', 'ThemeConfigurator', 'Download');
  58. },
  59. onAction() {
  60. this.triggertProgressBar(true);
  61. const time = +new Date();
  62. updateVars(this.userConfig)
  63. .then(res => {
  64. this.applyStyle(res, time);
  65. })
  66. .catch(err => {
  67. this.onError(err);
  68. })
  69. .then(() => {
  70. this.triggertProgressBar(false);
  71. });
  72. },
  73. onError(err) {
  74. let message;
  75. try {
  76. message = JSON.parse(err).message;
  77. } catch (e) {
  78. message = err;
  79. }
  80. this.$message.error(message);
  81. },
  82. triggertProgressBar(isLoading) {
  83. bus.$emit('user-theme-config-loading', isLoading);
  84. },
  85. updateDocs(cb) {
  86. window.userThemeConfig = JSON.parse(JSON.stringify(this.userConfig));
  87. bus.$emit(ACTION_USER_CONFIG_UPDATE, this.userConfig);
  88. this.updateDocStyle(this.userConfig, cb);
  89. },
  90. checkLocalThemeConfig() {
  91. // load user local theme
  92. const previewConfig = loadPreviewFromLocal();
  93. if (previewConfig.type === 'user') {
  94. const userConfig = loadUserThemeFromLocal();
  95. this.$message(getActionDisplayName('load-local-theme-config'));
  96. const config = userConfig.filter(theme => (theme.name === previewConfig.name));
  97. if (config && config[0]) {
  98. this.userConfig = JSON.parse(config[0].theme);
  99. this.onAction();
  100. }
  101. }
  102. }
  103. }
  104. };
  105. </script>