Skip to content

Instantly share code, notes, and snippets.

@mikecann
Created December 20, 2011 16:17
Show Gist options
  • Save mikecann/1502132 to your computer and use it in GitHub Desktop.
Save mikecann/1502132 to your computer and use it in GitHub Desktop.
package base;
import js.Dom;
import js.JQuery;
import robothaxe.core.IViewContainer;
/**
* ...
* @author
*/
class BaseView implements IViewContainer
{
public var viewAdded:Dynamic -> Void;
public var viewRemoved:Dynamic -> Void;
public var element : HtmlDom;
public var parent : BaseView;
public var children : Array<BaseView>;
public function new(element:HtmlDom)
{
this.element = element;
this.children = [];
}
public function add(child:BaseView) : Void
{
children.push(child);
child.parent = this;
child.viewAdded = viewAdded;
child.viewRemoved = viewRemoved;
if(viewAdded!=null) child.addChildren();
element.appendChild(child.element);
if(viewAdded!=null) viewAdded(child);
}
public function remove(child:BaseView) : Void
{
if (viewRemoved != null) child.removeChildren();
children.remove(child);
child.parent = null;
child.viewAdded = null;
child.viewRemoved = null;
element.removeChild(child.element);
if (viewRemoved != null) viewRemoved(child);
}
public function addChildren() : Void
{
for (c in children)
{
c.viewAdded = viewAdded;
c.viewRemoved = viewRemoved;
c.addChildren();
viewAdded(c);
}
}
public function removeChildren() : Void
{
for (c in children)
{
c.removeChildren();
element.removeChild(c.element);
c.parent = null;
c.viewAdded = null;
c.viewRemoved = null;
if (viewRemoved != null) viewRemoved(c);
}
}
public function isAdded(view:Dynamic):Bool
{
return Lambda.has(children,view);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment