avoid leaks by adding custom empty/remove/replaceWith functions to Ox.Element

This commit is contained in:
rlx 2011-11-01 19:56:11 +00:00
parent 90bc4fb59e
commit 7848277593
4 changed files with 41 additions and 14 deletions

View file

@ -100,6 +100,7 @@ Ox.Element = function(options, self) {
var that = new Ox.JQueryElement(
$(self.options.element || '<div>')
)
.addClass('OxElement')
.mousedown(mousedown);
if (self.options.tooltip) {
@ -319,6 +320,14 @@ Ox.Element = function(options, self) {
return that;
};
that.empty = function() {
that.$element.children('.OxElement').each(function() {
Ox.UI.elements[$(this).data('oxid')].removeElement();
});
that.$element.empty();
return that;
};
/*@
gainFocus <function> Makes an element object gain focus
() -> <obj> This element object
@ -369,12 +378,20 @@ Ox.Element = function(options, self) {
removeElement <function> Removes an element object and its event handler
() -> <obj> This element
@*/
that.removeElement = function() {
that.loseFocus();
that.remove = that.removeElement = function() {
that.$element.children('.OxElement').each(function() {
Ox.UI.elements[$(this).data('oxid')].removeElement();
});
Ox.Focus.remove(that.id);
delete self.$eventHandler;
that.remove();
// fixme: ok to comment out the following line?
delete Ox.UI.elements[that.id];
that.$element.remove();
return that;
};
that.replaceWith = function($element) {
$element.insertAfter(that);
that.remove();
return that;
};

View file

@ -20,8 +20,9 @@ Ox.Focus = (function() {
blur: function(id) {
var index = stack.indexOf(id);
if (index > -1 && index == stack.length - 1) {
stack.length == 1 ? stack.pop() :
stack.splice(stack.length - 2, 0, stack.pop());
stack.length == 1
? stack.pop()
: stack.splice(stack.length - 2, 0, stack.pop());
//$elements[id].removeClass('OxFocus');
$('.OxFocus').removeClass('OxFocus'); // fixme: the above is better, and should work
stack.length && Ox.UI.elements[stack[stack.length - 1]].addClass('OxFocus');
@ -48,6 +49,10 @@ Ox.Focus = (function() {
@*/
focused: function() {
return stack.length ? stack[stack.length - 1] : null;
},
remove: function(id) {
var index = stack.indexOf(id);
index > -1 && stack.splice(index, 1);
}
};
}());

View file

@ -19,7 +19,7 @@ Ox.JQueryElement = function($element) {
that.ox = Ox.VERSION;
//@ $element <object> The jQuery-wrapped DOM element
that.$element = $element.data({
oxid: that.id
oxid: that.id // FIXME: Can this be just "id"?
});
// FIXME: the following two lines should make it possible to do
// $('<div>').appendTo($element) ... check if it works, then replace all

View file

@ -46,17 +46,17 @@ Ox.SplitPanel = function(options, self) {
// create elements
that.$elements = [];
self.options.elements.forEach(function(v, i) {
self.options.elements.forEach(function(element, i) {
self.options.elements[i] = Ox.extend({
collapsible: false,
collapsed: false,
resizable: false,
resize: [],
size: 'auto'
}, v);
that.$elements[i] = v.element
.css(self.edges[2], (parseInt(v.element.css(self.edges[2])) || 0) + 'px')
.css(self.edges[3], (parseInt(v.element.css(self.edges[3])) || 0) + 'px');
}, element);
that.$elements[i] = element.element
.css(self.edges[2], (parseInt(element.element.css(self.edges[2])) || 0) + 'px')
.css(self.edges[3], (parseInt(element.element.css(self.edges[3])) || 0) + 'px');
//alert(v.element.css(self.edges[3]))
});
@ -198,8 +198,13 @@ Ox.SplitPanel = function(options, self) {
.css(self.edges[3], (parseInt(element.css(self.edges[3])) || 0) + 'px');
// fixme: it would be better to call removeElement here,
// or have a custom replaceElement function that removes event handlers
self.options.elements[pos].element.replaceWith(element.$element.$element || element.$element);
self.options.elements[pos].element = element;
//self.options.elements[pos].element.replaceWith(element.$element.$element || element.$element)
//self.options.elements[pos].element = element;
///*
self.options.elements[pos].element.replaceWith(
self.options.elements[pos].element = element
);
//*/
setSizes();
self.$resizebars.forEach(function($resizebar, i) {
$resizebar.options({