2011-07-29 18:48:43 +00:00
|
|
|
// vim: et:ts=4:sw=4:sts=4:ft=javascript
|
2011-05-16 10:49:48 +00:00
|
|
|
|
2011-11-05 16:46:53 +00:00
|
|
|
'use strict';
|
|
|
|
|
2011-05-16 10:49:48 +00:00
|
|
|
/*@
|
|
|
|
Ox.Select <f:Ox.Element> Select Object
|
|
|
|
() -> <f> Select Object
|
|
|
|
(options) -> <f> Select Object
|
|
|
|
(options, self) -> <f> Select Object
|
|
|
|
options <o> Options object
|
2011-12-20 07:07:57 +00:00
|
|
|
disabled <b|false> If true, select is disabled
|
|
|
|
id <s> Element id
|
2011-12-21 13:42:47 +00:00
|
|
|
items <a|[]> Items (array of {id, title} or strings)
|
2011-10-03 15:02:19 +00:00
|
|
|
label <s|''> Label
|
|
|
|
labelWidth <n|64> Label width
|
2011-12-20 07:07:57 +00:00
|
|
|
max <n|1> Maximum number of selected items
|
|
|
|
maxWidth <n|0> Maximum menu width
|
|
|
|
min <n|1> Minimum number of selected items
|
|
|
|
overlap <s|'none'> Can be 'none', 'left' or 'right'
|
2011-12-20 07:29:14 +00:00
|
|
|
selectable <b|true> is selectable
|
2011-12-20 07:07:57 +00:00
|
|
|
size <s|'medium'> Size, can be small, medium, large
|
|
|
|
style <s|'rounded'> Style ('rounded' or 'square')
|
|
|
|
title <s|''> Select title
|
|
|
|
tooltip <s|f|''> Tooltip title, or function that returns one
|
|
|
|
(e) -> <string> Tooltip title
|
|
|
|
e <object> Mouse event
|
|
|
|
type <s|'text'> Type ('text' or 'image')
|
2011-12-21 13:42:47 +00:00
|
|
|
value <a|s> Selected id, or array of selected ids
|
2011-12-20 07:07:57 +00:00
|
|
|
width <s|n|'auto'> Width in px, or 'auto'
|
|
|
|
self <o> Shared private variable
|
|
|
|
click <!> Click event
|
|
|
|
change <!> Change event
|
2011-05-16 10:49:48 +00:00
|
|
|
@*/
|
|
|
|
|
2011-04-22 22:03:10 +00:00
|
|
|
Ox.Select = function(options, self) {
|
|
|
|
|
2011-06-19 17:48:32 +00:00
|
|
|
self = self || {};
|
|
|
|
var that = Ox.Element({
|
2011-05-22 12:39:57 +00:00
|
|
|
tooltip: options.tooltip || ''
|
2011-05-30 11:47:30 +00:00
|
|
|
}, self)
|
2011-04-22 22:03:10 +00:00
|
|
|
.defaults({
|
|
|
|
id: '',
|
|
|
|
items: [],
|
2011-10-03 15:02:19 +00:00
|
|
|
label: '',
|
|
|
|
labelWidth: 64,
|
2011-04-22 22:03:10 +00:00
|
|
|
max: 1,
|
2011-11-30 14:40:41 +00:00
|
|
|
maxWidth: 0,
|
2011-04-22 22:03:10 +00:00
|
|
|
min: 1,
|
2011-12-21 13:42:47 +00:00
|
|
|
overlap: 'none',
|
2011-04-22 22:03:10 +00:00
|
|
|
size: 'medium',
|
2011-11-30 14:40:41 +00:00
|
|
|
style: 'rounded',
|
2011-04-22 22:03:10 +00:00
|
|
|
title: '',
|
2011-12-21 13:42:47 +00:00
|
|
|
type: 'text',
|
|
|
|
value: options.max != 1 ? [] : '',
|
2011-09-17 07:09:17 +00:00
|
|
|
width: 'auto'
|
2011-04-22 22:03:10 +00:00
|
|
|
})
|
|
|
|
// fixme: make default selection restorable
|
|
|
|
.options(options)
|
|
|
|
.addClass(
|
2011-11-30 14:40:41 +00:00
|
|
|
'OxSelect Ox' + Ox.toTitleCase(self.options.size)
|
|
|
|
+ ' Ox' + Ox.toTitleCase(self.options.style) + (
|
2011-10-03 15:02:19 +00:00
|
|
|
self.options.overlap == 'none'
|
|
|
|
? '' : ' OxOverlap' + Ox.toTitleCase(self.options.overlap)
|
|
|
|
) + (self.options.label ? ' OxLabelSelect' : '')
|
2011-04-22 22:03:10 +00:00
|
|
|
)
|
|
|
|
.css(self.options.width == 'auto' ? {} : {
|
2011-09-08 08:16:31 +00:00
|
|
|
width: self.options.width - 2 + 'px'
|
2011-04-22 22:03:10 +00:00
|
|
|
})
|
|
|
|
.bindEvent({
|
2011-12-20 07:29:14 +00:00
|
|
|
anyclick: showMenu,
|
2011-04-22 22:03:10 +00:00
|
|
|
key_escape: loseFocus,
|
|
|
|
key_down: showMenu
|
|
|
|
});
|
|
|
|
|
2011-11-30 14:40:41 +00:00
|
|
|
Ox.Log('Form', 'Ox.Select', self.options);
|
|
|
|
|
2011-12-21 13:42:47 +00:00
|
|
|
self.options.items = self.options.items.map(function(item) {
|
|
|
|
return {
|
|
|
|
id: item.id || item,
|
|
|
|
title: item.title || item,
|
|
|
|
checked: Ox.toArray(self.options.value).indexOf(item.id || item) > -1
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
self.optionGroup = new Ox.OptionGroup(
|
|
|
|
self.options.items,
|
|
|
|
self.options.min,
|
|
|
|
self.options.max,
|
|
|
|
'checked'
|
|
|
|
);
|
|
|
|
self.options.items = self.optionGroup.init();
|
|
|
|
self.options.value = self.optionGroup.value();
|
2011-04-22 22:03:10 +00:00
|
|
|
|
2011-05-30 11:47:30 +00:00
|
|
|
if (self.options.label) {
|
|
|
|
self.$label = Ox.Label({
|
|
|
|
overlap: 'right',
|
|
|
|
textAlign: 'right',
|
|
|
|
title: self.options.label,
|
|
|
|
width: self.options.labelWidth
|
|
|
|
})
|
|
|
|
.appendTo(that);
|
2011-06-19 17:48:32 +00:00
|
|
|
}
|
2011-05-30 11:47:30 +00:00
|
|
|
|
2011-04-22 22:03:10 +00:00
|
|
|
if (self.options.type == 'text') {
|
2011-12-21 13:42:47 +00:00
|
|
|
Ox.Log('Form', 'S.O.V.', self.options.value)
|
2011-04-22 22:03:10 +00:00
|
|
|
self.$title = $('<div>')
|
|
|
|
.addClass('OxTitle')
|
|
|
|
.css({
|
2011-11-30 14:40:41 +00:00
|
|
|
width: getTitleWidth() + 'px'
|
2011-04-22 22:03:10 +00:00
|
|
|
})
|
|
|
|
.html(
|
2011-11-30 14:40:41 +00:00
|
|
|
self.options.title
|
2011-12-21 13:42:47 +00:00
|
|
|
|| getItem(self.options.value).title
|
2011-04-22 22:03:10 +00:00
|
|
|
)
|
2011-12-20 07:07:57 +00:00
|
|
|
.appendTo(that);
|
2011-04-22 22:03:10 +00:00
|
|
|
}
|
|
|
|
|
2011-06-19 17:48:32 +00:00
|
|
|
self.$button = Ox.Button({
|
2011-12-20 07:07:57 +00:00
|
|
|
id: self.options.id + 'Button',
|
2011-04-22 22:03:10 +00:00
|
|
|
style: 'symbol',
|
2011-12-20 07:07:57 +00:00
|
|
|
title: self.options.type == 'text' || !self.options.title
|
|
|
|
? 'select' : self.options.title,
|
2011-04-22 22:03:10 +00:00
|
|
|
type: 'image'
|
|
|
|
})
|
|
|
|
.appendTo(that);
|
|
|
|
|
2011-06-19 17:48:32 +00:00
|
|
|
self.$menu = Ox.Menu({
|
2011-04-22 22:03:10 +00:00
|
|
|
element: self.$title || self.$button,
|
2011-12-20 07:07:57 +00:00
|
|
|
id: self.options.id + 'Menu',
|
2011-12-21 13:42:47 +00:00
|
|
|
items: [{
|
2011-12-20 07:07:57 +00:00
|
|
|
group: self.options.id + 'Group',
|
2011-04-22 22:03:10 +00:00
|
|
|
items: self.options.items,
|
|
|
|
max: self.options.max,
|
|
|
|
min: self.options.min
|
2011-12-21 13:42:47 +00:00
|
|
|
}],
|
2011-11-30 14:40:41 +00:00
|
|
|
maxWidth: self.options.maxWidth,
|
2011-12-20 07:07:57 +00:00
|
|
|
side: 'bottom', // FIXME: should be edge
|
2011-04-22 22:03:10 +00:00
|
|
|
size: self.options.size
|
|
|
|
})
|
|
|
|
.bindEvent({
|
|
|
|
change: changeMenu,
|
|
|
|
click: clickMenu,
|
|
|
|
hide: hideMenu
|
|
|
|
});
|
|
|
|
|
|
|
|
self.options.type == 'image' && self.$menu.addClass('OxRight');
|
|
|
|
|
2011-09-17 17:39:38 +00:00
|
|
|
function clickMenu(data) {
|
2011-04-22 22:03:10 +00:00
|
|
|
that.triggerEvent('click', data);
|
|
|
|
}
|
|
|
|
|
2011-09-17 17:39:38 +00:00
|
|
|
function changeMenu(data) {
|
2011-12-21 13:42:47 +00:00
|
|
|
self.options.value = self.optionGroup.value();
|
|
|
|
Ox.Log('Form', 'changeMenu: ', data, 'value:', self.options.value, 'checked:', self.optionGroup.checked())
|
|
|
|
self.$title && self.$title.html(
|
|
|
|
self.options.title ? self.options.title : getItem(self.options.value).title
|
|
|
|
);
|
|
|
|
that.triggerEvent('change', {
|
|
|
|
value: self.options.value
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getItem(id) {
|
|
|
|
return Ox.getObjectById(self.options.items, id);
|
2011-04-22 22:03:10 +00:00
|
|
|
}
|
|
|
|
|
2011-11-30 14:40:41 +00:00
|
|
|
function getTitleWidth() {
|
|
|
|
// fixme: used to be 22. obscure
|
|
|
|
return self.options.width - 24 - (
|
|
|
|
self.options.label ? self.options.labelWidth : 0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-04-22 22:03:10 +00:00
|
|
|
function hideMenu() {
|
2011-12-21 13:42:47 +00:00
|
|
|
that.loseFocus();
|
2011-04-22 22:03:10 +00:00
|
|
|
that.removeClass('OxSelected');
|
|
|
|
}
|
|
|
|
|
|
|
|
function loseFocus() {
|
|
|
|
that.loseFocus();
|
|
|
|
}
|
|
|
|
|
2011-12-21 13:42:47 +00:00
|
|
|
function selectItem() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-04-22 22:03:10 +00:00
|
|
|
function showMenu() {
|
|
|
|
that.gainFocus();
|
|
|
|
that.addClass('OxSelected');
|
2011-05-20 14:11:42 +00:00
|
|
|
self.options.tooltip && that.$tooltip.hide();
|
2011-04-22 22:03:10 +00:00
|
|
|
self.$menu.showMenu();
|
|
|
|
}
|
|
|
|
|
2011-04-29 12:40:51 +00:00
|
|
|
self.setOption = function(key, value) {
|
2011-11-30 14:40:41 +00:00
|
|
|
if (key == 'labelWidth') {
|
|
|
|
self.$label.options({width: value});
|
|
|
|
self.$title.css({width: getTitleWidth() + 'px'});
|
|
|
|
} else if (key == 'title') {
|
2011-12-20 07:07:57 +00:00
|
|
|
if (self.options.type == 'text') {
|
|
|
|
self.$title.html(value);
|
|
|
|
} else {
|
|
|
|
self.$button.options({title: value});
|
|
|
|
}
|
2011-11-30 14:40:41 +00:00
|
|
|
} else if (key == 'width') {
|
2011-12-21 13:42:47 +00:00
|
|
|
Ox.Log('Form', 'SETTING WIDTH OPTION', value);
|
2011-12-01 10:52:23 +00:00
|
|
|
that.css({width: value - 2 + 'px'});
|
2011-12-20 07:07:57 +00:00
|
|
|
self.$title.css({width: getTitleWidth() + 'px'});
|
2011-11-30 14:40:41 +00:00
|
|
|
} else if (key == 'value') {
|
2011-12-21 13:42:47 +00:00
|
|
|
Ox.Log('Form', 'SETTING VALUE OPTION', value);
|
|
|
|
if (self.options.type == 'text' && !self.options.title) {
|
|
|
|
self.$title.html(getItem(value).title);
|
|
|
|
}
|
|
|
|
Ox.toArray(value).forEach(function(value) {
|
|
|
|
self.$menu.checkItem(value);
|
|
|
|
});
|
|
|
|
self.options.value = self.optionGroup.value();
|
2011-05-30 12:06:58 +00:00
|
|
|
}
|
2011-04-22 22:03:10 +00:00
|
|
|
};
|
|
|
|
|
2011-10-29 17:05:44 +00:00
|
|
|
that.disableItem = function(id) {
|
|
|
|
self.$menu.getItem(id).options({disabled: true});
|
|
|
|
};
|
|
|
|
|
|
|
|
that.enableItem = function(id) {
|
|
|
|
self.$menu.getItem(id).options({disabled: false});
|
|
|
|
};
|
|
|
|
|
2011-11-04 22:14:30 +00:00
|
|
|
self.superRemove = that.remove;
|
|
|
|
that.remove = function() {
|
|
|
|
self.$menu.remove();
|
|
|
|
self.superRemove();
|
|
|
|
};
|
|
|
|
|
2011-05-16 10:49:48 +00:00
|
|
|
/*@
|
2011-09-23 10:43:57 +00:00
|
|
|
selected <f> gets selected item
|
2011-12-21 13:42:47 +00:00
|
|
|
() -> <o> returns array of selected items with id and title
|
2011-05-16 10:49:48 +00:00
|
|
|
@*/
|
2011-04-22 22:03:10 +00:00
|
|
|
that.selected = function() {
|
2011-12-21 13:42:47 +00:00
|
|
|
return Ox.toArray(self.optionGroup.value()).map(function(id) {
|
2011-04-22 22:03:10 +00:00
|
|
|
return {
|
2011-12-21 13:42:47 +00:00
|
|
|
id: id,
|
|
|
|
title: getItem(id).title
|
2011-04-22 22:03:10 +00:00
|
|
|
};
|
2011-12-21 13:42:47 +00:00
|
|
|
});
|
2011-04-22 22:03:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
that.width = function(val) {
|
|
|
|
// fixme: silly hack, and won't work for css() ... remove!
|
|
|
|
that.$element.width(val + 16);
|
|
|
|
that.$button.width(val);
|
|
|
|
//that.$symbol.width(val);
|
|
|
|
return that;
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
|
|
|
|
return that;
|
|
|
|
|
|
|
|
};
|