diff --git a/spec/app/config-panel-spec.coffee b/spec/app/config-panel-spec.coffee index 79694592f..3fbf322d5 100644 --- a/spec/app/config-panel-spec.coffee +++ b/spec/app/config-panel-spec.coffee @@ -1,4 +1,5 @@ ConfigPanel = require 'config-panel' +Editor = require 'editor' describe "ConfigPanel", -> it "automatically binds named input fields to their corresponding config keys", -> @@ -40,3 +41,34 @@ describe "ConfigPanel", -> panel.stringInput.val('moo').change() expect(config.get('foo.string')).toBe 'moo' + + it "automatically binds named editors to their corresponding config keys", -> + class TestPanel extends ConfigPanel + @content: -> + @div => + @subview 'intEditor', new Editor(mini: true, attributes: { id: 'foo.int', type: 'int' }) + @subview 'floatEditor', new Editor(mini: true, attributes: { id: 'foo.float', type: 'float' }) + @subview 'stringEditor', new Editor(mini: true, attributes: { id: 'foo.string', type: 'string' }) + + config.set('foo.int', 1) + config.set('foo.float', 1.1) + config.set('foo.string', 'I think therefore I am.') + panel = new TestPanel + expect(panel.intEditor.getText()).toBe '1' + expect(panel.floatEditor.getText()).toBe '1.1' + expect(panel.stringEditor.getText()).toBe 'I think therefore I am.' + + config.set('foo.int', 2) + config.set('foo.float', 2.2) + config.set('foo.string', 'We are what we think.') + expect(panel.intEditor.getText()).toBe '2' + expect(panel.floatEditor.getText()).toBe '2.2' + expect(panel.stringEditor.getText()).toBe 'We are what we think.' + + panel.intEditor.setText('3') + panel.floatEditor.setText('3.3') + panel.stringEditor.setText('All limitations are self imposed.') + window.advanceClock(10000) # wait for contents-modified to be triggered + expect(config.get('foo.int')).toBe 3 + expect(config.get('foo.float')).toBe 3.3 + expect(config.get('foo.string')).toBe 'All limitations are self imposed.' diff --git a/src/app/config-panel.coffee b/src/app/config-panel.coffee index 8da5c4082..2482718a1 100644 --- a/src/app/config-panel.coffee +++ b/src/app/config-panel.coffee @@ -5,6 +5,7 @@ module.exports = class ConfigPanel extends View initialize: -> @bindFormFields() + @bindEditors() bindFormFields: -> for input in @find('input[id]').toArray() @@ -28,3 +29,18 @@ class ConfigPanel extends View !!input.attr('checked') else value + + bindEditors: -> + for editor in @find('.editor[id]').views() + do (editor) => + name = editor.attr('id') + type = editor.attr('type') + + @observeConfig name, (value) -> + editor.setText(value.toString()) + + editor.getBuffer().on 'contents-modified', -> + value = editor.getText() + if type == 'int' then value = parseInt(value) + if type == 'float' then value = parseFloat(value) + config.set name, value