// THIS FILE IS PART OF SVG PROJECT // THE SVG PROJECT IS AN OPENSOURCE LIBRARY LICENSED UNDER THE MS-PL License. // COPYRIGHT (C) svg-net. ALL RIGHTS RESERVED. // GITHUB: https://github.com/svg-net/SVG using System; using System.Collections.Generic; using System.Linq; namespace AntdUI.Svg { /// /// Represents a collection of s. /// public sealed class SvgElementCollection : IList { private List _elements; private SvgElement _owner; private bool _mock; /// /// Initialises a new instance of an class. /// /// The owner of the collection. internal SvgElementCollection(SvgElement owner) : this(owner, false) { } internal SvgElementCollection(SvgElement owner, bool mock) { if (owner == null) throw new ArgumentNullException("owner"); _elements = new List(); _owner = owner; _mock = mock; } /// /// Returns the index of the specified in the collection. /// /// The to search for. /// The index of the element if it is present; otherwise -1. public int IndexOf(SvgElement item) { return _elements.IndexOf(item); } /// /// Inserts the given to the collection at the specified index. /// /// The index that the should be added at. /// The to be added. public void Insert(int index, SvgElement item) { InsertAndForceUniqueID(index, item, true, true, LogIDChange); } private void LogIDChange(SvgElement elem, string oldId, string newID) { //System.Diagnostics.Debug.WriteLine("ID of SVG element " + elem.ToString() + " changed from " + oldId + " to " + newID); } public void InsertAndForceUniqueID(int index, SvgElement item, bool autoForceUniqueID = true, bool autoFixChildrenID = true, Action logElementOldIDNewID = null) { AddToIdManager(item, _elements[index], autoForceUniqueID, autoFixChildrenID, logElementOldIDNewID); _elements.Insert(index, item); item._parent.OnElementAdded(item, index); } public void RemoveAt(int index) { SvgElement element = this[index]; if (element != null) Remove(element); } public SvgElement this[int index] { get { return _elements[index]; } set { _elements[index] = value; } } public void Add(SvgElement item) { AddAndForceUniqueID(item, true, true, LogIDChange); } public void AddAndForceUniqueID(SvgElement item, bool autoForceUniqueID = true, bool autoFixChildrenID = true, Action logElementOldIDNewID = null) { AddToIdManager(item, null, autoForceUniqueID, autoFixChildrenID, logElementOldIDNewID); _elements.Add(item); item._parent.OnElementAdded(item, Count - 1); } private void AddToIdManager(SvgElement item, SvgElement sibling, bool autoForceUniqueID = true, bool autoFixChildrenID = true, Action logElementOldIDNewID = null) { if (!_mock) { if (_owner.OwnerDocument != null) { _owner.OwnerDocument.IdManager.AddAndForceUniqueID(item, sibling, autoForceUniqueID, logElementOldIDNewID); } //if all checked, set parent item._parent = _owner; } } public void Clear() { while (Count > 0) { SvgElement element = this[0]; Remove(element); } } public bool Contains(SvgElement item) { return _elements.Contains(item); } public void CopyTo(SvgElement[] array, int arrayIndex) { _elements.CopyTo(array, arrayIndex); } public int Count { get { return _elements.Count; } } public bool IsReadOnly { get { return false; } } public bool Remove(SvgElement item) { bool removed = _elements.Remove(item); if (removed) { _owner.OnElementRemoved(item); if (!_mock) item._parent = null; } return removed; } /// /// expensive recursive search for nodes of type T /// /// /// public IEnumerable FindSvgElementsOf() where T : SvgElement { return _elements.Where(x => x is T).Select(x => x as T).Concat(_elements.SelectMany(x => x.Children.FindSvgElementsOf())); } /// /// expensive recursive search for first node of type T /// /// /// public T FindSvgElementOf() where T : SvgElement { return _elements.OfType().FirstOrDefault() ?? _elements.Select(x => x.Children.FindSvgElementOf()).FirstOrDefault(x => x != null); } public T GetSvgElementOf() where T : SvgElement { return _elements.FirstOrDefault(x => x is T) as T; } public IEnumerator GetEnumerator() { return _elements.GetEnumerator(); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return _elements.GetEnumerator(); } } }