Browse Source

Refactored

pull/509/head
Alper Ebicoglu 8 years ago
parent
commit
33305088ee
  1. 2
      modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/ContentWithDetailsDto.cs
  2. 93
      modules/docs/src/Volo.Docs.Web/Areas/Documents/Helpers/TagHelpers/TreeTagHelper.cs

2
modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/ContentWithDetailsDto.cs

@ -42,6 +42,8 @@ namespace Volo.Docs.Documents
[JsonProperty("items")]
public List<NavigationNode> Items { get; set; }
public bool IsLeaf => !HasChildItems;
public bool HasChildItems => Items != null && Items.Any();
public bool IsEmpty => Text == null && Path == null;

93
modules/docs/src/Volo.Docs.Web/Areas/Documents/Helpers/TagHelpers/TreeTagHelper.cs

@ -9,19 +9,13 @@ namespace Volo.Docs.Areas.Documents.Helpers.TagHelpers
[HtmlTargetElement("ul", Attributes = "root-node")]
public class TreeTagHelper : TagHelper
{
private const string LiItemTemplateWithLink = @"<li class='{0}'>
<span class='plus-icon'><i class='fa fa-{1}'></i></span>
{2}
{3}
</li>";
private const string LiItemTemplateWithLink = @"<li class='{0}'><span class='plus-icon'><i class='fa fa-{1}'></i></span>{2}{3}</li>";
private const string ListItemAnchor = @"<a href='{0}' class='{1}'>{2}</a>";
private const string ListItemSpan = @"<span class='{0}'>{1}</span>";
private const string UlItemTemplate = @"<ul class='nav nav-list tree' style='{1}'>
{0}
</ul>";
private const string UlItemTemplate = @"<ul class='nav nav-list tree' style='{1}'>{0}</ul>";
[HtmlAttributeName("root-node")]
public NavigationNode RootNode { get; set; }
@ -98,61 +92,46 @@ namespace Volo.Docs.Areas.Documents.Helpers.TagHelpers
{
var anchorCss = node.Path.IsNullOrEmpty() ? "tree-toggle" : "";
var isNodeSelected = node.IsSelected(SelectedDocumentName);
var normalizedPath = node.Path != null && node.Path.EndsWith("." + ProjectFormat)
? node.Path.Left(node.Path.Length - ProjectFormat.Length - 1)
: node.Path;
if (node.Path.IsNullOrEmpty() && !node.HasChildItems)
var listItemCss = node.HasChildItems ? "nav-header" : "last-link";
if (isNodeSelected)
{
//span
var span = string.Format(ListItemSpan, anchorCss, node.Text.IsNullOrEmpty() ? "?" : node.Text);
var listItemCss = node.HasChildItems ? "nav-header" : "last-link";
if (isNodeSelected)
{
listItemCss += " selected-tree";
}
return string.Format(LiItemTemplateWithLink,
listItemCss,
node.HasChildItems ? "chevron-right" : "long-arrow-right",
span,
content);
listItemCss += " selected-tree";
}
else if (node.Path.IsNullOrEmpty() && node.HasChildItems)
string listInnerItem;
if (node.Path.IsNullOrEmpty() && node.IsLeaf)
{
//anchor
var path = "javascript:;";
var anchor = string.Format(ListItemAnchor, path, anchorCss, node.Text.IsNullOrEmpty() ? "?" : node.Text);
var listItemCss = node.HasChildItems ? "nav-header" : "last-link";
if (isNodeSelected)
{
listItemCss += " selected-tree";
}
return string.Format(LiItemTemplateWithLink,
listItemCss,
node.HasChildItems ? "chevron-right" : "long-arrow-right",
anchor,
content);
listInnerItem = string.Format(ListItemSpan, anchorCss, node.Text.IsNullOrEmpty() ? "?" : node.Text);
}
else
{
//anchor
var path = "/documents/" + ProjectName + "/" + Version + "/" + normalizedPath;
var anchor = string.Format(ListItemAnchor, path, anchorCss, node.Text.IsNullOrEmpty() ? "?" : node.Text);
var listItemCss = node.HasChildItems ? "nav-header" : "last-link";
if (isNodeSelected)
{
listItemCss += " selected-tree";
}
return string.Format(LiItemTemplateWithLink,
listItemCss,
node.HasChildItems ? "chevron-right" : "long-arrow-right",
anchor,
content);
listInnerItem = string.Format(ListItemAnchor, NormalizePath(node.Path, node.HasChildItems), anchorCss, node.Text.IsNullOrEmpty() ? "?" : node.Text);
}
return string.Format(LiItemTemplateWithLink,
listItemCss,
node.HasChildItems ? "chevron-right" : "long-arrow-right",
listInnerItem,
content);
}
private string NormalizePath(string path, bool hasChildItems)
{
var pathWithoutFileExtension = RemoveFileExtensionFromPath(path);
return hasChildItems ? "javascript:;" : "/documents/" + ProjectName + "/" + Version + "/" + pathWithoutFileExtension;
}
private string RemoveFileExtensionFromPath(string path)
{
if (path == null)
{
return null;
}
return path.EndsWith("." + ProjectFormat)
? path.Left(path.Length - ProjectFormat.Length - 1)
: path;
}
}

Loading…
Cancel
Save