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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
namespace System.Web.Mvc {
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Web.WebPages.Scope;
public class ViewContext : ControllerContext {
private const string _clientValidationScript = @"<script type=""text/javascript"">
//<![CDATA[
if (!window.mvcClientValidationMetadata) {{ window.mvcClientValidationMetadata = []; }}
window.mvcClientValidationMetadata.push({0});
//]]>
</script>";
internal static readonly string ClientValidationKeyName = "ClientValidationEnabled";
internal static readonly string UnobtrusiveJavaScriptKeyName = "UnobtrusiveJavaScriptEnabled";
// Some values have to be stored in HttpContext.Items in order to be propagated between calls
// to RenderPartial(), RenderAction(), etc.
private static readonly object _formContextKey = new object();
private static readonly object _lastFormNumKey = new object();
private Func<IDictionary<object, object>> _scopeThunk;
private IDictionary<object, object> _transientScope;
private Func<string> _formIdGenerator;
// parameterless constructor used for mocking
public ViewContext() {
}
[SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors", Justification = "The virtual property setters are only to support mocking frameworks, in which case this constructor shouldn't be called anyway.")]
public ViewContext(ControllerContext controllerContext, IView view, ViewDataDictionary viewData, TempDataDictionary tempData, TextWriter writer)
: base(controllerContext) {
if (controllerContext == null) {
throw new ArgumentNullException("controllerContext");
}
if (view == null) {
throw new ArgumentNullException("view");
}
if (viewData == null) {
throw new ArgumentNullException("viewData");
}
if (tempData == null) {
throw new ArgumentNullException("tempData");
}
if (writer == null) {
throw new ArgumentNullException("writer");
}
View = view;
ViewData = viewData;
Writer = writer;
TempData = tempData;
}
public virtual bool ClientValidationEnabled {
get {
return GetClientValidationEnabled(Scope, HttpContext);
}
set {
SetClientValidationEnabled(value, Scope, HttpContext);
}
}
public virtual FormContext FormContext {
get {
return HttpContext.Items[_formContextKey] as FormContext;
}
set {
HttpContext.Items[_formContextKey] = value;
}
}
internal Func<string> FormIdGenerator {
get {
if (_formIdGenerator == null) {
_formIdGenerator = DefaultFormIdGenerator;
}
return _formIdGenerator;
}
set {
_formIdGenerator = value;
}
}
internal static Func<IDictionary<object, object>> GlobalScopeThunk {
get;
set;
}
private IDictionary<object, object> Scope {
get {
if (ScopeThunk != null) {
return ScopeThunk();
}
if (_transientScope == null) {
_transientScope = new Dictionary<object, object>();
}
return _transientScope;
}
}
internal Func<IDictionary<object, object>> ScopeThunk {
get {
return _scopeThunk ?? GlobalScopeThunk;
}
set {
_scopeThunk = value;
}
}
[SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Justification = "The property setter is only here to support mocking this type and should not be called at runtime.")]
public virtual TempDataDictionary TempData {
get;
set;
}
public virtual bool UnobtrusiveJavaScriptEnabled {
get {
return GetUnobtrusiveJavaScriptEnabled(Scope, HttpContext);
}
set {
SetUnobtrusiveJavaScriptEnabled(value, Scope, HttpContext);
}
}
public virtual IView View {
get;
set;
}
[SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Justification = "The property setter is only here to support mocking this type and should not be called at runtime.")]
public virtual ViewDataDictionary ViewData {
get;
set;
}
public virtual TextWriter Writer {
get;
set;
}
private string DefaultFormIdGenerator() {
int formNum = IncrementFormCount(HttpContext.Items);
return String.Format(CultureInfo.InvariantCulture, "form{0}", formNum);
}
internal static bool GetClientValidationEnabled(IDictionary<object, object> scope = null, HttpContextBase httpContext = null) {
return ScopeCache.Get(scope, httpContext).ClientValidationEnabled;
}
internal FormContext GetFormContextForClientValidation() {
return (ClientValidationEnabled) ? FormContext : null;
}
internal static bool GetUnobtrusiveJavaScriptEnabled(IDictionary<object, object> scope = null, HttpContextBase httpContext = null) {
return ScopeCache.Get(scope, httpContext).UnobtrusiveJavaScriptEnabled;
}
private static int IncrementFormCount(IDictionary items) {
object lastFormNum = items[_lastFormNumKey];
int newFormNum = (lastFormNum != null) ? ((int)lastFormNum) + 1 : 0;
items[_lastFormNumKey] = newFormNum;
return newFormNum;
}
public void OutputClientValidation() {
FormContext formContext = GetFormContextForClientValidation();
if (formContext == null || UnobtrusiveJavaScriptEnabled) {
return; // do nothing
}
string scriptWithCorrectNewLines = _clientValidationScript.Replace("\r\n", Environment.NewLine);
string validationJson = formContext.GetJsonValidationMetadata();
string formatted = String.Format(CultureInfo.InvariantCulture, scriptWithCorrectNewLines, validationJson);
Writer.Write(formatted);
FormContext = null;
}
internal static void SetClientValidationEnabled(bool enabled, IDictionary<object, object> scope = null, HttpContextBase httpContext = null) {
ScopeCache.Get(scope, httpContext).ClientValidationEnabled = enabled;
}
internal static void SetUnobtrusiveJavaScriptEnabled(bool enabled, IDictionary<object, object> scope = null, HttpContextBase httpContext = null) {
ScopeCache.Get(scope, httpContext).UnobtrusiveJavaScriptEnabled = enabled;
}
private static TValue ScopeGet<TValue>(IDictionary<object, object> scope, string name, TValue defaultValue = default(TValue)) {
object result;
if (scope.TryGetValue(name, out result)) {
return (TValue)Convert.ChangeType(result, typeof(TValue), CultureInfo.InvariantCulture);
}
return defaultValue;
}
private sealed class ScopeCache {
private static readonly object _cacheKey = new object();
private bool _clientValidationEnabled;
private IDictionary<object, object> _scope;
private bool _unobtrusiveJavaScriptEnabled;
private ScopeCache(IDictionary<object, object> scope) {
_scope = scope;
_clientValidationEnabled = ScopeGet(scope, ClientValidationKeyName, false);
_unobtrusiveJavaScriptEnabled = ScopeGet(scope, UnobtrusiveJavaScriptKeyName, false);
}
public bool ClientValidationEnabled {
get {
return _clientValidationEnabled;
}
set {
_clientValidationEnabled = value;
_scope[ClientValidationKeyName] = value;
}
}
public bool UnobtrusiveJavaScriptEnabled {
get {
return _unobtrusiveJavaScriptEnabled;
}
set {
_unobtrusiveJavaScriptEnabled = value;
_scope[UnobtrusiveJavaScriptKeyName] = value;
}
}
public static ScopeCache Get(IDictionary<object, object> scope, HttpContextBase httpContext) {
if (httpContext == null && System.Web.HttpContext.Current != null) {
httpContext = new HttpContextWrapper(System.Web.HttpContext.Current);
}
ScopeCache result = null;
scope = scope ?? ScopeStorage.CurrentScope;
if (httpContext != null) {
result = httpContext.Items[_cacheKey] as ScopeCache;
}
if (result == null || result._scope != scope) {
result = new ScopeCache(scope);
if (httpContext != null) {
httpContext.Items[_cacheKey] = result;
}
}
return result;
}
}
}
}
|