}
public static String getAutoScrollFunction(FacesContext facesContext)
{
ScriptContext script = new ScriptContext(MyfacesConfig
.getCurrentInstance(facesContext.getExternalContext())
.isPrettyHtml());
script.prettyLineIncreaseIndent();
script.append("function ");
script.append(AUTO_SCROLL_FUNCTION);
script.append("()");
script.append("{");
script.append("var x = 0; var y = 0;");
script.append("if (self.pageXOffset || self.pageYOffset)");
script.append("{");
script.append("x = self.pageXOffset;");
script.prettyLine();
script.append("y = self.pageYOffset;");
script.append("}");
script.append(" else if ((document.documentElement && document.documentElement.scrollLeft)||"+
"(document.documentElement && document.documentElement.scrollTop))");
script.append("{");
script.append("x = document.documentElement.scrollLeft;");
script.prettyLine();
script.append("y = document.documentElement.scrollTop;");
script.append("}");
script.append(" else if (document.body) ");
script.append("{");
script.append("x = document.body.scrollLeft;");
script.prettyLine();
script.append("y = document.body.scrollTop;");
script.append("}");
script.append("return x + \",\" + y;");
script.append("}");
ExternalContext externalContext = facesContext.getExternalContext();
String oldViewId = JavascriptUtils.getOldViewId(externalContext);
if (oldViewId != null
&& oldViewId.equals(facesContext.getViewRoot().getViewId()))
{
//ok, we stayed on the same page, so let's scroll it to the former place
String scrolling = (String) externalContext
.getRequestParameterMap().get(AUTO_SCROLL_PARAM);
if (scrolling != null && scrolling.length() > 0)
{
int x = 0;
int y = 0;
int comma = scrolling.indexOf(',');
if (comma == -1)
{
log.warning("Illegal autoscroll request parameter: "
+ scrolling);
}
else
{
try
{
//we convert to int against XSS vulnerability
x = Integer.parseInt(scrolling.substring(0, comma));
}
catch (NumberFormatException e)
{
log.warning("Error getting x offset for autoscroll feature. Bad param value: "
+ scrolling);
x = 0; //ignore false numbers
}
try
{
//we convert to int against XSS vulnerability
y = Integer.parseInt(scrolling.substring(comma + 1));
}
catch (NumberFormatException e)
{
log.warning("Error getting y offset for autoscroll feature. Bad param value: "
+ scrolling);
y = 0; //ignore false numbers
}
}
script.append("window.scrollTo(").append(x).append(",")
.append(y).append(");\n");
}
}
return script.toString();
}