1 |
|
2 |
|
3 |
|
4 | 'use strict';
|
5 |
|
6 | const Base = require('../base/Base');
|
7 |
|
8 | module.exports = class ThemeSet extends Base {
|
9 |
|
10 | constructor (config) {
|
11 | super({
|
12 |
|
13 |
|
14 |
|
15 | defaultThemeDirectory: 'view',
|
16 | themeDirectory: 'theme',
|
17 | modulePriority: false,
|
18 | Theme: require('./Theme'),
|
19 | ...config
|
20 | });
|
21 | this.defaultThemeDirectory = path.join(this.directory, this.defaultThemeDirectory);
|
22 | this.themeDirectory = path.join(this.directory, this.themeDirectory);
|
23 | this.createDefault();
|
24 | this.createThemes();
|
25 | this.setParents();
|
26 | this.init();
|
27 | }
|
28 |
|
29 | has (name) {
|
30 | return this._themes[name] instanceof this.Theme;
|
31 | }
|
32 |
|
33 | get (name) {
|
34 | name = name || this.theme;
|
35 | if (!name) {
|
36 | return this._defaultTheme;
|
37 | }
|
38 | if (this.has(name)) {
|
39 | return this._themes[name];
|
40 | }
|
41 | if (this.parent) {
|
42 | return this.parent.get(name);
|
43 | }
|
44 | return this._defaultTheme;
|
45 | }
|
46 |
|
47 | createDefault () {
|
48 | this._defaultTheme = this.spawn(this.Theme, {
|
49 | name: null,
|
50 | directory: this.defaultThemeDirectory,
|
51 | parent: this.parent ? this.parent.get() : null,
|
52 | view: this,
|
53 | isOrigin: this.isOrigin
|
54 | });
|
55 | }
|
56 |
|
57 | createThemes () {
|
58 | this._themes = {};
|
59 | try {
|
60 | for (const name of fs.readdirSync(this.themeDirectory)) {
|
61 | const dir = path.join(this.themeDirectory, name);
|
62 | if (fs.lstatSync(dir).isDirectory()) {
|
63 | this.createTheme(name, dir);
|
64 | }
|
65 | }
|
66 | } catch {}
|
67 | }
|
68 |
|
69 | createTheme (name, directory) {
|
70 | this._themes[name] = this.spawn(this.Theme, {view: this, name, directory});
|
71 | }
|
72 |
|
73 | setParents () {
|
74 | for (const name of Object.keys(this._themes)) {
|
75 | this._themes[name].parent = this.getParentByName(name) || this._defaultTheme;
|
76 | }
|
77 | }
|
78 |
|
79 | getParentByName (name) {
|
80 | const pos = name.lastIndexOf('.');
|
81 | if (pos > 0) {
|
82 | const parentName = name.substring(0, pos);
|
83 | if (this.has(parentName)) {
|
84 | return this._themes[parentName];
|
85 | }
|
86 | } else if (this.parent && !this.modulePriority) {
|
87 | return this.parent.get(name);
|
88 | }
|
89 | }
|
90 |
|
91 | init () {
|
92 | this._defaultTheme.init();
|
93 | for (const theme of Object.values(this._themes)) {
|
94 | theme.init();
|
95 | }
|
96 | }
|
97 |
|
98 | isEmpty () {
|
99 | if (!this._defaultTheme.isEmpty()) {
|
100 | return false;
|
101 | }
|
102 | for (const theme of Object.values(this._themes)) {
|
103 | if (!theme.isEmpty()) {
|
104 | return false;
|
105 | }
|
106 | }
|
107 | return true;
|
108 | }
|
109 | };
|
110 |
|
111 | const fs = require('fs');
|
112 | const path = require('path'); |
\ | No newline at end of file |