This repository has been archived by the owner on Sep 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
WidgetRegistry.cs
50 lines (42 loc) · 1.51 KB
/
WidgetRegistry.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;
namespace LeeConlin.Kentico12.MVC.WidgetResolver
{
public class WidgetRegistry : IWidgetRegistry
{
private List<WidgetRegistration> Registry { get; set; }
public WidgetRegistry()
{
Registry = new List<WidgetRegistration>();
}
/// <inheritdoc />
public IWidgetRegistry RegisterWidget<TWidgetModel>(string codename, Func<JObject, JObject> beforeAction = null, Func<IWidgetModel, IWidgetModel> afterAction = null)
where TWidgetModel : IWidgetModel
{
if (Registry.Any(x => x.Codename == codename))
{
var item = Registry.First(x => x.Codename == codename);
Registry.Remove(item);
}
Registry.Add(new WidgetRegistration(codename, typeof(TWidgetModel), beforeAction, afterAction));
return this;
}
/// <inheritdoc />
public Type GetWidgetType(string codename)
{
return Registry.FirstOrDefault(x => x.Codename == codename)?.Type;
}
/// <inheritdoc />
public Delegate GetBeforeAction(string codename)
{
return Registry.FirstOrDefault(x => x.Codename == codename)?.BeforeAction;
}
/// <inheritdoc />
public Delegate GetAfterAction(string codename)
{
return Registry.FirstOrDefault(x => x.Codename == codename)?.AfterAction;
}
}
}