-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
75 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,80 @@ | ||
import get from 'lodash/get' | ||
import each from 'lodash/each' | ||
import Vue from 'vue' | ||
import defaultValues from './config.default' | ||
|
||
const _VALUES = typeof Symbol === 'function' ? Symbol('values') : '_VALUES' | ||
const _SCOPES = typeof Symbol === 'function' ? Symbol('scopes') : '_SCOPES' | ||
const _VALUES = '_VALUES' | ||
const _SCOPES = '_SCOPES' | ||
|
||
export class Config { | ||
constructor (values = {}) { | ||
this[_VALUES] = {} | ||
this.merge(values) | ||
return this; | ||
} | ||
merge (values = {}) { | ||
return each(values, (value, key) => { | ||
this.set(key, value) | ||
}) | ||
} | ||
set (key, value) { | ||
const levels = key.split('.') | ||
if (levels.length > 1) { | ||
this[_VALUES][key] = this.scope(levels.shift()).set(levels.join('.'), value) | ||
} else { | ||
this[_VALUES][key] = value | ||
export const Config = Vue.extend({ | ||
props: { | ||
defaultValues: { | ||
type: Object, | ||
default: () => ({}) | ||
} | ||
return value | ||
} | ||
get (key, defaultValue) { | ||
return get(this[_VALUES], key, defaultValue) | ||
} | ||
is (key) { | ||
const value = this.get(key, null) | ||
switch(value) { | ||
case 1: return true | ||
case true: return true | ||
case '1': return true | ||
case 'true': return true | ||
case 0: return false | ||
case false: return false | ||
case '0': return false | ||
case 'false': return false | ||
default: return !!value | ||
}, | ||
data () { | ||
return { | ||
[_VALUES]: null, | ||
[_SCOPES]: null | ||
} | ||
}, | ||
created () { | ||
this[_VALUES] = Vue.observable({}) | ||
this[_SCOPES] = Vue.observable({}) | ||
this.merge(this.defaultValues) | ||
}, | ||
methods: { | ||
merge (values = {}) { | ||
return each(values, (value, key) => { | ||
this.set(key, value) | ||
}) | ||
}, | ||
set (key, value) { | ||
const levels = key.split('.') | ||
if (levels.length > 1) { | ||
this.$set(this[_VALUES], key, this.scope(levels.shift()).set(levels.join('.'), value)) | ||
} else { | ||
this.$set(this[_VALUES], key, value) | ||
} | ||
return value | ||
}, | ||
get (key, defaultValue) { | ||
return get(this[_VALUES], key, defaultValue) | ||
}, | ||
is (key) { | ||
const value = this.get(key, null) | ||
switch(value) { | ||
case 1: return true | ||
case true: return true | ||
case '1': return true | ||
case 'true': return true | ||
case 0: return false | ||
case false: return false | ||
case '0': return false | ||
case 'false': return false | ||
default: return !!value | ||
} | ||
}, | ||
isnt (key) { | ||
return !this.is(key) | ||
}, | ||
scope (name) { | ||
this.$set(this[_SCOPES], name, get(this.scopes, name, new Config())) | ||
return this[_SCOPES][name] | ||
}, | ||
}, | ||
computed: { | ||
values () { | ||
return this[_VALUES] | ||
}, | ||
scopes () { | ||
this[_SCOPES] = get(this, _SCOPES, {}) | ||
return this[_SCOPES] | ||
} | ||
} | ||
isnt (key) { | ||
return !this.is(key) | ||
} | ||
scope (name) { | ||
this.scopes[name] = get(this.scopes, name, new Config()) | ||
return this.scopes[name] | ||
} | ||
get values () { | ||
return this[_VALUES] | ||
} | ||
get scopes () { | ||
this[_SCOPES] = get(this, _SCOPES, {}) | ||
return this[_SCOPES] | ||
} | ||
} | ||
}) | ||
|
||
export default new Config(defaultValues) | ||
export default new Config({ | ||
propsData: { defaultValues } | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters