Search in sources :

Example 1 with BrowserSniffer

use of com.liferay.faces.util.client.BrowserSniffer in project liferay-faces-alloy by liferay.

the class InputDate method getPattern.

@Override
public String getPattern() {
    String datePattern;
    FacesContext facesContext = FacesContext.getCurrentInstance();
    BrowserSniffer browserSniffer = BrowserSnifferFactory.getBrowserSnifferInstance(facesContext.getExternalContext());
    if (browserSniffer.isMobile() && isNativeWhenMobile()) {
        datePattern = DEFAULT_HTML5_DATE_PATTERN;
    } else {
        datePattern = super.getPattern();
        if (datePattern == null) {
            // Provide a default datePattern based on the locale.
            Object locale = getLocale();
            datePattern = getDefaultDatePattern(locale);
        }
    }
    return datePattern;
}
Also used : FacesContext(javax.faces.context.FacesContext) BrowserSniffer(com.liferay.faces.util.client.BrowserSniffer)

Example 2 with BrowserSniffer

use of com.liferay.faces.util.client.BrowserSniffer in project liferay-faces-alloy by liferay.

the class ScriptsEncoderAlloyImpl method encodeScripts.

private void encodeScripts(FacesContext facesContext, ResponseWriter responseWriter, List<Script> scripts) throws IOException {
    Set<String> sortedModules = new TreeSet<String>();
    List<Script> alloyScripts = new ArrayList<Script>();
    List<Script> basicScripts = new ArrayList<Script>();
    for (Script script : scripts) {
        Script.ModulesType modulesType = script.getModulesType();
        if (Script.ModulesType.ALLOY.equals(modulesType)) {
            String[] modules = script.getModules();
            if (modules != null) {
                for (String module : modules) {
                    sortedModules.add(module.trim());
                }
            }
            alloyScripts.add(script);
        } else {
            basicScripts.add(script);
        }
    }
    for (Script script : basicScripts) {
        responseWriter.write(script.getSourceCode());
    }
    if (!alloyScripts.isEmpty()) {
        BrowserSniffer browserSniffer = BrowserSnifferFactory.getBrowserSnifferInstance(facesContext.getExternalContext());
        String alloyBeginScript = AlloyRendererUtil.getAlloyBeginScript(sortedModules, null, browserSniffer);
        responseWriter.write(alloyBeginScript);
        for (Script alloyScript : alloyScripts) {
            responseWriter.write("(function(){");
            responseWriter.write(alloyScript.getSourceCode());
            responseWriter.write("})();");
        }
        responseWriter.write("});");
    }
}
Also used : Script(com.liferay.faces.util.client.Script) BrowserSniffer(com.liferay.faces.util.client.BrowserSniffer) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList)

Example 3 with BrowserSniffer

use of com.liferay.faces.util.client.BrowserSniffer in project liferay-faces-alloy by liferay.

the class OutputScriptRenderer method encodeChildren.

