Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / MonoRail / Castle.MonoRail.Framework / Helpers / BehaviourHelper.cs
blob232999c7611bdc1f3c74474bbf1046de5e9a78b8
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.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 Constructors
31 /// <summary>
32 /// Initializes a new instance of the <see cref="BehaviourHelper"/> class.
33 /// </summary>
34 public BehaviourHelper() { }
35 /// <summary>
36 /// Initializes a new instance of the <see cref="BehaviourHelper"/> class.
37 /// setting the Controller, Context and ControllerContext.
38 /// </summary>
39 /// <param name="engineContext">The engine context.</param>
40 public BehaviourHelper(IEngineContext engineContext) : base(engineContext) { }
41 #endregion
44 #region Scripts
46 /// <summary>
47 /// Renders a script tag refering the Behaviour library code.
48 /// </summary>
49 /// <returns></returns>
50 public String InstallScripts()
52 return RenderScriptBlockToSource("/MonoRail/Files/BehaviourScripts");
55 #endregion
57 /// <summary>
58 /// Renders a script block invoking <c>Behaviour.apply()</c>
59 /// </summary>
60 public String ReApply()
62 return ScriptBlock("Behaviour.apply();");
65 /// <summary>
66 /// Renders a script block invoking <c>Behaviour.addLoadEvent(loadFunctionName);</c>
67 /// </summary>
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 + ");");
74 /// <summary>
75 /// Renders a script block starting the association of events to selector rules
76 /// <seealso cref="Register"/>
77 /// <seealso cref="EndBehaviourRegister"/>
78 /// </summary>
79 public String StartBehaviourRegister()
81 behaviourCommaNeeded = false;
83 return "<script type=\"text/javascript\">" + Environment.NewLine +
84 "Behaviour.register({" + Environment.NewLine;
87 /// <summary>
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"/>
93 /// </summary>
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 + "; }" +
101 Environment.NewLine;
103 if (!behaviourCommaNeeded)
105 behaviourCommaNeeded = true;
108 return val;
111 /// <summary>
112 /// Renders the end of a script block that associated events to selector rules
113 /// <seealso cref="StartBehaviourRegister"/>
114 /// <seealso cref="Register"/>
115 /// </summary>
116 public String EndBehaviourRegister()
118 return Environment.NewLine + "});" + Environment.NewLine + "</script>";