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

View file

@ -20,8 +20,9 @@ Ox.Focus = (function() {
blur: function(id) { blur: function(id) {
var index = stack.indexOf(id); var index = stack.indexOf(id);
if (index > -1 && index == stack.length - 1) { if (index > -1 && index == stack.length - 1) {
stack.length == 1 ? stack.pop() : stack.length == 1
stack.splice(stack.length - 2, 0, stack.pop()); ? stack.pop()
: stack.splice(stack.length - 2, 0, stack.pop());
//$elements[id].removeClass('OxFocus'); //$elements[id].removeClass('OxFocus');
$('.OxFocus').removeClass('OxFocus'); // fixme: the above is better, and should work $('.OxFocus').removeClass('OxFocus'); // fixme: the above is better, and should work
stack.length && Ox.UI.elements[stack[stack.length - 1]].addClass('OxFocus'); stack.length && Ox.UI.elements[stack[stack.length - 1]].addClass('OxFocus');
@ -48,6 +49,10 @@ Ox.Focus = (function() {
@*/ @*/
focused: function() { focused: function() {
return stack.length ? stack[stack.length - 1] : null; 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; that.ox = Ox.VERSION;
//@ $element <object> The jQuery-wrapped DOM element //@ $element <object> The jQuery-wrapped DOM element
that.$element = $element.data({ 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 // FIXME: the following two lines should make it possible to do
// $('<div>').appendTo($element) ... check if it works, then replace all // $('<div>').appendTo($element) ... check if it works, then replace all

View file

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