001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *   http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package org.apache.myfaces.tobago.taglib.component;
021    
022    import org.apache.myfaces.tobago.apt.annotation.BodyContent;
023    import org.apache.myfaces.tobago.apt.annotation.Tag;
024    import org.apache.myfaces.tobago.apt.annotation.TagAttribute;
025    import org.apache.myfaces.tobago.component.ComponentUtil;
026    import org.apache.myfaces.tobago.component.UIPage;
027    
028    import javax.faces.context.FacesContext;
029    import javax.servlet.jsp.JspException;
030    import javax.servlet.jsp.tagext.BodyTagSupport;
031    
032    /*
033     * $Id: ScriptTag.java 1368577 2012-08-02 16:20:31Z lofwyr $
034     */
035    
036    /**
037     * This tag add client side script to the rendered page.
038     */
039    @Tag(name = "script", bodyContent = BodyContent.JSP)
040    //    @Tag(name="script", bodyContent=BodyContent.TAGDEPENDENT)
041    //    @BodyContentDescription(contentType="javascript")
042    public class ScriptTag extends BodyTagSupport {
043    
044      private static final long serialVersionUID = 3253751129824779272L;
045    
046      private String file;
047      private String onload;
048      private String onunload;
049      private String onexit;
050      private String onsubmit;
051    
052      @Override
053      public int doEndTag() throws JspException {
054    
055        FacesContext facesContext = FacesContext.getCurrentInstance();
056        UIPage page = ComponentUtil.findPage(facesContext);
057        if (page == null) {
058          throw new JspException("The ScriptTag cannot find UIPage. "
059              + "Check you have defined the ScriptTag inside of the PageTag!");
060        }
061    
062        if (file != null) {
063          page.getScriptFiles().add(ComponentUtil.getValueFromEl(file));
064        }
065        if (onload != null) {
066          page.getOnloadScripts().add(ComponentUtil.getValueFromEl(onload));
067        }
068        if (onunload != null) {
069          page.getOnunloadScripts().add(ComponentUtil.getValueFromEl(onunload));
070        }
071        if (onexit != null) {
072          page.getOnexitScripts().add(ComponentUtil.getValueFromEl(onexit));
073        }
074        if (onsubmit != null) {
075          page.getOnsubmitScripts().add(ComponentUtil.getValueFromEl(onsubmit));
076        }
077        if (bodyContent != null) {
078          String script = bodyContent.getString();
079          bodyContent.clearBody();
080          page.getScriptBlocks().add(ComponentUtil.getValueFromEl(script));
081        }
082    
083        return EVAL_PAGE;
084      }
085    
086      @Override
087      public int doStartTag() throws JspException {
088        return EVAL_BODY_BUFFERED;
089      }
090    
091      @Override
092      public void release() {
093        super.release();
094        file = null;
095        onload = null;
096        onunload = null;
097        onexit = null;
098        onsubmit = null;
099      }
100    
101      public String getFile() {
102        return file;
103      }
104    
105    
106      /**
107       * Absolute url to script file or script name to lookup in tobago resource path
108       */
109      @TagAttribute
110      public void setFile(String file) {
111        this.file = file;
112      }
113    
114      public String getOnload() {
115        return onload;
116      }
117    
118    
119      /**
120       * A script function which is invoked during onLoad Handler on the client.
121       */
122      @TagAttribute
123      public void setOnload(String onload) {
124        this.onload = onload;
125      }
126    
127      /**
128       * A script function which is invoked during onUnload Handler on the client,
129       * if the action is a normal submit inside of Tobago.
130       */
131      @TagAttribute
132      public void setOnunload(String onunload) {
133        this.onunload = onunload;
134      }
135    
136      /**
137       * A script function which is invoked during onUnload Handler on the client,
138       * when the unload is invoked to a non Tobago page.
139       * E.g. close-button, back-button, entering new url, etc.
140       */
141      @TagAttribute
142      public void setOnexit(String onexit) {
143        this.onexit = onexit;
144      }
145    
146      /**
147       * A script function which is invoked on client just before submitting the action.
148       * This should be a single function call. If the result is typeof 'boolean' and false
149       * the further processing is canceled and the page is not submitted.
150       */
151      @TagAttribute
152      public void setOnsubmit(String onsubmit) {
153        this.onsubmit = onsubmit;
154      }
155    }
156