/*
---
 
script: Value.js
 
description: Add your widget have a real form value.
 
license: Public domain (http://unlicense.org).
 
requires:
  - LSD.Trait
 
provides: 
  - LSD.Mixin.Value
 
...
*/

LSD.Mixin.Value = new Class({
  behaviour: ':read-write, :valued',
  
  options: {
    events: {
      _value: {
        dominject: function() {
          if (!('value' in this)) this.value = this.processValue(this.options.value || this.getRawValue());
        },
        change: 'callChain'
      }
    }
  },
  
  setValue: function(item) {
    if (item == null || (item.event && item.type)) item = this.getRawValue();
    this.oldValue = this.value;
    this.value = this.processValue(item);
    if (this.oldValue !== this.value) {
      var result = this.applyValue(this.value);
      this.onChange(this.value, this.oldValue);
      return result;
    }
  },
  
  applyValue: Macro.defaults(function(item) {
    if (this.attributes.itemprop) this.element.set('itemvalue', item);
  }),
  
  getRawValue: Macro.defaults(function() {
    return this.attributes.value || LSD.Module.DOM.getID(this) || this.innerText;
  }),

  getValue: function() {
    return this.formatValue(('value' in this) ? this.value : this.getRawValue());
  },

  formatValue: function(value) {
    return value;
  },
  
  processValue: function(value) {
    return value;
  },
  
  onChange: function() {
    this.fireEvent('change', arguments)
    return true;
  }
});
@@ -349,32 +349,172 @@
     
-  if (!('value' in this)
+%7D,%0A        change: 'callChain'%0A      %7D%0A    %7D,%0A    multiple: false,%0A    actions: %7B%0A      value: %7B%0A        enable: function() %7B%0A          if (this.attributes.multiple
 ) th
@@ -525,69 +525,146 @@
 alue
+s
  = 
-this.processValue(this.options.value %7C%7C
+%5B%5D%0A          if (this.getValue() != null) return;%0A          var raw = this.getRawValue();%0A          if (raw != null)
  this.
-g
+s
 et
-Raw
 Value(
-)
+raw
 );%0A 
@@ -681,35 +681,58 @@
         
-change: 'callChain'
+disable: function() %7B%0A          %0A        %7D
 %0A      %7D
@@ -861,77 +861,245 @@
     
-this.oldValue = this.
+var value = this.processValue(item), result = false;%0A    if (this.isValueDifferent(value)) %7B%0A      result = this.writeValue(
 value
+)
 ;%0A    
+  
 this.
+onChange(
 value
- =
+,
  this.
-processValue
+oldValue);%0A    %7D%0A    return result%0A  %7D,%0A  %0A  unsetValue: function
 (item)
-;
+ %7B
 %0A   
@@ -1107,37 +1107,171 @@
 if (
-this.oldValue !== this.
+item == null %7C%7C (item.event && item.type)) item = this.getRawValue();%0A    var value = this.processValue(''), result = false;%0A    if (!this.isValueDifferent(
 value)
+)
  %7B%0A 
@@ -1275,20 +1275,16 @@
 %7B%0A      
-var 
 result =
@@ -1300,21 +1300,16 @@
 lyValue(
-this.
 value);%0A
@@ -1328,21 +1328,16 @@
 nChange(
-this.
 value, t
@@ -1355,31 +1355,184 @@
 e);%0A    
-  return result
+%7D%0A  %7D,%0A  %0A  isValueDifferent: function(value) %7B%0A    if (this.attributes.multiple) %7B%0A      return this.values.indexOf == -1%0A    %7D else %7B%0A      return this.value != value
 ;%0A    %7D%0A
@@ -1545,127 +1545,359 @@
  %0A  
-apply
+write
 Value: 
-Macro.defaults(function(item) %7B%0A    if (this.attributes.itemprop) this.element.set('itemvalue', item
+function(value, unset) %7B%0A    if (this.attributes.multiple) %7B%0A      if (unset) %7B%0A        var index = this.values.indexOf(value);%0A        if (index %3E -1) this.values.splice(index, 1);%0A      %7D else this.values.push(value);%0A      this.applyValue(this.values);%0A    %7D else %7B%0A      this.value = value;%0A      this.applyValue(this.value
 );%0A  
+ 
+ %7D%0A  
 %7D
-)
 ,%0A  
@@ -1916,23 +1916,8 @@
 ue: 
-Macro.defaults(
 func
@@ -2010,17 +2010,16 @@
 ext;%0A  %7D
-)
 ,%0A%0A  get
@@ -2046,85 +2046,154 @@
     
-return this.formatValue(('value' in this) ? this.v
+if (this.attributes.multiple) %7B%0A      return this.values.map(this.formatValue, this)%0A    %7D else %7B%0A      return this.formatV
 alue
- : 
+(
 this.
-getRawValue());
+value);%0A    %7D
 %0A  %7D

Add multiple value support