Go to the documentation of this file.00001 using System;
00002 using System.IO;
00003 using System.Web.Mvc;
00004 using System.Web.UI;
00005 using N2.Web.UI;
00006 using N2.Web.UI.WebControls;
00007 using N2.Details;
00008 using System.Web.Routing;
00009 using System.Diagnostics;
00010
00011 namespace N2.Web.Mvc.Html
00012 {
00013 public class Displayable : ItemHelper
00014 {
00015 readonly string propertyName;
00016 string path;
00017 bool swallowExceptions;
00018
00019 public Displayable(HtmlHelper helper, string propertyName, ContentItem currentItem)
00020 : base(helper, currentItem)
00021 {
00022 if (propertyName == null) throw new ArgumentNullException("propertyName");
00023
00024 this.propertyName = propertyName;
00025 }
00026
00027 protected System.Web.Mvc.TagBuilder Wrapper { get; set; }
00028 public string Value
00029 {
00030 get
00031 {
00032 try
00033 {
00034 return Convert.ToString(CurrentItem[propertyName]);
00035 }
00036 catch
00037 {
00038 if (swallowExceptions)
00039 return String.Empty;
00040
00041 throw;
00042 }
00043 }
00044 }
00045
00046 public Displayable WrapIn(string tagName, object attributes)
00047 {
00048 Wrapper = new System.Web.Mvc.TagBuilder(tagName);
00049 Wrapper.MergeAttributes(new RouteValueDictionary(attributes));
00050
00051 return this;
00052 }
00053
00054 public Displayable InPath(string path)
00055 {
00056 this.path = path;
00057
00058 return this;
00059 }
00060
00061 public Displayable SwallowExceptions()
00062 {
00063 swallowExceptions = true;
00064
00065 return this;
00066 }
00067
00068 public override string ToString()
00069 {
00070 var previousWriter = Html.ViewContext.Writer;
00071 try
00072 {
00073 using (var writer = new StringWriter())
00074 {
00075 Html.ViewContext.Writer = writer;
00076
00077 Render(Html);
00078
00079 return writer.ToString();
00080 }
00081 }
00082 finally
00083 {
00084 Html.ViewContext.Writer = previousWriter;
00085 }
00086 }
00087
00088 internal void Render(HtmlHelper helper)
00089 {
00090 if (!string.IsNullOrEmpty(path))
00091 CurrentItem = ItemUtility.WalkPath(CurrentItem, path);
00092
00093 if (CurrentItem == null)
00094 return;
00095
00096 if (swallowExceptions)
00097 {
00098 try
00099 {
00100 RenderDisplayable(helper);
00101 }
00102 catch (Exception ex)
00103 {
00104 Debug.WriteLine(ex);
00105 }
00106 }
00107 else
00108 RenderDisplayable(helper);
00109 }
00110
00111 private void RenderDisplayable(HtmlHelper helper)
00112 {
00113 var displayable = Display.GetDisplayableAttribute(propertyName, CurrentItem, swallowExceptions);
00114 if (displayable == null) return;
00115
00116 if (Wrapper != null)
00117 helper.ViewContext.Writer.Write(Wrapper.ToString(TagRenderMode.StartTag));
00118
00119 var referencedItem = CurrentItem[propertyName] as ContentItem;
00120 if (referencedItem != null)
00121 {
00122 var adapter = Adapters.ResolveAdapter<MvcAdapter>(referencedItem);
00123 adapter.RenderTemplate(helper, referencedItem);
00124 }
00125 else
00126 {
00127 var container = CreateContainer(displayable);
00128 container.RenderControl(new HtmlTextWriter(helper.ViewContext.Writer));
00129 }
00130 if (Wrapper != null)
00131 helper.ViewContext.Writer.Write(Wrapper.ToString(TagRenderMode.EndTag));
00132 }
00133
00134 private ViewUserControl CreateContainer(IDisplayable displayable)
00135 {
00136 var viewData = new ViewDataDictionary(CurrentItem);
00137
00138 var container = new ViewUserControl
00139 {
00140 Page = (Html.ViewContext.View is Control) ? ((Control)Html.ViewContext.View).Page : null,
00141 ViewData = viewData,
00142 };
00143 displayable.AddTo(CurrentItem, propertyName, container);
00144
00145 return container;
00146 }
00147 }
00148 }