// Copyright 2004-2008 Castle Project - http://www.castleproject.org/ // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Castle.MonoRail.Framework.JSGeneration.Prototype { using System; using Castle.MonoRail.Framework.Helpers; /// /// Pendent /// public class PrototypeGenerator : AbstractJSGenerator { private enum Position { Top, Bottom, Before, After } /// /// Initializes a new instance of the class. /// /// The code generator. public PrototypeGenerator(IJSCodeGenerator codeGenerator) : base(codeGenerator) { } /// /// Inserts a content snippet relative to the element specified by the /// /// The supported positions are /// Top, After, Before, Bottom /// /// /// The position to insert the content relative to the element id /// The target element id /// Defines what to render /// /// The following example uses nvelocity syntax: /// /// $page.InsertHtml('Bottom', 'messagestable', "%{partial='shared/newmessage.vm'}") /// /// public override void InsertHtml(string position, string id, object renderOptions) { position = Enum.Parse(typeof(Position), position, true).ToString(); CodeGenerator.Call("new Insertion." + position, AbstractHelper.Quote(id), Render(renderOptions)); } /// /// Replaces the content of the specified target element. /// /// The target element id /// Defines what to render /// /// The following example uses nvelocity syntax: /// /// $page.ReplaceHtml('messagediv', "%{partial='shared/newmessage.vm'}") /// /// public override void ReplaceHtml(String id, object renderOptions) { CodeGenerator.Call("Element.update", AbstractHelper.Quote(id), Render(renderOptions)); } /// /// Replaces the entire target element -- and not only its innerHTML -- /// by the content evaluated. /// /// The target element id /// Defines what to render /// /// The following example uses nvelocity syntax: /// /// $page.Replace('messagediv', "%{partial='shared/newmessage.vm'}") /// /// public override void Replace(String id, object renderOptions) { CodeGenerator.Call("Element.replace", AbstractHelper.Quote(id), Render(renderOptions)); } /// /// Shows the specified elements. /// /// The elements ids. /// /// The elements must exist. /// /// /// The following example uses nvelocity syntax: /// /// $page.Show('div1', 'div2') /// /// public override void Show(params string[] ids) { CodeGenerator.Call("Element.show", AbstractHelper.Quote(ids)); } /// /// Hides the specified elements. /// /// The elements ids. /// /// The elements must exist. /// /// /// The following example uses nvelocity syntax: /// /// $page.Hide('div1', 'div2') /// /// public override void Hide(params string[] ids) { CodeGenerator.Call("Element.hide", AbstractHelper.Quote(ids)); } /// /// Toggles the display status of the specified elements. /// /// The elements ids. /// /// The elements must exist. /// /// /// The following example uses nvelocity syntax: /// /// $page.Toggle('div1', 'div2') /// /// public override void Toggle(params string[] ids) { CodeGenerator.Call("Element.toggle", AbstractHelper.Quote(ids)); } /// /// Remove the specified elements from the DOM. /// /// The elements ids. /// /// The elements must exist. /// /// /// The following example uses nvelocity syntax: /// /// $page.Remove('div1', 'div2') /// /// public override void Remove(params string[] ids) { CodeGenerator.Record("[" + CodeGenerator.BuildJSArguments(Quote(ids)) + "].each(Element.remove)"); } /// /// Creates a generator for an element. /// /// The root expression. /// public override IJSElementGenerator CreateElementGenerator(string root) { return new PrototypeElementGenerator(this, root); } } }