Skip to content

Commit 7c93d34

Browse files
committed
Widgets model added
1 parent 19773a5 commit 7c93d34

File tree

4 files changed

+77
-15
lines changed

4 files changed

+77
-15
lines changed

src/App/Pages/Admin/Settings/Themes.cshtml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323
<a class="btn-unstyled item-favorite ml-3" data-tooltip="" title="" data-original-title="default">
2424
<i class="fas fa-star"></i>
2525
</a>
26-
<a class="item-show ml-3" href="#" onclick="return openSettings('@theme.Title')" data-tooltip="" title="" data-original-title="settings">
27-
<i class="fas fa-sliders-h"></i>
28-
</a>
26+
@if (Model.Widgets != null && Model.Widgets.Widgets.Count > 0)
27+
{
28+
<a class="item-show ml-3" href="#" onclick="return openSettings('@theme.Title')" data-tooltip="" title="" data-original-title="settings">
29+
<i class="fas fa-sliders-h"></i>
30+
</a>
31+
}
2932
}
3033
else
3134
{

src/App/Pages/Admin/Settings/Themes.cshtml.cs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Core.Data;
33
using Core.Services;
44
using Microsoft.AspNetCore.Mvc;
5+
using Newtonsoft.Json;
56
using System;
67
using System.Collections.Generic;
78
using System.IO;
@@ -15,9 +16,11 @@ public class ThemesModel : AdminPageModel
1516
IDataService _db;
1617
IStorageService _storage;
1718
INotificationService _ns;
19+
string slash = Path.DirectorySeparatorChar.ToString();
1820

1921
public IEnumerable<ThemeItem> Themes { get; set; }
2022
public BlogItem BlogItem { get; set; }
23+
public WidgetsModel Widgets { get; set; }
2124

2225
public ThemesModel(IDataService db, IStorageService storage, INotificationService ns)
2326
{
@@ -28,30 +31,22 @@ public ThemesModel(IDataService db, IStorageService storage, INotificationServic
2831

2932
public async Task<IActionResult> OnGetAsync()
3033
{
31-
var author = await _db.Authors.GetItem(a => a.AppUserName == User.Identity.Name);
32-
IsAdmin = author.IsAdmin;
33-
34-
Notifications = await _ns.GetNotifications(author.Id);
34+
await SetModel();
3535

3636
if (!IsAdmin)
3737
return RedirectToPage("../Shared/_Error", new { code = 403 });
3838

39-
BlogItem = await _db.CustomFields.GetBlogSettings();
40-
Themes = GetThemes();
41-
4239
return Page();
4340
}
4441

4542
public async Task<IActionResult> OnPostAsync()
4643
{
4744
string act = Request.Form["hdnAct"];
4845
string id = Request.Form["hdnTheme"];
49-
46+
5047
var author = await _db.Authors.GetItem(a => a.AppUserName == User.Identity.Name);
5148
IsAdmin = author.IsAdmin;
5249

53-
Notifications = await _ns.GetNotifications(author.Id);
54-
5550
if (!IsAdmin)
5651
return RedirectToPage("../Shared/_Error", new { code = 403 });
5752

@@ -74,7 +69,7 @@ public async Task<IActionResult> OnPostAsync()
7469

7570
if (act == "del" && !string.IsNullOrEmpty(id))
7671
{
77-
var slash = Path.DirectorySeparatorChar.ToString();
72+
7873
var themeContent = $"{AppSettings.WebRootPath}{slash}themes{slash}{id.ToLower()}";
7974
var themeViews = $"{AppSettings.ContentRootPath}{slash}Views{slash}Themes{slash}{id}";
8075

@@ -94,10 +89,30 @@ public async Task<IActionResult> OnPostAsync()
9489
}
9590
}
9691

92+
await SetModel();
93+
94+
return Page();
95+
}
96+
97+
async Task SetModel()
98+
{
99+
var author = await _db.Authors.GetItem(a => a.AppUserName == User.Identity.Name);
100+
IsAdmin = author.IsAdmin;
101+
102+
Notifications = await _ns.GetNotifications(author.Id);
103+
97104
BlogItem = await _db.CustomFields.GetBlogSettings();
98105
Themes = GetThemes();
99106

100-
return Page();
107+
string jsonFile = $"{AppSettings.ContentRootPath}{slash}Views{slash}Themes{slash}{BlogItem.Theme}{slash}{BlogItem.Theme}.json";
108+
if (System.IO.File.Exists(jsonFile))
109+
{
110+
using (StreamReader r = new StreamReader(jsonFile))
111+
{
112+
string json = r.ReadToEnd();
113+
Widgets = JsonConvert.DeserializeObject<WidgetsModel>(json);
114+
}
115+
}
101116
}
102117

103118
List<ThemeItem> GetThemes()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"Name": "Simple",
3+
"Widgets": [
4+
{
5+
"Name": "PostList",
6+
"Props": [
7+
{
8+
"Id": "one",
9+
"Value": "1"
10+
},
11+
{
12+
"Id": "two",
13+
"Value": "2"
14+
}
15+
]
16+
},
17+
{
18+
"Name": "HtmlBlock",
19+
"Props": []
20+
}
21+
]
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Collections.Generic;
2+
3+
namespace Core.Data
4+
{
5+
public class WidgetsModel
6+
{
7+
public string Name { get; set; }
8+
public List<Widget> Widgets { get; set; }
9+
}
10+
11+
public class Prop
12+
{
13+
public string Id { get; set; }
14+
public string Value { get; set; }
15+
}
16+
17+
public class Widget
18+
{
19+
public string Name { get; set; }
20+
public List<Prop> Props { get; set; }
21+
}
22+
}

0 commit comments

Comments
 (0)