Skip to content

Commit ba7aa33

Browse files
committed
Html widget updates
1 parent 2226096 commit ba7aa33

File tree

8 files changed

+77
-64
lines changed

8 files changed

+77
-64
lines changed

plugins/Common/Views/Widgets/HtmlBlock/Edit.cshtml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
@model Core.Data.Widget
1+
@model Core.Data.ThemeWidget
2+
@inject Core.Services.IDataService _db
23
@{
3-
var prop = Model.Props.Where(p => p.Id == "Title").FirstOrDefault();
4-
string title = prop == null ? "HTML Widget" : prop.Value;
4+
var existing = _db.HtmlWidgets.Single(w => w.Name == Model.Widget.Title && w.Theme == Model.Theme && w.Author == "0");
5+
var content = existing == null ? "" : existing.Content;
56
}
6-
<h4>@title</h4>
7-
<form id="usrform">
7+
<h4>@Model.Widget.Title</h4>
8+
<form id="usrform" method="post" action="~/widgets/api/htmlblock/edit">
89
<div class="form-group">
9-
<textarea rows="4" name="txtHtml" class="form-control"></textarea>
10+
<input type="hidden" id="txtWidget" name="txtWidget" value="@Model.Widget.Title" />
11+
<input type="hidden" id="txtTheme" name="txtTheme" value="@Model.Theme" />
12+
<textarea rows="4" id="txtHtml" name="txtHtml" class="form-control">@content</textarea>
1013
</div>
1114
<div class="form-group">
1215
<button type="submit" class="btn btn-primary btn-main">Save</button>

plugins/Common/Widgets/HtmlBlock.cs

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,17 @@ public HtmlBlock(IDataService db)
1313
_db = db;
1414
}
1515

