Skip to content

Commit 3dff461

Browse files
committed
#348 SpaceBar Widget - offer user another choice to upload icons
1 parent 21f552f commit 3dff461

File tree

10 files changed

+104
-70
lines changed

10 files changed

+104
-70
lines changed

src/Modules/SimplCommerce.Module.Cms/Components/SpaceBarWidgetViewComponent.cs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
using Microsoft.AspNetCore.Mvc;
1+
using System.Collections.Generic;
2+
using Microsoft.AspNetCore.Mvc;
23
using Newtonsoft.Json;
34
using SimplCommerce.Module.Cms.ViewModels;
5+
using SimplCommerce.Module.Core.Services;
46
using SimplCommerce.Module.Core.ViewModels;
5-
using System;
6-
using System.Collections.Generic;
7-
using System.Linq;
8-
using System.Threading.Tasks;
97

108
namespace SimplCommerce.Module.Cms.Components
119
{
1210
public class SpaceBarWidgetViewComponent : ViewComponent
1311
{
12+
private IMediaService _mediaService;
13+
14+
public SpaceBarWidgetViewComponent(IMediaService mediaService)
15+
{
16+
_mediaService = mediaService;
17+
}
18+
1419
public IViewComponentResult Invoke(WidgetInstanceViewModel widgetInstance)
1520
{
1621
var model = new SpaceBarWidgetComponentVm
@@ -19,6 +24,17 @@ public IViewComponentResult Invoke(WidgetInstanceViewModel widgetInstance)
1924
WidgetName = widgetInstance.Name,
2025
Items = JsonConvert.DeserializeObject<List<SpaceBarWidgetSetting>>(widgetInstance.Data)
2126
};
27+
28+
foreach (var item in model.Items)
29+
{
30+
if (string.IsNullOrEmpty(item.Image))
31+
{
32+
continue;
33+
}
34+
35+
item.ImageUrl = _mediaService.GetMediaUrl(item.Image);
36+
}
37+
2238
return View("/Modules/SimplCommerce.Module.Cms/Views/Components/SpaceBarWidget.cshtml", model);
2339
}
2440
}

src/Modules/SimplCommerce.Module.Cms/Controllers/CarouselWidgetApiController.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Linq;
45
using System.Threading.Tasks;
56
using Microsoft.AspNetCore.Authorization;
67
using Microsoft.AspNetCore.Http;
@@ -12,7 +13,6 @@
1213
using SimplCommerce.Module.Cms.ViewModels;
1314
using SimplCommerce.Module.Core.Models;
1415
using SimplCommerce.Module.Core.Services;
15-
using System.Linq;
1616

