module.exports = class Node extends require './base'
module.exports = class Node extends require './base'
constructor: -> super
appendChild: (child) ->
If the child is not this instance
if child isnt this
initialize the #children if necessary.
@children ||= []
If child isnt already int the children list,
if -1 is @children.indexOf child
remove the child from it’s parent if it has one
child.parent?.removeChild? child
and update the child’s parent.
child.parent = this
Finally, append the child to the #children list.
@children.push child
return this
Removes the child from the #children.
removeChild: (child) ->
If there are any children and the child belongs to this parent
if @children?.length and -1 isnt idx = @children.indexOf child
make the child forget about it’s parent
delete child.parent
and remove the child from #children.
@children.splice idx, 1
Delete the #children if it is empty.
delete @children if @children.length is 0
return this