2011-11-05 16:46:53 +00:00
|
|
|
'use strict';
|
2012-05-21 10:38:18 +00:00
|
|
|
|
2011-05-16 08:24:46 +00:00
|
|
|
/*@
|
2012-05-31 10:32:54 +00:00
|
|
|
Ox.SplitPanel <f> SpliPanel Object
|
|
|
|
([options[, self]]) -> <o:Ox.Element> SpliPanel Object
|
2011-05-16 08:24:46 +00:00
|
|
|
options <o> Options object
|
2011-09-04 21:15:16 +00:00
|
|
|
elements <[o]|[]> Array of two or three element objects
|
|
|
|
collapsible <b|false> If true, can be collapsed (if outer element)
|
|
|
|
collapsed <b|false> If true, is collapsed (if collapsible)
|
|
|
|
element <o> Any Ox.Element
|
|
|
|
If any element is collapsible or resizable, all elements must
|
|
|
|
have an id.
|
|
|
|
resizable <b|false> If true, can be resized (if outer element)
|
|
|
|
resize <[n]|[]> Min size, optional snappy points, and max size
|
|
|
|
size <n|s|"auto"> Size in px (one element must be "auto")
|
|
|
|
tooltip <b|s|false> If true, show tooltip, if string, append it
|
|
|
|
orientation <s|"horizontal"> orientation ("horizontal" or "vertical")
|
2011-05-16 08:24:46 +00:00
|
|
|
self <o> shared private variable
|
|
|
|
resize <!> resize
|
2011-09-04 21:15:16 +00:00
|
|
|
Fires on resize, on both elements being resized
|
2011-05-16 08:24:46 +00:00
|
|
|
toggle <!> toggle
|
2011-09-04 21:15:16 +00:00
|
|
|
Fires on collapse or expand, on the element being toggled
|
2011-05-16 08:24:46 +00:00
|
|
|
@*/
|
|
|
|
|
2011-04-22 22:03:10 +00:00
|
|
|
Ox.SplitPanel = function(options, self) {
|
|
|
|
|
2011-10-16 12:32:02 +00:00
|
|
|
// fixme: doubleclick on resizebar should reset to initial size
|
|
|
|
|
2011-06-19 17:48:32 +00:00
|
|
|
self = self || {};
|
2011-09-04 21:15:16 +00:00
|
|
|
var that = Ox.Element({}, self) // fixme: Container?
|
2011-04-22 22:03:10 +00:00
|
|
|
.defaults({
|
|
|
|
elements: [],
|
|
|
|
orientation: 'horizontal'
|
|
|
|
})
|
|
|
|
.options(options || {})
|
|
|
|
.addClass('OxSplitPanel');
|
|
|
|
|
2011-09-17 18:36:09 +00:00
|
|
|
Ox.extend(self, {
|
2011-04-22 22:03:10 +00:00
|
|
|
dimensions: Ox.UI.DIMENSIONS[self.options.orientation],
|
|
|
|
edges: Ox.UI.EDGES[self.options.orientation],
|
2012-04-23 09:37:49 +00:00
|
|
|
defaultSize: self.options.elements.map(function(element) {
|
|
|
|
return !Ox.isUndefined(element.defaultSize)
|
|
|
|
? element.defaultSize : element.size;
|
|
|
|
}),
|
2011-04-22 22:03:10 +00:00
|
|
|
length: self.options.elements.length,
|
|
|
|
resizebarElements: [],
|
|
|
|
$resizebars: []
|
|
|
|
});
|
|
|
|
|
|
|
|
// create elements
|
|
|
|
that.$elements = [];
|
2011-11-01 19:56:11 +00:00
|
|
|
self.options.elements.forEach(function(element, i) {
|
2011-09-17 18:36:09 +00:00
|
|
|
self.options.elements[i] = Ox.extend({
|
2011-04-22 22:03:10 +00:00
|
|
|
collapsible: false,
|
|
|
|
collapsed: false,
|
|
|
|
resizable: false,
|
|
|
|
resize: [],
|
|
|
|
size: 'auto'
|
2011-11-01 19:56:11 +00:00
|
|
|
}, element);
|
|
|
|
that.$elements[i] = element.element
|
2012-06-13 08:28:21 +00:00
|
|
|
.css(self.edges[2], (parseInt(element.element.css(self.edges[2]), 10) || 0) + 'px')
|
|
|
|
.css(self.edges[3], (parseInt(element.element.css(self.edges[3]), 10) || 0) + 'px');
|
2011-04-22 22:03:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// create resizebars
|
2011-09-04 21:15:16 +00:00
|
|
|
self.options.elements.forEach(function(element, i) {
|
2011-04-22 22:03:10 +00:00
|
|
|
var index = i == 0 ? 0 : 1;
|
|
|
|
that.$elements[i].appendTo(that.$element); // fixme: that.$content
|
2011-09-04 21:15:16 +00:00
|
|
|
if (element.collapsible || element.resizable) {
|
2011-04-22 22:03:10 +00:00
|
|
|
self.resizebarElements[index] = i < 2 ? [0, 1] : [1, 2];
|
2011-06-19 17:48:32 +00:00
|
|
|
self.$resizebars[index] = Ox.Resizebar({
|
2011-09-04 21:15:16 +00:00
|
|
|
collapsed: element.collapsed,
|
|
|
|
collapsible: element.collapsible,
|
2011-04-22 22:03:10 +00:00
|
|
|
edge: self.edges[index],
|
|
|
|
elements: [
|
|
|
|
that.$elements[self.resizebarElements[index][0]],
|
|
|
|
that.$elements[self.resizebarElements[index][1]]
|
|
|
|
],
|
2011-09-04 21:15:16 +00:00
|
|
|
id: element.element.options('id'),
|
2011-04-22 22:03:10 +00:00
|
|
|
orientation: self.options.orientation == 'horizontal' ? 'vertical' : 'horizontal',
|
|
|
|
parent: that, // fixme: that.$content
|
2011-09-04 21:15:16 +00:00
|
|
|
resizable: element.resizable,
|
|
|
|
resize: element.resize,
|
|
|
|
size: element.size,
|
|
|
|
tooltip: element.tooltip
|
2011-04-22 22:03:10 +00:00
|
|
|
});
|
|
|
|
self.$resizebars[index][i == 0 ? 'insertAfter' : 'insertBefore'](that.$elements[i]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-09-04 21:15:16 +00:00
|
|
|
self.options.elements.forEach(function(element, i) {
|
|
|
|
element.collapsed && that.css(
|
2011-04-22 22:03:10 +00:00
|
|
|
self.edges[i == 0 ? 0 : 1], -self.options.elements[i].size + 'px'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
setSizes(true);
|
|
|
|
|
|
|
|
function getPositionById(id) {
|
|
|
|
var position = -1;
|
|
|
|
Ox.forEach(self.options.elements, function(element, i) {
|
|
|
|
if (element.element.options('id') == id) {
|
|
|
|
position = i;
|
2012-05-25 07:46:34 +00:00
|
|
|
Ox.Break();
|
2011-04-22 22:03:10 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSize(element) {
|
|
|
|
return element.size + (element.collapsible || element.resizable);
|
|
|
|
//return (element.size + (element.collapsible || element.resizable)) * !element.collapsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getVisibleSize(element) {
|
|
|
|
return getSize(element) * !element.collapsed;
|
|
|
|
}
|
|
|
|
|
2011-09-18 21:17:39 +00:00
|
|
|
function setSizes(init, animate) {
|
2012-05-27 19:32:39 +00:00
|
|
|
// will animate if animate is truthy and call animate if it's a function
|
2011-09-04 21:15:16 +00:00
|
|
|
self.options.elements.forEach(function(element, i) {
|
2011-04-22 22:03:10 +00:00
|
|
|
// fixme: maybe we can add a conditional here, since init
|
|
|
|
// is about elements that are collapsed splitpanels
|
2011-09-18 21:17:39 +00:00
|
|
|
var css = {},
|
|
|
|
edges = [
|
2012-06-13 08:28:21 +00:00
|
|
|
(init && parseInt(that.$elements[i].css(self.edges[0]), 10)) || 0,
|
|
|
|
(init && parseInt(that.$elements[i].css(self.edges[1]), 10)) || 0
|
2011-09-18 21:17:39 +00:00
|
|
|
];
|
|
|
|
if (element.size != 'auto') {
|
|
|
|
css[self.dimensions[0]] = element.size + 'px';
|
|
|
|
}
|
2011-04-22 22:03:10 +00:00
|
|
|
if (i == 0) {
|
2011-09-18 21:17:39 +00:00
|
|
|
css[self.edges[0]] = edges[0] + 'px';
|
|
|
|
if (element.size == 'auto') {
|
|
|
|
css[self.edges[1]] = getSize(self.options.elements[1])
|
|
|
|
+ (
|
|
|
|
self.length == 3 ? getVisibleSize(self.options.elements[2]) : 0
|
|
|
|
) + 'px';
|
|
|
|
}
|
2011-04-22 22:03:10 +00:00
|
|
|
} else if (i == 1) {
|
2011-09-18 21:17:39 +00:00
|
|
|
if (self.options.elements[0].size != 'auto') {
|
|
|
|
css[self.edges[0]] = edges[0] + getSize(self.options.elements[0]) + 'px'
|
|
|
|
} else {
|
|
|
|
css[self.edges[0]] = 'auto'; // fixme: why is this needed?
|
|
|
|
}
|
|
|
|
css[self.edges[1]] = (
|
2011-11-02 08:22:55 +00:00
|
|
|
self.length == 3 ? getSize(self.options.elements[2]) : 0
|
2011-09-18 21:17:39 +00:00
|
|
|
) + 'px';
|
|
|
|
} else {
|
|
|
|
if (element.size == 'auto') {
|
|
|
|
css[self.edges[0]] = getVisibleSize(self.options.elements[0])
|
|
|
|
+ getSize(self.options.elements[1]) + 'px';
|
|
|
|
} else {
|
|
|
|
css[self.edges[0]] = 'auto'; // fixme: why is this needed?
|
|
|
|
}
|
|
|
|
css[self.edges[1]] = edges[1] + 'px';
|
|
|
|
}
|
|
|
|
if (animate) {
|
|
|
|
that.$elements[i].animate(css, 250, function() {
|
|
|
|
i == 0 && Ox.isFunction(animate) && animate();
|
|
|
|
});
|
2011-04-22 22:03:10 +00:00
|
|
|
} else {
|
2011-09-18 21:17:39 +00:00
|
|
|
that.$elements[i].css(css);
|
2011-04-22 22:03:10 +00:00
|
|
|
}
|
2011-09-04 21:15:16 +00:00
|
|
|
if (element.collapsible || element.resizable) {
|
2011-09-18 21:17:39 +00:00
|
|
|
css = {};
|
|
|
|
css[self.edges[i == 0 ? 0 : 1]] = element.size + 'px'
|
|
|
|
if (animate) {
|
|
|
|
self.$resizebars[i == 0 ? 0 : 1].animate(css, 250);
|
|
|
|
} else {
|
|
|
|
self.$resizebars[i == 0 ? 0 : 1].css(css);
|
|
|
|
}
|
2011-09-30 10:34:33 +00:00
|
|
|
self.$resizebars[i == 0 ? 0 : 1].options({size: element.size});
|
2011-04-22 22:03:10 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-04-08 18:22:27 +00:00
|
|
|
/*@
|
|
|
|
getSize <f> get size of panel
|
|
|
|
(id) -> <i> id or position of panel, returns size
|
|
|
|
id <s|i> The element's id or position
|
|
|
|
@*/
|
|
|
|
// fixme: what is this? there is that.size()
|
|
|
|
that.getSize = function(id) {
|
|
|
|
var pos = Ox.isNumber(id) ? id : getPositionById(id),
|
|
|
|
element = self.options.elements[pos];
|
|
|
|
return element.element[self.dimensions[0]]() * !that.isCollapsed(pos);
|
|
|
|
};
|
|
|
|
|
2011-09-05 12:42:37 +00:00
|
|
|
/*@
|
2011-11-03 15:42:41 +00:00
|
|
|
isCollapsed <f> panel collapsed state
|
2011-09-05 12:42:37 +00:00
|
|
|
(id) -> <b> id or position of panel, returns collapsed state
|
2011-09-18 21:17:39 +00:00
|
|
|
id <i> The element's id or position
|
2011-09-05 12:42:37 +00:00
|
|
|
@*/
|
2011-04-22 22:03:10 +00:00
|
|
|
that.isCollapsed = function(id) {
|
|
|
|
var pos = Ox.isNumber(id) ? id : getPositionById(id);
|
|
|
|
return self.options.elements[pos].collapsed;
|
|
|
|
};
|
|
|
|
|
2011-09-05 12:42:37 +00:00
|
|
|
/*@
|
2012-04-07 10:43:41 +00:00
|
|
|
replaceElement <f> Replace panel element
|
2011-09-05 12:42:37 +00:00
|
|
|
(id, element) -> <f> replace element
|
2012-06-02 09:25:52 +00:00
|
|
|
id <s|n> The element's id or position
|
2011-09-05 12:42:37 +00:00
|
|
|
element <o> new element
|
|
|
|
@*/
|
2011-04-22 22:03:10 +00:00
|
|
|
that.replaceElement = function(id, element) {
|
|
|
|
// one can pass pos instead of id
|
|
|
|
var pos = Ox.isNumber(id) ? id : getPositionById(id);
|
|
|
|
that.$elements[pos] = element
|
2012-06-13 08:28:21 +00:00
|
|
|
.css(self.edges[2], (parseInt(element.css(self.edges[2]), 10) || 0) + 'px')
|
|
|
|
.css(self.edges[3], (parseInt(element.css(self.edges[3]), 10) || 0) + 'px');
|
2011-11-04 15:54:28 +00:00
|
|
|
Ox.Log('Panel', 'REPLACE ELEMENT')
|
2011-11-01 19:56:11 +00:00
|
|
|
self.options.elements[pos].element.replaceWith(
|
|
|
|
self.options.elements[pos].element = element
|
|
|
|
);
|
2011-04-22 22:03:10 +00:00
|
|
|
setSizes();
|
|
|
|
self.$resizebars.forEach(function($resizebar, i) {
|
|
|
|
$resizebar.options({
|
|
|
|
elements: [
|
|
|
|
that.$elements[self.resizebarElements[i][0]],
|
|
|
|
that.$elements[self.resizebarElements[i][1]]
|
|
|
|
]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return that;
|
|
|
|
};
|
|
|
|
|
2011-09-05 12:42:37 +00:00
|
|
|
/*@
|
2011-09-18 21:17:39 +00:00
|
|
|
replaceElements <f> replace panel elements
|
2011-09-05 12:42:37 +00:00
|
|
|
(elements) -> <f> replace elements
|
|
|
|
elements <a> array of new elements
|
|
|
|
@*/
|
2011-04-22 22:03:10 +00:00
|
|
|
that.replaceElements = function(elements) {
|
|
|
|
elements.forEach(function(element, i) {
|
|
|
|
if (Ox.isNumber(element.size)) {
|
|
|
|
that.size(i, element.size);
|
|
|
|
if (element.collapsible || element.resizable) {
|
|
|
|
self.$resizebars[i == 0 ? 0 : 1].options({
|
|
|
|
collapsible: element.collapsible,
|
|
|
|
resizable: element.resizable,
|
|
|
|
size: element.size
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
that.replace(i, element.element);
|
|
|
|
});
|
|
|
|
self.options.elements = elements;
|
|
|
|
self.$resizebars.forEach(function($resizebar, i) {
|
|
|
|
$resizebar.options({
|
|
|
|
elements: [
|
|
|
|
that.$elements[self.resizebarElements[i][0]],
|
|
|
|
that.$elements[self.resizebarElements[i][1]]
|
|
|
|
]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return that;
|
2011-06-19 17:48:32 +00:00
|
|
|
};
|
2011-04-22 22:03:10 +00:00
|
|
|
|
2012-04-23 09:37:49 +00:00
|
|
|
/*@
|
|
|
|
reset <f> Reset an outer element to its initial size
|
|
|
|
@*/
|
|
|
|
that.reset = function(id) {
|
|
|
|
// one can pass pos instead of id
|
|
|
|
var pos = Ox.isNumber(id) ? id : getPositionById(id),
|
|
|
|
element = self.options.elements[pos];
|
|
|
|
element.size = self.defaultSize[pos];
|
|
|
|
setSizes(false, function() {
|
|
|
|
element.element.triggerEvent('resize', {
|
|
|
|
size: element.size
|
|
|
|
});
|
|
|
|
element = self.options.elements[pos == 0 ? 1 : pos - 1];
|
|
|
|
element.element.triggerEvent('resize', {
|
|
|
|
size: element.element[self.dimensions[0]]()
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2011-09-05 12:42:37 +00:00
|
|
|
/*@
|
2011-09-18 21:17:39 +00:00
|
|
|
size <f> Get or set size of an element
|
|
|
|
(id) -> <i> Returns size
|
|
|
|
(id, size) -> <o> Sets size, returns SplitPanel
|
2011-09-30 10:34:33 +00:00
|
|
|
(id, size, callback) -> <o> Sets size with animation, returns SplitPanel
|
2011-09-18 21:17:39 +00:00
|
|
|
id <i> The element's id or position
|
|
|
|
size <i> New size, in px
|
|
|
|
callback <b|f> Callback function (passing true animates w/o callback)
|
2011-09-05 12:42:37 +00:00
|
|
|
@*/
|
2011-09-18 21:17:39 +00:00
|
|
|
that.size = function(id, size, callback) {
|
2011-04-22 22:03:10 +00:00
|
|
|
// one can pass pos instead of id
|
|
|
|
var pos = Ox.isNumber(id) ? id : getPositionById(id),
|
2011-09-18 21:17:39 +00:00
|
|
|
element = self.options.elements[pos],
|
|
|
|
animate = {};
|
2011-04-22 22:03:10 +00:00
|
|
|
if (arguments.length == 1) {
|
|
|
|
return element.element[self.dimensions[0]]() * !that.isCollapsed(pos);
|
|
|
|
} else {
|
2011-09-18 21:17:39 +00:00
|
|
|
element.size = size;
|
|
|
|
setSizes(false, callback);
|
2011-04-22 22:03:10 +00:00
|
|
|
return that;
|
|
|
|
}
|
|
|
|
};
|
2011-09-05 12:42:37 +00:00
|
|
|
|
|
|
|
/*@
|
2011-09-18 21:17:39 +00:00
|
|
|
toggle <f> Toggles collapsed state of an outer element
|
|
|
|
(id) -> <o> The SplitPanel
|
|
|
|
id <s|i> The element's id or position
|
2011-09-05 12:42:37 +00:00
|
|
|
@*/
|
2011-04-22 22:03:10 +00:00
|
|
|
that.toggle = function(id) {
|
|
|
|
// one can pass pos instead of id
|
|
|
|
var pos = Ox.isNumber(id) ? id : getPositionById(id),
|
|
|
|
element = self.options.elements[pos],
|
2012-06-13 08:28:21 +00:00
|
|
|
value = parseInt(that.css(self.edges[pos == 0 ? 0 : 1]), 10)
|
|
|
|
+ element.element[self.dimensions[0]]() * (element.collapsed ? 1 : -1),
|
2011-04-22 22:03:10 +00:00
|
|
|
animate = {};
|
|
|
|
animate[self.edges[pos == 0 ? 0 : 1]] = value;
|
2011-09-18 21:17:39 +00:00
|
|
|
that.animate(animate, 250, function() {
|
2011-04-22 22:03:10 +00:00
|
|
|
element.collapsed = !element.collapsed;
|
|
|
|
element.element.triggerEvent('toggle', {
|
2012-04-23 09:37:49 +00:00
|
|
|
collapsed: element.collapsed
|
2011-04-22 22:03:10 +00:00
|
|
|
});
|
2011-10-13 10:10:39 +00:00
|
|
|
self.$resizebars[pos == 0 ? 0 : 1].options({collapsed: element.collapsed});
|
2011-04-22 22:03:10 +00:00
|
|
|
element = self.options.elements[pos == 0 ? 1 : pos - 1];
|
2011-09-20 21:50:00 +00:00
|
|
|
element.element.triggerEvent('resize', {
|
|
|
|
size: element.element[self.dimensions[0]]()
|
2011-09-17 11:49:29 +00:00
|
|
|
});
|
2011-04-22 22:03:10 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2011-09-05 12:42:37 +00:00
|
|
|
/*@
|
|
|
|
updateSize <f> update size of element
|
|
|
|
(pos, size) -> <o> update size of element
|
|
|
|
pos <i> position of element
|
|
|
|
size <n> new size
|
|
|
|
@*/
|
2011-04-22 22:03:10 +00:00
|
|
|
that.updateSize = function(pos, size) {
|
|
|
|
// this is called from resizebar
|
2011-06-19 17:48:32 +00:00
|
|
|
pos = pos == 0 ? 0 : self.options.elements.length - 1; // fixme: silly that 0 or 1 is passed, and not pos
|
2011-04-22 22:03:10 +00:00
|
|
|
self.options.elements[pos].size = size;
|
2011-06-19 17:48:32 +00:00
|
|
|
};
|
2011-04-22 22:03:10 +00:00
|
|
|
|
|
|
|
return that;
|
|
|
|
|
2012-05-21 10:38:18 +00:00
|
|
|
};
|