diff --git a/spec/app/config-panel-spec.coffee b/spec/app/config-panel-spec.coffee index a1f9dabed..be8717246 100644 --- a/spec/app/config-panel-spec.coffee +++ b/spec/app/config-panel-spec.coffee @@ -42,6 +42,19 @@ describe "ConfigPanel", -> panel.stringInput.val('moo').change() expect(config.get('foo.string')).toBe 'moo' + panel.intInput.val('abcd').change() + expect(config.get('foo.int')).toBe 'abcd' + + panel.floatInput.val('defg').change() + expect(config.get('foo.float')).toBe 'defg' + + panel.intInput.val('').change() + expect(config.get('foo.int')).toBe undefined + panel.floatInput.val('').change() + expect(config.get('foo.float')).toBe undefined + panel.stringInput.val('').change() + expect(config.get('foo.string')).toBe undefined + it "automatically binds named editors to their corresponding config keys", -> class TestPanel extends ConfigPanel @content: -> @@ -73,10 +86,17 @@ describe "ConfigPanel", -> expect(config.get('foo.float')).toBe 3.3 expect(config.get('foo.string')).toBe 'All limitations are self imposed.' + panel.intEditor.setText('not an int') panel.floatEditor.setText('not a float') + window.advanceClock(10000) # wait for contents-modified to be triggered + expect(config.get('foo.int')).toBe 'not an int' + expect(config.get('foo.float')).toBe 'not a float' + + panel.intEditor.setText('') + panel.floatEditor.setText('') panel.stringEditor.setText('') window.advanceClock(10000) # wait for contents-modified to be triggered - expect(config.get('foo.int')).toBe 0 - expect(config.get('foo.float')).toBe 0 + expect(config.get('foo.int')).toBe undefined + expect(config.get('foo.float')).toBe undefined expect(config.get('foo.string')).toBe undefined diff --git a/src/app/config-panel.coffee b/src/app/config-panel.coffee index 16aa17ab9..178248a90 100644 --- a/src/app/config-panel.coffee +++ b/src/app/config-panel.coffee @@ -22,17 +22,20 @@ class ConfigPanel extends View input.attr('checked', value) else input.val(value) if value - input.on 'change', -> + input.on 'change', => value = input.val() - config.set name, switch type - when 'int' - parseInt(value) - when 'float' - parseFloat(value) - when 'checkbox' - !!input.attr('checked') - else - value + if type == 'checkbox' + value = !!input.attr('checked') + else + value = @parseValue(type, value) + config.set(name, value) + + parseValue: (type, value) -> + switch type + when 'int' then value = parseInt(value) or value + when 'float' then value = parseFloat(value) or value + value = undefined if value == '' + value bindEditors: -> for editor in @find('.editor[id]').views() @@ -45,9 +48,5 @@ class ConfigPanel extends View value ?= "" editor.setText(value.toString()) - editor.getBuffer().on 'contents-modified', -> - value = editor.getText() - if type == 'int' then value = parseInt(value) or 0 - if type == 'float' then value = parseFloat(value) or 0 - if value == "" then value = undefined - config.set name, value + editor.getBuffer().on 'contents-modified', => + config.set(name, @parseValue(type, editor.getText()))