Fixing an issue with output parameters that are of type IntPtr
[castle.git] / MonoRail / Castle.MonoRail.Framework / JSGeneration / CommonJSExtension.cs
blobf9683025cd1a1bc3d121f831617f4bbdbc0e07c7
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.Framework.JSGeneration
17 using System;
18 using System.Collections;
19 using Helpers;
21 /// <summary>
22 /// Pendent
23 /// </summary>
24 public class CommonJSExtension
26 private readonly IJSCodeGenerator jsCodeGenerator;
28 /// <summary>
29 /// Initializes a new instance of the <see cref="CommonJSExtension"/> class.
30 /// </summary>
31 /// <param name="jsCodeGenerator">The js code generator.</param>
32 public CommonJSExtension(IJSCodeGenerator jsCodeGenerator)
34 this.jsCodeGenerator = jsCodeGenerator;
37 /// <summary>
38 /// Assigns a javascript variable with the expression.
39 /// </summary>
40 ///
41 /// <example>
42 /// The following example uses nvelocity syntax:
43 ///
44 /// <code>
45 /// $page.Assign('myvariable', '10')
46 /// </code>
47 ///
48 /// <para>
49 /// Which outputs:
50 /// </para>
51 ///
52 /// <code>
53 /// myvariable = 10;
54 /// </code>
55 ///
56 /// <para>
57 /// With strings you can escape strings:
58 /// </para>
59 ///
60 /// <code>
61 /// $page.Assign('myvariable', '\'Hello world\'')
62 /// </code>
63 ///
64 /// <para>
65 /// Which outputs:
66 /// </para>
67 ///
68 /// <code>
69 /// myvariable = 'Hello world';
70 /// </code>
71 ///
72 /// </example>
73 ///
74 /// <param name="variable">The target variable</param>
75 /// <param name="expression">The right side expression</param>
76 [DynamicOperation]
77 public void Assign(String variable, String expression)
79 jsCodeGenerator.Record(variable + " = " + expression);
82 /// <summary>
83 /// Declares the specified variable as null.
84 /// </summary>
85 ///
86 /// <seealso cref="Assign"/>
87 ///
88 /// <param name="variable">The variable name.</param>
89 [DynamicOperation]
90 public void Declare(String variable)
92 jsCodeGenerator.Record(string.Format("var {0} = null", variable));
95 /// <summary>
96 /// Calls the specified function with the optional arguments.
97 /// </summary>
98 ///
99 /// <example>
100 /// The following example uses nvelocity syntax:
101 ///
102 /// <code>
103 /// $page.call('myJsFunctionAlreadyDeclared', '10', "'message'", $somethingfrompropertybag, $anothermessage.to_squote)
104 /// </code>
105 ///
106 /// <para>
107 /// Which outputs:
108 /// </para>
109 ///
110 /// <code>
111 /// myJsFunctionAlreadyDeclared(10, 'message', 1001, 'who let the dogs out?')
112 /// </code>
113 ///
114 /// </example>
115 ///
116 /// <param name="function">The function name.</param>
117 /// <param name="args">The arguments.</param>
118 [DynamicOperation]
119 public void Call(object function, params object[] args)
121 jsCodeGenerator.Call(function, args);
124 /// <summary>
125 /// Shows a JS alert
126 /// </summary>
127 ///
128 /// <example>
129 /// The following example uses nvelocity syntax:
130 ///
131 /// <code>
132 /// $page.Alert('You won a Mercedez')
133 /// </code>
134 /// </example>
135 ///
136 /// <param name="message">The message to display.</param>
137 [DynamicOperation]
138 public void Alert(object message)
140 Call("alert", AbstractHelper.Quote(message));
143 /// <summary>
144 /// Redirects to an url using the <c>location.href</c>.
145 /// This is required as most ajax libs don't care for the redirect status
146 /// in the http reply.
147 /// </summary>
148 ///
149 /// <example>
150 /// The following redirects to a static page
151 ///
152 /// <code>
153 /// $page.RedirectTo('about.aspx')
154 /// </code>
155 ///
156 /// <para>
157 /// The following redirects using the <see cref="UrlHelper"/>
158 /// </para>
159 ///
160 /// <code>
161 /// $page.RedirectTo("%{controller='Home',action='index'}")
162 /// </code>
163 /// </example>
164 ///
165 /// <param name="url">The URL.</param>
166 [DynamicOperation]
167 public void RedirectTo(object url)
169 string target;
171 if (url is IDictionary)
173 target = jsCodeGenerator.UrlBuilder.BuildUrl(
174 jsCodeGenerator.EngineContext.UrlInfo, url as IDictionary);
176 else
178 target = url.ToString();
181 Assign("window.location.href", AbstractHelper.Quote(target));