New tests.
[mono.git] / mcs / class / System.Web.Mvc2 / System.Web.Mvc / ViewContext.cs
1 /* ****************************************************************************\r
2  *\r
3  * Copyright (c) Microsoft Corporation. All rights reserved.\r
4  *\r
5  * This software is subject to the Microsoft Public License (Ms-PL). \r
6  * A copy of the license can be found in the license.htm file included \r
7  * in this distribution.\r
8  *\r
9  * You must not remove this notice, or any other, from this software.\r
10  *\r
11  * ***************************************************************************/\r
12 \r
13 namespace System.Web.Mvc {\r
14     using System;\r
15     using System.Collections;\r
16     using System.Diagnostics.CodeAnalysis;\r
17     using System.Globalization;\r
18     using System.IO;\r
19     using System.Web.Script.Serialization;\r
20 \r
21     public class ViewContext : ControllerContext {\r
22 \r
23         private const string _clientValidationScript = @"<script type=""text/javascript"">\r
24 //<![CDATA[\r
25 if (!window.mvcClientValidationMetadata) {{ window.mvcClientValidationMetadata = []; }}\r
26 window.mvcClientValidationMetadata.push({0});\r
27 //]]>\r
28 </script>";\r
29 \r
30         // Some values have to be stored in HttpContext.Items in order to be propagated between calls\r
31         // to RenderPartial(), RenderAction(), etc.\r
32         private static readonly object _clientValidationEnabledKey = new object();\r
33         private static readonly object _formContextKey = new object();\r
34         private static readonly object _lastFormNumKey = new object();\r
35 \r
36         private Func<string> _formIdGenerator;\r
37 \r
38         // parameterless constructor used for mocking\r
39         public ViewContext() {\r
40         }\r
41 \r
42         [SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors",\r
43             Justification = "The virtual property setters are only to support mocking frameworks, in which case this constructor shouldn't be called anyway.")]\r
44         public ViewContext(ControllerContext controllerContext, IView view, ViewDataDictionary viewData, TempDataDictionary tempData, TextWriter writer)\r
45             : base(controllerContext) {\r
46             if (controllerContext == null) {\r
47                 throw new ArgumentNullException("controllerContext");\r
48             }\r
49             if (view == null) {\r
50                 throw new ArgumentNullException("view");\r
51             }\r
52             if (viewData == null) {\r
53                 throw new ArgumentNullException("viewData");\r
54             }\r
55             if (tempData == null) {\r
56                 throw new ArgumentNullException("tempData");\r
57             }\r
58             if (writer == null) {\r
59                 throw new ArgumentNullException("writer");\r
60             }\r
61 \r
62             View = view;\r
63             ViewData = viewData;\r
64             Writer = writer;\r
65             TempData = tempData;\r
66         }\r
67 \r
68         public virtual bool ClientValidationEnabled {\r
69             get {\r
70                 return (HttpContext.Items[_clientValidationEnabledKey] as bool?).GetValueOrDefault();\r
71             }\r
72             set {\r
73                 HttpContext.Items[_clientValidationEnabledKey] = value;\r
74             }\r
75         }\r
76 \r
77         public virtual FormContext FormContext {\r
78             get {\r
79                 return HttpContext.Items[_formContextKey] as FormContext;\r
80             }\r
81             set {\r
82                 HttpContext.Items[_formContextKey] = value;\r
83             }\r
84         }\r
85 \r
86         internal Func<string> FormIdGenerator {\r
87             get {\r
88                 if (_formIdGenerator == null) {\r
89                     _formIdGenerator = DefaultFormIdGenerator;\r
90                 }\r
91                 return _formIdGenerator;\r
92             }\r
93             set {\r
94                 _formIdGenerator = value;\r
95             }\r
96         }\r
97 \r
98         [SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly",\r
99             Justification = "The property setter is only here to support mocking this type and should not be called at runtime.")]\r
100         public virtual TempDataDictionary TempData {\r
101             get;\r
102             set;\r
103         }\r
104 \r
105         public virtual IView View {\r
106             get;\r
107             set;\r
108         }\r
109 \r
110         [SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly",\r
111             Justification = "The property setter is only here to support mocking this type and should not be called at runtime.")]\r
112         public virtual ViewDataDictionary ViewData {\r
113             get;\r
114             set;\r
115         }\r
116 \r
117         public virtual TextWriter Writer {\r
118             get;\r
119             set;\r
120         }\r
121 \r
122         private string DefaultFormIdGenerator() {\r
123             int formNum = IncrementFormCount(HttpContext.Items);\r
124             return String.Format(CultureInfo.InvariantCulture, "form{0}", formNum);\r
125         }\r
126 \r
127         internal FormContext GetFormContextForClientValidation() {\r
128             return (ClientValidationEnabled) ? FormContext : null;\r
129         }\r
130 \r
131         private static int IncrementFormCount(IDictionary items) {\r
132             object lastFormNum = items[_lastFormNumKey];\r
133             int newFormNum = (lastFormNum != null) ? ((int)lastFormNum) + 1 : 0;\r
134             items[_lastFormNumKey] = newFormNum;\r
135             return newFormNum;\r
136         }\r
137 \r
138         public void OutputClientValidation() {\r
139             FormContext formContext = GetFormContextForClientValidation();\r
140             if (formContext == null) {\r
141                 return; // do nothing\r
142             }\r
143                         \r
144             string scriptWithCorrectNewLines = _clientValidationScript.Replace("\r\n", Environment.NewLine);\r
145             string validationJson = formContext.GetJsonValidationMetadata();\r
146             string formatted = String.Format(CultureInfo.InvariantCulture, scriptWithCorrectNewLines, validationJson);\r
147 \r
148             Writer.Write(formatted);\r
149             FormContext = null;\r
150         }\r
151 \r
152     }\r
153 }\r