public void DeepCopy_Success() { Article testArticle = new Article() { Id = "666-666" }; testArticle.XpmPropertyMetadata = new Dictionary<string, string> { { "xxx", "yyy" } }; RegionModel testRegionModel = new RegionModel("test"); testRegionModel.Entities.Add(testArticle); PageModel testPageModel = new PageModel("666") { Url = "/test" }; testPageModel.Regions.Add(testRegionModel); testPageModel.Meta.Add("aaa", "bbb"); testPageModel.XpmMetadata = new Dictionary<string, object> { {"ccc", "ddd"}, {"eee", 666} }; testPageModel.ExtensionData = new Dictionary<string, object> { { "fff", "ggg" }, { "hhh", 6666 } }; OutputJson(testPageModel); PageModel clonedPageModel = testPageModel.DeepCopy() as PageModel; Assert.IsNotNull(clonedPageModel, "clonedPageModel"); OutputJson(clonedPageModel); Assert.AreNotSame(testPageModel, clonedPageModel, "clonedPageModel"); Assert.AreEqual(testPageModel.Id, clonedPageModel.Id, "clonedPageModel.Id"); Assert.AreEqual(testPageModel.Url, clonedPageModel.Url, "clonedPageModel.Url"); AssertEqualCollections(testPageModel.Meta, clonedPageModel.Meta, "clonedPageModel.Meta"); AssertEqualCollections(testPageModel.XpmMetadata, clonedPageModel.XpmMetadata, "clonedPageModel.XpmMetadata"); AssertEqualCollections(testPageModel.ExtensionData, clonedPageModel.ExtensionData, "clonedPageModel.ExtensionData"); AssertEqualCollections(testPageModel.Regions, clonedPageModel.Regions, "clonedPageModel.Regions"); RegionModel clonedRegionModel = clonedPageModel.Regions[testRegionModel.Name]; Assert.IsNotNull(clonedRegionModel, "clonedRegionModel"); Assert.AreNotSame(testRegionModel, clonedRegionModel, "clonedRegionModel"); AssertEqualCollections(testRegionModel.Entities, clonedRegionModel.Entities, "clonedRegionModel.Entities"); AssertEqualCollections(testRegionModel.Regions, clonedRegionModel.Regions, "clonedRegionModel.Regions"); Article clonedArticle = clonedRegionModel.Entities[0] as Article; Assert.IsNotNull(clonedArticle, "clonedArticle"); Assert.AreNotSame(testArticle, clonedArticle, "clonedArticle"); Assert.AreEqual(testArticle.Id, clonedArticle.Id, "clonedArticle.Id"); AssertEqualCollections(testArticle.XpmPropertyMetadata, clonedArticle.XpmPropertyMetadata, "clonedArticle.XpmPropertyMetadata"); }
/// <summary> /// Generates semantic markup (HTML/RDFa attributes) for a given Region Model. /// </summary> /// <param name="htmlHelper">The HtmlHelper instance on which the extension method operates.</param> /// <param name="region">The Region Model to generate semantic markup for.</param> /// <returns>The HTML/RDFa attributes for the Region. These should be included in an HTML start tag.</returns> public static MvcHtmlString DxaRegionMarkup(this HtmlHelper htmlHelper, RegionModel region) { return Markup.RenderRegionAttributes(region); }
/// <summary> /// Renders the current (Include) Page as a Region. /// </summary> /// <param name="htmlHelper">The HtmlHelper instance on which the extension method operates.</param> /// <returns>The rendered HTML.</returns> public static MvcHtmlString DxaRegion(this HtmlHelper htmlHelper) { using (new Tracer(htmlHelper)) { PageModel pageModel = (PageModel)htmlHelper.ViewData.Model; // Create a new Region Model which reflects the Page Model string regionName = pageModel.Title; MvcData mvcData = new MvcData { ViewName = regionName, AreaName = SiteConfiguration.GetDefaultModuleName(), ControllerName = SiteConfiguration.GetRegionController(), ControllerAreaName = SiteConfiguration.GetDefaultModuleName(), ActionName = SiteConfiguration.GetRegionAction() }; RegionModel regionModel = new RegionModel(regionName) { MvcData = mvcData }; regionModel.Regions.UnionWith(pageModel.Regions); return htmlHelper.DxaRegion(regionModel); } }
/// <summary> /// Renders a Region (of the current Page or Region Model) with a given name. /// </summary> /// <param name="htmlHelper">The HtmlHelper instance on which the extension method operates.</param> /// <param name="regionName">The name of the Region to render. This object determines the View that will be used.</param> /// <param name="emptyViewName"> /// The name of the View to use when no Region with the given name is found in the Page Model (i.e. no Entities exist in the given Region). /// If <c>null</c> (the default) then nothing will be rendered in that case. /// If the View is not in the Core Area, the View name has to be in the format AreaName:ViewName. /// </param> /// <param name="containerSize">The size (in grid column units) of the containing element.</param> /// <returns>The rendered HTML or an empty string if no Region with a given name is found and <paramref name="emptyViewName"/> is <c>null</c>.</returns> /// <remarks>This method will throw an exception if the current Model does not represent a Page.</remarks> public static MvcHtmlString DxaRegion(this HtmlHelper htmlHelper, string regionName, string emptyViewName = null, int containerSize = 0) { using (new Tracer(htmlHelper, regionName, emptyViewName, containerSize)) { RegionModelSet regions = GetRegions(htmlHelper.ViewData.Model); RegionModel region; if (!regions.TryGetValue(regionName, out region)) { if (emptyViewName == null) { Log.Debug("Region '{0}' not found and no empty View specified. Skipping.", regionName); return MvcHtmlString.Empty; } Log.Debug("Region '{0}' not found. Using empty View '{1}'.", regionName, emptyViewName); region = new RegionModel(regionName, emptyViewName); } return htmlHelper.DxaRegion(region, containerSize); } }
/// <summary> /// Renders a given Region Model /// </summary> /// <param name="htmlHelper">The HtmlHelper instance on which the extension method operates.</param> /// <param name="region">The Region Model to render. This object determines the View that will be used.</param> /// <param name="containerSize">The size (in grid column units) of the containing element.</param> /// <returns>The rendered HTML or an empty string if <paramref name="region"/> is <c>null</c>.</returns> public static MvcHtmlString DxaRegion(this HtmlHelper htmlHelper, RegionModel region, int containerSize = 0) { if (region == null) { return MvcHtmlString.Empty; } if (containerSize == 0) { containerSize = SiteConfiguration.MediaHelper.GridSize; } using (new Tracer(htmlHelper, region, containerSize)) { MvcData mvcData = region.MvcData; string actionName = mvcData.ActionName ?? SiteConfiguration.GetRegionAction(); string controllerName = mvcData.ControllerName ?? SiteConfiguration.GetRegionController(); string controllerAreaName = mvcData.ControllerAreaName ?? SiteConfiguration.GetDefaultModuleName(); MvcHtmlString result = htmlHelper.Action(actionName, controllerName, new { Region = region, containerSize = containerSize, area = controllerAreaName }); if (WebRequestContext.IsPreview) { result = new MvcHtmlString(Markup.TransformXpmMarkupAttributes(result.ToString())); } return result; } }
public virtual ActionResult Region(RegionModel region, int containerSize = 0) { SetupViewData(region, containerSize); RegionModel model = (EnrichModel(region) as RegionModel) ?? region; return View(model.MvcData.ViewName, model); }
public void ExtractSyndicationFeedItems_Teasers_Success() { Localization testLocalization = TestFixture.ParentLocalization; Teaser testTeaser1 = new Teaser { Headline = "Test Teaser 1", Text = new RichText("This is the text of Test Teaser 1."), Link = new Link() { Url = "http://www.sdl.com/" } }; Teaser testTeaser2 = new Teaser { Headline = "Test Teaser 2", }; RegionModel testRegion1 = new RegionModel("test1"); testRegion1.Entities.Add(testTeaser1); RegionModel testRegion2 = new RegionModel("test2"); testRegion2.Entities.Add(testTeaser2); PageModel testPageModel = new PageModel("666"); testPageModel.Regions.Add(testRegion1); testPageModel.Regions.Add(testRegion2); OutputJson(testPageModel); SyndicationItem[] syndicationItems = testPageModel.ExtractSyndicationFeedItems(testLocalization).ToArray(); Assert.IsNotNull(syndicationItems); Assert.AreEqual(2, syndicationItems.Length, "syndicationItems.Length"); SyndicationItem firstSyndicationItem = syndicationItems[0]; Assert.IsNotNull(firstSyndicationItem, "firstSyndicationItem"); Assert.IsNotNull(firstSyndicationItem.Title, "firstSyndicationItem.Title"); Assert.IsNotNull(firstSyndicationItem.Summary, "firstSyndicationItem.Summary"); Assert.IsNotNull(firstSyndicationItem.Links, "firstSyndicationItem.Links"); Assert.AreEqual(testTeaser1.Headline, firstSyndicationItem.Title.Text, "firstSyndicationItem.Title.Text"); Assert.AreEqual(testTeaser1.Text.ToString(), firstSyndicationItem.Summary.Text, "firstSyndicationItem.Summary.Text"); Assert.AreEqual(1, firstSyndicationItem.Links.Count, "firstSyndicationItem.Links.Count"); Assert.AreEqual(testTeaser1.Link.Url, firstSyndicationItem.Links[0].Uri.ToString(), "firstSyndicationItem.Links[0].Uri"); SyndicationItem secondSyndicationItem = syndicationItems[1]; Assert.IsNotNull(secondSyndicationItem, "secondSyndicationItem"); Assert.IsNotNull(secondSyndicationItem.Title, "secondSyndicationItem.Title"); Assert.IsNull(secondSyndicationItem.Summary, "secondSyndicationItem.Summary"); Assert.IsNotNull(secondSyndicationItem.Links, "secondSyndicationItem.Links"); Assert.AreEqual(testTeaser2.Headline, secondSyndicationItem.Title.Text, "secondSyndicationItem.Title.Text"); Assert.AreEqual(0, secondSyndicationItem.Links.Count, "secondSyndicationItem.Links.Count"); }
/// <summary> /// Renders a given Region Model /// </summary> /// <param name="htmlHelper">The HtmlHelper instance on which the extension method operates.</param> /// <param name="region">The Region Model to render. This object determines the View that will be used.</param> /// <param name="containerSize">The size (in grid column units) of the containing element.</param> /// <returns>The rendered HTML or an empty string if <paramref name="region"/> is <c>null</c>.</returns> public static MvcHtmlString DxaRegion(this HtmlHelper htmlHelper, RegionModel region, int containerSize = 0) { if (region == null) { return MvcHtmlString.Empty; } if (containerSize == 0) { containerSize = SiteConfiguration.MediaHelper.GridSize; } using (new Tracer(htmlHelper, region, containerSize)) { MvcData mvcData = region.MvcData; string actionName = mvcData.ActionName ?? SiteConfiguration.GetRegionAction(); string controllerName = mvcData.ControllerName ?? SiteConfiguration.GetRegionController(); string controllerAreaName = mvcData.ControllerAreaName ?? SiteConfiguration.GetDefaultModuleName(); RouteValueDictionary parameters = new RouteValueDictionary(); parameters["containerSize"] = containerSize; parameters["region"] = region; parameters["area"] = controllerAreaName; if (mvcData.RouteValues != null) { foreach (string key in mvcData.RouteValues.Keys) { parameters[key] = mvcData.RouteValues[key]; } } MvcHtmlString result = htmlHelper.Action(actionName, controllerName, parameters); if (WebRequestContext.IsPreview) { result = new MvcHtmlString(Markup.TransformXpmMarkupAttributes(result.ToString())); } return Markup.DecorateMarkup(result, region); } }