#19657: Implementing a Taxonomy Navigation Provider

- Replacing the previous Taxonomy Widget, for better integration with Orchard's
new navigation system.
- No migration is provided, the menu item and a menu widget need to be created.

Work Item: 19657

--HG--
branch : 1.x
This commit is contained in:
Sebastien Ros
2013-05-01 12:11:59 -07:00
parent aaa78faa9f
commit 453286e347
8 changed files with 279 additions and 5 deletions

View File

@@ -0,0 +1,71 @@
using System;
using System.Linq;
using System.Web.Mvc;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.Taxonomies.Models;
using Orchard.Taxonomies.Services;
using Orchard.Taxonomies.ViewModels;
namespace Orchard.Taxonomies.Drivers {
public class TaxonomyNavigationPartDriver : ContentPartDriver<TaxonomyNavigationPart> {
private readonly ITaxonomyService _taxonomyService;
public TaxonomyNavigationPartDriver(ITaxonomyService taxonomyService) {
_taxonomyService = taxonomyService;
}
protected override string Prefix { get { return "TaxonomyNavigationPart"; } }
protected override DriverResult Editor(TaxonomyNavigationPart part, dynamic shapeHelper) {
return Editor(part, null, shapeHelper);
}
protected override DriverResult Editor(TaxonomyNavigationPart part, IUpdateModel updater, dynamic shapeHelper) {
return ContentShape(
"Parts_Navigation_Taxonomy_Edit", () => {
var model = new TaxonomyNavigationViewModel {
SelectedTaxonomyId = part.TaxonomyId,
SelectedTermId = part.TermId,
DisplayContentCount = part.DisplayContentCount,
DisplayTopMenuItem = part.DisplayRootTerm,
HideEmptyTerms = part.HideEmptyTerms,
};
if (updater != null) {
if (updater.TryUpdateModel(model, Prefix, null, null)) {
// taxonomy to render
part.TaxonomyId = model.SelectedTaxonomyId;
// root term (can be null)
part.TermId = model.SelectedTermId;
part.DisplayContentCount = model.DisplayContentCount;
part.DisplayRootTerm = model.DisplayTopMenuItem;
part.HideEmptyTerms = model.HideEmptyTerms;
}
}
var taxonomies = _taxonomyService.GetTaxonomies().ToArray();
var listItems = taxonomies.Select(taxonomy => new SelectListItem {
Value = Convert.ToString(taxonomy.Id),
Text = taxonomy.Name,
Selected = taxonomy.Id == part.TaxonomyId,
}).ToList();
model.AvailableTaxonomies = new SelectList(listItems, "Value", "Text", model.SelectedTaxonomyId);
// if no taxonomy is selected, take the first available one as
// the terms drop down needs one by default
if (model.SelectedTaxonomyId == -1) {
var firstTaxonomy = taxonomies.FirstOrDefault();
if (firstTaxonomy != null) {
model.SelectedTaxonomyId = firstTaxonomy.Id;
}
}
return shapeHelper.EditorTemplate(TemplateName: "Parts/Navigation.Taxonomy.Edit", Model: model, Prefix: Prefix);
});
}
}
}

View File

@@ -61,5 +61,19 @@ namespace Orchard.Taxonomies {
return 1;
}
public int UpdateFrom1() {
ContentDefinitionManager.AlterTypeDefinition("TaxonomyNavigationMenuItem",
cfg => cfg
.WithPart("TaxonomyNavigationPart")
.WithPart("MenuPart")
.WithPart("CommonPart")
.DisplayedAs("Taxonomy Link")
.WithSetting("Description", "Injects menu items from a Taxonomy")
.WithSetting("Stereotype", "MenuItem")
);
return 2;
}
}
}

View File

