forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplayModeProvider.cs
More file actions
124 lines (110 loc) · 5.2 KB
/
Copy pathDisplayModeProvider.cs
File metadata and controls
124 lines (110 loc) · 5.2 KB
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Linq;
namespace System.Web.WebPages
{
public sealed class DisplayModeProvider
{
public static readonly string MobileDisplayModeId = "Mobile";
public static readonly string DefaultDisplayModeId = String.Empty;
private static readonly object _displayModeKey = new object();
private static readonly DisplayModeProvider _instance = new DisplayModeProvider();
private readonly List<IDisplayMode> _displayModes = new List<IDisplayMode>
{
new DefaultDisplayMode(MobileDisplayModeId)
{
ContextCondition = context => context.GetOverriddenBrowser().IsMobileDevice
},
new DefaultDisplayMode()
};
internal DisplayModeProvider()
{
// The type is a psuedo-singleton. A user would gain nothing from constructing it since we won't use anything but DisplayModeProvider.Instance internally.
}
/// <summary>
/// Restricts the search for Display Info to Display Modes either equal to or following the current
/// Display Mode in Modes. For example, a page being rendered in the Default Display Mode will not
/// display Mobile partial views in order to achieve a consistent look and feel.
/// </summary>
public bool RequireConsistentDisplayMode { get; set; }
public static DisplayModeProvider Instance
{
get { return _instance; }
}
/// <summary>
/// All Display Modes that are available to handle a request.
/// </summary>
public IList<IDisplayMode> Modes
{
get { return _displayModes; }
}
private int FindFirstAvailableDisplayMode(IDisplayMode currentDisplayMode, bool requireConsistentDisplayMode)
{
if (requireConsistentDisplayMode && currentDisplayMode != null)
{
int first = _displayModes.IndexOf(currentDisplayMode);
return (first >= 0) ? first : _displayModes.Count;
}
return 0;
}
/// <summary>
/// Returns any IDisplayMode that can handle the given request.
/// </summary>
public IEnumerable<IDisplayMode> GetAvailableDisplayModesForContext(HttpContextBase httpContext, IDisplayMode currentDisplayMode)
{
return GetAvailableDisplayModesForContext(httpContext, currentDisplayMode, RequireConsistentDisplayMode);
}
internal IEnumerable<IDisplayMode> GetAvailableDisplayModesForContext(HttpContextBase httpContext, IDisplayMode currentDisplayMode, bool requireConsistentDisplayMode)
{
int first = FindFirstAvailableDisplayMode(currentDisplayMode, requireConsistentDisplayMode);
for (int i = first; i < _displayModes.Count; i++)
{
IDisplayMode mode = _displayModes[i];
if (mode.CanHandleContext(httpContext))
{
yield return mode;
}
}
}
/// <summary>
/// Returns DisplayInfo from the first IDisplayMode in Modes that can handle the given request and locate the virtual path.
/// If currentDisplayMode is not null and RequireConsistentDisplayMode is set to true the search for DisplayInfo will only
/// start with the currentDisplayMode.
/// </summary>
public DisplayInfo GetDisplayInfoForVirtualPath(string virtualPath, HttpContextBase httpContext, Func<string, bool> virtualPathExists, IDisplayMode currentDisplayMode)
{
return GetDisplayInfoForVirtualPath(virtualPath, httpContext, virtualPathExists, currentDisplayMode, RequireConsistentDisplayMode);
}
internal DisplayInfo GetDisplayInfoForVirtualPath(string virtualPath, HttpContextBase httpContext, Func<string, bool> virtualPathExists, IDisplayMode currentDisplayMode,
bool requireConsistentDisplayMode)
{
// Performance sensitive
int first = FindFirstAvailableDisplayMode(currentDisplayMode, requireConsistentDisplayMode);
for (int i = first; i < _displayModes.Count; i++)
{
IDisplayMode mode = _displayModes[i];
if (mode.CanHandleContext(httpContext))
{
DisplayInfo info = mode.GetDisplayInfo(httpContext, virtualPath, virtualPathExists);
if (info != null)
{
return info;
}
}
}
return null;
}
internal static IDisplayMode GetDisplayMode(HttpContextBase context)
{
return context != null ? context.Items[_displayModeKey] as IDisplayMode : null;
}
internal static void SetDisplayMode(HttpContextBase context, IDisplayMode displayMode)
{
if (context != null)
{
context.Items[_displayModeKey] = displayMode;
}
}
}
}