1717
namespace SimplCommerce.Module.Cms.Controllers
1818
{
@@ -35,7 +35,6 @@ public CarouselWidgetApiController(IRepository<WidgetInstance> widgetInstanceRep
3535
public async Task<IActionResult> Get(long id)
3636
{
3737
var totalWidgets = _widgetInstanceRepository.Query().ToList().Count();
38-
3938
var widgetInstance = await _widgetInstanceRepository.Query().FirstOrDefaultAsync(x => x.Id == id);
4039
var model = new CarouselWidgetForm
4140
{
@@ -82,6 +81,7 @@ public async Task<IActionResult> Post(IFormCollection formCollection)
8281
await _widgetInstanceRepository.SaveChangesAsync();
8382
return CreatedAtAction(nameof(Get), new { id = widgetInstance.Id }, null);
8483
}
84+
8585
return BadRequest(ModelState);
8686
}
8787

@@ -126,18 +126,16 @@ public async Task<IActionResult> Put(long id, IFormCollection formCollection)
126126

127127
private CarouselWidgetForm ToCarouselWidgetFormModel(IFormCollection formCollection)
128128
{
129-
DateTimeOffset publishStart;
130-
DateTimeOffset publishEnd;
131129
var model = new CarouselWidgetForm();
132130
model.Name = formCollection["name"];
133131
model.WidgetZoneId = int.Parse(formCollection["widgetZoneId"]);
134132
model.DisplayOrder = int.Parse(formCollection["displayOrder"]);
135-
if (DateTimeOffset.TryParse(formCollection["publishStart"], out publishStart))
133+
if (DateTimeOffset.TryParse(formCollection["publishStart"], out DateTimeOffset publishStart))
136134
{
137135
model.PublishStart = publishStart;
138136
}
139137

140-
if(DateTimeOffset.TryParse(formCollection["publishEnd"], out publishEnd))
138+
if(DateTimeOffset.TryParse(formCollection["publishEnd"], out DateTimeOffset publishEnd))
141139
{
142140
model.PublishEnd = publishEnd;
143141
}

src/Modules/SimplCommerce.Module.Cms/Controllers/SpaceBarWidgetApiContorller.cs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
using Microsoft.AspNetCore.Authorization;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Authorization;
27
using Microsoft.AspNetCore.Http;
38
using Microsoft.AspNetCore.Mvc;
49
using Microsoft.Net.Http.Headers;
@@ -7,11 +12,6 @@
712
using SimplCommerce.Module.Cms.ViewModels;
813
using SimplCommerce.Module.Core.Models;
914
using SimplCommerce.Module.Core.Services;
10-
using System;
11-
using System.Collections.Generic;
12-
using System.IO;
13-
using System.Linq;
14-
using System.Threading.Tasks;
1515

1616
namespace SimplCommerce.Module.Cms.Controllers
1717
{
@@ -44,10 +44,17 @@ public IActionResult Get(long id)
4444
DisplayOrder = widgetInstance.DisplayOrder,
4545
Items = JsonConvert.DeserializeObject<IList<SpaceBarWidgetSetting>>(widgetInstance.Data)
4646
};
47+
4748
foreach (var item in model.Items)
4849
{
50+
if(string.IsNullOrEmpty(item.Image))
51+
{
52+
continue;
53+
}
54+
4955
item.ImageUrl = _mediaService.GetMediaUrl(item.Image);
5056
}
57+
5158
return Json(model);
5259
}
5360

@@ -72,12 +79,15 @@ public async Task<IActionResult> Post(IFormCollection formCollection)
7279
DisplayOrder = model.DisplayOrder,
7380
Data = JsonConvert.SerializeObject(model.Items)
7481
};
82+
7583
_widgetInstanceRepository.Add(widgetInstance);
7684
_widgetInstanceRepository.SaveChanges();
77-
return Ok();
85+
return CreatedAtAction(nameof(Get), new { id = widgetInstance.Id }, null);
7886
}
79-
return new BadRequestObjectResult(ModelState);
87+
88+
return BadRequest(ModelState);
8089
}
90+
8191
[HttpPut("{id}")]
8292
public async Task<IActionResult> Put(long id, IFormCollection formCollection)
8393
{
@@ -94,6 +104,7 @@ public async Task<IActionResult> Put(long id, IFormCollection formCollection)
94104
item.Image = await SaveFile(item.UploadImage);
95105
}
96106
}
107+
97108
if (ModelState.IsValid)
98109
{
99110
var widgetInstance = _widgetInstanceRepository.Query().FirstOrDefault(x => x.Id == id);
@@ -104,26 +115,28 @@ public async Task<IActionResult> Put(long id, IFormCollection formCollection)
104115
widgetInstance.DisplayOrder = model.DisplayOrder;
105116
widgetInstance.Data = JsonConvert.SerializeObject(model.Items);
106117
_widgetInstanceRepository.SaveChanges();
107-
return Ok();
118+
return Accepted();
108119
}
109-
return new BadRequestObjectResult(ModelState);
120+
121+
return BadRequest(ModelState);
110122
}
123+
111124
private SpaceBarWidgetForm ToSpaceBarWidgetFormModel(IFormCollection formCollection)
112125
{
113-
DateTimeOffset publishStart;
114-
DateTimeOffset publishEnd;
115126
var model = new SpaceBarWidgetForm();
116127
model.Name = formCollection["name"];
117128
model.WidgetZoneId = int.Parse(formCollection["widgetZoneId"]);
118129
model.DisplayOrder = int.Parse(formCollection["displayOrder"]);
119-
if (DateTimeOffset.TryParse(formCollection["publishStart"], out publishStart))
130+
if (DateTimeOffset.TryParse(formCollection["publishStart"], out DateTimeOffset publishStart))
120131
{
121132
model.PublishStart = publishStart;
122133
}
123-
if (DateTimeOffset.TryParse(formCollection["publishEnd"], out publishEnd))
134+
135+
if (DateTimeOffset.TryParse(formCollection["publishEnd"], out DateTimeOffset publishEnd))
124136
{
125137
model.PublishEnd = publishEnd;
126138
}
139+
127140
int numberOfItems = int.Parse(formCollection["numberOfItems"]);
128141
for (var i = 0; i < numberOfItems; i++)
129142
{
@@ -135,8 +148,10 @@ private SpaceBarWidgetForm ToSpaceBarWidgetFormModel(IFormCollection formCollect
135148
item.UploadImage = formCollection.Files[$"items[{i}][uploadImage]"];
136149
model.Items.Add(item);
137150
}
151+
138152
return model;
139153
}
154+
140155
private async Task<string> SaveFile(IFormFile file)
141156
{
142157
var originalFileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Value.Trim('"');
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
1+
using System.Collections.Generic;
52

63
namespace SimplCommerce.Module.Cms.ViewModels
74
{
85
public class SpaceBarWidgetComponentVm
96
{
107
public long Id { get; set; }
8+
119
public string WidgetName { get; set; }
10+
1211
public List<SpaceBarWidgetSetting> Items { get; set; }
1312
}
1413
}

src/Modules/SimplCommerce.Module.Cms/ViewModels/SpaceBarWidgetForm.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
using SimplCommerce.Module.Core.ViewModels;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Threading.Tasks;
1+
using System.Collections.Generic;
2+
using SimplCommerce.Module.Core.ViewModels;
63

74
namespace SimplCommerce.Module.Cms.ViewModels
85
{

src/Modules/SimplCommerce.Module.Cms/ViewModels/SpaceBarWidgetSetting.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
using Microsoft.AspNetCore.Http;
22
using Newtonsoft.Json;
3-
using System;
4-
using System.Collections.Generic;
5-
using System.Linq;
6-
using System.Threading.Tasks;
73

84
namespace SimplCommerce.Module.Cms.ViewModels
95
{
106
public class SpaceBarWidgetSetting
117
{
128
public string IconHtml { get; set; }
9+
1310
public string Title { get; set; }
11+
1412
public string Description { get; set; }
13+
1514
public string Image { get; set; }
1615

1716
public string ImageUrl { get; set; }
Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,24 @@
11
@model SimplCommerce.Module.Cms.ViewModels.SpaceBarWidgetComponentVm
2+
23
<div align="center" class="row">
34
@foreach (var item in Model.Items)
45
{
5-
@if (!(string.IsNullOrEmpty(item.IconHtml)))
6-
{
7-
<div class="col-md-4 col-sm-6 col-xs-12">
6+
<div class="col-md-4 col-sm-6 col-xs-12">
7+
@if (!string.IsNullOrEmpty(item.ImageUrl))
8+
{
9+
<img src="@item.ImageUrl" alt="" class="img-responsive" />
10+
}
11+
else if(!(string.IsNullOrEmpty(item.IconHtml)))
12+
{
813
<i class='@item.IconHtml space-bar-widget-icon' aria-hidden='true'></i>
9-
<div class="portfolio-overlay">
10-
<h4>@item.Title</h4>
11-
<p>
12-
@item.Description
13-
</p>
14-
</div>
15-
</div>
16-
}
17-
else
18-
{
19-
<div class="col-md-4 col-sm-6 col-xs-12">
20-
<i aria-hidden='true'><img src="/user-content/@item.Image" alt="" style="width: 150px; height: 150px;" class="img-responsive" /></i>
21-
<div class="portfolio-overlay">
22-
<h4>@item.Title</h4>
23-
<p>
24-
@item.Description
25-
</p>
26-
</div>
14+
}
15+
<div class="portfolio-overlay">
16+
<h4>@item.Title</h4>
17+
<p>
18+
@item.Description
19+
</p>
2720
</div>
28-
}
21+
</div>
2922
}
3023
</div>
3124

src/Modules/SimplCommerce.Module.Cms/wwwroot/admin/spacebar-widget/spacebar-widget-form.html

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ <h2 ng-if="vm.isEditMode">{{::vm.translate.get('Edit SpaceBar Widget')}}</h2>
5757
<div class="col-sm-10">
5858
<div class="form-group row tabbable-panel-line" ng-repeat="item in vm.widgetInstance.items">
5959
<div class="col-md-11">
60+
<div class="form-group">
61+
<label class="col-sm-2 control-label">{{::vm.translate.get('Image')}}</label>
62+
<div class="col-sm-10">
63+
<input class="form-control" type="file" ngf-select ng-model="item.uploadImage" name="images" ngf-accept="'image/*'">
64+
<img ngf-src="item.uploadImage" ngf-resize="{width: 100, height: 100, quality: 0.9}">
65+
<img ng-show="!item.uploadImage && item.imageUrl" ng-src="{{item.imageUrl}}" style="width: 320px; height: 100px" />
66+
</div>
67+
</div>
6068
<div class="form-group">
6169
<label class="col-sm-2 control-label">{{::vm.translate.get('Icon:')}}</label>
6270
<div class="col-sm-10">
@@ -75,14 +83,6 @@ <h2 ng-if="vm.isEditMode">{{::vm.translate.get('Edit SpaceBar Widget')}}</h2>
7583
<input ng-model="item.description" class="form-control" />
7684
</div>
7785
</div>
78-
<div class="form-group">
79-
<label class="col-sm-2 control-label">{{::vm.translate.get('Image')}}</label>
80-
<div class="col-sm-10">
81-
<input class="form-control" type="file" ngf-select ng-model="item.uploadImage" name="images" ngf-accept="'image/*'">
82-
<img ngf-src="item.uploadImage" ngf-resize="{width: 100, height: 100, quality: 0.9}">
83-
<img ng-show="!item.uploadImage && item.imageUrl" ng-src="{{item.imageUrl}}" style="width: 320px; height: 100px" />
84-
</div>
85-
</div>
8686
</div>
8787
<div class="col-md-1 text-right">
8888
<button ng-click="vm.removeItem(item)" type="button" class="btn btn-default btn-xs">

src/Modules/SimplCommerce.Module.Cms/wwwroot/admin/spacebar-widget/spacebar-widget-form.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
angular
44
.module('simplAdmin.cms')
55
.controller('SpaceBarWidgetFormCtrl', SpaceBarWidgetFormCtrl);
6+
67
/* @ngInject */
78
function SpaceBarWidgetFormCtrl($state, $stateParams, spacebarWidgetService, translateService) {
89
var vm = this;
@@ -17,15 +18,23 @@
1718
vm.openCalendar = function (e, picker) {
1819
vm[picker].open = true;
1920
};
21+
2022
vm.addItem = function addItem() {
2123
vm.widgetInstance.items.push({});
2224
}
25+
2326
vm.removeItem = function removeItem(item) {
2427
var index = vm.widgetInstance.items.indexOf(item);
2528
vm.widgetInstance.items.splice(index, 1);
2629
}
30+
2731
vm.save = function save() {
2832
var promise;
33+
// ng-upload will post null as text
34+
angular.forEach(vm.widgetInstance.items, function (item) {
35+
item.image = item.image === null ? '' : item.image;
36+
});
37+
2938
if (vm.isEditMode) {
3039
promise = spacebarWidgetService.editSpaceBarWidget(vm.widgetInstance);
3140
} else {
@@ -46,7 +55,8 @@
4655
vm.validationErrors.push('Could not carousel widget.');
4756
}
4857
});
49-
};
58+
};
59+
5060
function init() {
5161
spacebarWidgetService.getWidgetZones().then(function (result) {
5262
vm.widgetZones = result.data;
@@ -75,6 +85,7 @@
7585
});
7686
}
7787
}
88+
7889
init();
7990
}
8091
})(jQuery);

0 commit comments

Comments
 (0)