From 0ea4e2f8913629fce4afbb6309f7b074319e96e2 Mon Sep 17 00:00:00 2001 From: "agrath@gmail.com" Date: Sun, 12 Jun 2011 11:50:35 -0200 Subject: [PATCH] Added IsHelpers to DynamicXml Added As() [ return (item as T); ] to @Library Added ToDynamicXml(string) to @Library --- .../RazorDynamicNode/DynamicXml.cs | 393 ++++++++++++++++++ .../RazorDynamicNode/RazorLibraryCore.cs | 12 + 2 files changed, 405 insertions(+) diff --git a/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicXml.cs b/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicXml.cs index 587c4c724f..d4305ac65d 100644 --- a/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicXml.cs +++ b/umbraco.MacroEngines.Juno/RazorDynamicNode/DynamicXml.cs @@ -129,5 +129,398 @@ namespace umbraco.MacroEngines { return true; } + + public bool IsFirst() + { + return IsHelper(n => n.Index() == 0); + } + public string IsFirst(string valueIfTrue) + { + return IsHelper(n => n.Index() == 0, valueIfTrue); + } + public string IsFirst(string valueIfTrue, string valueIfFalse) + { + return IsHelper(n => n.Index() == 0, valueIfTrue, valueIfFalse); + } + public bool IsNotFirst() + { + return !IsHelper(n => n.Index() == 0); + } + public string IsNotFirst(string valueIfTrue) + { + return IsHelper(n => n.Index() != 0, valueIfTrue); + } + public string IsNotFirst(string valueIfTrue, string valueIfFalse) + { + return IsHelper(n => n.Index() != 0, valueIfTrue, valueIfFalse); + } + public bool IsPosition(int index) + { + if (this.BaseElement == null || this.BaseElement.Parent == null) + { + return false; + } + return IsHelper(n => n.Index() == index); + } + public string IsPosition(int index, string valueIfTrue) + { + if (this.BaseElement == null || this.BaseElement.Parent == null) + { + return string.Empty; + } + return IsHelper(n => n.Index() == index, valueIfTrue); + } + public string IsPosition(int index, string valueIfTrue, string valueIfFalse) + { + if (this.BaseElement == null || this.BaseElement.Parent == null) + { + return valueIfFalse; + } + return IsHelper(n => n.Index() == index, valueIfTrue, valueIfFalse); + } + public bool IsModZero(int modulus) + { + if (this.BaseElement == null || this.BaseElement.Parent == null) + { + return false; + } + return IsHelper(n => n.Index() % modulus == 0); + } + public string IsModZero(int modulus, string valueIfTrue) + { + if (this.BaseElement == null || this.BaseElement.Parent == null) + { + return string.Empty; + } + return IsHelper(n => n.Index() % modulus == 0, valueIfTrue); + } + public string IsModZero(int modulus, string valueIfTrue, string valueIfFalse) + { + if (this.BaseElement == null || this.BaseElement.Parent == null) + { + return valueIfFalse; + } + return IsHelper(n => n.Index() % modulus == 0, valueIfTrue, valueIfFalse); + } + + public bool IsNotModZero(int modulus) + { + if (this.BaseElement == null || this.BaseElement.Parent == null) + { + return false; + } + return IsHelper(n => n.Index() % modulus != 0); + } + public string IsNotModZero(int modulus, string valueIfTrue) + { + if (this.BaseElement == null || this.BaseElement.Parent == null) + { + return string.Empty; + } + return IsHelper(n => n.Index() % modulus != 0, valueIfTrue); + } + public string IsNotModZero(int modulus, string valueIfTrue, string valueIfFalse) + { + if (this.BaseElement == null || this.BaseElement.Parent == null) + { + return valueIfFalse; + } + return IsHelper(n => n.Index() % modulus != 0, valueIfTrue, valueIfFalse); + } + public bool IsNotPosition(int index) + { + if (this.BaseElement == null || this.BaseElement.Parent == null) + { + return false; + } + return !IsHelper(n => n.Index() == index); + } + public string IsNotPosition(int index, string valueIfTrue) + { + if (this.BaseElement == null || this.BaseElement.Parent == null) + { + return string.Empty; + } + return IsHelper(n => n.Index() != index, valueIfTrue); + } + public string IsNotPosition(int index, string valueIfTrue, string valueIfFalse) + { + if (this.BaseElement == null || this.BaseElement.Parent == null) + { + return valueIfFalse; + } + return IsHelper(n => n.Index() != index, valueIfTrue, valueIfFalse); + } + public bool IsLast() + { + if (this.BaseElement == null || this.BaseElement.Parent == null) + { + return false; + } + int count = this.BaseElement.Parent.Elements().Count(); + return IsHelper(n => n.Index() == count - 1); + } + public string IsLast(string valueIfTrue) + { + if (this.BaseElement == null || this.BaseElement.Parent == null) + { + return string.Empty; + } + int count = this.BaseElement.Parent.Elements().Count(); + return IsHelper(n => n.Index() == count - 1, valueIfTrue); + } + public string IsLast(string valueIfTrue, string valueIfFalse) + { + if (this.BaseElement == null || this.BaseElement.Parent == null) + { + return valueIfFalse; + } + int count = this.BaseElement.Parent.Elements().Count(); + return IsHelper(n => n.Index() == count - 1, valueIfTrue, valueIfFalse); + } + public bool IsNotLast() + { + if (this.BaseElement == null || this.BaseElement.Parent == null) + { + return false; + } + int count = this.BaseElement.Parent.Elements().Count(); + return !IsHelper(n => n.Index() == count - 1); + } + public string IsNotLast(string valueIfTrue) + { + if (this.BaseElement == null || this.BaseElement.Parent == null) + { + return string.Empty; + } + int count = this.BaseElement.Parent.Elements().Count(); + return IsHelper(n => n.Index() != count - 1, valueIfTrue); + } + public string IsNotLast(string valueIfTrue, string valueIfFalse) + { + if (this.BaseElement == null || this.BaseElement.Parent == null) + { + return valueIfFalse; + } + int count = this.BaseElement.Parent.Elements().Count(); + return IsHelper(n => n.Index() != count - 1, valueIfTrue, valueIfFalse); + } + public bool IsEven() + { + return IsHelper(n => n.Index() % 2 == 0); + } + public string IsEven(string valueIfTrue) + { + return IsHelper(n => n.Index() % 2 == 0, valueIfTrue); + } + public string IsEven(string valueIfTrue, string valueIfFalse) + { + return IsHelper(n => n.Index() % 2 == 0, valueIfTrue, valueIfFalse); + } + public bool IsOdd() + { + return IsHelper(n => n.Index() % 2 == 1); + } + public string IsOdd(string valueIfTrue) + { + return IsHelper(n => n.Index() % 2 == 1, valueIfTrue); + } + public string IsOdd(string valueIfTrue, string valueIfFalse) + { + return IsHelper(n => n.Index() % 2 == 1, valueIfTrue, valueIfFalse); + } + public bool IsEqual(DynamicXml other) + { + return IsHelper(n => n.BaseElement == other.BaseElement); + } + public string IsEqual(DynamicXml other, string valueIfTrue) + { + return IsHelper(n => n.BaseElement == other.BaseElement, valueIfTrue); + } + public string IsEqual(DynamicXml other, string valueIfTrue, string valueIfFalse) + { + return IsHelper(n => n.BaseElement == other.BaseElement, valueIfTrue, valueIfFalse); + } + public bool IsDescendant(DynamicXml other) + { + var ancestors = this.Ancestors(); + return IsHelper(n => ancestors.Find(ancestor => ancestor.BaseElement == other.BaseElement) != null); + } + public string IsDescendant(DynamicXml other, string valueIfTrue) + { + var ancestors = this.Ancestors(); + return IsHelper(n => ancestors.Find(ancestor => ancestor.BaseElement == other.BaseElement) != null, valueIfTrue); + } + public string IsDescendant(DynamicXml other, string valueIfTrue, string valueIfFalse) + { + var ancestors = this.Ancestors(); + return IsHelper(n => ancestors.Find(ancestor => ancestor.BaseElement == other.BaseElement) != null, valueIfTrue, valueIfFalse); + } + public bool IsDescendantOrSelf(DynamicXml other) + { + var ancestors = this.AncestorsOrSelf(); + return IsHelper(n => ancestors.Find(ancestor => ancestor.BaseElement == other.BaseElement) != null); + } + public string IsDescendantOrSelf(DynamicXml other, string valueIfTrue) + { + var ancestors = this.AncestorsOrSelf(); + return IsHelper(n => ancestors.Find(ancestor => ancestor.BaseElement == other.BaseElement) != null, valueIfTrue); + } + public string IsDescendantOrSelf(DynamicXml other, string valueIfTrue, string valueIfFalse) + { + var ancestors = this.AncestorsOrSelf(); + return IsHelper(n => ancestors.Find(ancestor => ancestor.BaseElement == other.BaseElement) != null, valueIfTrue, valueIfFalse); + } + public bool IsAncestor(DynamicXml other) + { + var descendants = this.Descendants(); + return IsHelper(n => descendants.Find(descendant => descendant.BaseElement == other.BaseElement) != null); + } + public string IsAncestor(DynamicXml other, string valueIfTrue) + { + var descendants = this.Descendants(); + return IsHelper(n => descendants.Find(descendant => descendant.BaseElement == other.BaseElement) != null, valueIfTrue); + } + public string IsAncestor(DynamicXml other, string valueIfTrue, string valueIfFalse) + { + var descendants = this.Descendants(); + return IsHelper(n => descendants.Find(descendant => descendant.BaseElement == other.BaseElement) != null, valueIfTrue, valueIfFalse); + } + public bool IsAncestorOrSelf(DynamicXml other) + { + var descendants = this.DescendantsOrSelf(); + return IsHelper(n => descendants.Find(descendant => descendant.BaseElement == other.BaseElement) != null); + } + public string IsAncestorOrSelf(DynamicXml other, string valueIfTrue) + { + var descendants = this.DescendantsOrSelf(); + return IsHelper(n => descendants.Find(descendant => descendant.BaseElement == other.BaseElement) != null, valueIfTrue); + } + public string IsAncestorOrSelf(DynamicXml other, string valueIfTrue, string valueIfFalse) + { + var descendants = this.DescendantsOrSelf(); + return IsHelper(n => descendants.Find(descendant => descendant.BaseElement == other.BaseElement) != null, valueIfTrue, valueIfFalse); + } + public List Descendants() + { + return Descendants(n => true); + } + public List Descendants(Func func) + { + var flattenedNodes = this.BaseElement.Elements().Map(func, (XElement n) => { return n.Elements(); }); + return flattenedNodes.ToList().ConvertAll(n => new DynamicXml(n)); + } + public List DescendantsOrSelf() + { + return DescendantsOrSelf(n => true); + } + public List DescendantsOrSelf(Func func) + { + var flattenedNodes = this.BaseElement.Elements().Map(func, (XElement n) => { return n.Elements(); }); + var list = new List(); + list.Add(this); + list.AddRange(flattenedNodes.ToList().ConvertAll(n => new DynamicXml(n))); + return list; + } + public List Ancestors() + { + return Ancestors(item => true); + } + public List Ancestors(Func func) + { + List ancestorList = new List(); + var node = this.BaseElement; + while (node != null) + { + if (node.Parent == null) break; + XElement parent = node.Parent; + if (parent != null) + { + if (this.BaseElement != parent) + { + node = parent; + if (func(node)) + { + ancestorList.Add(node); + } + } + else + { + break; + } + } + else + { + break; + } + } + ancestorList.Reverse(); + return ancestorList.ConvertAll(item => new DynamicXml(item)); + } + public List AncestorsOrSelf() + { + return AncestorsOrSelf(item => true); + } + public List AncestorsOrSelf(Func func) + { + List ancestorList = new List(); + var node = this.BaseElement; + ancestorList.Add(node); + while (node != null) + { + if (node.Parent == null) break; + XElement parent = node.Parent; + if (parent != null) + { + if (this.BaseElement != parent) + { + node = parent; + if (func(node)) + { + ancestorList.Add(node); + } + } + else + { + break; + } + } + else + { + break; + } + } + ancestorList.Reverse(); + return ancestorList.ConvertAll(item => new DynamicXml(item)); + } + + public int Index() + { + if (this.BaseElement != null && this.BaseElement.Parent != null) + { + var elements = this.BaseElement.Parent.Elements(); + int index = 0; + foreach (var element in elements) + { + if (element == this.BaseElement) break; + index++; + } + return index; + } + return 0; + } + + public bool IsHelper(Func test) + { + return test(this); + } + public string IsHelper(Func test, string valueIfTrue) + { + return IsHelper(test, valueIfTrue, string.Empty); + } + public string IsHelper(Func test, string valueIfTrue, string valueIfFalse) + { + return test(this) ? valueIfTrue : valueIfFalse; + } } } diff --git a/umbraco.MacroEngines.Juno/RazorDynamicNode/RazorLibraryCore.cs b/umbraco.MacroEngines.Juno/RazorDynamicNode/RazorLibraryCore.cs index 11dbdbde96..a57e5109c1 100644 --- a/umbraco.MacroEngines.Juno/RazorDynamicNode/RazorLibraryCore.cs +++ b/umbraco.MacroEngines.Juno/RazorDynamicNode/RazorLibraryCore.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using umbraco.interfaces; +using System.Xml.Linq; namespace umbraco.MacroEngines.Library { @@ -89,5 +90,16 @@ namespace umbraco.MacroEngines.Library return MediaById(Ids.ToList()); } + public T As() where T : class + { + return (this as T); + } + + public DynamicXml ToDynamicXml(string xml) + { + if (string.IsNullOrWhiteSpace(xml)) return null; + var xElement = XElement.Parse(xml); + return new umbraco.MacroEngines.DynamicXml(xElement); + } } }