1 // Copyright 2004-2007 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
.Helpers
20 /// Exposes the functionality available on the Behaviour js library
21 /// which Uses css selectors to bind javascript code to DOM elements
24 /// Before using it, you must install the scripts. See <see cref="BehaviourHelper.InstallScripts"/>
26 public class BehaviourHelper
: AbstractHelper
28 private bool behaviourCommaNeeded
;
33 /// Renders a script tag refering the Behaviour library code.
35 /// <returns></returns>
36 public String
InstallScripts()
38 return RenderScriptBlockToSource("/MonoRail/Files/BehaviourScripts");
44 /// Renders a script block invoking <c>Behaviour.apply()</c>
46 public String
ReApply()
48 return ScriptBlock("Behaviour.apply();");
52 /// Renders a script block invoking <c>Behaviour.addLoadEvent(loadFunctionName);</c>
54 /// <param name="loadFunctionName">The name of the js function to be invoked when the body is loaded</param>
55 public String
AddLoadEvent(String loadFunctionName
)
57 return ScriptBlock("Behaviour.addLoadEvent(" + loadFunctionName
+ ");");
61 /// Renders a script block starting the association of events to selector rules
62 /// <seealso cref="Register"/>
63 /// <seealso cref="EndBehaviourRegister"/>
65 public String
StartBehaviourRegister()
67 behaviourCommaNeeded
= false;
69 return "<script type=\"text/javascript\">" + Environment
.NewLine
+
70 "Behaviour.register({" + Environment
.NewLine
;
74 /// Adds a entry to a registration array. Invoking it
75 /// with <c>#form</c>, <c>onsubmit</c> and <c>validate</c> will produce
76 /// <c>'#form' : function(e){ e.onsubmit = validate; },</c>
77 /// <seealso cref="StartBehaviourRegister"/>
78 /// <seealso cref="EndBehaviourRegister"/>
80 /// <param name="selector">The css selector rule</param>
81 /// <param name="eventName">The name of the event on the element</param>
82 /// <param name="jsFunctionName">The function to be invoked in response to the event</param>
83 public String
Register(String selector
, String eventName
, String jsFunctionName
)
85 String val
= (behaviourCommaNeeded
? "," : String
.Empty
) +
86 "\t'" + selector
+ "' : function(e){ e." + eventName + " = " + jsFunctionName + "; }" +
89 if (!behaviourCommaNeeded
)
91 behaviourCommaNeeded
= true;
98 /// Renders the end of a script block that associated events to selector rules
99 /// <seealso cref="StartBehaviourRegister"/>
100 /// <seealso cref="Register"/>
102 public String
EndBehaviourRegister()
104 return Environment
.NewLine
+ "});" + Environment
.NewLine
+ "</script>";