org.apache.click.element
Class JsScript

java.lang.Object
  extended by org.apache.click.element.Element
      extended by org.apache.click.element.ResourceElement
          extended by org.apache.click.element.JsScript
All Implemented Interfaces:
Serializable

public class JsScript
extends ResourceElement

Provides a HEAD element for including inline JavaScript using the <script> tag.

Example usage:

 public class MyPage extends Page {

     public List getHeadElements() {
         // We use lazy loading to ensure the JS is only added the
         // first time this method is called.
         if (headElements == null) {
             // Get the head elements from the super implementation
             headElements = super.getHeadElements();

             JsScript jsScript = new JsScript("alert('Hello World!);");
             headElements.add(jsScript);
         }
         return headElements;
     }
 } 
The jsScript instance will be rendered as follows:
 <script type="text/javascript">
 alert('Hello World');
 </script> 
Below is an example showing how to render inline Javascript from a Velocity template.

First we create a Velocity template (/js/mycorp-template.js) which contains the variable $divId that must be replaced at runtime with the real Div ID attribute:

 hide = function() {
     var div = document.getElementById('$divId');
     div.style.display = "none";
 } 
Next is the Page implementation:
 public class MyPage extends Page {

     public List getHeadElements() {
         // We use lazy loading to ensure the JS is only added the
         // first time this method is called.
         if (headElements == null) {
             // Get the head elements from the super implementation
             headElements = super.getHeadElements();

             // Create a default template model to pass to the template
             Map model = ClickUtils.createTemplateModel(this, getContext());

             // Add the id of the div to hide
             model.put("divId", "myDiv");

             // Specify the path to the JavaScript template
             String templatePath = "/js/mycorp-template.js";

             // Create the inline JavaScript for the given template path and model
             JsScript jsScript = new JsScript(templatePath, model);
             headElements.add(jsScript);
         }
         return headElements;
     }
 } 
The jsScript instance will render as follows (assuming the context path is myApp):
 <script type="text/javascript">
     hide = function() {
         var div = document.getElementById('myDiv');
         div.style.display = "none";
     }
 </style> 

Character data (CDATA) support

Sometimes it is necessary to wrap inline JavaScript in CDATA tags. Two use cases are common for doing this: To wrap the JavaScript content in CDATA tags, set setCharacterData(boolean) to true. Below is shown how the JavaScript content would be rendered:
 <script type="text/javascript">
  /∗<![CDATA[∗/

  if(x < y) alert('Hello');

  /∗]]>∗/
 </script> 
Notice the CDATA tags are commented out which ensures older browsers that don't understand the CDATA tag, will ignore it and only process the actual content.

For an overview of XHTML validation and CDATA tags please see http://javascript.about.com/library/blxhtml.htm.

See Also:
Serialized Form

Field Summary
 
Fields inherited from class org.apache.click.element.ResourceElement
IF_IE, IF_IE7, IF_LESS_THAN_IE7, IF_LESS_THAN_IE9, IF_LESS_THAN_OR_EQUAL_TO_IE7
 
Constructor Summary
JsScript()
          Construct a new inline JavaScript element.
JsScript(String content)
          Construct a new inline JavaScript element with the given content.
JsScript(String template, Map<String,Object> model)
          Construct a new inline JavaScript element for the given template path and template model.
 
Method Summary
 boolean equals(Object o)
           
 String getContent()
          Return the JsScript content.
 Map<String,Object> getModel()
          Return the model of the template to render.
 String getTag()
          Returns the JavaScript HTML tag: <script>.
 String getTemplate()
          Return the path of the template to render.
 int hashCode()
           
 boolean isCharacterData()
          Return true if the JsScript's content should be wrapped in CDATA tags, false otherwise.
 boolean isExecuteOnDomReady()
          Return true if the JsScript content must be executed as soon as the browser DOM is ready, false otherwise.
 void render(HtmlStringBuffer buffer)
          Render the HTML representation of the JsScript element to the specified buffer.
protected  void renderContent(HtmlStringBuffer buffer, Context context)
          Render the JsScript content to the specified buffer.
protected  void renderDomReadyPrefix(HtmlStringBuffer buffer)
          Render the "Click.addLoadEvent" function prefix to ensure the script is executed as soon as the browser DOM is available.
protected  void renderDomReadySuffix(HtmlStringBuffer buffer)
          Render the "Click.addLoadEvent" function suffix.
 void setCharacterData(boolean characterData)
          Sets whether the JsScript's content should be wrapped in CDATA tags or not.
 void setContent(String content)
          Set the JsScript content.
 void setExecuteOnDomReady(boolean executeOnDomReady)
          Sets whether the JsScript content must be executed as soon as the browser DOM is ready.
 void setModel(Map<String,Object> model)
          Set the model of the template to render.
 void setTemplate(String template)
          Set the path of the template to render.
 
Methods inherited from class org.apache.click.element.ResourceElement
getConditionalComment, getVersionIndicator, isRenderId, isUnique, setConditionalComment, setRenderId, setVersionIndicator
 
