Skip to content

Commit 06bc568

Browse files
committed
feat: tag's autocompleting for post create form
1 parent e401638 commit 06bc568

File tree

193 files changed

+45582
-46
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

193 files changed

+45582
-46
lines changed

Blog/Blog/Blog.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88

99
<ItemGroup>
10+
<PackageReference Include="jQuery.UI.Combined" Version="1.12.1" />
1011
<PackageReference Include="Markdig" Version="0.18.3" />
12+
<PackageReference Include="Microsoft.AspNetCore.Blazor" Version="0.7.0" />
1113
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.1.1" />
1214
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.1" />
1315
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.1.1" />

Blog/Blog/Controllers/HomeController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ public IActionResult Details(int? id)
7070
if (post == null)
7171
return NotFound();
7272

73+
++post.Views;
74+
postsRepository.SaveAsync();
7375
return View(post);
7476
}
7577

Blog/Blog/Controllers/PostsController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class PostsController : Controller
1919
private readonly ITagRepository tagsRepository;
2020
private readonly IFileManager fileManager;
2121

22-
private readonly int ItemsPerPage = 1;
22+
private readonly int ItemsPerPage = 50;
2323
private readonly string PostForm = "PostForm";
2424

2525
public PostsController(IPostRepository postsRepository, ITagRepository tagsRepository, IFileManager fileManager)

Blog/Blog/Controllers/TagsController.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public IActionResult Index()
2323
return View(repository.Get().OrderBy(x => x.Name));
2424
}
2525

26+
public JsonResult GetTags(string term)
27+
{
28+
return Json(repository.Get().Where(c => c.Name.Contains(term)).Select(x => x.Name).ToList());
29+
}
30+
2631
public IActionResult Details(int? id)
2732
{
2833
if (id == null)

Blog/Blog/TagsHelper/PageLinkTagHelper.cs

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,36 +43,51 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
4343
tag.InnerHtml.AppendHtml(previousPage);
4444
}
4545

46+
CreateMiddlePages(urlHelper, tag);
47+
48+
if (PagingInformation.HasNextPage)
49+
{
50+
TagBuilder nextPage = CreateTag(PagingInformation.CurrentPage + 1, urlHelper, "Next");
51+
TagBuilder lastPage = CreateTag(PagingInformation.TotalPages, urlHelper, "Last Page");
52+
tag.InnerHtml.AppendHtml(nextPage);
53+
tag.InnerHtml.AppendHtml(lastPage);
54+
}
55+
56+
output.Content.AppendHtml(tag);
57+
}
58+
59+
private void CreateMiddlePages(IUrlHelper urlHelper, TagBuilder tag)
60+
{
4661
if (PagingInformation.TotalPages <= 6)
4762
{
4863
CreatePagination(tag, urlHelper, 1, PagingInformation.TotalPages);
4964
}
5065
else if (PagingInformation.TotalPages > 6)
5166
{
67+
int leftPages = PagingInformation.TotalPages - PagingInformation.CurrentPage;
68+
5269
if (PagingInformation.CurrentPage <= 3)
70+
{
5371
CreatePagination(tag, urlHelper, 1, 3);
72+
}
5473
else if (PagingInformation.CurrentPage > 3)
74+
{
5575
CreatePagination(tag, urlHelper, PagingInformation.CurrentPage - 3, PagingInformation.CurrentPage);
76+
}
5677

57-
int leftPages = PagingInformation.TotalPages - PagingInformation.CurrentPage;
58-
59-
if (leftPages > 0 && leftPages <= 2)
60-
CreatePagination(tag, urlHelper, PagingInformation.CurrentPage + 1, PagingInformation.CurrentPage + leftPages);
61-
else if (PagingInformation.CurrentPage <= 3)
78+
if (PagingInformation.CurrentPage <= 3)
79+
{
6280
CreatePagination(tag, urlHelper, PagingInformation.TotalPages - 2, PagingInformation.TotalPages);
81+
}
82+
else if (leftPages > 0 && leftPages <= 2)
83+
{
84+
CreatePagination(tag, urlHelper, PagingInformation.CurrentPage + 1, PagingInformation.CurrentPage + leftPages);
85+
}
6386
else if (leftPages > 0)
87+
{
6488
CreatePagination(tag, urlHelper, PagingInformation.CurrentPage + 1, PagingInformation.CurrentPage + 3);
89+
}
6590
}
66-
67-
if (PagingInformation.HasNextPage)
68-
{
69-
TagBuilder nextPage = CreateTag(PagingInformation.CurrentPage + 1, urlHelper, "Next");
70-
TagBuilder lastPage = CreateTag(PagingInformation.TotalPages, urlHelper, "Last Page");
71-
tag.InnerHtml.AppendHtml(nextPage);
72-
tag.InnerHtml.AppendHtml(lastPage);
73-
}
74-
75-
output.Content.AppendHtml(tag);
7691
}
7792

