Search in sources :

Example 6 with ClientComponent

use of com.liferay.faces.util.component.ClientComponent in project liferay-faces-alloy by liferay.

the class AlloyRendererCommon method encodeJavaScriptBegin.

/* package-private */
static void encodeJavaScriptBegin(FacesContext facesContext, UIComponent uiComponent, AlloyRenderer alloyRenderer, String[] modules, boolean sandboxed) throws IOException {
    ResponseWriter responseWriter = facesContext.getResponseWriter();
    if (sandboxed) {
        String yuiConfig = alloyRenderer.getYUIConfig(facesContext, responseWriter, uiComponent);
        BrowserSniffer browserSniffer = BrowserSnifferFactory.getBrowserSnifferInstance(facesContext.getExternalContext());
        String alloyBeginScript = AlloyRendererUtil.getAlloyBeginScript(modules, yuiConfig, browserSniffer);
        responseWriter.write(alloyBeginScript);
    }
    if (facesContext.getPartialViewContext().isAjaxRequest() && (uiComponent instanceof ClientComponent)) {
        ClientComponent clientComponent = (ClientComponent) uiComponent;
        String clientVarName = alloyRenderer.getClientVarName(facesContext, clientComponent);
        String clientKey = clientComponent.getClientKey();
        if (clientKey == null) {
            clientKey = clientVarName;
        }
        encodeLiferayComponentVar(responseWriter, clientVarName, clientKey);
        responseWriter.write("if(");
        responseWriter.write(clientVarName);
        responseWriter.write("){");
        responseWriter.write(clientVarName);
        responseWriter.write(".destroy();}");
    }
}
Also used : BrowserSniffer(com.liferay.faces.util.client.BrowserSniffer) ResponseWriter(javax.faces.context.ResponseWriter) BufferedScriptResponseWriter(com.liferay.faces.util.render.BufferedScriptResponseWriter) ClientComponent(com.liferay.faces.util.component.ClientComponent)

Example 7 with ClientComponent

use of com.liferay.faces.util.component.ClientComponent in project liferay-faces-alloy by liferay.

the class PopoverRenderer method encodeJavaScriptCustom.

@Override
public void encodeJavaScriptCustom(FacesContext facesContext, UIComponent uiComponent) throws IOException {
    ResponseWriter responseWriter = facesContext.getResponseWriter();
    Popover popover = (Popover) uiComponent;
    ClientComponent clientComponent = (ClientComponent) uiComponent;
    String clientVarName = getClientVarName(facesContext, clientComponent);
    String clientKey = clientComponent.getClientKey();
    if (clientKey == null) {
        clientKey = clientVarName;
    }
    if (popover.isHideIconRendered()) {
        // Add an "x" toolbar icon so that the popover can be hidden just like alloy:dialog can.
        responseWriter.write("Liferay.component('");
        responseWriter.write(clientKey);
        responseWriter.write("').addToolbar([{cssClass:'close',label:'\u00D7',on:{click:function(event){Liferay.component('");
        responseWriter.write(clientKey);
        responseWriter.write("').hide();}},render:true}],'header');");
    }
    // Move the overlayBody div into the popover-content div.
    String clientId = popover.getClientId(facesContext);
    String overlayBodyClientId = clientId.concat(OVERLAY_BODY_SUFFIX);
    String escapedOverlayBodyClientId = ComponentUtil.escapeClientId(overlayBodyClientId);
    String contentBoxClientId = clientId.concat(CONTENT_BOX_SUFFIX);
    String escapedContentBoxClientId = ComponentUtil.escapeClientId(contentBoxClientId);
    responseWriter.write("A.one('#");
    responseWriter.write(escapedOverlayBodyClientId);
    responseWriter.write("').appendTo(A.one('div#");
    responseWriter.write(escapedContentBoxClientId);
    responseWriter.write(">div.popover-content'));");
    if (popover.isDismissible()) {
        encodeOverlayDismissible(responseWriter, popover, clientKey);
    }
    encodeOverlayJavaScriptCustom(responseWriter, facesContext, popover, clientKey);
    if ((popover.getFor() == null) && facesContext.isProjectStage(ProjectStage.Development)) {
        logger.error("The 'for' attribute is required for alloy:popover");
    }
}
Also used : ResponseWriter(javax.faces.context.ResponseWriter) ClientComponent(com.liferay.faces.util.component.ClientComponent) Popover(com.liferay.faces.alloy.component.popover.Popover)

