This repository was archived by the owner on Feb 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 81
Rework preview mechanism based on StackedContent implementation #105
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
src/Our.Umbraco.DocTypeGridEditor/Models/UnpublishedContent.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
38
src/Our.Umbraco.DocTypeGridEditor/Models/UnpublishedProperty.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 0 additions & 32 deletions
32
src/Our.Umbraco.DocTypeGridEditor/Web/Attributes/DocTypeGridEditorPreviewAttribute.cs
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/Our.Umbraco.DocTypeGridEditor/Web/Helpers/ViewHelper.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.