Skip to content

Commit 584561c

Browse files
committed
Categories widget
1 parent 441ec54 commit 584561c

File tree

7 files changed

+108
-1
lines changed

7 files changed

+108
-1
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
@model Core.Data.ThemeWidget
2+
@inject Core.Services.IDataService _db
3+
@{
4+
var keyTmpl = $"{Model.Theme}-{Model.Widget.Title}-tmpl";
5+
var keyMax = $"{Model.Theme}-{Model.Widget.Title}-max";
6+
7+
var template = _db.CustomFields.GetCustomValue(keyTmpl);
8+
var maxRecords = _db.CustomFields.GetCustomValue(keyMax);
9+
10+
if (string.IsNullOrEmpty(maxRecords)) { maxRecords = "10"; }
11+
if (string.IsNullOrEmpty(template)) { template = "<a href=\"/categories/{0}\" class=\"list-group-item list-group-item-action\">{0}</a>"; }
12+
}
13+
<form method="post" action="~/widgets/api/postlist/edit" asp-antiforgery="true">
14+
<div class="form-group">
15+
<label class="form-group-label">Template ({0} = slug {1} = title)</label>
16+
<textarea rows="2" id="txtTemplate" name="txtTemplate" class="form-control">@template</textarea>
17+
</div>
18+
<div class="form-group">
19+
<label class="form-group-label">Max records</label>
20+
<input type="text" id="txtMaxRecords" name="txtMaxRecords" class="form-control" value="@maxRecords" />
21+
</div>
22+
<div class="form-group">
23+
<button type="submit" class="btn btn-primary btn-main">Save</button>
24+
</div>
25+
<input type="hidden" id="hdnWidget" name="hdnWidget" value="@Model.Widget.Title" />
26+
<input type="hidden" id="hdnTheme" name="hdnTheme" value="@Model.Theme" />
27+
</form>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@{
2+
foreach (var cat in Model)
3+
{
4+
@Html.Raw(string.Format(cat))
5+
}
6+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using Core.Services;
2+
using Microsoft.AspNetCore.Mvc;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
7+
namespace Common.Widgets
8+
{
9+
[ViewComponent(Name = "Categories")]
10+
public class Categories : ViewComponent
11+
{
12+
IDataService _db;
13+
14+
public Categories(IDataService db)
15+
{
16+
_db = db;
17+
}
18+
19+
public async Task<IViewComponentResult> InvokeAsync(string theme, string widget)
20+
{
21+
var keyMax = $"{theme}-{widget}-max";
22+
var keyTmpl = $"{theme}-{widget}-tmpl";
23+
24+
var max = _db.CustomFields.GetCustomValue(keyMax);
25+
var tmpl = _db.CustomFields.GetCustomValue(keyTmpl);
26+
27+
if (string.IsNullOrEmpty(max)) max = "10";
28+
if (string.IsNullOrEmpty(tmpl)) tmpl = "<a href=\"/categories/{0}\" class=\"list-group-item list-group-item-action\">{0}</a>";
29+
30+
var cats = await _db.BlogPosts.Categories();
31+
var model = new List<string>();
32+
33+
foreach (var cat in cats.Take(int.Parse(max)))
34+
{
35+
model.Add(string.Format(tmpl, cat));
36+
}
37+
38+
return View("~/Views/Widgets/Categories/Index.cshtml", model);
39+
}
40+
}
41+
42+
[Route("widgets/api/categories")]
43+
public class CategoriesController : Controller
44+
{
45+
IDataService _db;
46+
47+
public CategoriesController(IDataService db)
48+
{
49+
_db = db;
50+
}
51+
52+
[HttpPost]
53+
[Route("edit")]
54+
public IActionResult Edit(string selAuthors, string selCats, string txtMaxRecords, string txtTemplate, string hdnWidget, string hdnTheme)
55+
{
56+
var keyMax = $"{hdnTheme}-{hdnWidget}-max";
57+
var keyTmpl = $"{hdnTheme}-{hdnWidget}-tmpl";
58+
59+
_db.CustomFields.SaveCustomValue(keyMax, txtMaxRecords);
60+
_db.CustomFields.SaveCustomValue(keyTmpl, txtTemplate);
61+
62+
return Redirect("~/admin/settings/themes");
63+
}
64+
}
65+
}

src/App/Views/Themes/Simple/Shared/_Sidebar.cshtml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,9 @@
3030
<div class="card my-4">
3131
<h5 class="card-header">Post List</h5>
3232
@await Component.InvokeAsync("PostList", new { theme = "Simple", widget = "Post List" })
33+
</div>
34+
35+
<div class="card my-4">
36+
<h5 class="card-header">Categories</h5>
37+
@await Component.InvokeAsync("Categories", new { theme = "Simple", widget = "Categories" })
3338
</div>

src/App/Views/Themes/Simple/Simple.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@
66
{
77
"Widget": "PostList",
88
"Title": "Post List"
9+
},
10+
{
11+
"Widget": "Categories",
12+
"Title": "Categories"
913
}
1014
]

src/App/app.db

0 Bytes
Binary file not shown.

src/Core/Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.1</TargetFramework>
5-
<Version>2.1.1.1</Version>
5+
<Version>2.1.1.2</Version>
66
</PropertyGroup>
77

88
<ItemGroup>

0 commit comments

Comments
 (0)