@@ -0,0 +1,52 @@
using System;
using System.Globalization;
using Orchard.ContentManagement;
using Orchard.ContentManagement.FieldStorage.InfosetStorage;
namespace Orchard.Taxonomies.Models {
public class TaxonomyNavigationPart : ContentPart {
/// <summary>
/// The taxonomy to display
/// </summary>
public int TaxonomyId {
get { return Convert.ToInt32(this.As<InfosetPart>().Get("TaxonomyNavigationPart", "TaxonomyId", null), CultureInfo.InvariantCulture); }
set { this.As<InfosetPart>().Set("TaxonomyNavigationPart", "TaxonomyId", null, Convert.ToString(value, CultureInfo.InvariantCulture)); }
}
/// <summary>
/// Top term to display in the menu.
/// If null, the taxonomy is supposed to be the top term.
/// </summary>
public int TermId {
get { return Convert.ToInt32(this.As<InfosetPart>().Get("TaxonomyNavigationPart", "TermId", null), CultureInfo.InvariantCulture); }
set { this.As<InfosetPart>().Set("TaxonomyNavigationPart", "TermId", null, Convert.ToString(value, CultureInfo.InvariantCulture)); }
}
/// <summary>
/// Whether to display the root node or not.
/// If <c>False</c>, the menu will have a flat first level.
/// </summary>
public bool DisplayRootTerm {
get { return Convert.ToBoolean(this.As<InfosetPart>().Get("TaxonomyNavigationPart", "DisplayRootTerm", null), CultureInfo.InvariantCulture); }
set { this.As<InfosetPart>().Set("TaxonomyNavigationPart", "DisplayRootTerm", null, Convert.ToString(value, CultureInfo.InvariantCulture)); }
}
/// <summary>
/// Whether to display the number of content items
/// associated with this term, in the generated menu item text
/// </summary>
public bool DisplayContentCount {
get { return Convert.ToBoolean(this.As<InfosetPart>().Get("TaxonomyNavigationPart", "DisplayContentCount", null), CultureInfo.InvariantCulture); }
set { this.As<InfosetPart>().Set("TaxonomyNavigationPart", "DisplayContentCount", null, Convert.ToString(value, CultureInfo.InvariantCulture)); }
}
/// <summary>
/// Whether to hide the terms without any associated content
/// </summary>
public bool HideEmptyTerms {
get { return Convert.ToBoolean(this.As<InfosetPart>().Get("TaxonomyNavigationPart", "HideEmptyTerms", null), CultureInfo.InvariantCulture); }
set { this.As<InfosetPart>().Set("TaxonomyNavigationPart", "HideEmptyTerms", null, Convert.ToString(value, CultureInfo.InvariantCulture)); }
}
}
}

View File

@@ -0,0 +1,71 @@
using System.Collections.Generic;
using Orchard.ContentManagement;
using Orchard.Localization;
using Orchard.Taxonomies.Models;
using Orchard.Taxonomies.Services;
using Orchard.UI.Navigation;
namespace Orchard.Taxonomies.Navigation {
/// <summary>
/// Dynamically injects query results as menu items on NavigationQueryMenuItem elements
/// </summary>
public class NavigationQueryProvider : INavigationFilter {
private readonly IContentManager _contentManager;
private readonly ITaxonomyService _taxonomyService;
public NavigationQueryProvider(
IContentManager contentManager,
ITaxonomyService taxonomyService) {
_contentManager = contentManager;
_taxonomyService = taxonomyService;
}
public IEnumerable<MenuItem> Filter(IEnumerable<MenuItem> items) {
foreach (var item in items) {
if (item.Content != null && item.Content.ContentItem.ContentType == "TaxonomyNavigationMenuItem") {
// expand query
var taxonomyNavigationPart = item.Content.As<TaxonomyNavigationPart>();
var rootTerm = _taxonomyService.GetTerm(taxonomyNavigationPart.TermId);
var allTerms = rootTerm != null
? _taxonomyService.GetChildren(rootTerm)
: _taxonomyService.GetTerms(taxonomyNavigationPart.TaxonomyId);
var menuPosition = item.Position;
int index = 0;
foreach (var contentItem in allTerms) {
if (contentItem != null) {
var part = contentItem;
var menuText = _contentManager.GetItemMetadata(part).DisplayText;
var routes = _contentManager.GetItemMetadata(part).DisplayRouteValues;
var inserted = new MenuItem {
Text = new LocalizedString(menuText),
IdHint = item.IdHint,
Classes = item.Classes,
Url = item.Url,
Href = item.Href,
LinkToFirstChild = false,
RouteValues = routes,
LocalNav = item.LocalNav,
Items = new MenuItem[0],
Position = menuPosition + contentItem.Path.TrimEnd('/').Replace("/", "."),
Permissions = item.Permissions,
Content = part
};
yield return inserted;
}
}
}
else {
yield return item;
}
}
}
}
}

View File

@@ -68,6 +68,7 @@
<Compile Include="AdminMenu.cs" />
<Compile Include="Controllers\AdminController.cs" />
<Compile Include="Controllers\TermAdminController.cs" />
<Compile Include="Drivers\TaxonomyNavigationPartDriver.cs" />
<Compile Include="Drivers\TermsPartDriver.cs" />
<Compile Include="Drivers\TaxonomyPartDriver.cs" />
<Compile Include="Drivers\TermWidgetPartDriver.cs" />
@@ -80,6 +81,7 @@
<Compile Include="Drivers\TaxonomyFieldDriver.cs" />
<Compile Include="Handlers\TermPartHandler.cs" />
<Compile Include="Helpers\PredicateBuilder.cs" />
<Compile Include="Models\TaxonomyNavigationPart.cs" />
<Compile Include="Models\TermContentItemPart.cs" />
<Compile Include="Models\TaxonomyPartRecord.cs" />
<Compile Include="Models\TermPart.cs" />
@@ -91,6 +93,7 @@
<Compile Include="Models\TermsPartRecord.cs" />
<Compile Include="Models\TermWidgetPart.cs" />
<Compile Include="Models\TermWidgetPartRecord.cs" />
<Compile Include="Navigation\TaxonomyNavigationProvider.cs" />
<Compile Include="Permissions.cs" />
<Compile Include="Projections\TermsFilter.cs" />
<Compile Include="Projections\TermsFilterForms.cs" />
@@ -111,7 +114,7 @@
<Compile Include="ViewModels\TaxonomyFieldViewModel.cs" />
<Compile Include="ViewModels\TermAdminIndexViewModel.cs" />
<Compile Include="ViewModels\TaxonomyAdminIndexViewModel.cs" />
<Compile Include="ViewModels\TaxonomyMenuViewModel.cs" />
<Compile Include="ViewModels\TaxonomyNavigationViewModel.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Scripts\admin-taxonomy-expando.js" />
@@ -207,6 +210,9 @@
<Content Include="Views\TaxonomyItem.cshtml" />
<Content Include="Views\TaxonomyItemLink.cshtml" />
</ItemGroup>
<ItemGroup>
<Content Include="Views\EditorTemplates\Parts\Navigation.Taxonomy.Edit.cshtml" />
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>

