Skip to content

Commit 1399b05

Browse files
committed
lift off
1 parent 9826bc2 commit 1399b05

File tree

4 files changed

+316
-0
lines changed

4 files changed

+316
-0
lines changed

.gitignore

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
lerna-debug.log*
8+
9+
# Diagnostic reports (https://nodejs.org/api/report.html)
10+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
11+
12+
# Runtime data
13+
pids
14+
*.pid
15+
*.seed
16+
*.pid.lock
17+
18+
# Directory for instrumented libs generated by jscoverage/JSCover
19+
lib-cov
20+
21+
# Coverage directory used by tools like istanbul
22+
coverage
23+
*.lcov
24+
25+
# nyc test coverage
26+
.nyc_output
27+
28+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
29+
.grunt
30+
31+
# Bower dependency directory (https://bower.io/)
32+
bower_components
33+
34+
# node-waf configuration
35+
.lock-wscript
36+
37+
# Compiled binary addons (https://nodejs.org/api/addons.html)
38+
build/Release
39+
40+
# Dependency directories
41+
node_modules/
42+
jspm_packages/
43+
44+
# TypeScript v1 declaration files
45+
typings/
46+
47+
# TypeScript cache
48+
*.tsbuildinfo
49+
50+
# Optional npm cache directory
51+
.npm
52+
53+
# Optional eslint cache
54+
.eslintcache
55+
56+
# Microbundle cache
57+
.rpt2_cache/
58+
.rts2_cache_cjs/
59+
.rts2_cache_es/
60+
.rts2_cache_umd/
61+
62+
# Optional REPL history
63+
.node_repl_history
64+
65+
# Output of 'npm pack'
66+
*.tgz
67+
68+
# Yarn Integrity file
69+
.yarn-integrity
70+
71+
# dotenv environment variables file
72+
.env
73+
.env.test
74+
75+
# parcel-bundler cache (https://parceljs.org/)
76+
.cache
77+
78+
# Next.js build output
79+
.next
80+
81+
# Nuxt.js build / generate output
82+
.nuxt
83+
dist
84+
85+
# Gatsby files
86+
.cache/
87+
# Comment in the public line in if your project uses Gatsby and *not* Next.js
88+
# https://nextjs.org/blog/next-9-1#public-directory-support
89+
# public
90+
91+
# vuepress build output
92+
.vuepress/dist
93+
94+
# Serverless directories
95+
.serverless/
96+
97+
# FuseBox cache
98+
.fusebox/
99+
100+
# DynamoDB Local files
101+
.dynamodb/
102+
103+
# TernJS port file
104+
.tern-port

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = exports = require('./lib/core');

lib/core.js

+186
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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){WRAPPER.name = themeName}
72+
this.addName(themeName);
73+
74+
var data = val;
75+
if (typeof data !== 'object' && data !== null) {
76+
data = JSON.parse(data)
77+
} //convert to object
78+
79+
if (!WRAPPER.hasOwnProperty(themeName)) {//theme doesn't exist yet so we create one
80+
Vue.set(WRAPPER, themeName, {
81+
})
82+
}
83+
84+
85+
86+
87+
let xx = Object.keys(data);
88+
for (var i = 0; i < xx.length; i++) {
89+
let property = xx[i];
90+
if (!WRAPPER[themeName].hasOwnProperty(property)) { //if theme property doesn't exist yet
91+
Vue.set(WRAPPER[themeName], property, data[property])
92+
} else {
93+
WRAPPER[themeName][property] = data[property];
94+
}
95+
}
96+
this.updateBaseTheme();
97+
return this
98+
}
99+
100+
updateBaseTheme(){//update plugin.theme.... accessor
101+
let currentKeys = Object.keys(WRAPPER[WRAPPER.name]);
102+
let currentTheme = WRAPPER[WRAPPER.name];
103+
104+
for(var i = 0; i < currentKeys.length; i++){
105+
let c = currentKeys[i]
106+
Vue.set(WRAPPER,c,currentTheme[c]);
107+
}
108+
}
109+
110+
// flushTheme(){
111+
// let keys = Object.keys(WRAPPER);
112+
// for(var i = 0; i < keys.length; i++){
113+
// let prop = keys[i];
114+
// if(prop != ['currentTheme'] && prop != ['name'] && typeof WRAPPER[prop] !== 'object'){
115+
// console.log(prop)
116+
// Vue.delete(WRAPPER,prop)
117+
// }
118+
// }
119+
120+
// }
121+
122+
activateTheme(name){
123+
WRAPPER.name = name;
124+
this.updateBaseTheme();
125+
this.saveTheme();
126+
return this
127+
}
128+
129+
130+
getTheme(which, themeName = WRAPPER.name) {
131+
if(!Array.isArray(which)){
132+
throw new Error("color names shlould be in an Array");
133+
}
134+
var result = {};
135+
for (let i = 0; i < which.length; i++) {
136+
let c = which[i]
137+
try {
138+
Object.defineProperty(result, c, {
139+
value: WRAPPER[themeName][c],
140+
writable: true,
141+
enumerable: true
142+
})
143+
} catch (err) {
144+
throw new Error(err)
145+
}
146+
147+
}
148+
return result;
149+
}
150+
151+
saveTheme() {
152+
var options = {};
153+
let keys = Object.keys(WRAPPER);
154+
for(var i = 0; i < keys.length; i++){
155+
let prop = keys[i];
156+
if(typeof WRAPPER[prop] === 'object' && !Array.isArray(WRAPPER[prop])){//get all themes by object detection
157+
Vue.set(options,prop,WRAPPER[prop]);
158+
}
159+
}
160+
localStorage.setItem('APP_THEME', JSON.stringify({names:WRAPPER.names,activate:WRAPPER.name,options:options}));
161+
162+
return this
163+
}
164+
getThemeFromStorage() {
165+
let th = localStorage.getItem('APP_THEME');
166+
if (th !== null) {
167+
this.init(JSON.parse(th));
168+
169+
}
170+
//get from localStorge then initialize
171+
return this
172+
}
173+
}
174+
const plugin = {
175+
install(Vue, options) {
176+
Vue.prototype.$AppTheme = new ThemeManager(options);
177+
178+
Vue.mixin({
179+
mounted() {
180+
this.$AppTheme.getThemeFromStorage();
181+
}
182+
});
183+
}
184+
}
185+
186+
export default plugin;

package.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "vue-theme-manager",
3+
"version": "1.0.0",
4+
"description": "A vue plugin to manage one or more color themes in your web app",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/Xceldeveloper/vue-theme-manager.git"
12+
},
13+
"keywords": [
14+
"vue",
15+
"theme",
16+
"theme-manager",
17+
"color"
18+
],
19+
"author": "xceldeveloper <[email protected]> (xceldeveloper.com)",
20+
"license": "MIT",
21+
"bugs": {
22+
"url": "https://github.com/Xceldeveloper/vue-theme-manager/issues"
23+
},
24+
"homepage": "https://github.com/Xceldeveloper/vue-theme-manager#readme"
25+
}

0 commit comments

Comments
 (0)