2015-09-23 11:11:51 +08:00
|
|
|
React = require 'react'
|
|
|
|
_ = require 'underscore'
|
|
|
|
|
|
|
|
class ConfigPropContainer extends React.Component
|
|
|
|
@displayName: 'ConfigPropContainer'
|
|
|
|
|
|
|
|
constructor: (@props) ->
|
|
|
|
@state = @getStateFromStores()
|
|
|
|
|
|
|
|
componentDidMount: =>
|
2016-01-29 16:31:10 +08:00
|
|
|
@subscription = NylasEnv.config.onDidChange null, =>
|
2015-09-23 11:11:51 +08:00
|
|
|
@setState(@getStateFromStores())
|
|
|
|
|
|
|
|
componentWillUnmount: =>
|
|
|
|
@subscription?.dispose()
|
|
|
|
|
|
|
|
getStateFromStores: =>
|
|
|
|
config: @getConfigWithMutators()
|
|
|
|
|
|
|
|
getConfigWithMutators: =>
|
2015-11-12 02:25:11 +08:00
|
|
|
_.extend NylasEnv.config.get(), {
|
2015-09-23 11:11:51 +08:00
|
|
|
get: (key) =>
|
2015-11-12 02:25:11 +08:00
|
|
|
NylasEnv.config.get(key)
|
2015-09-23 11:11:51 +08:00
|
|
|
set: (key, value) =>
|
2015-11-12 02:25:11 +08:00
|
|
|
NylasEnv.config.set(key, value)
|
2015-09-23 11:11:51 +08:00
|
|
|
return
|
|
|
|
toggle: (key) =>
|
2015-11-12 02:25:11 +08:00
|
|
|
NylasEnv.config.set(key, !NylasEnv.config.get(key))
|
2015-09-23 11:11:51 +08:00
|
|
|
return
|
|
|
|
contains: (key, val) =>
|
2015-11-12 02:25:11 +08:00
|
|
|
vals = NylasEnv.config.get(key)
|
2015-09-23 11:11:51 +08:00
|
|
|
return false unless vals and vals instanceof Array
|
|
|
|
return val in vals
|
|
|
|
toggleContains: (key, val) =>
|
2015-11-12 02:25:11 +08:00
|
|
|
vals = NylasEnv.config.get(key)
|
2015-09-23 11:11:51 +08:00
|
|
|
vals = [] unless vals and vals instanceof Array
|
|
|
|
if val in vals
|
2015-11-12 02:25:11 +08:00
|
|
|
NylasEnv.config.set(key, _.without(vals, val))
|
2015-09-23 11:11:51 +08:00
|
|
|
else
|
2015-11-12 02:25:11 +08:00
|
|
|
NylasEnv.config.set(key, vals.concat([val]))
|
2015-09-23 11:11:51 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
render: =>
|
2015-11-24 04:20:51 +08:00
|
|
|
React.cloneElement(@props.children, {
|
|
|
|
config: @state.config,
|
|
|
|
configSchema: NylasEnv.config.getSchema('core')
|
|
|
|
})
|
2015-09-23 11:11:51 +08:00
|
|
|
|
|
|
|
module.exports = ConfigPropContainer
|