From f03f51267b1e0bfba0f451760d950aa5feae0c92 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 18 Jul 2013 15:06:33 -0500 Subject: [PATCH] only emit attach if the element was not previously attached. --- lib/widget.js | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/lib/widget.js b/lib/widget.js index cadb6f4..d6658e2 100644 --- a/lib/widget.js +++ b/lib/widget.js @@ -57,7 +57,7 @@ Node.prototype.__proto__ = EventEmitter.prototype; Node.prototype.type = 'node'; Node.prototype.insert = function(element, i) { - // var old = element.parent; + var old = element.parent; element.detach(); element.parent = this; @@ -79,16 +79,16 @@ Node.prototype.insert = function(element, i) { element.emit('reparent', this); this.emit('adopt', element); - // if (!old) { - (function emit(el) { - el._detached = false; - el.emit('attach'); - if (el.children) el.children.forEach(emit); - })(element); - - // element.emitDescendants('attach', function(el) { - // el._detached = false; - // }); + if (!old) { + // element.emitDescendants('attach', function(el) { + // el._detached = false; + // }); + (function emit(el) { + el._detached = false; + el.emit('attach'); + if (el.children) el.children.forEach(emit); + })(element); + } }; Node.prototype.prepend = function(element) { @@ -110,6 +110,8 @@ Node.prototype.insertAfter = function(element, other) { }; Node.prototype.remove = function(element) { + if (element.parent !== this) return; + element.parent = null; var i = this.children.indexOf(element); @@ -131,22 +133,20 @@ Node.prototype.remove = function(element) { element.emit('reparent', null); this.emit('remove', element); + // element.emitDescendants('detach', function(el) { + // el._detached = true; + // }); (function emit(el) { el._detached = true; el.emit('detach'); if (el.children) el.children.forEach(emit); })(element); - // element.emitDescendants('detach', function(el) { - // el._detached = true; - // }); - // this.clearPos(); }; Node.prototype.detach = function() { - if (this.parent) this.parent.remove(this); - // this.clearPos(); + return this.parent.remove(this); }; Node.prototype.emitDescendants = function() { @@ -406,7 +406,6 @@ Screen.prototype._listenMouse = function(el) { // if (self.grabKeys && self.focused !== el // && !el.hasAncestor(self.focused)) continue; - // Get the true coordinates. ret = el.lpos; if (!ret) continue; left = ret.xi;