2011-11-05 16:46:53 +00:00
|
|
|
'use strict';
|
|
|
|
|
2011-05-16 10:49:48 +00:00
|
|
|
/*@
|
2012-05-31 10:32:54 +00:00
|
|
|
Ox.FormElementGroup <f> FormElementGroup Element
|
2011-05-16 10:49:48 +00:00
|
|
|
options <o> Options object
|
|
|
|
id <s> element id
|
2012-05-31 10:32:54 +00:00
|
|
|
elements <[o:Ox.Element]|[]> elements in group
|
2011-05-16 10:49:48 +00:00
|
|
|
float <s|left> alignment
|
|
|
|
separators <a|[]> separators (not implemented)
|
|
|
|
width <n|0> group width
|
|
|
|
self <o> Shared private variable
|
2012-07-04 11:29:18 +00:00
|
|
|
([options[, self]]) -> <o:Ox.Element> FormElementGroup Element
|
|
|
|
autovalidate <!> autovalidate
|
|
|
|
change <!> change
|
|
|
|
validate <!> validate
|
2011-05-16 10:49:48 +00:00
|
|
|
@*/
|
|
|
|
|
2011-04-22 22:03:10 +00:00
|
|
|
Ox.FormElementGroup = function(options, self) {
|
|
|
|
|
2011-06-19 17:48:32 +00:00
|
|
|
self = self || {};
|
|
|
|
var that = Ox.Element({}, self)
|
2011-04-22 22:03:10 +00:00
|
|
|
.defaults({
|
|
|
|
id: '',
|
|
|
|
elements: [],
|
|
|
|
float: 'left',
|
2011-12-21 13:42:47 +00:00
|
|
|
join: null,
|
2011-04-22 22:03:10 +00:00
|
|
|
separators: [],
|
2011-12-21 13:42:47 +00:00
|
|
|
split: null,
|
|
|
|
value: options.split ? '' : [],
|
2011-04-22 22:03:10 +00:00
|
|
|
width: 0
|
|
|
|
})
|
|
|
|
.options(options || {})
|
2012-05-28 19:35:41 +00:00
|
|
|
.update({
|
|
|
|
value: setValue
|
|
|
|
})
|
2011-04-22 22:03:10 +00:00
|
|
|
.addClass('OxInputGroup');
|
|
|
|
|
2011-12-21 13:42:47 +00:00
|
|
|
if (Ox.isEmpty(self.options.value)) {
|
|
|
|
self.options.value = getValue();
|
|
|
|
} else {
|
|
|
|
setValue();
|
|
|
|
}
|
|
|
|
|
2011-04-22 22:03:10 +00:00
|
|
|
(
|
|
|
|
self.options.float == 'left' ?
|
2011-11-10 19:52:26 +00:00
|
|
|
self.options.elements : Ox.clone(self.options.elements).reverse()
|
2011-12-21 13:42:47 +00:00
|
|
|
).forEach(function($element) {
|
2011-04-22 22:03:10 +00:00
|
|
|
$element.css({
|
|
|
|
float: self.options.float // fixme: make this a class
|
|
|
|
})
|
|
|
|
.bindEvent({
|
2011-10-22 14:50:31 +00:00
|
|
|
autovalidate: function(data) {
|
|
|
|
that.triggerEvent({autovalidate: data});
|
|
|
|
},
|
2011-12-21 13:42:47 +00:00
|
|
|
change: change,
|
2012-06-16 13:35:14 +00:00
|
|
|
//submit: change,
|
2011-09-17 17:39:38 +00:00
|
|
|
validate: function(data) {
|
2011-10-22 14:50:31 +00:00
|
|
|
that.triggerEvent({validate: data});
|
2011-04-22 22:03:10 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.appendTo(that);
|
|
|
|
});
|
|
|
|
|
2011-12-21 13:42:47 +00:00
|
|
|
function change(data) {
|
|
|
|
self.options.value = getValue();
|
2012-06-16 13:35:14 +00:00
|
|
|
that.triggerEvent('change', {value: self.options.value});
|
2011-12-21 13:42:47 +00:00
|
|
|
}
|
|
|
|
|
2011-04-22 22:03:10 +00:00
|
|
|
/*
|
|
|
|
if (self.options.width) {
|
|
|
|
setWidths();
|
|
|
|
} else {
|
|
|
|
self.options.width = getWidth();
|
|
|
|
}
|
|
|
|
that.css({
|
|
|
|
width: self.options.width + 'px'
|
|
|
|
});
|
|
|
|
*/
|
|
|
|
|
2011-12-21 13:42:47 +00:00
|
|
|
function getValue() {
|
|
|
|
var value = self.options.elements.map(function($element) {
|
2013-03-01 05:13:52 +00:00
|
|
|
return $element.value ? $element.value() : void 0;
|
2011-12-21 13:42:47 +00:00
|
|
|
});
|
|
|
|
return self.options.join ? self.options.join(value) : value;
|
|
|
|
}
|
|
|
|
|
2011-04-22 22:03:10 +00:00
|
|
|
function getWidth() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-12-21 13:42:47 +00:00
|
|
|
function setValue() {
|
|
|
|
var values = self.options.split
|
|
|
|
? self.options.split(self.options.value)
|
|
|
|
: self.options.value;
|
|
|
|
values.forEach(function(value, i) {
|
2013-03-01 05:13:52 +00:00
|
|
|
self.options.elements[i].value && self.options.elements[i].value(value);
|
2011-12-21 13:42:47 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-04-22 22:03:10 +00:00
|
|
|
function setWidth() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-05-21 10:38:18 +00:00
|
|
|
/*@
|
|
|
|
replaceElement <f> replaceElement
|
2015-02-15 10:02:11 +00:00
|
|
|
(pos, element) -> <u> replace element at position
|
2012-05-21 10:38:18 +00:00
|
|
|
@*/
|
2011-04-22 22:03:10 +00:00
|
|
|
that.replaceElement = function(pos, element) {
|
2011-11-04 15:54:28 +00:00
|
|
|
Ox.Log('Form', 'Ox.FormElementGroup replaceElement', pos, element)
|
2011-04-22 22:03:10 +00:00
|
|
|
self.options.elements[pos].replaceWith(element.$element);
|
|
|
|
self.options.elements[pos] = element;
|
|
|
|
};
|
|
|
|
|
2012-05-21 10:38:18 +00:00
|
|
|
/*@
|
|
|
|
value <f> value
|
|
|
|
@*/
|
2011-04-22 22:03:10 +00:00
|
|
|
that.value = function() {
|
2015-02-14 19:51:55 +00:00
|
|
|
var values;
|
|
|
|
if (arguments.length == 0) {
|
|
|
|
values = self.options.elements.map(function(element) {
|
|
|
|
return element.value ? element.value() : void 0;
|
|
|
|
});
|
|
|
|
return self.options.join
|
|
|
|
? self.options.join(values)
|
|
|
|
: values;
|
|
|
|
} else {
|
2015-02-15 10:18:25 +00:00
|
|
|
return that.options({value: arguments[0]});
|
2015-02-14 19:51:55 +00:00
|
|
|
return that;
|
|
|
|
}
|
2011-04-22 22:03:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return that;
|
|
|
|
|
|
|
|
};
|