Example 8 with ClientComponent

use of com.liferay.faces.util.component.ClientComponent in project liferay-faces-alloy by liferay.

the class AutoCompleteRenderer method encodeJavaScriptCustom.

@Override
public void encodeJavaScriptCustom(FacesContext facesContext, UIComponent uiComponent) throws IOException {
    // If the developer has specified a server-side filtering, then
    if (isServerFilteringEnabled(uiComponent)) {
        ResponseWriter responseWriter = facesContext.getResponseWriter();
        ClientComponent clientComponent = (ClientComponent) uiComponent;
        String clientVarName = getClientVarName(facesContext, clientComponent);
        String clientKey = clientComponent.getClientKey();
        if (clientKey == null) {
            clientKey = clientVarName;
        }
        // J-
        // var clientVarName = Liferay.component('clientKey');
        // J+
        encodeLiferayComponentVar(responseWriter, clientVarName, clientKey);
        JavaScriptFragment clientVarNameJSFragment = new JavaScriptFragment(clientVarName);
        String clientId = uiComponent.getClientId(facesContext);
        String hiddenClientId = clientId + HIDDEN_SUFFIX;
        String namingContainerId = null;
        UIViewRoot viewRoot = facesContext.getViewRoot();
        if (viewRoot instanceof NamingContainer) {
            namingContainerId = viewRoot.getContainerClientId(facesContext);
        }
        // J-
        // LFAI.setAutoCompleteEventListeners(A, clientVarName, 'escapedHiddenClientId', 'clientId',
        // 'namingContainerId');
        // J+
        encodeFunctionCall(responseWriter, "LFAI.initAutoCompleteServerMode", clientVarNameJSFragment, hiddenClientId, clientId, namingContainerId);
    }
}
Also used : BufferedScriptResponseWriter(com.liferay.faces.util.render.BufferedScriptResponseWriter) ResponseWriter(javax.faces.context.ResponseWriter) NamingContainer(javax.faces.component.NamingContainer) JavaScriptFragment(com.liferay.faces.util.render.JavaScriptFragment) ClientComponent(com.liferay.faces.util.component.ClientComponent) UIViewRoot(javax.faces.component.UIViewRoot)

Example 9 with ClientComponent

use of com.liferay.faces.util.component.ClientComponent in project liferay-faces-alloy by liferay.

the class AutoCompleteRenderer method encodeJavaScript.

/**
 * This method is being overridden in order to allow server-side filtering of autoComplete items when server-side
 * filtering is enabled during an Ajax request and there is a query. Otherwise, this method simply calls
 * super.encodeJavaScript() in order to render the component normally.
 */
