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

136 lines
3.8 KiB
JavaScript
Raw Normal View History

2011-11-05 16:46:53 +00:00
'use strict';
2011-05-16 08:24:46 +00:00
/*@
2011-05-16 10:49:48 +00:00
Ox.CollapsePanel <f:Ox.Panel> CollapsePanel Object
2012-05-22 13:14:40 +00:00
([options[, self]]) -> <o> CollapsePanel Object
2011-05-16 08:24:46 +00:00
options <o> Options object
2011-05-16 10:49:48 +00:00
collapsed <b|false> collapsed state
extras <a|[]> panel extras
size <n|16> size
title <s> title
2011-05-16 08:24:46 +00:00
self <o> shared private variable
@*/
2011-04-22 22:03:10 +00:00
Ox.CollapsePanel = function(options, self) {
self = self || {};
var that = Ox.Panel({}, self)
2011-04-22 22:03:10 +00:00
.defaults({
animate: true,
2011-04-22 22:03:10 +00:00
collapsed: false,
extras: [],
size: 16,
title: ''
})
.options(options)
2011-12-22 15:47:46 +00:00
.addClass('OxCollapsePanel');
self.$titlebar = Ox.Bar({
orientation: 'horizontal',
2012-05-26 15:48:19 +00:00
size: self.options.size
2011-12-22 15:47:46 +00:00
})
.bindEvent({
doubleclick: doubleclickTitlebar
})
.appendTo(that);
self.$button = Ox.Button({
style: 'symbol',
type: 'image',
value: self.options.collapsed ? 'expand' : 'collapse',
values: [
{id: 'expand', title: 'right'},
{id: 'collapse', title: 'down'}
]
})
.bindEvent({
click: toggleCollapsed
})
.appendTo(self.$titlebar);
self.$title = Ox.Element()
.addClass('OxTitle')
.html(self.options.title)
.appendTo(self.$titlebar);
2011-09-01 04:46:14 +00:00
2011-04-22 22:03:10 +00:00
if (self.options.extras.length) {
2011-12-22 15:47:46 +00:00
self.$extras = Ox.Element()
2011-04-22 22:03:10 +00:00
.addClass('OxExtras')
2012-01-13 16:25:47 +00:00
.css({width: self.options.extras.length * 16 + 1 + 'px'})
2011-12-22 15:47:46 +00:00
.appendTo(self.$titlebar);
2011-04-22 22:03:10 +00:00
self.options.extras.forEach(function($extra) {
2011-12-22 15:47:46 +00:00
$extra.appendTo(self.$extras);
2011-04-22 22:03:10 +00:00
});
}
2011-09-01 04:46:14 +00:00
that.$content = Ox.Element()
2011-04-22 22:03:10 +00:00
.addClass('OxContent')
.appendTo(that);
// fixme: doesn't work, content still empty
// need to hide it if collapsed
if (self.options.collapsed) {
that.$content.css({
marginTop: -that.$content.height() + 'px'
2012-01-02 14:25:04 +00:00
}).hide();
2011-04-22 22:03:10 +00:00
}
2011-09-01 04:46:14 +00:00
2011-12-22 15:47:46 +00:00
function doubleclickTitlebar(e) {
2011-04-22 22:03:10 +00:00
if (!$(e.target).hasClass('OxButton')) {
2011-12-22 15:47:46 +00:00
self.$button.trigger('click');
2011-04-22 22:03:10 +00:00
}
}
2011-09-01 04:46:14 +00:00
2011-04-22 22:03:10 +00:00
function toggleCollapsed() {
2012-01-02 14:05:14 +00:00
// show/hide is needed in case the collapsed content
// grows vertically when shrinking the panel horizontally
2011-04-22 22:03:10 +00:00
var marginTop;
self.options.collapsed = !self.options.collapsed;
marginTop = self.options.collapsed ? -that.$content.height() : 0;
2012-01-02 14:25:04 +00:00
!self.options.collapsed && that.$content.css({
marginTop: -that.$content.height() + 'px'
}).show();
if (self.options.animate) {
that.$content.animate({
marginTop: marginTop + 'px'
}, 250, function() {
self.options.collapsed && that.$content.hide();
});
} else {
that.$content.css({
marginTop: marginTop + 'px'
});
2012-01-02 14:16:08 +00:00
self.options.collapsed && that.$content.hide();
}
2011-04-22 22:03:10 +00:00
that.triggerEvent('toggle', {
collapsed: self.options.collapsed
});
}
2011-09-01 04:46:14 +00:00
2011-05-16 10:49:48 +00:00
/*@
setOption <f> setOption
(key, value) -> <u> set key to value
@*/
2011-04-29 12:40:51 +00:00
self.setOption = function(key, value) {
2011-04-22 22:03:10 +00:00
if (key == 'collapsed') {
2012-01-17 09:25:58 +00:00
// will be toggled again in toggleCollapsed
self.options.collapsed = !self.options.collapsed;
self.$button.toggle();
toggleCollapsed();
2011-04-22 22:03:10 +00:00
} else if (key == 'title') {
2011-12-22 15:47:46 +00:00
self.$title.html(self.options.title);
2011-04-22 22:03:10 +00:00
}
};
2011-05-16 10:49:48 +00:00
/*@
update <f> update // fixme: used anywhere?
@*/
that.update = function() {
2011-04-22 22:03:10 +00:00
self.options.collapsed && that.$content.css({
marginTop: -that.$content.height()
});
};
2011-09-01 04:46:14 +00:00
2011-04-22 22:03:10 +00:00
return that;
2011-09-01 04:46:14 +00:00
2011-04-22 22:03:10 +00:00
};