- More AJAX support (couple of bug fixes, added timing for AJAX calls, added support for adding AJAX scripts to a page once and once only)

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@3352 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Gavin Cornwell
2006-07-19 11:50:32 +00:00
parent 43746353a4
commit a1200deadb
17 changed files with 558 additions and 55 deletions

View File

@@ -88,6 +88,8 @@ public final class Utils
private static final String DEFAULT_FILE_IMAGE16 = IMAGE_PREFIX16 + "_default" + IMAGE_POSTFIX;
private static final String DEFAULT_FILE_IMAGE32 = IMAGE_PREFIX32 + "_default" + IMAGE_POSTFIX;
private static final String AJAX_SCRIPTS_WRITTEN = "_alfAjaxScriptsWritten";
private static final Map<String, String> s_fileExtensionMap = new HashMap<String, String>(89, 1.0f);
private static Log logger = LogFactory.getLog(Utils.class);
@@ -1238,4 +1240,37 @@ public final class Utils
return description;
}
/**
* Writes the script tags for including AJAX support, ensuring they
* only get written once per page render.
*
* @param context Faces context
* @param out The response writer
*/
@SuppressWarnings("unchecked")
public static void writeAjaxScripts(FacesContext context, ResponseWriter out)
throws IOException
{
Object present = context.getExternalContext().getRequestMap().get(AJAX_SCRIPTS_WRITTEN);
if (present == null)
{
// write out the scripts
out.write("\n<script type=\"text/javascript\" src=\"");
out.write(context.getExternalContext().getRequestContextPath());
out.write("/scripts/ajax/dojo.js\"> </script>\n");
out.write("<script type=\"text/javascript\" src=\"");
out.write(context.getExternalContext().getRequestContextPath());
out.write("/scripts/ajax/common.js\"> </script>\n");
// write out a global variable to hold the webapp context path
out.write("<script type=\"text/javascript\">var WEBAPP_CONTEXT = '");
out.write(context.getExternalContext().getRequestContextPath());
out.write("';</script>\n");
// add marker to request
context.getExternalContext().getRequestMap().put(AJAX_SCRIPTS_WRITTEN, Boolean.TRUE);
}
}
}

View File

@@ -0,0 +1,137 @@
package org.alfresco.web.ui.repo.component;
import java.io.IOException;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.el.ValueBinding;
import org.alfresco.web.app.Application;
import org.alfresco.web.bean.repository.Repository;
import org.alfresco.web.ui.common.Utils;
import org.alfresco.web.ui.common.component.SelfRenderingComponent;
/**
* JSF component that displays information about a node.
* <p>
* The node to show information on
*
* @author gavinc
*/
public class UINodeInfo extends SelfRenderingComponent
{
protected final static String NODE_INFO_SCRIPTS_WRITTEN = "_alfNodeInfoScripts";
protected Object value = null;
// ------------------------------------------------------------------------------
// Component Impl
@Override
public String getFamily()
{
return "org.alfresco.faces.NodeInfo";
}
@Override
public void restoreState(FacesContext context, Object state)
{
Object values[] = (Object[])state;
// standard component attributes are restored by the super class
super.restoreState(context, values[0]);
this.value = values[1];
}
@Override
public Object saveState(FacesContext context)
{
Object values[] = new Object[8];
// standard component attributes are saved by the super class
values[0] = super.saveState(context);
values[1] = this.value;
return values;
}
@Override
@SuppressWarnings("unchecked")
public void encodeBegin(FacesContext context) throws IOException
{
if (!isRendered()) return;
// if AJAX is disabled don't render anything
if (Application.getClientConfig(context).isAjaxEnabled())
{
ResponseWriter out = context.getResponseWriter();
// output the scripts required by the component (checks are
// made to make sure the scripts are only written once)
Utils.writeAjaxScripts(context, out);
// write out the JavaScript specific to the NodeInfo component,
// again, make sure it's only done once
Object present = context.getExternalContext().getRequestMap().
get(NODE_INFO_SCRIPTS_WRITTEN);
if (present == null)
{
out.write("<script type=\"text/javascript\" src=\"");
out.write(context.getExternalContext().getRequestContextPath());
out.write("/scripts/ajax/node-info.js\"> </script>\n");
context.getExternalContext().getRequestMap().put(
NODE_INFO_SCRIPTS_WRITTEN, Boolean.TRUE);
}
// wrap the child components in a <span> that has the onmouseover
// event which kicks off the request for node information
String id = (String)this.getValue();
out.write("<span onmouseover=\"showNodeInfo('");
out.write(Repository.getStoreRef().toString());
out.write("/");
out.write(id);
out.write("', this)\" onmouseout=\"hideNodeInfo()\">");
}
}
@Override
public void encodeEnd(FacesContext context) throws IOException
{
if (!isRendered()) return;
// if AJAX is disabled don't render anything
if (Application.getClientConfig(context).isAjaxEnabled())
{
context.getResponseWriter().write("</span>");
}
}
// ------------------------------------------------------------------------------
// Strongly typed component property accessors
/**
* Get the value - the value is used in a equals() match against the current value in the
* parent ModeList component to set the selected item.
*
* @return the value
*/
public Object getValue()
{
ValueBinding vb = getValueBinding("value");
if (vb != null)
{
this.value = vb.getValue(getFacesContext());
}
return this.value;
}
/**
* Set the value - the value is used in a equals() match against the current value in the
* parent ModeList component to set the selected item.
*
* @param value the value
*/
public void setValue(Object value)
{
this.value = value;
}
}

View File

@@ -0,0 +1,77 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.web.ui.repo.tag;
import javax.faces.component.UIComponent;
import org.alfresco.web.ui.common.tag.HtmlComponentTag;
/**
* Tag class for the UINodeInfo component
*
* @author gavinc
*/
public class NodeInfoTag extends HtmlComponentTag
{
private String value;
/**
* @see javax.faces.webapp.UIComponentTag#getComponentType()
*/
public String getComponentType()
{
return "org.alfresco.faces.NodeInfo";
}
/**
* @see javax.faces.webapp.UIComponentTag#getRendererType()
*/
public String getRendererType()
{
return null;
}
/**
* @see javax.faces.webapp.UIComponentTag#setProperties(javax.faces.component.UIComponent)
*/
protected void setProperties(UIComponent component)
{
super.setProperties(component);
setStringBindingProperty(component, "value", this.value);
}
/**
* @see org.alfresco.web.ui.common.tag.HtmlComponentTag#release()
*/
public void release()
{
super.release();
this.value = null;
}
/**
* Set the value
*
* @param value the value
*/
public void setValue(String value)
{
this.value = value;
}
}