@Override
public void encodeChildren(FacesContext facesContext, UIComponent uiComponent) throws IOException {
    OutputScript outputScript = (OutputScript) uiComponent;
    String name = outputScript.getName();
    // render a JSF resource.
    if ((name != null) && (name.length() > 0)) {
        super.encodeChildren(facesContext, uiComponent);
    } else // Otherwise, since it is not a JSF resource:
    {
        // If target="body" and this is not an Ajax request, then
        String use = outputScript.getUse();
        String target = outputScript.getTarget();
        if ("body".equals(target) && !facesContext.getPartialViewContext().isAjaxRequest()) {
            ResponseWriter responseWriter = facesContext.getResponseWriter();
            BufferedScriptResponseWriter bufferedScriptResponseWriter = new BufferedScriptResponseWriter();
            facesContext.setResponseWriter(bufferedScriptResponseWriter);
            super.encodeChildren(facesContext, uiComponent);
            facesContext.setResponseWriter(responseWriter);
            Script script;
            String bufferedScriptString = bufferedScriptResponseWriter.toString();
            ExternalContext externalContext = facesContext.getExternalContext();
            if ((use != null) && (use.length() > 0)) {
                String[] modules = use.split(",");
                script = ScriptFactory.getScriptInstance(externalContext, bufferedScriptString, modules, Script.ModulesType.ALLOY);
            } else {
                script = ScriptFactory.getScriptInstance(externalContext, bufferedScriptString);
            }
            // Render the script at the bottom of the page immediately before the closing </body> tag.
            FacesRequestContext facesRequestContext = FacesRequestContext.getCurrentInstance();
            facesRequestContext.addScript(script);
        } else // correct modules around the script.
        if ((use != null) && (use.length() > 0)) {
            // Delegate to the JSF runtime Renderer, but replace the JSF runtime ResponseWriter with an
            // OutputScriptResponseWriter which will render the YUI sandbox code around the script.
            ResponseWriter responseWriter = facesContext.getResponseWriter();
            // In order to determine the exact YUI sandbox string to write, the modules and browser information
            // must be passed to RendererUtil.getAlloyBeginScript().
            String[] modules = use.split(",");
            BrowserSniffer browserSniffer = BrowserSnifferFactory.getBrowserSnifferInstance(facesContext.getExternalContext());
            String alloyBeginScript = AlloyRendererUtil.getAlloyBeginScript(modules, null, browserSniffer);
            OutputScriptResponseWriter outputScriptResponseWriter = new OutputScriptResponseWriter(responseWriter, alloyBeginScript);
            super.encodeChildren(facesContext, uiComponent, outputScriptResponseWriter);
        } else // Otherwise, simply delegate to the JSF runtime renderer since target="head" or target="form".
        {
            super.encodeChildren(facesContext, uiComponent);
        }
    }
}
Also used : FacesRequestContext(com.liferay.faces.util.context.FacesRequestContext) OutputScript(com.liferay.faces.alloy.component.outputscript.OutputScript) Script(com.liferay.faces.util.client.Script) BrowserSniffer(com.liferay.faces.util.client.BrowserSniffer) BufferedScriptResponseWriter(com.liferay.faces.util.render.BufferedScriptResponseWriter) BufferedScriptResponseWriter(com.liferay.faces.util.render.BufferedScriptResponseWriter) ResponseWriter(javax.faces.context.ResponseWriter) ExternalContext(javax.faces.context.ExternalContext) OutputScript(com.liferay.faces.alloy.component.outputscript.OutputScript)

Example 4 with BrowserSniffer

use of com.liferay.faces.util.client.BrowserSniffer in project liferay-faces-alloy by liferay.

the class InputDateRenderer method getAlloyClassName.

@Override
public String getAlloyClassName(FacesContext facesContext, UIComponent uiComponent) {
    String alloyClassName = super.getAlloyClassName(facesContext, uiComponent);
    BrowserSniffer browserSniffer = BrowserSnifferFactory.getBrowserSnifferInstance(facesContext.getExternalContext());
    InputDate inputDate = (InputDate) uiComponent;
    if (isNative(browserSniffer, inputDate)) {
        alloyClassName = alloyClassName.concat("Native");
    }
    return alloyClassName;
}
Also used : InputDate(com.liferay.faces.alloy.component.inputdate.InputDate) BrowserSniffer(com.liferay.faces.util.client.BrowserSniffer)

Example 5 with BrowserSniffer

use of com.liferay.faces.util.client.BrowserSniffer in project liferay-faces-alloy by liferay.

the class InputDateTimeRenderer method encodeMarkupEnd.

