| | 1 | | namespace Songhay.Extensions; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// Extensions of <see cref="MenuDisplayItemModel"/> |
| | 5 | | /// </summary> |
| | 6 | | public static class MenuDisplayItemModelExtensions |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Returns <c>true</c> when the grouping has the specified identifier. |
| | 10 | | /// </summary> |
| | 11 | | /// <param name="data"></param> |
| | 12 | | /// <param name="groupId"></param> |
| 0 | 13 | | public static bool HasGroupId(this MenuDisplayItemModel? data, string groupId) => data != null && |
| 0 | 14 | | (!string.IsNullOrWhiteSpace(groupId) && data.GroupId.EqualsInvariant(groupId)); |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// Returns the Default Selection |
| | 18 | | /// <c>IsDefaultSelection == true</c> |
| | 19 | | /// or the First <see cref="MenuDisplayItemModel"/>. |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="data">The data.</param> |
| | 22 | | public static MenuDisplayItemModel? DefaultOrFirst(this IEnumerable<MenuDisplayItemModel>? data) |
| 0 | 23 | | { |
| 0 | 24 | | if (data == null) return null; |
| | 25 | |
|
| 0 | 26 | | var snapshot = data as MenuDisplayItemModel[] ?? data.ToArray(); |
| | 27 | |
|
| 0 | 28 | | return snapshot.Any(i => i.IsDefaultSelection == true) |
| 0 | 29 | | ? snapshot.FirstOrDefault(i => i.IsDefaultSelection == true) |
| 0 | 30 | | : snapshot.FirstOrDefault(); |
| 0 | 31 | | } |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Fluently returns <see cref="MenuDisplayItemModel" /> with child item. |
| | 35 | | /// </summary> |
| | 36 | | /// <param name="data">The data.</param> |
| | 37 | | /// <param name="child">The child.</param> |
| | 38 | | public static MenuDisplayItemModel? WithChildItem(this MenuDisplayItemModel? data, MenuDisplayItemModel? child) |
| 0 | 39 | | { |
| 0 | 40 | | if (data == null) return null; |
| 0 | 41 | | if (child == null) return data; |
| | 42 | |
|
| 0 | 43 | | data.ChildItems = new[] {child}; |
| | 44 | |
|
| 0 | 45 | | return data; |
| 0 | 46 | | } |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Fluently returns <see cref="MenuDisplayItemModel" /> with child items. |
| | 50 | | /// </summary> |
| | 51 | | /// <param name="data">The data.</param> |
| | 52 | | /// <param name="items">The items.</param> |
| | 53 | | public static MenuDisplayItemModel? WithChildItems(this MenuDisplayItemModel? data, |
| | 54 | | IEnumerable<MenuDisplayItemModel>? items) |
| 0 | 55 | | { |
| 0 | 56 | | if (data == null) return null; |
| 0 | 57 | | if (items == null) return data; |
| | 58 | |
|
| 0 | 59 | | var snapshot = items as MenuDisplayItemModel[] ?? items.ToArray(); |
| | 60 | |
|
| 0 | 61 | | data.ChildItems = snapshot.ToArray(); |
| | 62 | |
|
| 0 | 63 | | return data; |
| 0 | 64 | | } |
| | 65 | | } |