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
.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
;
32 /// Initializes a new instance of the <see cref="BehaviourHelper"/> class.
34 public BehaviourHelper() { }
36 /// Initializes a new instance of the <see cref="BehaviourHelper"/> class.
37 /// setting the Controller, Context and ControllerContext.
39 /// <param name="engineContext">The engine context.</param>
40 public BehaviourHelper(IEngineContext engineContext
) : base(engineContext
) { }
47 /// Renders a script tag refering the Behaviour library code.
49 /// <returns></returns>
50 public String
InstallScripts()
52 return RenderScriptBlockToSource("/MonoRail/Files/BehaviourScripts");
58 /// Renders a script block invoking <c>Behaviour.apply()</c>
60 public String
ReApply()
62 return ScriptBlock("Behaviour.apply();");
66 /// Renders a script block invoking <c>Behaviour.addLoadEvent(loadFunctionName);</c>
68 /// <param name="loadFunctionName">The name of the js function to be invoked when the body is loaded</param>
69 public String
AddLoadEvent(String loadFunctionName
)
71 return ScriptBlock("Behaviour.addLoadEvent(" + loadFunctionName
+ ");");
75 /// Renders a script block starting the association of events to selector rules
76 /// <seealso cref="Register"/>
77 /// <seealso cref="EndBehaviourRegister"/>
79 public String
StartBehaviourRegister()
81 behaviourCommaNeeded
= false;
83 return "<script type=\"text/javascript\">" + Environment
.NewLine
+
84 "Behaviour.register({" + Environment
.NewLine
;
88 /// Adds a entry to a registration array. Invoking it
89 /// with <c>#form</c>, <c>onsubmit</c> and <c>validate</c> will produce
90 /// <c>'#form' : function(e){ e.onsubmit = validate; },</c>
91 /// <seealso cref="StartBehaviourRegister"/>
92 /// <seealso cref="EndBehaviourRegister"/>
94 /// <param name="selector">The css selector rule</param>
95 /// <param name="eventName">The name of the event on the element</param>
96 /// <param name="jsFunctionName">The function to be invoked in response to the event</param>
97 public String
Register(String selector
, String eventName
, String jsFunctionName
)
99 String val
= (behaviourCommaNeeded
? "," : String
.Empty
) +
100 "\t'" + selector
+ "' : function(e){ e." + eventName + " = " + jsFunctionName + "; }" +
103 if (!behaviourCommaNeeded
)
105 behaviourCommaNeeded
= true;
112 /// Renders the end of a script block that associated events to selector rules
113 /// <seealso cref="StartBehaviourRegister"/>
114 /// <seealso cref="Register"/>
116 public String
EndBehaviourRegister()
118 return Environment
.NewLine
+ "});" + Environment
.NewLine
+ "</script>";