@Override
public void encodeMarkupEnd(FacesContext facesContext, UIComponent uiComponent) throws IOException {
    ResponseWriter responseWriter = facesContext.getResponseWriter();
    String clientId = uiComponent.getClientId(facesContext);
    String inputClientId = clientId.concat(INPUT_SUFFIX);
    BrowserSniffer browserSniffer = BrowserSnifferFactory.getBrowserSnifferInstance(facesContext.getExternalContext());
    InputDateTime inputDateTime = (InputDateTime) uiComponent;
    InputDateTimeResponseWriter inputDateTimeResponseWriter = getInputDateTimeResponseWriter(responseWriter, inputClientId, isNative(browserSniffer, inputDateTime));
    super.encodeMarkupEnd(facesContext, uiComponent, inputDateTimeResponseWriter);
    // Determine whether or not the text input is enabled.
    boolean disabled = inputDateTime.isDisabled();
    // If the "showOn" attribute indicates that a button is to be rendered and the component is not being rendered
    // for a mobile browser, then render the button.
    String showOn = inputDateTime.getShowOn();
    if (("both".equals(showOn) || "button".equals(showOn)) && !isNative(browserSniffer, inputDateTime)) {
        ApplicationFactory applicationFactory = (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
        Application application = applicationFactory.getApplication();
        // Create an icon component that is to remain detached from the component tree.
        Icon icon = (Icon) application.createComponent(Icon.COMPONENT_TYPE);
        String buttonIconName = getButtonIconName();
        icon.setName(buttonIconName);
        // Create a button component that that is also to remain detached from the component tree.
        Button button = (Button) application.createComponent(Button.COMPONENT_TYPE);
        List<UIComponent> buttonChildren = button.getChildren();
        buttonChildren.add(icon);
        button.setDisabled(disabled);
        // If the component is enabled, then
        if (!disabled) {
            if ("button".equals(showOn)) {
                String buttonClientId = getButtonClientId(facesContext, inputDateTime);
                button.setId(buttonClientId);
            } else {
                // If the both the button and the input are supposed to trigger the datePicker, set the button's
                // onclick to focus and click the input.
                String onClick = BUTTON_ON_CLICK_EVENT.replace("{0}", inputClientId);
                button.setOnclick(onClick);
            }
        }
        // Invoke the button's renderer so that it renders itself and its child icon.
        button.encodeAll(facesContext);
    }
    // If the component is enabled, then create the boundingBox and contentBox of the picker.
    if (!disabled) {
        responseWriter.startElement("div", uiComponent);
        String boundingBoxClientId = clientId.concat(BOUNDING_BOX_SUFFIX);
        responseWriter.writeAttribute("id", boundingBoxClientId, null);
        responseWriter.startElement("div", uiComponent);
        String contentBoxClientId = clientId.concat(CONTENT_BOX_SUFFIX);
        responseWriter.writeAttribute("id", contentBoxClientId, null);
        responseWriter.endElement("div");
        responseWriter.endElement("div");
    }
    // Finish the encoding of the outermost </div> element.
    responseWriter.endElement("div");
}
Also used : BrowserSniffer(com.liferay.faces.util.client.BrowserSniffer) ResponseWriter(javax.faces.context.ResponseWriter) Button(com.liferay.faces.alloy.component.button.Button) UIComponent(javax.faces.component.UIComponent) ApplicationFactory(javax.faces.application.ApplicationFactory) Icon(com.liferay.faces.alloy.component.icon.Icon) InputDateTime(com.liferay.faces.alloy.component.inputdatetime.InputDateTime) Application(javax.faces.application.Application)

Aggregations

BrowserSniffer (com.liferay.faces.util.client.BrowserSniffer)15 ResponseWriter (javax.faces.context.ResponseWriter)6 InputDateTime (com.liferay.faces.alloy.component.inputdatetime.InputDateTime)3 InputDate (com.liferay.faces.alloy.component.inputdate.InputDate)2 InputDateTimeResponseWriter (com.liferay.faces.alloy.component.inputdatetime.internal.InputDateTimeResponseWriter)2 InputTime (com.liferay.faces.alloy.component.inputtime.InputTime)2 Script (com.liferay.faces.util.client.Script)2 BufferedScriptResponseWriter (com.liferay.faces.util.render.BufferedScriptResponseWriter)2 JavaScriptFragment (com.liferay.faces.util.render.JavaScriptFragment)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 TimeZone (java.util.TimeZone)2 Application (javax.faces.application.Application)2 ExternalContext (javax.faces.context.ExternalContext)2 FacesContext (javax.faces.context.FacesContext)2 Button (com.liferay.faces.alloy.component.button.Button)1 Icon (com.liferay.faces.alloy.component.icon.Icon)1 OutputScript (com.liferay.faces.alloy.component.outputscript.OutputScript)1 EscapedClientId (com.liferay.faces.alloy.render.internal.EscapedClientId)1 FacesResource (com.liferay.faces.util.application.FacesResource)1