7893
private void CreatePagination(TagBuilder tag, IUrlHelper urlHelper, int start, int end)

Blog/Blog/Views/Home/Details.cshtml

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,10 @@
77
<section class="post">
88
<h1>@Model.Title</h1>
99
<p class="content">
10-
@Html.Raw(Markdig.Markdown.ToHtml(Model.Content))
11-
12-
<!--
13-
Html.Row() - Anything in between the ( ) will be rendered as actual HTML.
14-
Without this part all of our HTML tags would be automatically
15-
escaped, which means we'd essentially be showing out users HTML
16-
code instead of simple plain text.
17-
---------------------------------------------------------------------------------
18-
Markdig - We're specifying that we wish to use something from the Markdig
19-
namespace.
20-
---------------------------------------------------------------------------------
21-
Markdown - A static class inside of the Markdig namespace.
22-
---------------------------------------------------------------------------------
23-
ToHtml() - A method that turns everything between ( ) from markdown to HTML
24-
-->
10+
@Html.Raw(Model.Content)
2511

2612
</p>
27-
<a href="/">Back</a>
13+
<a href="/" class="btn btn-primary">Back</a>
2814
</section>
2915

3016
<section id="admin">

Blog/Blog/Views/Home/Index.cshtml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
</div>
6363
}
6464

65-
6665
<!-- PAGINATION -->
6766
<div paging-information="@Model.PagingInformation" page-action="Index" class="mx-auto"></div>
6867
</div>

Blog/Blog/Views/Posts/PostForm.cshtml

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,10 @@ else
2424
<input asp-for="Title" class="form-control" />
2525
<span asp-validation-for="Title" class="text-danger"></span>
2626
</div>
27-
<div class="form-group">
28-
<label asp-for="Excerpt" class="control-label"></label>
29-
<textarea asp-for="Excerpt" class="form-control"></textarea>
30-
<span asp-validation-for="Excerpt" class="text-danger"></span>
31-
</div>
32-
<div class="form-group">
33-
<label asp-for="Content" class="control-label"></label>
34-
<textarea asp-for="Content" class="form-control"></textarea>
35-
<span asp-validation-for="Content" class="text-danger"></span>
36-
</div>
27+
3728
<div class="form-group">
3829
<label asp-for="Tags" class="control-label"></label>
39-
<textarea asp-for="Tags" class="form-control"></textarea>
30+
<input asp-for="Tags" class="form-control" id="tags" />
4031
<span asp-validation-for="Tags" class="text-danger"></span>
4132
</div>
4233

@@ -45,11 +36,25 @@ else
4536
<input asp-for="Views" class="form-control" />
4637
<span asp-validation-for="Views" class="text-danger"></span>
4738
</div>
39+
40+
<div class="form-group">
41+
<label asp-for="Excerpt" class="control-label"></label>
42+
<textarea asp-for="Excerpt" class="form-control"></textarea>
43+
<span asp-validation-for="Excerpt" class="text-danger"></span>
44+
</div>
45+
46+
<div class="form-group">
47+
<label asp-for="Content" class="control-label"></label>
48+
<textarea asp-for="Content" id="content" class="form-control"></textarea>
49+
<span asp-validation-for="Content" class="text-danger"></span>
50+
</div>
51+
4852
<div class="form-group">
4953
<label asp-for="ImagePath" class="control-label"></label>
5054
<input type="file" name="image" class="form-control-file" />
5155
<span asp-validation-for="ImagePath" class="text-danger"></span>
5256
</div>
57+
5358
<div class="form-group form-check">
5459
<label class="form-check-label">
5560
<input class="form-check-input" asp-for="Public" /> @Html.DisplayNameFor(model => model.Public)
@@ -68,9 +73,26 @@ else
6873
<a asp-action="Index"><i class="fas fa-arrow-left"></i> Back to List</a>
6974
</div>
7075

71-
72-
7376
@section Scripts {
7477
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
78+
79+
<script src="~/lib/trumbowyg/trumbowyg.min.js"></script>
80+
<link rel="stylesheet" href="~/lib/trumbowyg/ui/trumbowyg.min.css">
81+
82+
<script src="~/lib/jquery-ui/jquery-ui-1.12.1/jquery-ui.min.js"></script>
83+
<link rel="stylesheet" href="~/lib/jquery-ui/jquery-ui-1.12.1/jquery-ui.min.css">
84+
85+
<script>
86+
$('#content').trumbowyg();
87+
</script>
88+
89+
<script type="text/javascript">
90+
$(document).ready(function () {
91+
$('#tags').autocomplete({
92+
source: '@Url.Action("GetTags", "Tags")',
93+
multiple: true
94+
});
95+
});
96+
</script>
7597
}
7698

0 commit comments

Comments
 (0)