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
18 using System
.Collections
;
24 /// Depicts the contract for javascript generators.
29 /// Urls can be specified as string or a dictionary. If the latter, the <see cref="UrlHelper"/>
30 /// is used. See <see cref="DefaultUrlBuilder.BuildUrl(UrlInfo,IDictionary)"/>
34 /// The <c>renderOptions</c> is also a common parameter. If you pass a string,
35 /// the string will be rendered. If it is a dictionary, it instructs the infrastructure to
36 /// render a partial content. The dictionary must contain an entry named <c>partial</c>
37 /// with the absolute path to the view to render.
43 /// The following is an example of using it with a nvelocity
44 /// syntax and renders static content:
47 /// $page.InsertHtml('Bottom', 'messagestable', "Message sent")
51 /// The following uses a partial view:
55 /// $page.InsertHtml('Bottom', 'messagestable', "%{partial='shared/newmessage.vm'}")
59 /// The following redirects to a static page
63 /// $page.RedirectTo('about.aspx')
67 /// The following redirects using the <see cref="UrlHelper"/>
71 /// $page.RedirectTo("%{controller='Home',action='index'}")
75 public interface IJSGenerator
78 /// Inserts a content snippet relative to the element specified by the <paramref name="id"/>
81 /// The supported positions are
82 /// Top, After, Before, Bottom
87 /// The following example uses nvelocity syntax:
90 /// $page.InsertHtml('Bottom', 'messagestable', "%{partial='shared/newmessage.vm'}")
94 /// <param name="position">The position to insert the content relative to the element id</param>
95 /// <param name="id">The target element id</param>
96 /// <param name="renderOptions">Defines what to render</param>
97 void InsertHtml(string position
, string id
, object renderOptions
);
100 /// Replaces the content of the specified target element.
104 /// The following example uses nvelocity syntax:
107 /// $page.ReplaceHtml('messagediv', "%{partial='shared/newmessage.vm'}")
111 /// <param name="id">The target element id</param>
112 /// <param name="renderOptions">Defines what to render</param>
113 void ReplaceHtml(String id
, object renderOptions
);
116 /// Replaces the entire target element -- and not only its innerHTML --
117 /// by the content evaluated.
121 /// The following example uses nvelocity syntax:
124 /// $page.Replace('messagediv', "%{partial='shared/newmessage.vm'}")
128 /// <param name="id">The target element id</param>
129 /// <param name="renderOptions">Defines what to render</param>
130 void Replace(String id
, object renderOptions
);
133 /// Shows the specified elements.
137 /// The elements must exist.
141 /// The following example uses nvelocity syntax:
144 /// $page.Show('div1', 'div2')
148 /// <param name="ids">The elements ids.</param>
149 void Show(params string[] ids
);
152 /// Hides the specified elements.
156 /// The elements must exist.
160 /// The following example uses nvelocity syntax:
163 /// $page.Hide('div1', 'div2')
167 /// <param name="ids">The elements ids.</param>
168 void Hide(params string[] ids
);
171 /// Toggles the display status of the specified elements.
175 /// The elements must exist.
179 /// The following example uses nvelocity syntax:
182 /// $page.Toggle('div1', 'div2')
186 /// <param name="ids">The elements ids.</param>
187 void Toggle(params string[] ids
);
190 /// Remove the specified elements from the DOM.
194 /// The elements must exist.
198 /// The following example uses nvelocity syntax:
201 /// $page.Remove('div1', 'div2')
205 /// <param name="ids">The elements ids.</param>
206 void Remove(params string[] ids
);
213 /// The following example uses nvelocity syntax:
216 /// $page.Alert('You won a Mercedez')
220 /// <param name="message">The message to display.</param>
221 void Alert(object message
);
224 /// Redirects to an url using the <c>location.href</c>.
225 /// This is required as most ajax libs don't care for the redirect status
226 /// in the http reply.
230 /// The following redirects to a static page
233 /// $page.RedirectTo('about.aspx')
237 /// The following redirects using the <see cref="UrlHelper"/>
241 /// $page.RedirectTo("%{controller='Home',action='index'}")
245 /// <param name="url">The URL.</param>
246 void RedirectTo(object url
);
249 /// Re-apply Behaviour css' rules.
252 /// Only makes sense if you are using the Behaviour javascript library.
257 /// Generates a call to a scriptaculous' visual effect.
260 /// <seealso cref="ScriptaculousHelper"/>
263 /// The following example uses nvelocity syntax:
266 /// $page.VisualEffect('ToggleSlide', 'myelement')
270 /// This is especially useful to show which elements
271 /// where updated in an ajax call.
275 /// $page.ReplaceHtml('mydiv', "Hey, I've changed")
276 /// $page.VisualEffect('Highlight', 'mydiv')
281 /// <param name="name">The effect name.</param>
282 /// <param name="element">The target element.</param>
283 /// <param name="options">The optional options.</param>
284 void VisualEffect(String name
, String element
, IDictionary options
);
287 /// Generates a call to a scriptaculous' drop out visual effect.
290 /// <seealso cref="ScriptaculousHelper"/>
292 /// <param name="element">The target element.</param>
293 /// <param name="options">The optional options.</param>
294 void VisualEffectDropOut(String element
, IDictionary options
);
297 /// Assigns a javascript variable with the expression.
301 /// The following example uses nvelocity syntax:
304 /// $page.Assign('myvariable', '10')
316 /// With strings you can escape strings:
320 /// $page.Assign('myvariable', '\'Hello world\'')
328 /// myvariable = 'Hello world';
333 /// <param name="variable">The target variable</param>
334 /// <param name="expression">The right side expression</param>
335 void Assign(String variable
, String expression
);
338 /// Declares the specified variable as null.
341 /// <seealso cref="Assign"/>
343 /// <param name="variable">The variable name.</param>
344 void Declare(String variable
);
347 /// Calls the specified function with the optional arguments.
351 /// The following example uses nvelocity syntax:
354 /// $page.call('myJsFunctionAlreadyDeclared', '10', "'message'", $somethingfrompropertybag, $anothermessage.to_squote)
362 /// myJsFunctionAlreadyDeclared(10, 'message', 1001, 'who let the dogs out?')
367 /// <param name="function">The function name.</param>
368 /// <param name="args">The arguments.</param>
369 void Call(object function
, params object[] args
);
372 /// Outputs the content using the renderOptions approach.
375 /// If the renderOptions is a string, the content is escaped and quoted.
379 /// If the renderOptions is a dictionary, we extract the key <c>partial</c>
380 /// and evaluate the template it points to. The content is escaped and quoted.
386 /// The following example uses nvelocity syntax:
389 /// $page.Call('myJsFunction', $page.render("%{partial='shared/newmessage.vm'}") )
397 /// myJsFunction('the content from the newmessage partial view template')
402 /// <param name="renderOptions">The render options.</param>
403 /// <returns></returns>
404 object Render(object renderOptions
);
407 /// Writes the content specified to the generator instance
411 /// This is for advanced scenarios and for the infrastructure. Usually not useful.
414 /// <param name="content">The content.</param>
415 void Write(String content
);
418 /// Writes the content specified to the generator instance
422 /// This is for advanced scenarios and for the infrastructure. Usually not useful.
424 /// <param name="content">The content.</param>
425 void AppendLine(String content
);
428 /// Dump the operations recorded so far as javascript code.
431 /// <returns></returns>
435 /// Gets the js lines.
437 /// <value>The js lines.</value>
438 StringBuilder Lines { get; }
441 /// Creates a generator for a collection.
443 /// <param name="root">The root expression.</param>
444 /// <returns></returns>
445 IJSCollectionGenerator
CreateCollectionGenerator(string root
);
448 /// Creates a generator for an element.
450 /// <param name="root">The root expression.</param>
451 /// <returns></returns>
452 IJSElementGenerator
CreateElementGenerator(string root
);