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

141 lines
4 KiB
JavaScript
Raw Normal View History

2011-12-16 23:03:43 +00:00
'use strict';
2011-12-01 10:52:23 +00:00
2012-05-21 10:38:18 +00:00
/*@
Ox.SelectInput <function> Select Input
(options[, self]) -> <o> Select Input
options <o> Options
self <o> Shared private variable
@*/
2011-12-01 10:52:23 +00:00
Ox.SelectInput = function(options, self) {
var that;
self = Ox.extend(self || {}, {
options: Ox.extend({
inputWidth: 128,
items: [],
label: '',
labelWidth: 128,
2011-12-21 13:42:47 +00:00
max: 1,
2011-12-30 09:33:01 +00:00
min: 0,
2011-12-01 10:52:23 +00:00
placeholder: '',
title: '',
2011-12-21 13:42:47 +00:00
value: options.max == 1 ? '' : [],
2011-12-01 10:52:23 +00:00
width: 384
}, options)
});
self.other = self.options.items[self.options.items.length - 1].id;
self.otherWidth = self.options.width - self.options.inputWidth - 3; // fixme: 3? obscure!
self.$select = Ox.Select({
items: self.options.items,
label: self.options.label,
labelWidth: self.options.labelWidth,
max: self.options.max,
min: self.options.min,
2011-12-30 09:33:01 +00:00
title: getTitle(),
2011-12-21 13:42:47 +00:00
value: self.options.value,
2011-12-01 10:52:23 +00:00
width: self.options.width
})
.bindEvent({
change: function(data) {
2011-12-30 09:33:01 +00:00
self.options.value = getValue();
setValue(self.isOther);
2011-12-01 10:52:23 +00:00
}
});
self.$input = Ox.Input({
placeholder: self.options.placeholder,
width: 0
})
2011-12-21 13:42:47 +00:00
.bindEvent({
change: function(data) {
self.options.value = data.value;
}
})
2011-12-01 10:52:23 +00:00
.hide();
2011-12-30 09:33:01 +00:00
setValue();
2011-12-01 10:52:23 +00:00
that = Ox.FormElementGroup({
elements: [
self.$select,
self.$input
],
2011-12-30 09:33:01 +00:00
id: self.options.id,
2011-12-21 13:42:47 +00:00
join: function(value) {
return value[value[0] == self.other ? 1 : 0]
},
split: function(value) {
return Ox.map(self.options.items, function(item, i) {
return i < item.length - 1 ? item.id : null;
}).indexOf(value) > -1 ? [value, ''] : [self.other, value];
},
2011-12-01 10:52:23 +00:00
width: self.options.width
});
2011-12-30 09:33:01 +00:00
function getTitle() {
var value = self.$select ? self.$select.value() : self.options.value;
return Ox.isEmpty(value)
? self.options.title
: Ox.getObjectById(self.options.items, value).title;
}
function getValue() {
self.isOther = self.$select.value() == self.other;
return !self.isOther ? self.$select.value() : self.$input.value();
}
function setValue(isOther) {
if (
(!self.options.value && isOther !== true)
|| Ox.map(self.options.items, function(item) {
return item.id != self.other ? item.id : null;
}).indexOf(self.options.value) > -1
) {
self.$select.options({
title: !self.options.value
? self.options.title
: Ox.getObjectById(self.options.items, self.options.value).title,
value: self.options.value,
width: self.options.width
})
.removeClass('OxOverlapRight');
self.$input.hide().value('');
} else {
self.$select.options({
title: Ox.getObjectById(self.options.items, self.other).title,
value: self.other,
width: self.otherWidth
})
.addClass('OxOverlapRight');
self.$input.show().value(self.options.value);
}
self.$select.options({title: getTitle()});
}
2011-12-21 13:42:47 +00:00
self.setOption = function(key, value) {
2011-12-30 09:33:01 +00:00
if (key == 'value') {
setValue();
}
};
2012-05-21 10:38:18 +00:00
/*@
value <f> get/set value
() -> value get value
(value) -> <o> set value
@*/
2011-12-30 09:33:01 +00:00
that.value = function() {
if (arguments.length == 0) {
return getValue();
} else {
self.options.value = arguments[0];
setValue();
return that;
}
2011-12-01 10:52:23 +00:00
};
return that;
2012-05-21 10:38:18 +00:00
};