Search in sources :

Example 6 with JavaScriptFragment

use of com.liferay.faces.util.render.JavaScriptFragment in project liferay-faces-alloy by liferay.

the class InputTimeRenderer method encodeJavaScriptCustom.

@Override
public void encodeJavaScriptCustom(FacesContext facesContext, UIComponent uiComponent) throws IOException {
    InputTime inputTime = (InputTime) uiComponent;
    String clientVarName = getClientVarName(facesContext, inputTime);
    String clientKey = inputTime.getClientKey();
    if (clientKey == null) {
        clientKey = clientVarName;
    }
    ResponseWriter responseWriter = facesContext.getResponseWriter();
    BrowserSniffer browserSniffer = BrowserSnifferFactory.getBrowserSnifferInstance(facesContext.getExternalContext());
    if (isNative(browserSniffer, inputTime)) {
        JavaScriptFragment liferayComponent = new JavaScriptFragment("Liferay.component('" + clientKey + "')");
        String clientId = uiComponent.getClientId(facesContext);
        String inputClientId = clientId.concat(INPUT_SUFFIX);
        // Get the max and min times in the HTML5 format which does not include seconds.
        int defaultHTML5PatternLength = InputTime.DEFAULT_HTML5_TIME_PATTERN.length();
        String maxTime = inputTime.getMaxTime().substring(0, defaultHTML5PatternLength);
        String minTime = inputTime.getMinTime().substring(0, defaultHTML5PatternLength);
        encodeFunctionCall(responseWriter, "LFAI.initDateTimePickerMobile", liferayComponent, inputClientId, maxTime, minTime);
    } else {
        encodeLiferayComponentVar(responseWriter, clientVarName, clientKey);
        // Replace the default TimePicker._setValues() method with setValues() (defined in alloy.js) which simply
        // passes values through to the autocomplete without processing them.
        responseWriter.write(clientVarName);
        responseWriter.write("._setValues=LFAI.timePickerSetValues;");
        // Set the values of the timePicker.
        responseWriter.write(clientVarName);
        responseWriter.write(".set('values',[");
        String minTimeStamp = inputTime.getMinTime();
        String maxTimeStamp = inputTime.getMaxTime();
        long minTime;
        long maxTime;
        try {
            minTime = getMillisFromTimeStamp(minTimeStamp);
            maxTime = getMillisFromTimeStamp(maxTimeStamp);
        } catch (ParseException e) {
            throw new IOException(e);
        }
        if (minTime > maxTime) {
            throw new IOException("minTime must not be later than maxTime.");
        }
        String timePattern = inputTime.getPattern();
        Object objectLocale = inputTime.getLocale();
        Locale locale = inputTime.getObjectAsLocale(objectLocale);
        TimeZone timeZone = TimeZone.getTimeZone("Greenwich");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timePattern, locale);
        simpleDateFormat.setTimeZone(timeZone);
        Integer millisecondStep = inputTime.getStep() * 1000;
        boolean firstTimeStamp = true;
        if (millisecondStep < 1) {
            throw new IOException("step cannot be less than 1.");
        }
        // step, and printing each resulting value.
        for (long milliseconds = minTime; milliseconds <= maxTime; milliseconds = milliseconds + millisecondStep) {
            if (!firstTimeStamp) {
                responseWriter.write(",");
            } else {
                firstTimeStamp = false;
            }
            Date time = new Date(milliseconds);
            String dateString = simpleDateFormat.format(time);
            String escapedDateString = RendererUtil.escapeJavaScript(dateString);
            responseWriter.write("'");
            responseWriter.write(escapedDateString);
            responseWriter.write("'");
        }
        responseWriter.write("]);");
    }
}
Also used : Locale(java.util.Locale) BrowserSniffer(com.liferay.faces.util.client.BrowserSniffer) InputTime(com.liferay.faces.alloy.component.inputtime.InputTime) IOException(java.io.IOException) Date(java.util.Date) TimeZone(java.util.TimeZone) InputDateTimeResponseWriter(com.liferay.faces.alloy.component.inputdatetime.internal.InputDateTimeResponseWriter) ResponseWriter(javax.faces.context.ResponseWriter) JavaScriptFragment(com.liferay.faces.util.render.JavaScriptFragment) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 7 with JavaScriptFragment

use of com.liferay.faces.util.render.JavaScriptFragment in project liferay-faces-alloy by liferay.

the class OverlayRendererBase method encodeOverlayDismissible.

protected void encodeOverlayDismissible(ResponseWriter responseWriter, UIComponent overlay, String clientKey) throws IOException {
    JavaScriptFragment overlayClientVar = new JavaScriptFragment("Liferay.component('" + clientKey + "')");
    encodeFunctionCall(responseWriter, "LFAI.initOverlayDismissible", 'A', overlayClientVar);
}
Also used : JavaScriptFragment(com.liferay.faces.util.render.JavaScriptFragment)

