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

132 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
/*@
2012-05-31 10:32:54 +00:00
Ox.CollapsePanel <f> CollapsePanel Object
([options[, self]]) -> <o:Ox.Element> 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 || {};
2012-05-28 19:35:41 +00:00
var that = Ox.Element({}, 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)
2012-05-28 19:35:41 +00:00
.update({
collapsed: function() {
// will be toggled again in toggleCollapsed
self.options.collapsed = !self.options.collapsed;
self.$button.toggle();
toggleCollapsed();
},
title: function() {
self.$title.html(self.options.title);
}
})
.addClass('OxPanel OxCollapsePanel');
2011-12-22 15:47:46 +00:00
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
/*@
2012-05-28 15:53:21 +00:00
update <f> Update panel when in collapsed state
2011-05-16 10:49:48 +00:00
@*/
2012-05-28 15:53:21 +00:00
that.updatePanel = 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
};