-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathBreadCrumbTrail.cs
144 lines (118 loc) · 4.45 KB
/
BreadCrumbTrail.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//-----------------------------------------------------------------------
// <copyright file="BreadCrumbTrail.cs" company="(none)">
// Copyright © 2013 John Gietzen and the WebGit .NET Authors. All rights reserved.
// </copyright>
// <author>John Gietzen</author>
//-----------------------------------------------------------------------
namespace WebGitNet
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public enum TrailingSlashBehavior
{
NoTrailingSlashes,
AllTrailingSlashes,
LeaveOffLastTrailingSlash,
}
public class BreadCrumbTrail : IEnumerable<BreadCrumbTrail.BreadCrumb>
{
private readonly LinkedList<BreadCrumb> breadCrumbs = new LinkedList<BreadCrumb>();
public static IEnumerable<KeyValuePair<string, string>> EnumeratePath(string url, TrailingSlashBehavior slashBehavior = TrailingSlashBehavior.AllTrailingSlashes)
{
url = (url ?? string.Empty).Trim('/');
var parts = url.Split("/".ToArray(), StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < parts.Length; i++)
{
var name = parts[i];
var path = string.Join("/", parts.Take(i + 1));
if (slashBehavior == TrailingSlashBehavior.AllTrailingSlashes ||
(slashBehavior == TrailingSlashBehavior.LeaveOffLastTrailingSlash && i < parts.Length - 1))
{
path = path + "/";
}
yield return new KeyValuePair<string, string>(name, path);
}
}
public void Append(string controller, string action, string name, object routeValues = null)
{
this.breadCrumbs.AddLast(new BreadCrumb(name, controller, action, routeValues));
}
public void Append<TSource>(string controller, string action, IEnumerable<TSource> crumbSource, Func<TSource, string> nameSelector, Func<TSource, object> routeValuesSelector)
{
this.Append(controller, action, crumbSource, nameSelector, (item, name) => routeValuesSelector(item));
}
public void Append<TSource>(string controller, string action, IEnumerable<TSource> crumbSource, Func<TSource, string> nameSelector, Func<TSource, string, object> routeValuesSelector)
{
foreach (var crumb in crumbSource)
{
var name = nameSelector(crumb);
var routeValues = routeValuesSelector(crumb, name);
this.Append(controller, action, name, routeValues);
}
}
public IEnumerator<BreadCrumbTrail.BreadCrumb> GetEnumerator()
{
return this.breadCrumbs.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public sealed class BreadCrumb
{
private readonly string action;
private readonly string controller;
private readonly string name;
private readonly object routeValues;
public BreadCrumb(string name, string controller, string action, object routeValues)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentNullException("name");
}
this.name = name;
if (string.IsNullOrEmpty(controller))
{
throw new ArgumentNullException("controller");
}
this.controller = controller;
if (string.IsNullOrEmpty(action))
{
throw new ArgumentNullException("action");
}
this.action = action;
this.routeValues = routeValues;
}
public string Action
{
get
{
return this.action;
}
}
public string Controller
{
get
{
return this.controller;
}
}
public string Name
{
get
{
return this.name;
}
}
public object RouteValues
{
get
{
return this.routeValues;
}
}
}
}
}