themes.service.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { Injectable } from '@angular/core';
  2. const themeA = require('../../shared/styles/themes/theme-a.scss');
  3. const themeB = require('../../shared/styles/themes/theme-b.scss');
  4. const themeC = require('../../shared/styles/themes/theme-c.scss');
  5. const themeD = require('../../shared/styles/themes/theme-d.scss');
  6. const themeE = require('../../shared/styles/themes/theme-e.scss');
  7. const themeF = require('../../shared/styles/themes/theme-f.scss');
  8. const themeG = require('../../shared/styles/themes/theme-g.scss');
  9. const themeH = require('../../shared/styles/themes/theme-h.scss');
  10. /*******************
  11. * 色系方案
  12. */
  13. @Injectable()
  14. export class ThemesService {
  15. styleTag: any;
  16. defaultTheme: string = 'A';
  17. constructor() {
  18. this.createStyle();
  19. this.setTheme(this.defaultTheme);
  20. }
  21. private createStyle() {
  22. const head = document.head || document.getElementsByTagName('head')[0];
  23. this.styleTag = document.createElement('style');
  24. this.styleTag.type = 'text/css';
  25. this.styleTag.id = 'appthemes';
  26. head.appendChild(this.styleTag);
  27. }
  28. setTheme(name) {
  29. switch (name) {
  30. case 'A':
  31. this.injectStylesheet(themeA);
  32. break;
  33. case 'B':
  34. this.injectStylesheet(themeB);
  35. break;
  36. case 'C':
  37. this.injectStylesheet(themeC);
  38. break;
  39. case 'D':
  40. this.injectStylesheet(themeD);
  41. break;
  42. case 'E':
  43. this.injectStylesheet(themeE);
  44. break;
  45. case 'F':
  46. this.injectStylesheet(themeF);
  47. break;
  48. case 'G':
  49. this.injectStylesheet(themeG);
  50. break;
  51. case 'H':
  52. this.injectStylesheet(themeH);
  53. break;
  54. }
  55. }
  56. // since v9, content is available in 'default'
  57. injectStylesheet(css) {
  58. this.styleTag.innerHTML = css.default;
  59. }
  60. getDefaultTheme() {
  61. return this.defaultTheme;
  62. }
  63. }