@Override
public void encodeJavaScript(FacesContext facesContext, UIComponent uiComponent) throws IOException {
    AutoComplete autoComplete = (AutoComplete) uiComponent;
    // If the developer has specified a server-side filtering during Ajax, then
    if (isServerFilteringEnabled(autoComplete) && facesContext.getPartialViewContext().isAjaxRequest()) {
        // If the user has specified a query, then
        ExternalContext externalContext = facesContext.getExternalContext();
        Map<String, String> requestParameterMap = externalContext.getRequestParameterMap();
        String clientId = uiComponent.getClientId(facesContext);
        String hiddenClientId = clientId + HIDDEN_SUFFIX;
        String query = requestParameterMap.get(hiddenClientId);
        if ((query != null) && (query.length() > 0)) {
            // Get the entire list of completion items.
            List<String> items = autoComplete.getAllItems(facesContext);
            // If the developer has specified a serverCustomFilter, then call their custom filtering method.
            MethodExpression serverCustomFilter = autoComplete.getServerCustomFilter();
            if (serverCustomFilter != null) {
                items = invokeServerCustomFilter(facesContext.getELContext(), serverCustomFilter, query, items);
            } else // Otherwise, if the developer has specified a serverFilterType, then call the corresponding filtering
            // method.
            {
                String serverFilterType = autoComplete.getServerFilterType();
                if (serverFilterType != null) {
                    Locale locale = facesContext.getViewRoot().getLocale();
                    AutoCompleteFilterFactory autoCompleteFilterFactory = new AutoCompleteFilterFactoryImpl();
                    AutoCompleteFilter autoCompleteFilter = autoCompleteFilterFactory.getAutoCompleteFilter(serverFilterType);
                    if (autoCompleteFilter != null) {
                        boolean caseSensitive = serverFilterType.contains("Case");
                        items = autoCompleteFilter.doFilter(query, items, caseSensitive, locale);
                    } else {
                        throw new IOException(serverFilterType + " is not a valid serverFilterType.");
                    }
                }
            }
            // Build up a fragment of JavaScript that gets the client-side component.
            ClientComponent clientComponent = (ClientComponent) uiComponent;
            String clientVarName = getClientVarName(facesContext, clientComponent);
            String clientKey = clientComponent.getClientKey();
            if (clientKey == null) {
                clientKey = clientVarName;
            }
            // J-
            // Liferay.component('clientKey')
            // J+
            JavaScriptFragment liferayComponentJavaScriptFragment = new JavaScriptFragment("Liferay.component('" + clientKey + "')");
            // Build up a fragment of JavaScript that contains an array of the results.
            // J-
            // ['item1', 'item2', 'item3', ... 'itemN']
            // J+
            JavaScriptFragment[] results = AlloyRendererUtil.toEscapedJavaScriptStringArray(items);
            // Buffer all JavaScript so that it is rendered in the <eval> section of the partial response.
            BufferedScriptResponseWriter bufferedScriptResponseWriter = new BufferedScriptResponseWriter();
            // J-
            // LFAI.recieveAutoCompleteResults(Liferay.component('clientKey'), ['item1', 'item2', 'item3'],
            // 'hiddenClientId')
            // J+
            encodeFunctionCall(bufferedScriptResponseWriter, "LFAI.setAutoCompleteServerResults", liferayComponentJavaScriptFragment, results, hiddenClientId);
            String[] modules = getModules(facesContext, uiComponent);
            renderScript(facesContext, bufferedScriptResponseWriter.toString(), modules, Script.ModulesType.ALLOY);
        } else {
            super.encodeJavaScript(facesContext, uiComponent);
        }
    } else {
        super.encodeJavaScript(facesContext, uiComponent);
    }
}
Also used : Locale(java.util.Locale) BufferedScriptResponseWriter(com.liferay.faces.util.render.BufferedScriptResponseWriter) IOException(java.io.IOException) MethodExpression(javax.el.MethodExpression) ClientComponent(com.liferay.faces.util.component.ClientComponent) ExternalContext(javax.faces.context.ExternalContext) JavaScriptFragment(com.liferay.faces.util.render.JavaScriptFragment) AutoComplete(com.liferay.faces.alloy.component.autocomplete.AutoComplete)

Aggregations

ClientComponent (com.liferay.faces.util.component.ClientComponent)9 ResponseWriter (javax.faces.context.ResponseWriter)8 BufferedScriptResponseWriter (com.liferay.faces.util.render.BufferedScriptResponseWriter)4 JavaScriptFragment (com.liferay.faces.util.render.JavaScriptFragment)3 AutoComplete (com.liferay.faces.alloy.component.autocomplete.AutoComplete)1 Dialog (com.liferay.faces.alloy.component.dialog.Dialog)1 InputSourceCode (com.liferay.faces.alloy.component.inputsourcecode.InputSourceCode)1 OutputTooltip (com.liferay.faces.alloy.component.outputtooltip.OutputTooltip)1 Popover (com.liferay.faces.alloy.component.popover.Popover)1 BrowserSniffer (com.liferay.faces.util.client.BrowserSniffer)1 HiddenTextResponseWriter (com.liferay.faces.util.render.HiddenTextResponseWriter)1 IOException (java.io.IOException)1 Locale (java.util.Locale)1 MethodExpression (javax.el.MethodExpression)1 NamingContainer (javax.faces.component.NamingContainer)1 UIViewRoot (javax.faces.component.UIViewRoot)1 ValueHolder (javax.faces.component.ValueHolder)1 ExternalContext (javax.faces.context.ExternalContext)1