View File

@@ -28,4 +28,8 @@
<Place Parts_TaxonomyPart="Content:5" />
<Place Parts_TermPart="Content:5" />
<Place Parts_TermPart_Feed="Content" />
<!-- Navigation -->
<Place Parts_Navigation_Taxonomy_Edit="Content:5"/>
</Placement>

View File

@@ -2,16 +2,14 @@
using System.Web.Mvc;
namespace Orchard.Taxonomies.ViewModels {
public class TaxonomyMenuViewModel {
public class TaxonomyNavigationViewModel {
public SelectList AvailableTaxonomies { get; set; }
[Required, Range(0, int.MaxValue, ErrorMessage = "You must select a taxonomy")]
[Required, Range(1, int.MaxValue, ErrorMessage = "You must select a taxonomy")]
public int SelectedTaxonomyId { get; set; }
public int SelectedTermId { get; set; }
public bool DisplayTopMenuItem { get; set; }
[Required, Range(0, 99)]
public int LevelsToDisplay { get; set; }
public bool DisplayContentCount { get; set; }
public bool HideEmptyTerms { get; set; }
}

View File

@@ -0,0 +1,58 @@
@model TaxonomyNavigationViewModel
@{
Script.Require("jQuery");
}
<fieldset>
@Html.LabelFor(m => m.SelectedTaxonomyId, T("Taxonomy"))
@Html.DropDownListFor(m => m.SelectedTaxonomyId, Model.AvailableTaxonomies)
<span class="hint">@T("Select the taxonomy whose terms will be rendered in the menu.")</span>
</fieldset>
<fieldset>
@Html.LabelFor(m => m.SelectedTermId, T("Root Term"))
<select id="@Html.FieldIdFor(m => m.SelectedTermId)" name="@Html.FieldNameFor(m => m.SelectedTermId)">
@Html.SelectOption(-1, false, T("- Parent Taxonomy -").ToString())
@{Html.RenderAction("RenderTermSelect", "TermAdmin", new { area = "Orchard.Taxonomies", taxonomyId = Model.SelectedTaxonomyId, selectedTermId = Model.SelectedTermId });}
</select>
<span class="hint">@T("Select the root term to display")</span>
</fieldset>
<fieldset>
@Html.CheckBoxFor(m => m.DisplayTopMenuItem)
<label for="@Html.FieldIdFor(m => m.DisplayTopMenuItem)" class="forcheckbox">@T("Display top level menu item")</label>
<span class="hint">@T("When checked, the selected term to display will be rendered as a root element in the menu.")</span>
</fieldset>
<fieldset>
@Html.CheckBoxFor(m => m.HideEmptyTerms)
<label for="@Html.FieldIdFor(m => m.HideEmptyTerms)" class="forcheckbox">@T("Hide empty entries")</label>
<span class="hint">@T("When checked, any term which has no content item associated to it won't be rendered.")</span>
</fieldset>
<fieldset>
@Html.CheckBoxFor(m => m.DisplayContentCount)
<label for="@Html.FieldIdFor(m => m.DisplayContentCount)" class="forcheckbox">@T("Display content count")</label>
<span class="hint">@T("When checked, the number of content items is rendered following the term name.")</span>
</fieldset>
@using (Script.Foot()) {
<script type="text/javascript">
//<![CDATA[
$(function () {
$('#@Html.FieldIdFor(m => m.SelectedTaxonomyId)').change(function () {
var self = $(this);
var id = self.find("option:selected").attr("value");
// do nothing if no term
if (!id) return;
$.get(
"@Url.Action("RenderTermSelect", "TermAdmin", new { area = "Orchard.Taxonomies" })", { taxonomyId: id }, function (data) {
$('#@Html.FieldIdFor(m => m.SelectedTermId) option[value != -1]').remove();
$('#@Html.FieldIdFor(m => m.SelectedTermId)').append(data);
});
});
})
//]]>
</script>
}