|
| 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; |
0 commit comments