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 578592 2007-09-23 18:51:32Z bommel $
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 static final long serialVersionUID = 3253751129824779272L;
043
044 private String file;
045 private String onload;
046 private String onunload;
047 private String onexit;
048 private String onsubmit;
049
050 @Override
051 public int doEndTag() throws JspException {
052
053 FacesContext facesContext = FacesContext.getCurrentInstance();
054 UIPage page = ComponentUtil.findPage(facesContext);
055 if (page == null) {
056 throw new JspException("The ScriptTag cannot find UIPage. "
057 + "Check you have defined the ScriptTag inside of the PageTag!");
058 }
059
060 if (file != null) {
061 page.getScriptFiles().add(ComponentUtil.getValueFromEl(file));
062 }
063 if (onload != null) {
064 page.getOnloadScripts().add(ComponentUtil.getValueFromEl(onload));
065 }
066 if (onunload != null) {
067 page.getOnunloadScripts().add(ComponentUtil.getValueFromEl(onunload));
068 }
069 if (onexit != null) {
070 page.getOnexitScripts().add(ComponentUtil.getValueFromEl(onexit));
071 }
072 if (onsubmit != null) {
073 page.getOnsubmitScripts().add(ComponentUtil.getValueFromEl(onsubmit));
074 }
075 if (bodyContent != null) {
076 String script = bodyContent.getString();
077 bodyContent.clearBody();
078 page.getScriptBlocks().add(ComponentUtil.getValueFromEl(script));
079 }
080
081 return EVAL_PAGE;
082 }
083
084 @Override
085 public int doStartTag() throws JspException {
086 return EVAL_BODY_BUFFERED;
087 }
088
089 @Override
090 public void release() {
091 super.release();
092 file = null;
093 onload = null;
094 onunload = null;
095 onexit = null;
096 onsubmit = null;
097 }
098
099 public String getFile() {
100 return file;
101 }
102
103
104 /**
105 * Absolute url to script file or script name to lookup in tobago resource path
106 */
107 @TagAttribute
108 public void setFile(String file) {
109 this.file = file;
110 }
111
112 public String getOnload() {
113 return onload;
114 }
115
116
117 /**
118 * A script function which is invoked during onLoad Handler on the client.
119 */
120 @TagAttribute
121 public void setOnload(String onload) {
122 this.onload = onload;
123 }
124
125 /**
126 * A script function which is invoked during onUnload Handler on the client,
127 * if the action is a normal submit inside of Tobago.
128 */
129 @TagAttribute
130 public void setOnunload(String onunload) {
131 this.onunload = onunload;
132 }
133
134 /**
135 * A script function which is invoked during onUnload Handler on the client,
136 * when the unload is invoked to a non Tobago page.
137 * E.g. close-button, back-button, entering new url, etc.
138 */
139 @TagAttribute
140 public void setOnexit(String onexit) {
141 this.onexit = onexit;
142 }
143
144 /**
145 * A script function which is invoked on client just before submitting the action.
146 * This should be a single function call. If the result is typeof 'boolean' and false
147 * the further processing is canceled and the page is not submitted.
148 */
149 @TagAttribute
150 public void setOnsubmit(String onsubmit) {
151 this.onsubmit = onsubmit;
152 }
153 }
154