1
// Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
18 using System
.Collections
;
24 public class CommonJSExtension
26 private readonly IJSCodeGenerator jsCodeGenerator
;
29 /// Initializes a new instance of the <see cref="CommonJSExtension"/> class.
31 /// <param name="jsCodeGenerator">The js code generator.</param>
32 public CommonJSExtension(IJSCodeGenerator jsCodeGenerator
)
34 this.jsCodeGenerator
= jsCodeGenerator
;
38 /// Assigns a javascript variable with the expression.
42 /// The following example uses nvelocity syntax:
45 /// $page.Assign('myvariable', '10')
57 /// With strings you can escape strings:
61 /// $page.Assign('myvariable', '\'Hello world\'')
69 /// myvariable = 'Hello world';
74 /// <param name="variable">The target variable</param>
75 /// <param name="expression">The right side expression</param>
77 public void Assign(String variable
, String expression
)
79 jsCodeGenerator
.Record(variable
+ " = " + expression
);
83 /// Declares the specified variable as null.
86 /// <seealso cref="Assign"/>
88 /// <param name="variable">The variable name.</param>
90 public void Declare(String variable
)
92 jsCodeGenerator
.Record(string.Format("var {0} = null", variable
));
96 /// Calls the specified function with the optional arguments.
100 /// The following example uses nvelocity syntax:
103 /// $page.call('myJsFunctionAlreadyDeclared', '10', "'message'", $somethingfrompropertybag, $anothermessage.to_squote)
111 /// myJsFunctionAlreadyDeclared(10, 'message', 1001, 'who let the dogs out?')
116 /// <param name="function">The function name.</param>
117 /// <param name="args">The arguments.</param>
119 public void Call(object function
, params object[] args
)
121 jsCodeGenerator
.Call(function
, args
);
129 /// The following example uses nvelocity syntax:
132 /// $page.Alert('You won a Mercedez')
136 /// <param name="message">The message to display.</param>
138 public void Alert(object message
)
140 Call("alert", AbstractHelper
.Quote(message
));
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.
150 /// The following redirects to a static page
153 /// $page.RedirectTo('about.aspx')
157 /// The following redirects using the <see cref="UrlHelper"/>
161 /// $page.RedirectTo("%{controller='Home',action='index'}")
165 /// <param name="url">The URL.</param>
167 public void RedirectTo(object url
)
171 if (url
is IDictionary
)
173 target
= jsCodeGenerator
.UrlBuilder
.BuildUrl(
174 jsCodeGenerator
.EngineContext
.UrlInfo
, url
as IDictionary
);
178 target
= url
.ToString();
181 Assign("window.location.href", AbstractHelper
.Quote(target
));