16-
public IViewComponentResult Invoke(string id, string theme, string author)
16+
public IViewComponentResult Invoke(string theme, string widget)
1717
{
18-
string model = @"<ul class=""blog-social nav ml-auto my-auto"">
19-
<li class=""blog-social-item""><a href=""#"" target=""_blank"" class=""blog-social-link""><i class=""blog-social-icon fa fa-twitter""></i></a></li>
20-
<li class=""blog-social-item""><a href=""#"" target=""_blank"" class=""blog-social-link""><i class=""blog-social-icon fa fa-google-plus""></i></a></li>
21-
<li class=""blog-social-item""><a href=""#"" target=""_blank"" class=""blog-social-link""><i class=""blog-social-icon fa fa-facebook-official""></i></a></li>
22-
</ul>";
23-
24-
var existing = _db.HtmlWidgets.Single(w => w.Name == id && w.Theme == theme && w.Author == author);
18+
string model = "";
19+
var existing = _db.HtmlWidgets.Single(w => w.Name == widget && w.Theme == theme && w.Author == "0");
2520

2621
if (existing == null)
2722
{
2823
_db.HtmlWidgets.Add(new Core.Data.HtmlWidget {
29-
Name = id,
24+
Name = widget,
3025
Theme = theme,
31-
Author = author,
26+
Author = "0",
3227
Content = model
3328
});
3429
_db.Complete();
@@ -41,4 +36,39 @@ public IViewComponentResult Invoke(string id, string theme, string author)
4136
return View("~/Views/Widgets/HtmlBlock/Index.cshtml", model);
4237
}
4338
}
39+
40+
[Route("widgets/api/htmlblock")]
41+
public class HtmlBlockController : Controller
42+
{
43+
IDataService _db;
44+
45+
public HtmlBlockController(IDataService db)
46+
{
47+
_db = db;
48+
}
49+
50+
[HttpPost]
51+
[Route("edit")]
52+
public IActionResult Edit(string txtWidget, string txtTheme, string txtHtml)
53+
{
54+
var existing = _db.HtmlWidgets.Single(w => w.Name == txtWidget && w.Theme == txtTheme && w.Author == "0");
55+
56+
if (existing == null)
57+
{
58+
_db.HtmlWidgets.Add(new Core.Data.HtmlWidget
59+
{
60+
Name = txtWidget,
61+
Theme = txtTheme,
62+
Author = "0",
63+
Content = txtHtml
64+
});
65+
}
66+
else
67+
{
68+
existing.Content = txtHtml;
69+
}
70+
_db.Complete();
71+
return Redirect("~/admin/settings/themes");
72+
}
73+
}
4474
}

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
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-
@if (Model.Widgets != null && Model.Widgets.Widgets.Count > 0)
26+
@if (Model.Widgets != null && Model.Widgets.Count > 0)
2727
{
2828
<a class="item-show ml-3" href="#" onclick="return openSettings('@theme.Title')" data-tooltip="" title="" data-original-title="settings">
2929
<i class="fas fa-sliders-h"></i>
@@ -65,17 +65,18 @@
6565
<div class="settings-body">
6666
@if (Model.Widgets != null)
6767
{
68-
foreach (var widget in Model.Widgets.Widgets)
68+
foreach (var widget in Model.Widgets)
6969
{
70-
var path = $"~/Views/Widgets/{widget.Name}/Edit.cshtml";
71-
await Html.RenderPartialAsync(path, widget);
70+
var path = $"~/Views/Widgets/{widget.Widget}/Edit.cshtml";
71+
await Html.RenderPartialAsync(path,
72+
new ThemeWidget {
73+
Theme = Model.BlogItem.Theme,
74+
Widget = widget
75+
});
7276
}
7377
}
7478
</div>
7579
</div>
76-
<div class="modal-footer d-flex">
77-
<button class="btn btn-link btn-sm btn-rounded ml-auto" type="button" data-dismiss="modal">cancel</button>
78-
</div>
7980
</div>
8081
</div>
8182
</div>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class ThemesModel : AdminPageModel
2020

2121
public IEnumerable<ThemeItem> Themes { get; set; }
2222
public BlogItem BlogItem { get; set; }
23-
public WidgetsModel Widgets { get; set; }
23+
public List<WidgetItem> Widgets { get; set; }
2424

2525
public ThemesModel(IDataService db, IStorageService storage, INotificationService ns)
2626
{
@@ -110,7 +110,7 @@ async Task SetModel()
110110
using (StreamReader r = new StreamReader(jsonFile))
111111
{
112112
string json = r.ReadToEnd();
113-
Widgets = JsonConvert.DeserializeObject<WidgetsModel>(json);
113+
Widgets = JsonConvert.DeserializeObject<List<WidgetItem>>(json);
114114
}
115115
}
116116
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@
1313
</form>
1414

1515
<div class="card my-4">
16-
<h5 class="card-header">Side Widget</h5>
16+
<h5 class="card-header">Static Widget</h5>
1717
<div class="card-body">
18-
You can put anything you want inside of these side widgets. They are easy to use, and feature the new Bootstrap 4 card containers!
18+
This widget has static HTML that theme author put in. It is not updatable and can only display <b>static</b> HTML text.
19+
To update it, blogger would need to update theme itself.
1920
</div>
2021
</div>
2122

2223
<div class="card my-4">
23-
<h5 class="card-header">PostList Widget</h5>
24+
<h5 class="card-header">Dynamic Widget</h5>
2425
<div class="card-body">
25-
@await Component.InvokeAsync("PostList")
26+
@await Component.InvokeAsync("HtmlBlock", new { theme = "Simple", widget = "Sidebar" })
2627
</div>
2728
</div>
Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,6 @@
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-
"Id": "Title",
22-
"Value": "Social Buttons"
23-
}
24-
]
25-
}
26-
]
27-
}
1+
[
2+
{
3+
"Widget": "HtmlBlock",
4+
"Title": "Sidebar"
5+
}
6+
]

src/App/app.db

0 Bytes
Binary file not shown.

src/Core/Data/Models/WidgetsModel.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@ namespace Core.Data
44
{
55
public class WidgetsModel
66
{
7-
public string Name { get; set; }
8-
public List<Widget> Widgets { get; set; }
7+
public List<WidgetItem> Widgets { get; set; }
98
}
109

11-
public class Prop
10+
public class WidgetItem
1211
{
13-
public string Id { get; set; }
14-
public string Value { get; set; }
12+
public string Widget { get; set; }
13+
public string Title { get; set; }
1514
}
1615

17-
public class Widget
16+
public class ThemeWidget
1817
{
19-
public string Name { get; set; }
20-
public List<Prop> Props { get; set; }
18+
public string Theme { get; set; }
19+
public WidgetItem Widget { get; set; }
2120
}
2221
}

0 commit comments

Comments
 (0)