Ooui-tws-port/Ooui/Node.cs

71 lines
2.1 KiB
C#
Raw Normal View History

2017-06-12 20:45:27 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
namespace Ooui
{
2017-06-15 01:24:59 +00:00
public abstract class Node : EventTarget
2017-06-12 20:45:27 +00:00
{
readonly List<Node> children = new List<Node> ();
2017-06-15 01:24:59 +00:00
public IEnumerable<Message> AllStateMessages =>
StateMessages
.Concat (from c in children from m in c.AllStateMessages select m)
2017-06-13 07:03:01 +00:00
.OrderBy (x => x.Id);
2017-06-13 03:31:47 +00:00
2017-06-14 04:17:50 +00:00
public virtual string Text {
get { return String.Join ("", from c in children select c.Text); }
2017-06-13 07:51:24 +00:00
set {
2017-06-14 04:17:50 +00:00
ReplaceAll (new TextNode (value ?? ""));
2017-06-13 07:51:24 +00:00
}
}
2017-06-15 06:20:51 +00:00
protected Node (string tagName)
: base (tagName)
{
2017-06-12 20:45:27 +00:00
}
public Node AppendChild (Node newChild)
{
return InsertBefore (newChild, null);
}
public Node ParentNode { get; private set; }
public Node InsertBefore (Node newChild, Node referenceChild)
{
if (referenceChild == null) {
children.Add (newChild);
}
else {
var index = children.IndexOf (referenceChild);
if (index < 0) {
throw new ArgumentException ("Reference must be a child of this element", nameof(referenceChild));
}
children.Insert (index, newChild);
}
newChild.ParentNode = this;
2017-06-15 01:24:59 +00:00
SendCall ("insertBefore", newChild, referenceChild);
2017-06-12 20:45:27 +00:00
return newChild;
}
public Node RemoveChild (Node child)
{
if (!children.Remove (child)) {
throw new ArgumentException ("Child not contained in this element", nameof(child));
}
child.ParentNode = null;
2017-06-15 01:24:59 +00:00
SendCall ("removeChild", child);
2017-06-12 20:45:27 +00:00
return child;
}
2017-06-13 07:51:24 +00:00
protected void ReplaceAll (Node newNode)
{
var toRemove = new List<Node> (children);
foreach (var c in toRemove)
RemoveChild (c);
InsertBefore (newNode, null);
}
2017-06-12 20:45:27 +00:00
}
}