Adding support for layout element indexing.

This commit is contained in:
Sipke Schoorstra
2014-11-13 15:13:39 -08:00
parent e3ab41a78c
commit a91d06a4b9
12 changed files with 101 additions and 8 deletions

View File

@@ -1,5 +1,6 @@
using Orchard.Layouts.Elements;
using Orchard.Layouts.Framework.Drivers;
using Orchard.Layouts.Framework.Elements;
using Orchard.Layouts.ViewModels;
namespace Orchard.Layouts.Drivers {
@@ -18,5 +19,11 @@ namespace Orchard.Layouts.Drivers {
return Editor(context, editor);
}
protected override void OnIndexing(Html element, ElementIndexingContext context) {
context.DocumentIndex
.Add("body", element.Content).RemoveTags().Analyze()
.Add("format", "html").Store();
}
}
}

View File

@@ -1,6 +1,7 @@
using Orchard.Environment.Extensions;
using Orchard.Layouts.Framework.Display;
using Orchard.Layouts.Framework.Drivers;
using Orchard.Layouts.Framework.Elements;
using Orchard.Layouts.ViewModels;
using MarkdownElement = Orchard.Layouts.Elements.Markdown;
@@ -25,5 +26,11 @@ namespace Orchard.Layouts.Drivers {
protected override void OnDisplaying(MarkdownElement element, ElementDisplayContext context) {
context.ElementShape.ProcessedContent = new MarkdownSharp.Markdown().Transform(element.Content);
}
protected override void OnIndexing(MarkdownElement element, ElementIndexingContext context) {
context.DocumentIndex
.Add("body", element.Content).RemoveTags().Analyze()
.Add("format", "markdown").Store();
}
}
}

View File

@@ -1,5 +1,6 @@
using Orchard.Layouts.Elements;
using Orchard.Layouts.Framework.Drivers;
using Orchard.Layouts.Framework.Elements;
using Orchard.Layouts.ViewModels;
namespace Orchard.Layouts.Drivers {
@@ -18,5 +19,11 @@ namespace Orchard.Layouts.Drivers {
return Editor(context, editor);
}
protected override void OnIndexing(Paragraph element, ElementIndexingContext context) {
context.DocumentIndex
.Add("body", element.Content).RemoveTags().Analyze()
.Add("format", "text").Store();
}
}
}

View File

