-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThemesPlusPlusViewEngine.cs
126 lines (107 loc) · 5.08 KB
/
ThemesPlusPlusViewEngine.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
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.WebPages;
using Nop.Core.Infrastructure;
using Nop.Web.Framework.Themes;
namespace Nop.Plugin.Development.ThemesPlusPlus
{
public class ThemesPlusPlusViewEngine : ThemeableRazorViewEngine
{
protected override string GetPathFromGeneralName(
ControllerContext controllerContext,
List<ViewLocation> locations,
string name,
string controllerName,
string areaName,
string theme,
string cacheKey,
ref string[] searchedLocations)
{
var parentThemes = LoadParentThemes(theme);
string result = string.Empty;
var checkedLocations = new List<string>();
for (int i = 0; i < locations.Count; i++)
{
var location = locations[i];
string virtualPath = location.Format(name, controllerName, areaName, theme);
var virtualPathDisplayInfo = DisplayModeProvider.GetDisplayInfoForVirtualPath(
virtualPath,
controllerContext.HttpContext,
path => FileExists(controllerContext, path),
controllerContext.DisplayMode);
if (virtualPathDisplayInfo == null && virtualPath.IndexOf("/Themes/", StringComparison.InvariantCultureIgnoreCase) > -1)
{
// We're checking themes location. Try parent themes as well.
foreach(var parentTheme in parentThemes)
{
// 1. Track the last checked location
checkedLocations.Add(virtualPath);
// 2. Try parent theme
virtualPath = location.Format(name, controllerName, areaName, parentTheme);
virtualPathDisplayInfo = DisplayModeProvider.GetDisplayInfoForVirtualPath(
virtualPath,
controllerContext.HttpContext,
path => FileExists(controllerContext, path),
controllerContext.DisplayMode);
if (virtualPathDisplayInfo != null)
{
// 3. We found target view in the parent theme
break;
}
}
}
if (virtualPathDisplayInfo != null)
{
string resolvedVirtualPath = virtualPathDisplayInfo.FilePath;
checkedLocations.Clear();
result = resolvedVirtualPath;
ViewLocationCache.InsertViewLocation(
controllerContext.HttpContext,
AppendDisplayModeToCacheKey(cacheKey, virtualPathDisplayInfo.DisplayMode.DisplayModeId), result);
if (controllerContext.DisplayMode == null)
{
controllerContext.DisplayMode = virtualPathDisplayInfo.DisplayMode;
}
var allDisplayModes = DisplayModeProvider.Modes;
foreach (IDisplayMode displayMode in allDisplayModes)
{
if (displayMode.DisplayModeId != virtualPathDisplayInfo.DisplayMode.DisplayModeId)
{
var displayInfoToCache = displayMode.GetDisplayInfo(controllerContext.HttpContext, virtualPath, virtualPathExists: path => FileExists(controllerContext, path));
string cacheValue = string.Empty;
if (displayInfoToCache != null && displayInfoToCache.FilePath != null)
{
cacheValue = displayInfoToCache.FilePath;
}
ViewLocationCache.InsertViewLocation(
controllerContext.HttpContext,
AppendDisplayModeToCacheKey(cacheKey, displayMode.DisplayModeId), cacheValue);
}
}
break;
}
checkedLocations.Add(virtualPath);
}
searchedLocations = checkedLocations.ToArray();
return result;
}
private IList<string> LoadParentThemes(string currentTheme)
{
var provider = EngineContext.Current.Resolve<IThemeProvider>();
var parentThemes = new List<string>();
var theme = provider.GetThemeConfiguration(currentTheme) as ThemesPlusPlusConfiguration;
while (theme.ParentTheme != null)
{
theme = provider.GetThemeConfiguration(theme.ParentTheme) as ThemesPlusPlusConfiguration;
if (theme == null)
{
// Parent theme is not found
break;
}
parentThemes.Add(theme.ThemeName);
}
return parentThemes;
}
}
}