Skip to content
This repository was archived by the owner on Feb 10, 2024. It is now read-only.

Rework preview mechanism based on StackedContent implementation #105

Merged
merged 4 commits into from
Jul 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/Our.Umbraco.DocTypeGridEditor/Bootstrap.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Web.Mvc;
using Newtonsoft.Json;
using Our.Umbraco.DocTypeGridEditor.Web.Attributes;
using Our.Umbraco.DocTypeGridEditor.Web.Mvc;
using Umbraco.Core;
using Umbraco.Core.Sync;
Expand All @@ -14,8 +12,6 @@ internal class Bootstrap : ApplicationEventHandler
{
protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
GlobalFilters.Filters.Add(new DocTypeGridEditorPreviewAttribute());

if (DefaultDocTypeGridEditorSurfaceControllerResolver.HasCurrent == false)
{
DefaultDocTypeGridEditorSurfaceControllerResolver.Current = new DefaultDocTypeGridEditorSurfaceControllerResolver();
Expand Down
125 changes: 125 additions & 0 deletions src/Our.Umbraco.DocTypeGridEditor/Models/UnpublishedContent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Services;
using Umbraco.Web.Models;

namespace Our.Umbraco.DocTypeGridEditor.Models
{
internal class UnpublishedContent : PublishedContentWithKeyBase
{
private readonly IContent content;

private readonly Lazy<IEnumerable<IPublishedContent>> children;
private readonly Lazy<PublishedContentType> contentType;
private readonly Lazy<string> creatorName;
private readonly Lazy<IPublishedContent> parent;
private readonly Lazy<Dictionary<string, IPublishedProperty>> properties;
private readonly Lazy<string> urlName;
private readonly Lazy<string> writerName;

public UnpublishedContent(int id, ServiceContext serviceContext)
: this(serviceContext.ContentService.GetById(id), serviceContext)
{ }

public UnpublishedContent(IContent content, ServiceContext serviceContext)
: base()
{
Mandate.ParameterNotNull(content, nameof(content));
Mandate.ParameterNotNull(serviceContext, nameof(serviceContext));

var userService = new Lazy<IUserService>(() => serviceContext.UserService);

this.content = content;

this.children = new Lazy<IEnumerable<IPublishedContent>>(() => this.content.Children().Select(x => new UnpublishedContent(x, serviceContext)).ToList());
this.contentType = new Lazy<PublishedContentType>(() => PublishedContentType.Get(this.ItemType, this.DocumentTypeAlias));
this.creatorName = new Lazy<string>(() => this.content.GetCreatorProfile(userService.Value).Name);
this.parent = new Lazy<IPublishedContent>(() => new UnpublishedContent(this.content.Parent(), serviceContext));
this.properties = new Lazy<Dictionary<string, IPublishedProperty>>(() => MapProperties(PropertyEditorResolver.Current, serviceContext));
this.urlName = new Lazy<string>(() => this.content.Name.ToUrlSegment());
this.writerName = new Lazy<string>(() => this.content.GetWriterProfile(userService.Value).Name);
}

public override Guid Key => this.content.Key;

public override PublishedItemType ItemType => PublishedItemType.Content;

public override int Id => this.content.Id;

public override int TemplateId => this.content.Template?.Id ?? default(int);

public override int SortOrder => this.content.SortOrder;

public override string Name => this.content.Name;

public override string UrlName => this.urlName.Value;

public override string DocumentTypeAlias => this.content.ContentType?.Alias;

public override int DocumentTypeId => this.content.ContentType?.Id ?? default(int);

public override string WriterName => this.writerName.Value;

public override string CreatorName => this.creatorName.Value;

public override int WriterId => this.content.WriterId;

public override int CreatorId => this.content.CreatorId;

public override string Path => this.content.Path;

public override DateTime CreateDate => this.content.CreateDate;

public override DateTime UpdateDate => this.content.UpdateDate;

public override Guid Version => this.content.Version;

public override int Level => this.content.Level;

public override bool IsDraft => true;

public override IPublishedContent Parent => this.parent.Value;

public override IEnumerable<IPublishedContent> Children => this.children.Value;

public override PublishedContentType ContentType => this.contentType.Value;

public override ICollection<IPublishedProperty> Properties => this.properties.Value.Values;

public override IPublishedProperty GetProperty(string alias)
{
return this.properties.Value.TryGetValue(alias, out IPublishedProperty property) ? property : null;
}

private Dictionary<string, IPublishedProperty> MapProperties(PropertyEditorResolver resolver, ServiceContext services)
{
var contentType = this.contentType.Value;
var properties = this.content.Properties;

var items = new Dictionary<string, IPublishedProperty>(StringComparer.InvariantCultureIgnoreCase);

foreach (var propertyType in contentType.PropertyTypes)
{
var property = properties.FirstOrDefault(x => x.Alias.InvariantEquals(propertyType.PropertyTypeAlias));
var value = property?.Value;
if (value != null)
{
var propertyEditor = resolver.GetByAlias(propertyType.PropertyEditorAlias);
if (propertyEditor != null)
{
value = propertyEditor.ValueEditor.ConvertDbToString(property, property.PropertyType, services.DataTypeService);
}
}

items.Add(propertyType.PropertyTypeAlias, new UnpublishedProperty(propertyType, value));
}

return items;
}
}
}
38 changes: 38 additions & 0 deletions src/Our.Umbraco.DocTypeGridEditor/Models/UnpublishedProperty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;

namespace Our.Umbraco.DocTypeGridEditor.Models
{
internal class UnpublishedProperty : IPublishedProperty
{
private readonly PublishedPropertyType propertyType;
private readonly object dataValue;
private readonly Lazy<bool> hasValue;
private readonly Lazy<object> sourceValue;
private readonly Lazy<object> objectValue;
private readonly Lazy<object> xpathValue;

public UnpublishedProperty(PublishedPropertyType propertyType, object value)
{
this.propertyType = propertyType;

this.dataValue = value;
this.hasValue = new Lazy<bool>(() => value != null && value.ToString().Trim().Length > 0);

this.sourceValue = new Lazy<object>(() => this.propertyType.ConvertDataToSource(this.dataValue, true));
this.objectValue = new Lazy<object>(() => this.propertyType.ConvertSourceToObject(this.sourceValue.Value, true));
this.xpathValue = new Lazy<object>(() => this.propertyType.ConvertSourceToXPath(this.sourceValue.Value, true));
}

public string PropertyTypeAlias => this.propertyType.PropertyTypeAlias;

public bool HasValue => this.hasValue.Value;

public object DataValue => this.dataValue;

public object Value => this.objectValue.Value;

public object XPathValue => this.xpathValue.Value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Net.Http.Formatting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System.Web" />
<Reference Include="System.Web.Http, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll</HintPath>
Expand Down Expand Up @@ -100,16 +106,19 @@
<Compile Include="Models\DetachedPublishedContent.cs" />
<Compile Include="Models\DetachedPublishedProperty.cs" />
<Compile Include="Models\JsonDbRow.cs" />
<Compile Include="Models\UnpublishedContent.cs" />
<Compile Include="Models\UnpublishedProperty.cs" />
<Compile Include="PackageActions\AddObjectToJsonArray.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\VersionInfo.cs" />
<Compile Include="Web\Attributes\DocTypeGridEditorPreviewAttribute.cs" />
<Compile Include="Web\Controllers\DocTypeGridEditorApiController.cs" />
<Compile Include="Extensions\ContentTypeServiceExtensions.cs" />
<Compile Include="Web\Controllers\DocTypeGridEditorSurfaceController.cs" />
<Compile Include="Web\Extensions\HtmlHelperExtensions.cs" />
<Compile Include="Web\Helpers\SurfaceControllerHelper.cs" />
<Compile Include="Web\Helpers\ViewHelper.cs" />
<Compile Include="Web\Mvc\DefaultDocTypeGridEditorSurfaceControllerResolver.cs" />
<Compile Include="Web\PreviewModel.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Net.Mime;
using System.Text.RegularExpressions;
using System.Web.Http;
using System.Web.Http.ModelBinding;
using Our.Umbraco.DocTypeGridEditor.Extensions;
using Our.Umbraco.DocTypeGridEditor.Models;
using Umbraco.Core.Models;
using Umbraco.Core.PropertyEditors;
using Umbraco.Web.Editors;
using Umbraco.Web.Models;
using Umbraco.Web.Mvc;

namespace Our.Umbraco.DocTypeGridEditor.Web.Controllers
Expand Down Expand Up @@ -86,5 +92,43 @@ public object GetDataTypePreValues(string dtdId)
var propEditor = PropertyEditorResolver.Current.GetByAlias(dtd.PropertyEditorAlias);
return propEditor.PreValueEditor.ConvertDbToEditor(propEditor.DefaultPreValues, preValue);
}

[HttpPost]
public HttpResponseMessage GetPreviewMarkup([FromBody] FormDataCollection item, [FromUri] int pageId)
{
var page = default(IPublishedContent);

// If the page is new, then the ID will be zero
if (pageId > 0)
{
// Get page container node
page = UmbracoContext.ContentCache.GetById(pageId);
if (page == null)
{
// If unpublished, then fake PublishedContent (with IContent object)
page = new UnpublishedContent(pageId, Services);
}
}

var culture = UmbracoContext.Application.Services.ContentService.GetById(pageId).GetCulture();
System.Threading.Thread.CurrentThread.CurrentCulture = culture;
System.Threading.Thread.CurrentThread.CurrentUICulture = culture;

// Construct preview model
var model = new PreviewModel { Page = page, Values = item };

// Render view
var markup = Helpers.ViewHelper.RenderPartial("~/App_Plugins/DocTypeGridEditor/Render/DocTypeGridEditorPreviewer.cshtml", model);

// Return response
var response = new HttpResponseMessage
{
Content = new StringContent(markup ?? string.Empty)
};

response.Content.Headers.ContentType = new MediaTypeHeaderValue(MediaTypeNames.Text.Html);

return response;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Our.Umbraco.DocTypeGridEditor.Web.Mvc;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Web;

namespace Our.Umbraco.DocTypeGridEditor.Web.Extensions
{
Expand Down
37 changes: 37 additions & 0 deletions src/Our.Umbraco.DocTypeGridEditor/Web/Helpers/ViewHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.IO;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Umbraco.Core.Logging;

namespace Our.Umbraco.DocTypeGridEditor.Web.Helpers
{
public static class ViewHelper
{
private class DummyController : Controller { }

internal static string RenderPartial(string partialName, object model)
{
using (var sw = new StringWriter())
{
var httpContext = new HttpContextWrapper(HttpContext.Current);

var routeData = new RouteData();
routeData.Values.Add("controller", "DummyController");

var controllerContext = new ControllerContext(new RequestContext(httpContext, routeData), new DummyController());

var viewResult = ViewEngines.Engines.FindPartialView(controllerContext, partialName);
if (viewResult.View == null)
{
LogHelper.Warn(typeof(ViewHelper), $"No view found for partial '{partialName}'");
return null;
}

viewResult.View.Render(new ViewContext(controllerContext, viewResult.View, new ViewDataDictionary { Model = model }, new TempDataDictionary(), sw), sw);

return sw.ToString();
}
}
}
}
12 changes: 12 additions & 0 deletions src/Our.Umbraco.DocTypeGridEditor/Web/PreviewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Net.Http.Formatting;
using Umbraco.Core.Models;

namespace Our.Umbraco.DocTypeGridEditor.Web
{
public class PreviewModel
{
public IPublishedContent Page { get; set; }

public FormDataCollection Values { get; set; }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about exposing FormDataCollection from the preview model. It's not a showstopper, but thinking that we already know what the properties are, (e.g. "editorAlias", "contentTypeAlias", etc), then we could strongly-type them? (since we're already introducing a new view-model for the previewer.

Like I say, it's not a showstopper for this PR ... just something for me & @mattbrailsford to discuss.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@
dtgeResources.getEditorMarkupForDocTypePartial(editorState.current.id, model.id,
$scope.control.editor.alias, model.dtgeContentTypeAlias, model.value,
$scope.control.editor.config.viewPath,
$scope.control.editor.config.previewViewPath,
!!editorState.current.publishDate)
$scope.control.editor.config.previewViewPath)
.success(function (htmlResult) {
if (htmlResult.trim().length > 0) {
$scope.preview = htmlResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
'Failed to retrieve datatypes'
);
},
getEditorMarkupForDocTypePartial: function (nodeId, id, editorAlias, contentTypeAlias, value, viewPath, previewViewPath, published) {
var url = umbRequestHelper.convertVirtualToAbsolutePath("~/" + (published ? nodeId : "") + "?dtgePreview=1" + (published ? "" : "&nodeId=" + nodeId));
getEditorMarkupForDocTypePartial: function (pageId, id, editorAlias, contentTypeAlias, value, viewPath, previewViewPath) {
var url = umbRequestHelper.convertVirtualToAbsolutePath("~/umbraco/backoffice/DocTypeGridEditorApi/DocTypeGridEditorApi/GetPreviewMarkup?dtgePreview=1&pageId=" + pageId);
return $http({
method: 'POST',
url: url,
Expand Down
Loading