Skip to content

Commit

Permalink
Add DocumentNavigationController
Browse files Browse the repository at this point in the history
  • Loading branch information
realLiangshiwei committed Dec 23, 2024
1 parent 9362bdd commit a0de880
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using System.Threading.Tasks;
using Asp.Versioning;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Docs.Areas.Models.DocumentNavigation;
using Volo.Docs.Documents;
using Volo.Docs.Utils;

namespace Volo.Docs.Areas.Documents;

[RemoteService(Name = DocsRemoteServiceConsts.RemoteServiceName)]
[Area(DocsRemoteServiceConsts.ModuleName)]
[ControllerName("DocumentNavigation")]
[Route("/docs/document-navigation")]
public class DocumentNavigationController : AbpController
{
private readonly IDocumentAppService _documentAppService;
private readonly IDocsLinkGenerator _docsLinkGenerator;

public DocumentNavigationController(IDocumentAppService documentAppService, IDocsLinkGenerator docsLinkGenerator)
{
_documentAppService = documentAppService;
_docsLinkGenerator = docsLinkGenerator;
}

[HttpGet]
[Route("")]
public virtual async Task<NavigationNode> GetNavigationAsync(GetNavigationNodeWithLinkModel input)
{
var navigationNode = await _documentAppService.GetNavigationAsync(new GetNavigationDocumentInput
{
LanguageCode = input.LanguageCode,
Version = input.Version,
ProjectId = input.ProjectId
});

NormalPath(navigationNode, input);

return navigationNode;
}

protected virtual void NormalPath(NavigationNode node, GetNavigationNodeWithLinkModel input)
{
if (node.HasChildItems)
{
foreach (var item in node.Items)
{
NormalPath(item, input);
}
}

if (UrlHelper.IsExternalLink(node.Path))
{
return;
}

node.Path = RemoveFileExtensionFromPath(node.Path, input.ProjectFormat);
if (node.Path.IsNullOrWhiteSpace())
{
node.Path = "javascript:;";
return;
}

node.Path = _docsLinkGenerator.GenerateLink(input.ProjectName, input.LanguageCode, input.RouteVersion, node.Path);
}

private string RemoveFileExtensionFromPath(string path, string projectFormat)
{
if (path == null)
{
return null;
}

return path.EndsWith("." + projectFormat)
? path.Left(path.Length - projectFormat.Length - 1)
: path;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.ComponentModel.DataAnnotations;
using Volo.Abp.Validation;
using Volo.Docs.Language;
using Volo.Docs.Projects;

namespace Volo.Docs.Areas.Models.DocumentNavigation;

public class GetNavigationNodeWithLinkModel
{
public Guid ProjectId { get; set; }

[DynamicStringLength(typeof(ProjectConsts), nameof(ProjectConsts.MaxVersionNameLength))]
public string Version { get; set; }

[Required]
[DynamicStringLength(typeof(LanguageConsts), nameof(LanguageConsts.MaxLanguageCodeLength))]
public string LanguageCode { get; set; }

[Required]
public string ProjectName { get; set; }

[Required]
public string ProjectFormat { get; set; }

[Required]
public string RouteVersion { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,11 @@
var doc = doc || {};
doc.project = {
id: '@Model.Project.Id',
name: '@Model.Project.Name',
name: '@Model.Project.ShortName',
languageCode: '@Model.LanguageCode',
version: '@Model.Version',
routeVersion: '@(Model.LatestVersionInfo == null || Model.LatestVersionInfo.IsSelected ? DocsAppConsts.Latest : Model.Version)',
}
doc.uiOptions = {
routePrefix: '@DocsUiOptions.Value.RoutePrefix',
multiLanguageMode : `@DocsUiOptions.Value.MultiLanguageMode`,
singleProjectMode : `@DocsUiOptions.Value.SingleProjectMode.Enable`,
format: '@Model.Project.Format',
routeVersion: '@(Model.LatestVersionInfo == null || Model.LatestVersionInfo.IsSelected ? DocsAppConsts.Latest : Model.Version)'
}
</script>
<abp-script-bundle name="@typeof(IndexModel).FullName">
Expand Down
77 changes: 21 additions & 56 deletions modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ var doc = doc || {};
(function ($) {
$(function () {

var _documentAppService = volo.docs.documents.docsDocument;

doc.lazyExpandableNavigation = {
isAllLoaded: false,
findNode : function(text, node){
if(node.text === text && node.isLazyExpandable){
findNode : function(text, href, node){
if(node.text === text && node.path === href && node.isLazyExpandable){
return node;
}
if(node.items){
for (let i = 0; i < node.items.length; i++) {
var result = doc.lazyExpandableNavigation.findNode(text, node.items[i]);
var result = doc.lazyExpandableNavigation.findNode(text, href, node.items[i]);
if(result){
return result;
}
Expand All @@ -26,12 +24,12 @@ var doc = doc || {};
return;
}

var textCss = node.path ? "": "tree-toggle";
var textCss = node.path === "javascript:;" ? "": "tree-toggle";
var uiCss = isRootLazyNode ? "" : "style='display: none;'";
var $ul = $(`<ul class="nav nav-list tree" ${uiCss}></ul>`);
var $li = $(`<li class="${node.hasChildItems ? 'nav-header' : 'last-link'}"></li>`);

$li.append(`<span class="plus-icon"> <i class="fa fa-${node.hasChildItems ? 'chevron-right' : node.path ? 'has-link' : 'no-link'}"></i></span><a href="${normalPath(node.path)}" class="${textCss}">${node.text}</a>`)
$li.append(`<span class="plus-icon"> <i class="fa fa-${node.hasChildItems ? 'chevron-right' : node.path === "javascript:;" ? 'has-link' : 'no-link'}"></i></span><a href="${node.path}" class="${textCss}">${node.text}</a>`)

if(node.isLazyExpandable){
$li.addClass("lazy-expand");
Expand All @@ -45,48 +43,6 @@ var doc = doc || {};
$lazyLiElement.append($ul)

window.Toc.helpers.initNavEvent();
function normalPath(path){
var pathWithoutFileExtension = removeFileExtensionFromPath(path);

if (!pathWithoutFileExtension)
{
return "javascript:;";
}

if (path.toLowerCase().startsWith("http://") || path.toLowerCase().startsWith("https://"))
{
return path;
}

path = abp.appPath + doc.uiOptions.routePrefix;

if(doc.uiOptions.multiLanguageMode === `True`){
path += doc.project.languageCode;
}

if(doc.uiOptions.singleProjectMode === `False`){
path += "/" + doc.project.name
}

path += "/" + doc.project.routeVersion;
path += "/" + pathWithoutFileExtension;
return path.replace("//","/");
}

function removeFileExtensionFromPath(path){
if (!path)
{
return null;
}

var lastDotIndex = path.lastIndexOf(".");
if (lastDotIndex < 0)
{
return path;
}

return path.substring(0, lastDotIndex);
}
},
loadAll : function(lazyLiElements){
if(doc.lazyExpandableNavigation.isAllLoaded){
Expand All @@ -95,7 +51,8 @@ var doc = doc || {};
for(var i = 0; i < lazyLiElements.length; i++){
var $li = $(lazyLiElements[i]);
if($li.has("ul").length === 0){
var node = doc.lazyExpandableNavigation.findNode($li.find("a").text(), doc.project.navigation);
var $a = $li.find("a");
var node = doc.lazyExpandableNavigation.findNode($a.text(), $a.attr("href"), doc.project.navigation);
node.items.forEach(item => {
doc.lazyExpandableNavigation.renderNodeAsHtml($li, item, true);
})
Expand Down Expand Up @@ -215,11 +172,18 @@ var doc = doc || {};
};

var initDocProject = function(){
_documentAppService.getNavigation({
projectId: doc.project.id,
version: doc.project.version,
languageCode: doc.project.languageCode
}).done((data) => {
abp.ajax({
type :"GET",
url: '/docs/document-navigation',
data: {
projectId: doc.project.id,
version: doc.project.version,
routeVersion: doc.project.routeVersion,
languageCode: doc.project.languageCode,
projectName: doc.project.name,
projectFormat: doc.project.format
}
}).done(data => {
doc.project.navigation = data;
})
}
Expand Down Expand Up @@ -405,7 +369,8 @@ var doc = doc || {};
return;
}

var node = doc.lazyExpandableNavigation.findNode($this.find("a").text(), doc.project.navigation);
var $a = $this.find("a");
var node = doc.lazyExpandableNavigation.findNode($a.text(), $a.attr("href") , doc.project.navigation);
node.items.forEach(item => {
doc.lazyExpandableNavigation.renderNodeAsHtml($this, item, true);
})
Expand Down

0 comments on commit a0de880

Please sign in to comment.