public void writeJSSDK(FacesContext context, UIComponent component, OAuthEndpoint bean)
throws IOException {
ResponseWriter writer = context.getResponseWriter();
UIViewRootEx2 rootEx = (UIViewRootEx2)context.getViewRoot();
if(rootEx.getEncodeProperty("xsp.extlib.facebook.jssdk")==null) {
writer.startElement("div", component);
writer.writeAttribute("id","fb-root", null);
writer.endElement("div");
writer.write("\n");
writer.startElement("script", component);
writer.writeAttribute("type", "text/javascript", null);
JsonObject config = createJSONConfig(bean);
StringBuilder b = new StringBuilder(256);
try {
JsonGenerator.toJson(JsonJavaFactory.instance,b,config,true);
}
catch(JsonException ex) {
throw new FacesExceptionEx(ex);
}
writer.writeText("\nwindow.fbAsyncInit = function() {", null);
writer.writeText("\nFB.init(" + b + ");};", null);
// We need to add the xmlns:fb to the HTML tag
writer.writeText("\n(function() {", null);
writer.writeText("var elements = document.getElementsByTagName(\"html\");", null);
writer.writeText("var htmlElement = elements[0];", null);
writer.writeText("var attribute = document.createAttribute(\"xmlns:fb\");", null);
writer.writeText("attribute.nodeValue = \"http://www.facebook.com/2008/fbml\";", null);
writer.writeText("htmlElement.setAttributeNode(attribute); ", null);
writer.writeText("}());", null);
// An now we can load the FB script asynchonously
// Load the SDK Asynchronously
writer.writeText("\n(function(d){",null);
writer.writeText("var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}",null);
writer.writeText("js = d.createElement('script'); js.id = id; js.async = true;",null);
writer.writeText("js.src = \"//connect.facebook.net/en_US/all.js\";",null);
writer.writeText("d.getElementsByTagName('head')[0].appendChild(js);",null);
writer.writeText("}(document));",null);
writer.endElement("script");
writer.write("\n");
}
rootEx.putEncodeProperty("xsp.extlib.facebook.jssdk", Boolean.TRUE);
}