oxjs/source/UI/js/Form/ObjectInput.js

70 lines
1.6 KiB
JavaScript
Raw Normal View History

2011-11-30 15:50:55 +01:00
'use strict';
2012-05-21 12:38:18 +02:00
/*@
2012-05-31 12:32:54 +02:00
Ox.ObjectInput <f> Object Input
2012-05-21 12:38:18 +02:00
options <o> Options
self <o> Shared private variable
([options[, self]]) -> <o:Ox.Element> Object Input
change <!> change
2012-05-21 12:38:18 +02:00
@*/
2011-11-30 15:50:55 +01:00
Ox.ObjectInput = function(options, self) {
self = self || {};
var that = Ox.Element({}, self)
.defaults({
elements: [],
labelWidth: 128,
2011-12-30 15:03:01 +05:30
value: {},
2011-11-30 15:50:55 +01:00
width: 256
})
.options(options || {})
2012-05-28 19:35:41 +00:00
.update({
value: function() {
setValue(self.options.value);
}
})
2011-11-30 15:50:55 +01:00
.addClass('OxObjectInput')
.css({
width: self.options.width + 'px',
});
2011-12-30 15:03:01 +05:30
if (Ox.isEmpty(self.options.value)) {
self.options.value = getValue();
} else {
setValue(self.options.value);
}
2011-11-30 15:50:55 +01:00
self.options.elements.forEach(function($element) {
$element.options({
labelWidth: self.options.labelWidth,
width: self.options.width
})
2011-12-17 04:33:43 +05:30
.bindEvent({
change: function(data) {
2011-12-30 15:03:01 +05:30
self.options.value = getValue();
2011-12-21 21:03:52 +05:30
that.triggerEvent('change', {
value: self.options.value
});
2011-12-17 04:33:43 +05:30
}
})
2011-11-30 15:50:55 +01:00
.appendTo(that);
});
2011-12-30 15:03:01 +05:30
function getValue() {
var value = {};
self.options.elements.forEach(function(element) {
value[element.options('id')] = element.value();
});
return value;
}
function setValue(value) {
self.options.elements.forEach(function(element) {
element.value(value[element.options('id')]);
});
}
2011-11-30 15:50:55 +01:00
return that;
2012-05-21 12:38:18 +02:00
};