Skip to content

Commit

Permalink
Fix a few problems related to checkboxes and their value representation
Browse files Browse the repository at this point in the history
  • Loading branch information
Inviz committed Nov 28, 2011
1 parent d9f32e7 commit 6428e8f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Source/Mixin/Command.js
Expand Up @@ -68,7 +68,7 @@ LSD.Mixin.Command = new Class({
this.fireEvent('click', arguments);
this.getCommand().click();
var method = this.getCommandState() ? 'callChain' : 'uncallChain';
return this[method].apply(this, arguments) != false;
return this[method].apply(this, arguments) != false || this.getCommandType() != 'command';
},

unclick: function() {
Expand Down
26 changes: 16 additions & 10 deletions Source/Mixin/Fieldset.js
Expand Up @@ -48,12 +48,12 @@ LSD.Mixin.Fieldset = new Class({
constructors: {
fieldset: function(options, state) {
if (state) {
this.params = new LSD.Object.Params;
this.values = new LSD.Object.Params;
this.fields = new LSD.Object.Params;
}
this.variables[state ? 'merge' : 'unmerge']({params: this.params, fields: this.fields});
this.variables[state ? 'merge' : 'unmerge']({values: this.values, fields: this.fields});
if (!state) {
delete this.params;
delete this.values;
delete this.field;
}
this[state ? 'addEvents' : 'removeEvents'](LSD.Mixin.Fieldset.events);
Expand Down Expand Up @@ -141,24 +141,31 @@ LSD.Mixin.Fieldset = new Class({
addField: function(widget) {
var name = widget.attributes.name;
if (!name || !widget.toData) return;
if ((LSD.Mixin.Command.getCommandType.call(widget) == 'command') || widget.checked)
this.params.set(name, widget.getValue())
var callback = function(value, old) {
if (typeof value == 'undefined') {
if (typeof old != 'undefined' && widget.getValue() === old) this.values.unset(name, old);
} else {
this.values.set(name, widget.getValue());
}
}.bind(this)
callback._callback = this;
widget.states.watch('checked', callback);
this.fields.set(name, widget)
var key = widget.lsd + ':value:callback'
var callback = this.retrieve(key);
if (!callback) {
callback = function(value, old) {
if (typeof old != 'undefined')
this.params.unset(name, old);
this.params.set(name, value)
this.values.unset(name, old);
this.values.set(name, value)
}.bind(this)
this.store(key, callback)
}
widget.addEvent('change', callback);
},

getParams: function(object) {
if (!object) object = this.params;
if (!object) object = this.values;
var result = {};
for (var name in object) {
var value = object[name];
Expand All @@ -171,8 +178,7 @@ LSD.Mixin.Fieldset = new Class({
removeField: function(widget) {
var name = widget.attributes.name;
if (!name) return;
if ((LSD.Mixin.Command.getCommandType.call(widget) == 'command') || widget.checked)
this.params.unset(name, widget.getValue())
widget.states.unwatch('checked', this);
this.fields.unset(name, widget)
var key = widget.lsd + ':value:callback'
var callback = this.retrieve(key);
Expand Down
15 changes: 1 addition & 14 deletions Source/Mixin/Value.js
Expand Up @@ -21,16 +21,8 @@ LSD.Mixin.Value = new Class({
actions: {
value: {
enable: function() {
if (LSD.Mixin.Command.getCommandType.call(this) == 'command') {
if (LSD.Mixin.Command.getCommandType.call(this) == 'command')
this.setDefaultValue()
} else {
this.states.watch('checked', this.bind(LSD.Mixin.Value.setValueOnCheck))
}
},
disable: function() {
if (!this.attributes.multiple && LSD.Mixin.Command.getCommandType.call(this) != 'command') {
this.states.unwatch('checked', this.bind(LSD.Mixin.Value.setValueOnCheck))
}
}
}
},
Expand Down Expand Up @@ -160,9 +152,4 @@ LSD.Options.value = {
remove: 'unsetValue'
};

LSD.Mixin.Value.setValueOnCheck = function(value) {
if (value) this.setValue();
else this.unsetValue();
};

LSD.Behavior.define(':value', 'value');
2 changes: 1 addition & 1 deletion Source/Tools/Command.js
Expand Up @@ -48,7 +48,7 @@ LSD.Command.prototype = Object.append(new Options, new Events, new States, {
if (!this.bound.enable) this.bound.enable = this.enable.bind(this);
if (!this.bound.disable) this.bound.disable = this.disable.bind(this);
if (this.type && this.type != 'command') {
widget.states.set('checked');
widget.states.set('checked', undefined, undefined, true);
if (this.checked) widget.states.include('checked');
if (!this.bound.check) this.bound.check = this.check.bind(this);
if (!this.bound.uncheck) this.bound.uncheck = this.uncheck.bind(this);
Expand Down

0 comments on commit 6428e8f

Please sign in to comment.