Skip to content

Commit e9e08ac

Browse files
committed
added support for vue3 , fixed some other bugs
1 parent de2530c commit e9e08ac

File tree

4 files changed

+543
-196
lines changed

4 files changed

+543
-196
lines changed

index.js

Lines changed: 10 additions & 195 deletions
Original file line numberDiff line numberDiff line change
@@ -1,195 +1,10 @@
1-
import Vue from 'vue'
2-
3-
// const version = Number(Vue.version.split('.')[0]);
4-
// if (version === 2) {
5-
// } else if (version === 3) {
6-
// }
7-
8-
var WRAPPER = {
9-
default: {},
10-
name: "",
11-
names: []
12-
}
13-
14-
15-
16-
const holder = Vue.observable(WRAPPER);
17-
18-
19-
class ThemeManager {
20-
constructor(options) {
21-
this.theme = holder
22-
this.currentTheme = holder[holder.name]
23-
this.name = holder.name
24-
this.names = holder[holder.names]
25-
this.init(options)
26-
}
27-
28-
init(theme) {
29-
if (Array.isArray(theme.names)) {
30-
if (theme.names.indexOf('default') == -1) {
31-
theme.names.push('default');
32-
}
33-
for (var i = 0; i < theme.names.length; i++) {
34-
this.addName(theme.names[i]);
35-
if (!WRAPPER.hasOwnProperty(theme.names[i])) {
36-
Vue.set(WRAPPER, theme.names[i], {})
37-
}
38-
}
39-
}
40-
41-
var _options = theme.options;
42-
if (typeof _options !== 'object' && _options !== null) { // incase it is a json string
43-
_options = JSON.parse(_options)
44-
} //convert to object
45-
46-
47-
48-
49-
if (typeof _options === 'object' && _options !== null) { //second check
50-
var getkeys = Object.keys(_options);
51-
for (var i = 0; i < getkeys.length; i++) {
52-
let currentKey = getkeys[i];
53-
let themex = _options[currentKey];
54-
this.addTheme(themex, currentKey, theme.activate !== 'undefined' ? theme.activate == currentKey : false);
55-
}
56-
} else {
57-
throw new Error("Options shlould be Object or JSON string");
58-
}
59-
60-
61-
}
62-
63-
addName(name) {
64-
if (WRAPPER.names.indexOf(name) == -1) {
65-
WRAPPER.names.push(name);
66-
}
67-
68-
}
69-
70-
addTheme(val, themeName = 'default', activate = false) {
71-
if (activate) {
72-
WRAPPER.name = themeName
73-
}
74-
this.addName(themeName);
75-
76-
var data = val;
77-
if (typeof data !== 'object' && data !== null) {
78-
data = JSON.parse(data)
79-
} //convert to object
80-
81-
if (!WRAPPER.hasOwnProperty(themeName)) { //theme doesn't exist yet so we create one
82-
Vue.set(WRAPPER, themeName, {})
83-
}
84-
85-
86-
87-
88-
let xx = Object.keys(data);
89-
for (var i = 0; i < xx.length; i++) {
90-
let property = xx[i];
91-
if (!WRAPPER[themeName].hasOwnProperty(property)) { //if theme property doesn't exist yet
92-
Vue.set(WRAPPER[themeName], property, data[property])
93-
} else {
94-
WRAPPER[themeName][property] = data[property];
95-
}
96-
}
97-
this.updateBaseTheme();
98-
return this
99-
}
100-
101-
updateBaseTheme() { //update plugin.theme.... accessor
102-
try {
103-
let currentKeys = Object.keys(WRAPPER[WRAPPER.name]);
104-
let currentTheme = WRAPPER[WRAPPER.name];
105-
106-
for (var i = 0; i < currentKeys.length; i++) {
107-
let c = currentKeys[i]
108-
Vue.set(WRAPPER, c, currentTheme[c]);
109-
}
110-
} catch (err) {
111-
//console.log(err)
112-
}
113-
}
114-
115-
// flushTheme(){
116-
// let keys = Object.keys(WRAPPER);
117-
// for(var i = 0; i < keys.length; i++){
118-
// let prop = keys[i];
119-
// if(prop != ['currentTheme'] && prop != ['name'] && typeof WRAPPER[prop] !== 'object'){
120-
// console.log(prop)
121-
// Vue.delete(WRAPPER,prop)
122-
// }
123-
// }
124-
125-
// }
126-
127-
activateTheme(name) {
128-
WRAPPER.name = name;
129-
this.updateBaseTheme();
130-
this.saveTheme();
131-
return this
132-
}
133-
134-
135-
getTheme(which, themeName = WRAPPER.name) {
136-
if (!Array.isArray(which)) {
137-
throw new Error("color names shlould be in an Array");
138-
}
139-
var result = {};
140-
for (let i = 0; i < which.length; i++) {
141-
let c = which[i]
142-
try {
143-
Object.defineProperty(result, c, {
144-
value: WRAPPER[themeName][c],
145-
writable: true,
146-
enumerable: true
147-
})
148-
} catch (err) {
149-
throw new Error(err)
150-
}
151-
152-
}
153-
return result;
154-
}
155-
156-
saveTheme() {
157-
var options = {};
158-
let keys = Object.keys(WRAPPER);
159-
for (var i = 0; i < keys.length; i++) {
160-
let prop = keys[i];
161-
if (typeof WRAPPER[prop] === 'object' && !Array.isArray(WRAPPER[prop])) { //get all themes by object detection
162-
Vue.set(options, prop, WRAPPER[prop]);
163-
}
164-
}
165-
localStorage.setItem('APP_THEME', JSON.stringify({
166-
names: WRAPPER.names,
167-
activate: WRAPPER.name,
168-
options: options
169-
}));
170-
171-
return this
172-
}
173-
getThemeFromStorage() {
174-
let th = localStorage.getItem('APP_THEME');
175-
if (th !== null) {
176-
this.init(JSON.parse(th));
177-
178-
}
179-
//get from localStorge then initialize
180-
return this
181-
}
182-
}
183-
const VueThemeManager = {
184-
install(Vue, options) {
185-
Vue.prototype.$AppTheme = new ThemeManager(options);
186-
187-
Vue.mixin({
188-
mounted() {
189-
this.$AppTheme.getThemeFromStorage();
190-
}
191-
});
192-
}
193-
}
194-
195-
export default VueThemeManager;
1+
try {
2+
const version = Number(require('vue/package.json').version.split('.')[0]);
3+
if (version === 2) {
4+
module.exports = exports = require('./lib/vue2xlib');
5+
} else if (version === 3) {
6+
module.exports = exports = require('./lib/vue3xlib');
7+
}
8+
} catch (error) {
9+
console.log('[VueThemeManger] '+error);
10+
}

0 commit comments

Comments
 (0)