Add ConfigPanel superclass that can bind fields to config values

When you give a config field a `name` attribute based on a config key
path, such as 'editor.fontSize', it is automatically kept in sync with
the config value. You can also specify a `type` attribute of 'int' or
'float' to automatically convert the field value to a numeric type.
Specifying a type of 'string' is optional to signal no conversion.
This commit is contained in:
Nathan Sobo
2013-04-09 18:32:11 -06:00
committed by Corey Johnson & Kevin Sawicki
parent 252159afcf
commit bb287cb465
3 changed files with 64 additions and 4 deletions

View File

@@ -0,0 +1,24 @@
$ = require 'jquery'
{View} = require 'space-pen'
module.exports =
class ConfigPanel extends View
initialize: ->
@bindFormFields()
bindFormFields: ->
for input in @find('input[name]').toArray()
do (input) =>
input = $(input)
name = input.attr('name')
type = input.attr('type')
@observeConfig name, (value) -> input.val(value) if value
input.on 'change', ->
value = input.val()
config.set name, switch type
when 'int'
parseInt(value)
when 'float'
parseFloat(value)
else
value

View File

@@ -1,13 +1,13 @@
{View} = require 'space-pen'
ConfigPanel = require 'config-panel'
module.exports =
class EditorConfigPanel extends View
class EditorConfigPanel extends ConfigPanel
@content: ->
@div class: 'config-panel', =>
@div class: 'row', =>
@label for: 'editor.fontSize', "Font Size:"
@input name: 'editor.fontSize', size: 2
@input name: 'editor.fontSize', type: 'integer', size: 2
@div class: 'row', =>
@label for: 'editor.fontFamily', "Font Family:"
@input name: 'editor.fontFamily'
@input name: 'editor.fontFamily', type: 'string'