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

64 lines
1.5 KiB
JavaScript
Raw Normal View History

2011-11-30 14:50:55 +00:00
'use strict';
Ox.ObjectInput = function(options, self) {
self = self || {};
var that = Ox.Element({}, self)
.defaults({
elements: [],
labelWidth: 128,
2011-12-30 09:33:01 +00:00
value: {},
2011-11-30 14:50:55 +00:00
width: 256
})
.options(options || {})
.addClass('OxObjectInput')
.css({
width: self.options.width + 'px',
height: self.options.elements.length * 24 - 8 + 'px'
});
2011-12-30 09:33:01 +00:00
if (Ox.isEmpty(self.options.value)) {
self.options.value = getValue();
} else {
setValue(self.options.value);
}
2011-11-30 14:50:55 +00:00
self.options.elements.forEach(function($element) {
$element.options({
labelWidth: self.options.labelWidth,
width: self.options.width
})
2011-12-16 23:03:43 +00:00
.bindEvent({
change: function(data) {
2011-12-30 09:33:01 +00:00
self.options.value = getValue();
2011-12-21 15:33:52 +00:00
that.triggerEvent('change', {
value: self.options.value
});
2011-12-16 23:03:43 +00:00
}
})
2011-11-30 14:50:55 +00:00
.appendTo(that);
});
2011-12-30 09:33:01 +00:00
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-12-21 15:33:52 +00:00
self.setOption = function(key, value) {
if (key == 'value') {
2011-12-30 09:33:01 +00:00
setValue(value);
2011-12-21 15:33:52 +00:00
}
2011-11-30 14:50:55 +00:00
};
return that;
};