oxjs/source/Ox.UI/js/Panel/Ox.TabPanel.js

95 lines
2.4 KiB
JavaScript
Raw Normal View History

2011-07-29 18:48:43 +00:00
// vim: et:ts=4:sw=4:sts=4:ft=javascript
/*@
Ox.TabPanel <f> Tabbed panel
(options) -> <o> Panel
(options, self) -> <o> Panel
options <o> Options
content <o|f> Content per tab
Either <code>({id1: $element1, id2: $element2}}</code> or
<code>function(id) { return $element; })</code>
size <n|24> Height of the tab bar
tabs [o] Tabs
id <s> Tab id
title <s> Tab title
self <o> Shared private variable
@*/
2011-11-05 16:46:53 +00:00
'use strict';
2011-04-22 22:03:10 +00:00
Ox.TabPanel = function(options, self) {
2011-08-17 14:59:37 +00:00
self = self || {};
var that = Ox.Element({}, self)
.defaults({
content: null,
size: 24,
2011-08-17 14:59:37 +00:00
tabs: []
})
.options(options || {});
2011-11-02 10:23:15 +00:00
self.selected = getSelected();
2011-08-17 14:59:37 +00:00
self.$bar = Ox.Bar({size: 24});
self.$tabs = Ox.ButtonGroup({
buttons: self.options.tabs,
id: 'tabs',
2011-12-22 15:47:46 +00:00
selectable: true,
value: self.selected
2011-08-17 14:59:37 +00:00
})
.css({top: (self.options.size - 16) / 2 + 'px'})
2011-08-17 14:59:37 +00:00
.bindEvent({
change: function(data) {
2011-12-21 15:33:52 +00:00
self.selected = data.value;
that.$element.replaceElement(1, getContent());
that.triggerEvent('change', {selected: self.selected});
2011-08-17 14:59:37 +00:00
}
})
.appendTo(self.$bar);
that.setElement(
Ox.SplitPanel({
2011-08-17 14:59:37 +00:00
elements: [
{
element: self.$bar,
size: self.options.size
2011-08-17 14:59:37 +00:00
},
{
element: getContent()
2011-08-17 14:59:37 +00:00
}
],
orientation: 'vertical'
})
.addClass('OxTabPanel')
);
2011-08-17 14:59:37 +00:00
function getContent() {
return Ox.isObject(self.options.content)
? self.options.content[self.selected]
: self.options.content(self.selected);
}
2011-11-02 10:23:15 +00:00
function getSelected() {
var selected = self.options.tabs.filter(function(tab) {
2011-11-02 10:23:15 +00:00
return tab.selected;
});
return (selected.length ? selected : self.options.tabs)[0].id;
2011-11-02 10:23:15 +00:00
}
// fixme: does this collide with a jquery fn?
that.select = function(id) {
if (Ox.getIndexById(self.options.tabs, id) > -1) {
self.$tabs.options({value: id});
}
return that;
};
2011-11-02 10:23:15 +00:00
that.selected = function() {
return self.selected;
};
2011-08-17 14:59:37 +00:00
return that;
2011-04-22 22:03:10 +00:00
};