Example 8 with JavaScriptFragment

use of com.liferay.faces.util.render.JavaScriptFragment in project liferay-faces-alloy by liferay.

the class TabViewRenderer method encodeJavaScriptCustom.

@Override
public void encodeJavaScriptCustom(FacesContext facesContext, UIComponent uiComponent) throws IOException {
    // Encode a script that will handle the client-side state of the selected tab index, as well as submitting
    // Ajax requests to support server-side events.
    TabView tabView = (TabView) uiComponent;
    // var tabViewClientVarName = Liferay.component('{clientKey}');
    String clientVarName = getClientVarName(facesContext, tabView);
    String clientKey = tabView.getClientKey();
    if (clientKey == null) {
        clientKey = clientVarName;
    }
    ResponseWriter responseWriter = facesContext.getResponseWriter();
    encodeLiferayComponentVar(responseWriter, clientVarName, clientKey);
    // Enable the enabled tabs, disable the disabled tabs, and select the selected tab on the client side.
    Integer selectedIndex = tabView.getSelectedIndex();
    DataEncoderBase dataEncoder = new DataEncoderTabJavaScriptImpl(selectedIndex, clientVarName);
    dataEncoder.encodeColumns(facesContext, tabView);
    // LFAI.initTabView(tabViewClientVarName, '{clientId}selectedIndex', '{clientId}',
    // [tabSelectClientBehaviorScriptFunction1, tabSelectClientBehaviorScriptFunction2,
    // tabSelectClientBehaviorScriptFunction3]);
    String clientId = uiComponent.getClientId(facesContext);
    String hiddenFieldId = clientId + "selectedIndex";
    StringBuilder clientBehaviorsArrayStringBuilder = new StringBuilder();
    clientBehaviorsArrayStringBuilder.append("[");
    boolean first = true;
    Map<String, List<ClientBehavior>> clientBehaviorMap = tabView.getClientBehaviors();
    Collection<String> eventNames = tabView.getEventNames();
    for (String eventName : eventNames) {
        List<ClientBehavior> clientBehaviorsForEvent = clientBehaviorMap.get(eventName);
        if (clientBehaviorsForEvent != null) {
            for (ClientBehavior clientBehavior : clientBehaviorsForEvent) {
                ClientBehaviorContext clientBehaviorContext = ClientBehaviorContext.createClientBehaviorContext(facesContext, tabView, eventName, clientId, null);
                // an Ajax request.
                if (TabSelectEvent.TAB_SELECT.equals(eventName)) {
                    if (!first) {
                        clientBehaviorsArrayStringBuilder.append(",");
                    }
                    // J-
                    // function(event) {
                    // jsf.ajax.request(this, event, {'javax.faces.behavior.event': 'tabSelect'})
                    // };
                    // J+
                    clientBehaviorsArrayStringBuilder.append("function(event){");
                    clientBehaviorsArrayStringBuilder.append(clientBehavior.getScript(clientBehaviorContext));
                    clientBehaviorsArrayStringBuilder.append("}");
                    first = false;
                }
            }
        }
    }
    clientBehaviorsArrayStringBuilder.append("]");
    encodeFunctionCall(responseWriter, "LFAI.initTabView", new JavaScriptFragment(clientVarName), hiddenFieldId, clientId, new JavaScriptFragment(clientBehaviorsArrayStringBuilder.toString()));
}
Also used : TabView(com.liferay.faces.alloy.component.tabview.TabView) ClientBehaviorContext(javax.faces.component.behavior.ClientBehaviorContext) ResponseWriter(javax.faces.context.ResponseWriter) JavaScriptFragment(com.liferay.faces.util.render.JavaScriptFragment) DataEncoderBase(com.liferay.faces.alloy.component.data.internal.DataEncoderBase) List(java.util.List) ClientBehavior(javax.faces.component.behavior.ClientBehavior)

Example 9 with JavaScriptFragment

use of com.liferay.faces.util.render.JavaScriptFragment in project liferay-faces-alloy by liferay.

the class AlloyRendererUtil method toEscapedJavaScriptStringArray.

public static JavaScriptFragment[] toEscapedJavaScriptStringArray(String[] strings) {
    JavaScriptFragment[] escapedJavaScriptStringArray = new JavaScriptFragment[strings.length];
    for (int i = 0; i < strings.length; i++) {
        String escapedJavaScript = RendererUtil.escapeJavaScript(strings[i]);
        escapedJavaScriptStringArray[i] = new JavaScriptFragment("'" + escapedJavaScript + "'");
    }
    return escapedJavaScriptStringArray;
}
Also used : JavaScriptFragment(com.liferay.faces.util.render.JavaScriptFragment)

Example 10 with JavaScriptFragment

use of com.liferay.faces.util.render.JavaScriptFragment in project liferay-faces-alloy by liferay.