Methods inherited from class org.apache.click.element.Element
appendAttributes, getAttribute, getAttributes, getContext, getId, hasAttribute, hasAttributes, setAttribute, setId, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

JsScript

public JsScript()
Construct a new inline JavaScript element.


JsScript

public JsScript(String content)
Construct a new inline JavaScript element with the given content.

Parameters:
content - the JavaScript content

JsScript

public JsScript(String template,
                Map<String,Object> model)
Construct a new inline JavaScript element for the given template path and template model.

When the JsScript is rendered the template and model will be merged and the result will be rendered together with any JsScript content.

For example:

 public class MyPage extends Page {
     public void onInit() {
         Context context = getContext();

         // Create a default template model
         Map model = ClickUtils.createTemplateModel(this, context);

         // Create JsScript for the given template path and model
         JsScript script = new JsScript("/mypage-template.js", model);

         // Add script to the Page Head elements
         getHeadElements().add(script);
     }
 } 

Parameters:
template - the path of the template to render
model - the template model
Method Detail

getTag

public String getTag()
Returns the JavaScript HTML tag: <script>.

Overrides:
getTag in class Element
Returns:
the JavaScript HTML tag: <script>

getContent

public String getContent()
Return the JsScript content.

Returns:
the JsScript content

setContent

public void setContent(String content)
Set the JsScript content.

Parameters:
content - the JsScript content

isCharacterData

public boolean isCharacterData()
Return true if the JsScript's content should be wrapped in CDATA tags, false otherwise.

Returns:
true if the JsScript's content should be wrapped in CDATA tags, false otherwise

setCharacterData

public void setCharacterData(boolean characterData)
Sets whether the JsScript's content should be wrapped in CDATA tags or not.

Parameters:
characterData - true indicates that the JsScript's content should be wrapped in CDATA tags, false otherwise

isExecuteOnDomReady

public boolean isExecuteOnDomReady()
Return true if the JsScript content must be executed as soon as the browser DOM is ready, false otherwise. Default value is false.

Returns:
true if the JsScript content must be executed as soon as the browser DOM is ready.
See Also:
setExecuteOnDomReady(boolean)

setExecuteOnDomReady

public void setExecuteOnDomReady(boolean executeOnDomReady)
Sets whether the JsScript content must be executed as soon as the browser DOM is ready.

If this flag is true, the JsScript content will be registered with the "Click.addLoadEvent" function from the JavaScript file "/click/control.js".

Please note: when setting this flag to true, the JavaScript file "/click/control.js" must already be included in the Page or Control, it won't be included automatically.

Also note: for Ajax requests the JsScript content won't be registered with the "Click.addLoadEvent" function because Ajax requests does not trigger the browser's DOM loaded event. Instead the JsScript content will be evaluated immediately by the browser.

Parameters:
executeOnDomReady - indicates whether the JsScript content must be executed as soon as the browser DOM is ready.

getTemplate

public String getTemplate()
Return the path of the template to render.

Returns:
the path of the template to render
See Also:
setTemplate(java.lang.String)

setTemplate

public void setTemplate(String template)
Set the path of the template to render.

If the template property is set, the template and model will be merged and the result will be rendered together with any JsScript content.

Parameters:
template - the path of the template to render

getModel

public Map<String,Object> getModel()
Return the model of the template to render.

Returns:
the model of the template to render
See Also:
setModel(java.util.Map)

setModel

public void setModel(Map<String,Object> model)
Set the model of the template to render.

If the template property is set, the template and model will be merged and the result will be rendered together with any JsScript content.

Parameters:
model - the model of the template to render

render

public void render(HtmlStringBuffer buffer)
Render the HTML representation of the JsScript element to the specified buffer.

Overrides:
render in class ResourceElement
Parameters:
buffer - the buffer to render output to

equals

public boolean equals(Object o)
Overrides:
equals in class Object
Parameters:
o - the object with which to compare this instance with
Returns:
true if the specified object is the same as this object
See Also:
Object.equals(java.lang.Object)

hashCode

public int hashCode()
Overrides:
hashCode in class Object
Returns:
a hash code value for this object
See Also:
Object.hashCode()

renderContent

protected void renderContent(HtmlStringBuffer buffer,
                             Context context)
Render the JsScript content to the specified buffer.

Please note: if the template property is set, this method will merge the template and model and the result will be rendered, together with the JsScript content, to the specified buffer.

Parameters:
buffer - the buffer to append the output to
context - the request context

renderDomReadyPrefix

protected void renderDomReadyPrefix(HtmlStringBuffer buffer)
Render the "Click.addLoadEvent" function prefix to ensure the script is executed as soon as the browser DOM is available. The prefix is "Click.addLoadEvent(function(){".

Parameters:
buffer - the buffer to append the Click.addLoadEvent function to
See Also:
renderDomReadySuffix(org.apache.click.util.HtmlStringBuffer)

renderDomReadySuffix

protected void renderDomReadySuffix(HtmlStringBuffer buffer)
Render the "Click.addLoadEvent" function suffix. The suffix is "});".

Parameters:
buffer - buffer to append the conditional comment prefix
See Also:
renderDomReadyPrefix(org.apache.click.util.HtmlStringBuffer)