forked from jongalloway/Presentations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewMobilizerModel.cs
72 lines (58 loc) · 2.52 KB
/
ViewMobilizerModel.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
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.Web.Mvc.Extensibility;
namespace MvcHaack.ViewMobilizer {
public class ViewMobilizerModel {
public ViewMobilizerModel(ProjectFolder folder)
: this(folder, "mobile") {
}
public ViewMobilizerModel(ProjectFolder folder, string suffix) {
Folder = folder;
Populate(suffix);
}
public ProjectFolder Folder { get; set; }
public string Suffix { get; private set; }
public string DeviceSpecificExtension { get; private set; }
public void Populate(string suffix) {
Suffix = suffix;
DeviceSpecificExtension = String.Format(".{0}.cshtml", Suffix);
var views = new Dictionary<string, ProjectFile>(StringComparer.OrdinalIgnoreCase);
FilterViews(Folder, views);
Views = views;
SelectedViews = new List<Tuple<ProjectFile, string>>();
}
private void FilterViews(ProjectFolder folder, Dictionary<string, ProjectFile> views) {
// Collect
Traverse(folder, file => {
views.Add(file.RelativePath, file);
});
// Filter
Traverse(folder, file => {
RemoveDeviceSpecificWithCorrespondingDesktop(file, views);
});
}
private void Traverse(ProjectFolder folder, Action<ProjectFile> action) {
foreach (var file in folder.Files) {
if (file.RelativePath.EndsWith(".cshtml", StringComparison.OrdinalIgnoreCase)) {
action(file);
}
}
foreach (var subfolder in folder.Folders) {
Traverse(subfolder, action);
}
}
private void RemoveDeviceSpecificWithCorrespondingDesktop(ProjectFile file, Dictionary<string, ProjectFile> views) {
string path = file.RelativePath;
if (path.EndsWith(DeviceSpecificExtension, StringComparison.OrdinalIgnoreCase)) {
int preSuffixIndex = path.Length - DeviceSpecificExtension.Length;
string desktopPath = path.Substring(0, preSuffixIndex) + ".cshtml";
if (views.ContainsKey(desktopPath)) {
views.Remove(desktopPath);
views.Remove(path);
}
}
}
public Dictionary<string, ProjectFile> Views { get; private set; }
public List<Tuple<ProjectFile, string>> SelectedViews { get; private set; }
}
}