Go to the documentation of this file.00001 using System;
00002 using N2.Engine;
00003 using N2.Web.UI;
00004 using N2.Web.UI.WebControls;
00005 using System.Web.Mvc;
00006 using System.Text;
00007 using System.IO;
00008 using N2.Definitions;
00009 using N2.Persistence;
00010
00011 namespace N2.Web.Mvc.Html
00012 {
00013 internal static class TextWriterExtensions
00014 {
00015 public static TextWriter WriteAttribute(this TextWriter writer, string attributeName, string value)
00016 {
00017 writer.Write(" {0}=\"{1}\"", attributeName, value);
00018 return writer;
00019 }
00020 }
00021
00022 public class DroppableZoneHelper : ZoneHelper
00023 {
00024 protected string ZoneTitle { get; set; }
00025
00026 ControlPanelState state = ControlPanelState.Hidden;
00027
00028 public DroppableZoneHelper(HtmlHelper helper, string zoneName, ContentItem currentItem)
00029 : base(helper, zoneName, currentItem)
00030 {
00031 state = helper.ViewContext.HttpContext.ControlPanelState();
00032 }
00033
00034 public ZoneHelper Title(string title)
00035 {
00036 ZoneTitle = title;
00037
00038 return this;
00039 }
00040
00041 public override void Render(TextWriter writer)
00042 {
00043 if (state == ControlPanelState.DragDrop)
00044 {
00045 if (ZoneName.IndexOfAny(new[] { '.', ',', ' ', '\'', '"', '\t', '\r', '\n' }) >= 0) throw new N2Exception("Zone '" + ZoneName + "' contains illegal characters.");
00046
00047 writer.Write("<div class='" + ZoneName + " dropZone'");
00048 writer.WriteAttribute(PartUtilities.PathAttribute, CurrentItem.Path)
00049 .WriteAttribute(PartUtilities.ZoneAttribute, ZoneName)
00050 .WriteAttribute(PartUtilities.AllowedAttribute, PartUtilities.GetAllowedNames(ZoneName, PartsAdapter.GetAllowedDefinitions(CurrentItem, ZoneName, Html.ViewContext.HttpContext.User)))
00051 .WriteAttribute("title", ZoneTitle ?? DroppableZone.GetToolTip(Html.ResolveService<IDefinitionManager>().GetDefinition(CurrentItem.GetContentType()), ZoneName))
00052 .Write(">");
00053
00054 if (string.IsNullOrEmpty(Html.ViewContext.HttpContext.Request["preview"]))
00055 {
00056 base.Render(writer);
00057 }
00058 else
00059 {
00060 string preview = Html.ViewContext.HttpContext.Request["preview"];
00061 RenderReplacingPreviewed(writer, preview);
00062 }
00063
00064 writer.Write("</div>");
00065 }
00066 else
00067 base.Render(writer);
00068 }
00069
00070 protected void RenderReplacingPreviewed(TextWriter writer, string preview)
00071 {
00072 int itemID;
00073 if (int.TryParse(preview, out itemID))
00074 {
00075 ContentItem previewedItem = Html.ResolveService<IPersister>().Get(itemID);
00076 if (previewedItem != null && previewedItem.VersionOf != null)
00077 {
00078 foreach (var child in PartsAdapter.GetItemsInZone(CurrentItem, ZoneName))
00079 {
00080 if (previewedItem.VersionOf == child)
00081 RenderTemplate(writer, previewedItem);
00082 else
00083 RenderTemplate(writer, child);
00084 }
00085 }
00086 }
00087 }
00088
00089 protected override void RenderTemplate(TextWriter writer, ContentItem model)
00090 {
00091 if (state == ControlPanelState.DragDrop)
00092 {
00093 ItemDefinition definition = Html.ResolveService<IDefinitionManager>().GetDefinition(model.GetContentType());
00094
00095 writer.Write("<div class='" + definition.Discriminator + " zoneItem'");
00096 writer.WriteAttribute(PartUtilities.PathAttribute, model.Path)
00097 .WriteAttribute(PartUtilities.TypeAttribute, definition.Discriminator)
00098 .Write(">");
00099
00100 Html.ResolveService<PartUtilities>().WriteTitleBar(writer, model, Html.ViewContext.HttpContext.Request.RawUrl);
00101
00102 base.RenderTemplate(writer, model);
00103
00104 writer.Write("</div>");
00105 }
00106 else
00107 base.RenderTemplate(writer, model);
00108 }
00109
00110 public DroppableZoneHelper AllowExternalManipulation()
00111 {
00112
00113 return this;
00114 }
00115 }
00116 }