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