@@ -4,6 +4,7 @@ using Orchard.ContentManagement;
using Orchard.Layouts.Elements;
using Orchard.Layouts.Framework.Display;
using Orchard.Layouts.Framework.Drivers;
using Orchard.Layouts.Framework.Elements;
using Orchard.Layouts.Models;
using Orchard.Layouts.Settings;
using Orchard.Layouts.ViewModels;
@@ -19,7 +20,7 @@ namespace Orchard.Layouts.Drivers {
protected override EditorResult OnBuildEditor(Text element, ElementEditorContext context) {
var content = context.Content;
var layoutPart = content.As<LayoutPart>();
var flavor = layoutPart != null ? layoutPart.GetFlavor() : LayoutPartSettings.FlavorDefaultDefault; // TODO: make this configurable.
var flavor = GetFlavor(layoutPart);
var viewModel = new TextEditorViewModel {
Flavor = flavor,
@@ -38,10 +39,23 @@ namespace Orchard.Layouts.Drivers {
protected override void OnDisplaying(Text element, ElementDisplayContext context) {
var text = element.Content;
var layoutPart = context.Content.As<LayoutPart>();
var flavor = layoutPart != null ? layoutPart.GetFlavor() : LayoutPartSettings.FlavorDefaultDefault; // TODO: provide a different way to configure flavor, so that an ElementWrapperWidget can control this setting as well.
var flavor = GetFlavor(layoutPart);
var processedText = _htmlFilters.Aggregate(text, (t, filter) => filter.ProcessContent(t, flavor));
context.ElementShape.ProcessedText = processedText;
}
protected override void OnIndexing(Text element, ElementIndexingContext context) {
var layoutPart = context.Layout.As<LayoutPart>();
var flavor = GetFlavor(layoutPart);
context.DocumentIndex
.Add("body", element.Content).RemoveTags().Analyze()
.Add("format", flavor).Store();
}
private static string GetFlavor(LayoutPart part) {
return part != null ? part.GetFlavor() : LayoutPartSettings.FlavorDefaultDefault; // TODO: make this configurable.
}
}
}

View File

@@ -25,8 +25,12 @@ namespace Orchard.Layouts.Framework.Drivers {
OnLayoutSaving((TElement) context.Element, context);
}
public void ElementRemoving(ElementRemovingContext context) {
OnElementRemoving((TElement) context.Element, context);
public void Removing(ElementRemovingContext context) {
OnRemoving((TElement) context.Element, context);
}
public void Indexing(ElementIndexingContext context) {
OnIndexing((TElement)context.Element, context);
}
protected virtual EditorResult OnBuildEditor(TElement element, ElementEditorContext context) {
@@ -43,7 +47,10 @@ namespace Orchard.Layouts.Framework.Drivers {
protected virtual void OnLayoutSaving(TElement element, ElementSavingContext context) {
}
protected virtual void OnElementRemoving(TElement element, ElementRemovingContext context) {
protected virtual void OnRemoving(TElement element, ElementRemovingContext context) {
}
protected virtual void OnIndexing(TElement element, ElementIndexingContext context) {
}
protected EditorResult Editor(ElementEditorContext context, params dynamic[] editorShapes) {

View File

@@ -8,6 +8,7 @@ namespace Orchard.Layouts.Framework.Drivers {
EditorResult UpdateEditor(ElementEditorContext context);
void Displaying(ElementDisplayContext context);
void LayoutSaving(ElementSavingContext context);
void ElementRemoving(ElementRemovingContext context);
void Removing(ElementRemovingContext context);
void Indexing(ElementIndexingContext context);
}
}

View File

@@ -0,0 +1,10 @@
namespace Orchard.Layouts.Framework.Elements {
public class ElementIndexingContext : LayoutIndexingContext {
public ElementIndexingContext(LayoutIndexingContext stub) {
DocumentIndex = stub.DocumentIndex;
Layout = stub.Layout;
Elements = stub.Elements;
}
public IElement Element { get; set; }
}
}

View File

@@ -0,0 +1,11 @@
using System.Collections.Generic;
using Orchard.Indexing;
using Orchard.Layouts.Models;
namespace Orchard.Layouts.Framework.Elements {
public class LayoutIndexingContext {
public ILayoutAspect Layout { get; set; }
public IEnumerable<IElement> Elements { get; set; }
public IDocumentIndex DocumentIndex { get; set; }
}
}

View File

@@ -1,6 +1,8 @@
using Orchard.ContentManagement;
using Orchard.ContentManagement.Handlers;
using Orchard.Data;
using Orchard.Layouts.Framework.Elements;
using Orchard.Layouts.Helpers;
using Orchard.Layouts.Models;
using Orchard.Layouts.Services;
@@ -8,12 +10,29 @@ namespace Orchard.Layouts.Handlers {
public class LayoutPartHandler : ContentHandler {
private readonly ILayoutManager _layoutManager;
private readonly IContentManager _contentManager;
private readonly IElementManager _elementManager;
public LayoutPartHandler(
IRepository<LayoutPartRecord> repository,
ILayoutManager layoutManager,
IContentManager contentManager,
IElementManager elementManager) {
public LayoutPartHandler(IRepository<LayoutPartRecord> repository, ILayoutManager layoutManager, IContentManager contentManager) {
_layoutManager = layoutManager;
_contentManager = contentManager;
_elementManager = elementManager;
Filters.Add(StorageFilter.For(repository));
OnPublished<LayoutPart>(UpdateTemplateClients);
OnIndexing<LayoutPart>(IndexElements);
}
private void IndexElements(IndexContentContext context, LayoutPart part) {
var elements = _layoutManager.LoadElements(part).Flatten();
_elementManager.Indexing(new LayoutIndexingContext {
Layout = part,
Elements = elements,
DocumentIndex = context.DocumentIndex
});
}
private void UpdateTemplateClients(PublishContentContext context, LayoutPart part) {

View File

@@ -224,6 +224,8 @@
<Compile Include="Elements\Projection.cs" />
<Compile Include="Filters\ControllerAccessorFilter.cs" />
<Compile Include="Framework\Display\ElementCreatingDisplayShapeContext.cs" />
<Compile Include="Framework\Elements\ElementIndexingContext.cs" />
<Compile Include="Framework\Elements\LayoutIndexingContext.cs" />
<Compile Include="Handlers\ElementDriversCoordinator.cs" />
<Compile Include="Helpers\DictionaryExtensions.cs" />
<Compile Include="Helpers\EditorResultExtensions.cs" />

View File

@@ -145,7 +145,14 @@ namespace Orchard.Layouts.Services {
public void Removing(LayoutSavingContext context) {
var elementInstances = context.RemovedElements.Flatten();
InvokeDriver(elementInstances, (driver, elementInstance) => driver.ElementRemoving(new ElementRemovingContext(context) {
InvokeDriver(elementInstances, (driver, elementInstance) => driver.Removing(new ElementRemovingContext(context) {
Element = elementInstance
}));
}
public void Indexing(LayoutIndexingContext context) {
var elementInstances = context.Elements.Flatten();
InvokeDriver(elementInstances, (driver, elementInstance) => driver.Indexing(new ElementIndexingContext(context) {
Element = elementInstance
}));
}

View File

@@ -22,5 +22,6 @@ namespace Orchard.Layouts.Services {
EditorResult UpdateEditor(ElementEditorContext context);
void Saving(LayoutSavingContext context);
void Removing(LayoutSavingContext context);
void Indexing(LayoutIndexingContext context);
}
}