- Fixed MR-84
[castle.git] / MonoRail / Castle.MonoRail.Framework / Helpers / BehaviourHelper.cs
blobc025232e5c26cafa5ac21a2a6c6fbbf5ca673921
1 // Copyright 2004-2007 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.Helpers
17 using System;
19 /// <summary>
20 /// Exposes the functionality available on the Behaviour js library
21 /// which Uses css selectors to bind javascript code to DOM elements
22 /// </summary>
23 /// <remarks>
24 /// Before using it, you must install the scripts. See <see cref="BehaviourHelper.InstallScripts"/>
25 /// </remarks>
26 public class BehaviourHelper : AbstractHelper
28 private bool behaviourCommaNeeded;
30 #region Scripts
32 /// <summary>
33 /// Renders a script tag refering the Behaviour library code.
34 /// </summary>
35 /// <returns></returns>
36 public String InstallScripts()
38 return RenderScriptBlockToSource("/MonoRail/Files/BehaviourScripts");
41 #endregion
43 /// <summary>
44 /// Renders a script block invoking <c>Behaviour.apply()</c>
45 /// </summary>
46 public String ReApply()
48 return ScriptBlock("Behaviour.apply();");
51 /// <summary>
52 /// Renders a script block invoking <c>Behaviour.addLoadEvent(loadFunctionName);</c>
53 /// </summary>
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 + ");");
60 /// <summary>
61 /// Renders a script block starting the association of events to selector rules
62 /// <seealso cref="Register"/>
63 /// <seealso cref="EndBehaviourRegister"/>
64 /// </summary>
65 public String StartBehaviourRegister()
67 behaviourCommaNeeded = false;
69 return "<script type=\"text/javascript\">" + Environment.NewLine +
70 "Behaviour.register({" + Environment.NewLine;
73 /// <summary>
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"/>
79 /// </summary>
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 + "; }" +
87 Environment.NewLine;
89 if (!behaviourCommaNeeded)
91 behaviourCommaNeeded = true;
94 return val;
97 /// <summary>
98 /// Renders the end of a script block that associated events to selector rules
99 /// <seealso cref="StartBehaviourRegister"/>
100 /// <seealso cref="Register"/>
101 /// </summary>
102 public String EndBehaviourRegister()
104 return Environment.NewLine + "});" + Environment.NewLine + "</script>";