2011-07-29 18:48:43 +00:00
|
|
|
// vim: et:ts=4:sw=4:sts=4:ft=javascript
|
2011-05-16 08:24:46 +00:00
|
|
|
/*@
|
|
|
|
Ox.Button <f:Ox.Element> Button Object
|
|
|
|
() -> <f> Button Object
|
|
|
|
(options) -> <f> Button Object
|
|
|
|
(options, self) -> <f> Button Object
|
|
|
|
options <o> Options object
|
|
|
|
disabled <b|false> disabled
|
|
|
|
group <b|false> is part of group
|
|
|
|
id: <s|''> button id
|
|
|
|
overlap <s|none> overlap
|
|
|
|
selectable <b|false> is selecatable
|
|
|
|
selected <b|false> is selected
|
|
|
|
size <s|medium> button size
|
|
|
|
style <s|default> // can be default, checkbox, symbol, or tab
|
|
|
|
title <s|a|''> title, can be array of titles
|
|
|
|
tooltip <s|a|''> tooltip if multiple must be same number as titles
|
|
|
|
type <s|text> button type, text or image, (for image, title must be symbolTitle.svg must be availabe)
|
|
|
|
width <s|auto> button width
|
|
|
|
self <o> Shared private variable
|
|
|
|
click <!> non-selectable button was clicked
|
|
|
|
deselect <!> selectable button was deselected
|
|
|
|
select <!> selectable button was selected
|
|
|
|
@*/
|
2011-04-22 22:03:10 +00:00
|
|
|
|
2011-05-16 08:24:46 +00:00
|
|
|
Ox.Button = function(options, self) {
|
2011-06-19 17:48:32 +00:00
|
|
|
|
|
|
|
self = self || {};
|
|
|
|
var that = Ox.Element('<input>', self)
|
2011-04-22 22:03:10 +00:00
|
|
|
.defaults({
|
|
|
|
disabled: false,
|
|
|
|
group: false,
|
|
|
|
id: '',
|
|
|
|
overlap: 'none',
|
|
|
|
selectable: false,
|
|
|
|
selected: false,
|
|
|
|
size: 'medium',
|
|
|
|
// fixme: 'default' or ''?
|
2011-10-06 04:06:49 +00:00
|
|
|
style: 'default',
|
2011-04-22 22:03:10 +00:00
|
|
|
title: '',
|
|
|
|
tooltip: '',
|
|
|
|
type: 'text',
|
|
|
|
width: 'auto'
|
|
|
|
})
|
|
|
|
.options(options || {})
|
2011-10-06 04:42:58 +00:00
|
|
|
.attr({
|
|
|
|
disabled: self.options.disabled,
|
2011-04-22 22:03:10 +00:00
|
|
|
type: self.options.type == 'text' ? 'button' : 'image'
|
2011-10-06 04:42:58 +00:00
|
|
|
})
|
2011-04-22 22:03:10 +00:00
|
|
|
.addClass('OxButton Ox' + Ox.toTitleCase(self.options.size) +
|
|
|
|
(self.options.disabled ? ' OxDisabled': '') +
|
|
|
|
(self.options.selected ? ' OxSelected': '') +
|
|
|
|
(self.options.style != 'default' ? ' Ox' + Ox.toTitleCase(self.options.style) : '') +
|
|
|
|
(self.options.overlap != 'none' ? ' OxOverlap' + Ox.toTitleCase(self.options.overlap) : ''))
|
|
|
|
.css(self.options.width == 'auto' ? {} : {
|
|
|
|
width: (self.options.width - 14) + 'px'
|
|
|
|
})
|
|
|
|
.mousedown(mousedown)
|
|
|
|
.click(click);
|
|
|
|
|
2011-09-17 18:36:09 +00:00
|
|
|
Ox.extend(self, Ox.isArray(self.options.title) ? {
|
2011-04-22 22:03:10 +00:00
|
|
|
selectedTitle: Ox.setPropertyOnce(self.options.title, 'selected'),
|
|
|
|
titles: self.options.title
|
|
|
|
} : {
|
|
|
|
selectedTitle: 0,
|
|
|
|
titles: [{
|
|
|
|
id: '',
|
|
|
|
title: self.options.title
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
|
|
|
|
setTitle(self.titles[self.selectedTitle].title);
|
|
|
|
|
|
|
|
if (self.options.tooltip) {
|
2011-11-04 22:14:30 +00:00
|
|
|
self.tooltips = Ox.isArray(self.options.tooltip)
|
|
|
|
? self.options.tooltip : [self.options.tooltip];
|
|
|
|
that.options({tooltip: self.tooltips[self.selectedTitle]});
|
2011-04-22 22:03:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function click() {
|
|
|
|
if (!self.options.disabled) {
|
|
|
|
var data = self.titles[self.selectedTitle];
|
|
|
|
if (!self.options.selectable) {
|
|
|
|
that.triggerEvent('click', data);
|
|
|
|
} else {
|
|
|
|
//self.options.selected = !self.options.selected;
|
|
|
|
//that.toggleClass('OxSelected');
|
|
|
|
if (self.options.group) {
|
|
|
|
that.triggerEvent('select', data);
|
|
|
|
} else {
|
|
|
|
that.toggleSelected();
|
|
|
|
//that.triggerEvent('change', {selected: self.options.selected});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (self.titles.length == 2) {
|
|
|
|
that.toggleTitle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function mousedown(e) {
|
|
|
|
if (self.options.type == 'image' && $.browser.safari) {
|
|
|
|
// keep image from being draggable
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setTitle(title) {
|
|
|
|
self.title = title;
|
|
|
|
if (self.options.type == 'image') {
|
|
|
|
that.attr({
|
2011-08-09 17:00:39 +00:00
|
|
|
src: Ox.UI.getImageURL(
|
|
|
|
'symbol' + title[0].toUpperCase() + title.substr(1)
|
2011-04-22 22:03:10 +00:00
|
|
|
)
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
that.val(title);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-29 12:40:51 +00:00
|
|
|
self.setOption = function(key, value) {
|
2011-04-22 22:03:10 +00:00
|
|
|
if (key == 'disabled') {
|
2011-10-06 04:42:58 +00:00
|
|
|
that.attr({disabled: value})
|
|
|
|
.toggleClass('OxDisabled');
|
2011-04-22 22:03:10 +00:00
|
|
|
} else if (key == 'selected') {
|
|
|
|
if (value != that.hasClass('OxSelected')) { // fixme: neccessary?
|
|
|
|
that.toggleClass('OxSelected');
|
|
|
|
}
|
|
|
|
that.triggerEvent('change');
|
2011-11-04 22:14:30 +00:00
|
|
|
} else if (key == 'tooltip') {
|
|
|
|
|
2011-04-22 22:03:10 +00:00
|
|
|
} else if (key == 'title') {
|
|
|
|
setTitle(value);
|
|
|
|
} else if (key == 'width') {
|
|
|
|
that.$element.css({
|
|
|
|
width: (value - 14) + 'px'
|
|
|
|
});
|
|
|
|
}
|
2011-11-04 22:14:30 +00:00
|
|
|
};
|
2011-04-22 22:03:10 +00:00
|
|
|
|
2011-05-16 08:24:46 +00:00
|
|
|
/*@
|
|
|
|
toggleDisabled <f>
|
|
|
|
() -> <u> toggle disabled state
|
|
|
|
@*/
|
2011-04-22 22:03:10 +00:00
|
|
|
that.toggleDisabled = function() {
|
|
|
|
that.options({
|
2011-05-19 17:58:00 +00:00
|
|
|
disabled: !self.options.disabled
|
2011-04-22 22:03:10 +00:00
|
|
|
});
|
2011-05-19 17:58:00 +00:00
|
|
|
return that;
|
2011-04-22 22:03:10 +00:00
|
|
|
//self.options.disabled = !self.options.disabled;
|
|
|
|
}
|
|
|
|
|
2011-05-16 08:24:46 +00:00
|
|
|
/*@
|
|
|
|
toggleSelected <f>
|
|
|
|
() -> <u> toggle selected state
|
|
|
|
@*/
|
2011-04-22 22:03:10 +00:00
|
|
|
that.toggleSelected = function() {
|
|
|
|
that.options({
|
|
|
|
selected: !self.options.selected
|
|
|
|
});
|
2011-05-19 17:58:00 +00:00
|
|
|
return that;
|
2011-04-22 22:03:10 +00:00
|
|
|
//self.options.selected = !self.options.selected;
|
|
|
|
}
|
|
|
|
|
2011-05-16 08:24:46 +00:00
|
|
|
/*@
|
|
|
|
toggleTitle <f>
|
|
|
|
() -> <u> toggle through titles
|
|
|
|
@*/
|
2011-04-22 22:03:10 +00:00
|
|
|
that.toggleTitle = function() {
|
|
|
|
self.selectedTitle = 1 - self.selectedTitle;
|
|
|
|
setTitle(self.titles[self.selectedTitle].title);
|
2011-09-04 04:14:58 +00:00
|
|
|
// fixme: if the tooltip is visible
|
|
|
|
// we also need to call show()
|
2011-11-04 22:14:30 +00:00
|
|
|
that.$tooltip && that.$tooltip.options({
|
2011-04-22 22:03:10 +00:00
|
|
|
title: self.tooltips[self.selectedTitle]
|
|
|
|
});
|
2011-05-19 17:58:00 +00:00
|
|
|
return that;
|
2011-04-22 22:03:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return that;
|
|
|
|
|
|
|
|
};
|