the class InputFileRenderer method encodeJavaScriptCustom.

@Override
public void encodeJavaScriptCustom(FacesContext facesContext, UIComponent uiComponent) throws IOException {
    ResponseWriter responseWriter = facesContext.getResponseWriter();
    InputFile inputFile = (InputFile) uiComponent;
    // Determine the valid content-types and maximum file size from the validator (if specified).
    String validContentTypesString = inputFile.getContentTypes();
    JavaScriptFragment[] validContentTypes;
    if ((validContentTypesString == null) || "".equals(validContentTypesString)) {
        validContentTypes = new JavaScriptFragment[] {};
    } else {
        validContentTypes = AlloyRendererUtil.toEscapedJavaScriptStringArray(validContentTypesString.split("\\s*,\\s*"));
    }
    String clientId = inputFile.getClientId(facesContext);
    Long maxFileSize = inputFile.getMaxFileSize();
    if (maxFileSize == null) {
        maxFileSize = Long.MAX_VALUE;
    }
    // If the component should render the upload progress table, then initialize the YUI progress uploader widget.
    if (inputFile.isShowProgress()) {
        String clientVarName = getClientVarName(facesContext, inputFile);
        String clientKey = inputFile.getClientKey();
        if (clientKey == null) {
            clientKey = clientVarName;
        }
        UIViewRoot viewRoot = facesContext.getViewRoot();
        Locale locale = viewRoot.getLocale();
        String formClientId = getParentFormClientId(inputFile);
        Application application = facesContext.getApplication();
        ViewHandler viewHandler = application.getViewHandler();
        String actionURL = viewHandler.getActionURL(facesContext, viewRoot.getViewId());
        String partialActionURL = facesContext.getExternalContext().encodePartialActionURL(actionURL);
        String namingContainerId = "";
        if (viewRoot instanceof NamingContainer) {
            namingContainerId = viewRoot.getContainerClientId(facesContext);
        }
        AjaxParameters ajaxParameters = new AjaxParameters(inputFile, clientId, formClientId);
        String execute = ajaxParameters.getExecute();
        String render = ajaxParameters.getRender();
        ExternalContext externalContext = facesContext.getExternalContext();
        I18n i18n = I18nFactory.getI18nInstance(externalContext);
        String notStartedMessage = i18n.getMessage(facesContext, locale, "not-started");
        JavaScriptFragment clientComponent = new JavaScriptFragment("Liferay.component('" + clientKey + "')");
        encodeFunctionCall(responseWriter, "LFAI.initProgressUploader", 'A', clientComponent, validContentTypes, clientId, formClientId, namingContainerId, inputFile.isAuto(), execute, render, partialActionURL, maxFileSize, notStartedMessage);
    } else // template and write it to the response.
    if (inputFile.isShowPreview()) {
        encodeFunctionCall(responseWriter, "LFAI.initPreviewUploader", 'A', validContentTypes, clientId, maxFileSize);
    }
}
Also used : Locale(java.util.Locale) ViewHandler(javax.faces.application.ViewHandler) InputFile(com.liferay.faces.alloy.component.inputfile.InputFile) ResponseWriter(javax.faces.context.ResponseWriter) NamingContainer(javax.faces.component.NamingContainer) ExternalContext(javax.faces.context.ExternalContext) JavaScriptFragment(com.liferay.faces.util.render.JavaScriptFragment) UIViewRoot(javax.faces.component.UIViewRoot) Application(javax.faces.application.Application) I18n(com.liferay.faces.util.i18n.I18n)

Aggregations

JavaScriptFragment (com.liferay.faces.util.render.JavaScriptFragment)16 ResponseWriter (javax.faces.context.ResponseWriter)9 List (java.util.List)6 ClientBehavior (javax.faces.component.behavior.ClientBehavior)6 EscapedClientId (com.liferay.faces.alloy.render.internal.EscapedClientId)4 BufferedScriptResponseWriter (com.liferay.faces.util.render.BufferedScriptResponseWriter)4 ClientBehaviorContext (javax.faces.component.behavior.ClientBehaviorContext)4 ClientComponent (com.liferay.faces.util.component.ClientComponent)3 Locale (java.util.Locale)3 DataEncoderBase (com.liferay.faces.alloy.component.data.internal.DataEncoderBase)2 InputDateTimeResponseWriter (com.liferay.faces.alloy.component.inputdatetime.internal.InputDateTimeResponseWriter)2 ProgressBar (com.liferay.faces.alloy.component.progressbar.ProgressBar)2 BrowserSniffer (com.liferay.faces.util.client.BrowserSniffer)2 IOException (java.io.IOException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 TimeZone (java.util.TimeZone)2 NamingContainer (javax.faces.component.NamingContainer)2 UIViewRoot (javax.faces.component.UIViewRoot)2 ExternalContext (javax.faces.context.ExternalContext)2