org.omnifaces.component.script
Class OnloadScript

java.lang.Object
  extended by javax.faces.component.UIComponent
      extended by javax.faces.component.UIComponentBase
          extended by org.omnifaces.component.script.ScriptFamily
              extended by org.omnifaces.component.script.OnloadScript
All Implemented Interfaces:
java.util.EventListener, javax.faces.component.PartialStateHolder, javax.faces.component.StateHolder, javax.faces.event.ComponentSystemEventListener, javax.faces.event.FacesListener, javax.faces.event.SystemEventListener, javax.faces.event.SystemEventListenerHolder
Direct Known Subclasses:
Highlight

public class OnloadScript
extends ScriptFamily
implements javax.faces.event.SystemEventListener

OnloadScript is an extension to <h:outputScript> which will be executed in the end of the HTML body (thus when all HTML elements are initialized in the HTML DOM tree) and will re-execute its script body on every ajax request. This is particularly useful if you want to re-execute a specific helper script to manipulate the HTML DOM tree, such as (re-)adding fancy tooltips, performing highlights, etcetera, also after changes in the HTML DOM tree on ajax responses.

You can put it anywhere in the view, it will always be relocated to the end of body.

 <o:onloadScript>alert('OnloadScript is invoked!');</o:onloadScript>
 

Author:
Bauke Scholtz

Field Summary
static java.lang.String COMPONENT_TYPE
          The standard component type.
 
Fields inherited from class org.omnifaces.component.script.ScriptFamily
COMPONENT_FAMILY
 
Fields inherited from class javax.faces.component.UIComponent
BEANINFO_KEY, bindings, COMPOSITE_COMPONENT_TYPE_KEY, COMPOSITE_FACET_NAME, CURRENT_COMPONENT, CURRENT_COMPOSITE_COMPONENT, FACETS_KEY, VIEW_LOCATION_KEY
 
Constructor Summary
OnloadScript()
           
 
Method Summary
 void encodeBegin(javax.faces.context.FacesContext context)
          If this component is rendered and there are any children, then start the <script> element.
 void encodeEnd(javax.faces.context.FacesContext context)
          If this component is rendered and there are any children, then end the <script> element.
 boolean isListenerForSource(java.lang.Object source)
          Returns true if the given source is an instance of OnloadScript or UIViewRoot.
 void processEvent(javax.faces.event.ComponentSystemEvent event)
          If the event is a PostAddToViewEvent, then relocate the component to end of body, so that we can make sure that the script is executed after all HTML DOM elements are been created.
 void processEvent(javax.faces.event.SystemEvent event)
          If the event is a PreRenderViewEvent and the current request is an ajax request and this component is rendered and there are any children, then encode the children as Ajax.oncomplete(String...).
 
Methods inherited from class org.omnifaces.component.script.ScriptFamily
getFamily, getRendersChildren
 
Methods inherited from class javax.faces.component.UIComponentBase
addClientBehavior, addFacesListener, broadcast, clearInitialState, decode, encodeChildren, findComponent, getAttributes, getChildCount, getChildren, getClientBehaviors, getClientId, getDefaultEventName, getEventNames, getFacesContext, getFacesListeners, getFacet, getFacetCount, getFacets, getFacetsAndChildren, getId, getParent, getRenderer, getRendererType, getValueBinding, invokeOnComponent, isRendered, isTransient, markInitialState, processDecodes, processRestoreState, processSaveState, processUpdates, processValidators, queueEvent, removeFacesListener, restoreAttachedState, restoreState, saveAttachedState, saveState, setId, setParent, setRendered, setRendererType, setTransient, setValueBinding
 
Methods inherited from class javax.faces.component.UIComponent
encodeAll, getClientId, getCompositeComponentParent, getContainerClientId, getCurrentComponent, getCurrentCompositeComponent, getListenersForEventClass, getNamingContainer, getResourceBundleMap, getStateHelper, getStateHelper, getValueExpression, initialStateMarked, isCompositeComponent, isInView, isVisitable, popComponentFromEL, pushComponentToEL, setInView, setValueExpression, subscribeToEvent, unsubscribeFromEvent, visitTree
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COMPONENT_TYPE

public static final java.lang.String COMPONENT_TYPE
The standard component type.

See Also:
Constant Field Values
Constructor Detail

OnloadScript

public OnloadScript()
Method Detail

isListenerForSource

public boolean isListenerForSource(java.lang.Object source)
Returns true if the given source is an instance of OnloadScript or UIViewRoot.

Specified by:
isListenerForSource in interface javax.faces.event.SystemEventListener

processEvent

public void processEvent(javax.faces.event.ComponentSystemEvent event)
                  throws javax.faces.event.AbortProcessingException
If the event is a PostAddToViewEvent, then relocate the component to end of body, so that we can make sure that the script is executed after all HTML DOM elements are been created. Else if the event is a PostRestoreStateEvent and the current request is an ajax request, then subscribe to the PreRenderViewEvent event.

Specified by:
processEvent in interface javax.faces.event.ComponentSystemEventListener
Overrides:
processEvent in class javax.faces.component.UIComponent
Throws:
javax.faces.event.AbortProcessingException

processEvent

public void processEvent(javax.faces.event.SystemEvent event)
                  throws javax.faces.event.AbortProcessingException
If the event is a PreRenderViewEvent and the current request is an ajax request and this component is rendered and there are any children, then encode the children as Ajax.oncomplete(String...).

Specified by:
processEvent in interface javax.faces.event.SystemEventListener
Throws:
javax.faces.event.AbortProcessingException

encodeBegin

public void encodeBegin(javax.faces.context.FacesContext context)
                 throws java.io.IOException
If this component is rendered and there are any children, then start the <script> element.

Overrides:
encodeBegin in class javax.faces.component.UIComponentBase
Throws:
java.io.IOException

encodeEnd

public void encodeEnd(javax.faces.context.FacesContext context)
               throws java.io.IOException
If this component is rendered and there are any children, then end the <script> element.

Overrides:
encodeEnd in class javax.faces.component.UIComponentBase
Throws:
java.io.IOException