Search in sources :

Example 1 with JavaScriptFragment

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

the class DataTableRenderer method encodeJavaScriptCustom.

@Override
public void encodeJavaScriptCustom(FacesContext facesContext, UIComponent uiComponent) throws IOException {
    DataTable dataTable = (DataTable) uiComponent;
    String selectionMode = dataTable.getSelectionMode();
    if ("checkbox".equals(selectionMode) || "radio".equals(selectionMode)) {
        String dataTableClientId = dataTable.getClientId(facesContext);
        EscapedClientId escapedDataTableClientId = new EscapedClientId(dataTableClientId);
        String hiddenFieldClientId = dataTableClientId.concat("_selectedRowIndexes");
        EscapedClientId escapedHiddenFieldClientId = new EscapedClientId(hiddenFieldClientId);
        ResponseWriter responseWriter = facesContext.getResponseWriter();
        // rowSelect
        JavaScriptFragment rowSelectClientBehaviorScript = getRowEventClientBehaviorScript(facesContext, dataTable, dataTableClientId, RowSelectEvent.ROW_SELECT, "rowIndex");
        // rowSelectRange
        JavaScriptFragment rowSelectRangeClientBehaviorScript = getRowEventClientBehaviorScript(facesContext, dataTable, dataTableClientId, RowSelectRangeEvent.ROW_SELECT_RANGE, "rowIndexRange");
        // rowDeselect
        JavaScriptFragment rowDeselectClientBehaviorScript = getRowEventClientBehaviorScript(facesContext, dataTable, dataTableClientId, RowDeselectEvent.ROW_DESELECT, "rowIndex");
        // rowDeSelectRange
        JavaScriptFragment rowDeselectRangeClientBehaviorScript = getRowEventClientBehaviorScript(facesContext, dataTable, dataTableClientId, RowDeselectRangeEvent.ROW_DESELECT_RANGE, "rowIndexRange");
        if ("checkbox".equals(selectionMode)) {
            // Register the onclick event callback for the "Select All" checkbox.
            String selectAllCheckboxClientId = dataTableClientId.concat("_selectAll");
            EscapedClientId escapedSelectAllCheckboxClientId = new EscapedClientId(selectAllCheckboxClientId);
            encodeFunctionCall(responseWriter, "LFAI.initDataTableSelectAllCheckbox", 'A', escapedDataTableClientId, escapedSelectAllCheckboxClientId, rowSelectRangeClientBehaviorScript, rowDeselectRangeClientBehaviorScript);
            // Register the onclick event callback for each row-level checkbox.
            encodeFunctionCall(responseWriter, "LFAI.initDataTableCheckboxSelection", 'A', escapedDataTableClientId, escapedHiddenFieldClientId, rowSelectClientBehaviorScript, rowDeselectClientBehaviorScript);
        } else if ("radio".equals(selectionMode)) {
            // Register the onclick event callback for each row-level radio button.
            encodeFunctionCall(responseWriter, "LFAI.initDataTableRadioSelection", 'A', escapedDataTableClientId, escapedHiddenFieldClientId, rowSelectClientBehaviorScript, rowDeselectClientBehaviorScript);
        }
    }
}
Also used : DataTable(com.liferay.faces.alloy.component.datatable.DataTable) ResponseWriter(javax.faces.context.ResponseWriter) JavaScriptFragment(com.liferay.faces.util.render.JavaScriptFragment) EscapedClientId(com.liferay.faces.alloy.render.internal.EscapedClientId)

Example 2 with JavaScriptFragment

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

the class DialogRenderer method encodeJavaScriptCustom.

@Override
public void encodeJavaScriptCustom(FacesContext facesContext, UIComponent uiComponent) throws IOException {
    ResponseWriter responseWriter = facesContext.getResponseWriter();
    Dialog dialog = (Dialog) uiComponent;
    ClientComponent clientComponent = (ClientComponent) uiComponent;
    String clientVarName = getClientVarName(facesContext, clientComponent);
    String clientKey = clientComponent.getClientKey();
    if (clientKey == null) {
        clientKey = clientVarName;
    }
    encodeLiferayComponentVar(responseWriter, "dialog", clientKey);
    // Prevent scrolling when the show() JavaScript function is called.
    responseWriter.write("var ");
    responseWriter.write(clientKey);
    responseWriter.write("_scrollx=window.scrollX;");
    responseWriter.write("var ");
    responseWriter.write(clientKey);
    responseWriter.write("_scrolly=window.scrollY;");
    responseWriter.write("A.Do.before(function(stuff) { ");
    responseWriter.write(clientKey);
    responseWriter.write("_scrollx=window.scrollX; ");
    responseWriter.write(clientKey);
    responseWriter.write("_scrolly=window.scrollY;},Liferay.component('");
    responseWriter.write(clientKey);
    responseWriter.write("'),'show');");
    responseWriter.write("A.Do.after(function(stuff){window.scrollTo(");
    responseWriter.write(clientKey);
    responseWriter.write("_scrollx,");
    responseWriter.write(clientKey);
    responseWriter.write("_scrolly);},Liferay.component('");
    responseWriter.write(clientKey);
    responseWriter.write("'),'show');");
    // FACES-2209 remove class="hide" from the "mask" div that is used for modal dialogs
    // the mask div is placed as the first child of the parent of the dialog
    // removing this class allows a modal dialog to be fully rendered upon a partial request.
    responseWriter.write("var " + clientKey + "_mask=A.one('#");
    String clientId = dialog.getClientId(facesContext);
    String escapedBoundingBoxClientId = ComponentUtil.escapeClientId(clientId);
    responseWriter.write(escapedBoundingBoxClientId);
    responseWriter.write("').ancestor().one('.hide');");
    responseWriter.write("if (");
    responseWriter.write(clientKey);
    responseWriter.write("_mask) { ");
    responseWriter.write(clientKey);
    responseWriter.write("_mask.removeClass('hide') }; ");
    if (!dialog.isHideIconRendered()) {
        responseWriter.write("dialog.removeToolbar('header');");
    }
    // move the overlayBody div into the modal-body div
    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.modal-body'));");
    if (!dialog.isModal() && dialog.isDismissible()) {
        encodeOverlayDismissible(responseWriter, dialog, clientKey);
    }
    JavaScriptFragment dialogJavaScriptFragment = new JavaScriptFragment("dialog");
    encodeFunctionCall(responseWriter, "LFAI.initDialog", dialogJavaScriptFragment);
    encodeOverlayJavaScriptCustom(responseWriter, facesContext, dialog, clientKey);
}
Also used : ResponseWriter(javax.faces.context.ResponseWriter) Dialog(com.liferay.faces.alloy.component.dialog.Dialog) JavaScriptFragment(com.liferay.faces.util.render.JavaScriptFragment) ClientComponent(com.liferay.faces.util.component.ClientComponent)

Example 3 with JavaScriptFragment

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

the class AccordionRenderer 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.
    Accordion accordion = (Accordion) uiComponent;
    // var accordionClientVarName = Liferay.component('{clientKey}');
    String clientVarName = getClientVarName(facesContext, accordion);
    String clientKey = accordion.getClientKey();
    if (clientKey == null) {
        clientKey = clientVarName;
    }
    ResponseWriter responseWriter = facesContext.getResponseWriter();
    encodeLiferayComponentVar(responseWriter, clientVarName, clientKey);
    // Expand the selected tab.
    Integer selectedIndex = accordion.getSelectedIndex();
    if ((selectedIndex != null) && (selectedIndex > -1)) {
        DataEncoderBase dataEncoder = new DataEncoderTabJavaScriptImpl(selectedIndex, clientVarName);
        dataEncoder.encodeColumns(facesContext, accordion);
    }
    // LFAI.initAccordion(multiple, accordionClientVarName, '{clientId}selectedIndex', '{clientId}',
    // [tabExpandClientBehaviorScriptFunction1, tabExpandClientBehaviorScriptFunction2,
    // tabExpandClientBehaviorScriptFunction3], '{clientId}collapsedTabIndex',
    // [tabCollapseClientBehaviorScriptFunction1, tabCollapseClientBehaviorScriptFunction2,
    // tabCollapseClientBehaviorScriptFunction3]);
    boolean multiple = accordion.isMultiple();
    String clientId = accordion.getClientId(facesContext);
    StringBuilder tabExpandedClientBehaviors = new StringBuilder();
    tabExpandedClientBehaviors.append("[");
    StringBuilder tabCollapsedClientBehaviors = new StringBuilder();
    tabCollapsedClientBehaviors.append("[");
    boolean firstTabExpandedClientBehavior = true;
    boolean firstTabCollapsedClientBehavior = true;
    Map<String, List<ClientBehavior>> clientBehaviorMap = accordion.getClientBehaviors();
    Collection<String> eventNames = accordion.getEventNames();
    for (String eventName : eventNames) {
        List<ClientBehavior> clientBehaviorsForEvent = clientBehaviorMap.get(eventName);
        if (clientBehaviorsForEvent != null) {
            for (ClientBehavior clientBehavior : clientBehaviorsForEvent) {
                ClientBehaviorContext clientBehaviorContext = ClientBehaviorContext.createClientBehaviorContext(facesContext, accordion, eventName, clientId, null);
                // an Ajax request.
                if (TabExpandEvent.TAB_EXPAND.equals(eventName)) {
                    if (!firstTabExpandedClientBehavior) {
                        tabExpandedClientBehaviors.append(",");
                    }
                    // J-
                    // function(event) {
                    // jsf.ajax.request(this, event, {'javax.faces.behavior.event': 'tabSelect'})
                    // };
                    // J+
                    tabExpandedClientBehaviors.append("function(event){");
                    tabExpandedClientBehaviors.append(clientBehavior.getScript(clientBehaviorContext));
                    tabExpandedClientBehaviors.append("}");
                    firstTabExpandedClientBehavior = false;
                } else // an Ajax request.
                if (TabCollapseEvent.TAB_COLLAPSE.equals(eventName)) {
                    if (!firstTabCollapsedClientBehavior) {
                        tabCollapsedClientBehaviors.append(",");
                    }
                    // J-
                    // function(event) {
                    // jsf.ajax.request(this, event, {'javax.faces.behavior.event': 'tabSelect'})
                    // };
                    // J+
                    tabCollapsedClientBehaviors.append("function(event){");
                    tabCollapsedClientBehaviors.append(clientBehavior.getScript(clientBehaviorContext));
                    tabCollapsedClientBehaviors.append("}");
                    firstTabCollapsedClientBehavior = false;
                }
            }
        }
    }
    tabExpandedClientBehaviors.append("]");
    tabCollapsedClientBehaviors.append("]");
    encodeFunctionCall(responseWriter, "LFAI.initAccordion", multiple, new JavaScriptFragment(clientVarName), clientId + "selectedIndex", clientId, new JavaScriptFragment(tabExpandedClientBehaviors.toString()), clientId + "collapsedTabIndex", new JavaScriptFragment(tabCollapsedClientBehaviors.toString()));
}
Also used : ClientBehaviorContext(javax.faces.component.behavior.ClientBehaviorContext) Accordion(com.liferay.faces.alloy.component.accordion.Accordion) 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 4 with JavaScriptFragment

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

the class InputDateRenderer method encodeCalendar.

protected void encodeCalendar(FacesContext facesContext, ResponseWriter responseWriter, InputDate inputDate, boolean first) throws IOException {
    // The calendar attribute value provides the opportunity to specify dateClick events, minDate, maxDate as
    // key:value pairs via JSON syntax. For example: "calendar: {minDate: new Date(2015,1,1,0,0,0,0)}"
    boolean calendarFirst = true;
    encodeNonEscapedObject(responseWriter, "calendar", "", first);
    responseWriter.write("{");
    Object maxDateObject = inputDate.getMaxDate();
    if (maxDateObject != null) {
        encodeDate(responseWriter, inputDate, "maximumDate", maxDateObject, calendarFirst);
        calendarFirst = false;
    }
    Object minDateObject = inputDate.getMinDate();
    if (minDateObject != null) {
        encodeDate(responseWriter, inputDate, "minimumDate", minDateObject, calendarFirst);
        calendarFirst = false;
    }
    String showOn = inputDate.getShowOn();
    boolean showOnButton = "button".equals(showOn);
    Map<String, List<ClientBehavior>> clientBehaviorMap = inputDate.getClientBehaviors();
    List<ClientBehavior> valueChangeClientBehaviors = clientBehaviorMap.get(VALUE_CHANGE);
    boolean valueChangeClientBehaviorsNotEmpty = (valueChangeClientBehaviors != null) && !valueChangeClientBehaviors.isEmpty();
    if (showOnButton || valueChangeClientBehaviorsNotEmpty) {
        encodeNonEscapedObject(responseWriter, "on", "", calendarFirst);
        responseWriter.write("{");
        encodeNonEscapedObject(responseWriter, "dateClick", "", true);
        responseWriter.write("function(event){");
        String clientId = inputDate.getClientId(facesContext);
        String inputClientId = clientId.concat(INPUT_SUFFIX);
        EscapedClientId escapedInputClientId = new EscapedClientId(inputClientId);
        JavaScriptFragment selectable = new JavaScriptFragment("this._canBeSelected(event.date)");
        JavaScriptFragment date = null;
        if (showOnButton) {
            // Each time a date is clicked, the input must be updated via javascript because the datePicker is not
            // attached to the input.
            String datePattern = inputDate.getPattern();
            String mask = getMaskFromDatePattern(datePattern);
            String escapedMask = RendererUtil.escapeJavaScript(mask);
            date = new JavaScriptFragment("A.Date.format(event.date,{format:'".concat(escapedMask).concat("'})"));
        }
        encodeFunctionCall(responseWriter, "LFAI.inputDateTimePickerSelect", 'A', escapedInputClientId, selectable, date, valueChangeClientBehaviorsNotEmpty);
        responseWriter.append(";}");
        responseWriter.write("}");
        calendarFirst = false;
    }
    responseWriter.write("}");
}
Also used : JavaScriptFragment(com.liferay.faces.util.render.JavaScriptFragment) List(java.util.List) EscapedClientId(com.liferay.faces.alloy.render.internal.EscapedClientId) ClientBehavior(javax.faces.component.behavior.ClientBehavior)

Example 5 with JavaScriptFragment

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

the class InputTimeRenderer method encodeAutocomplete.

protected void encodeAutocomplete(FacesContext facesContext, ResponseWriter responseWriter, InputTime inputTime, boolean first) throws IOException {
    encodeNonEscapedObject(responseWriter, "autocomplete", "", first);
    responseWriter.write("{");
    boolean autoCompleteFirst = true;
    boolean activateFirstItem = inputTime.isActivateFirstItem();
    encodeBoolean(responseWriter, ACTIVATE_FIRST_ITEM, activateFirstItem, autoCompleteFirst);
    autoCompleteFirst = false;
    Boolean circular = inputTime.getCircular();
    if (circular != null) {
        encodeBoolean(responseWriter, CIRCULAR, circular, autoCompleteFirst);
        autoCompleteFirst = false;
    }
    String filterType = inputTime.getFilterType();
    if (filterType != null) {
        encodeString(responseWriter, "resultFilters", filterType, autoCompleteFirst);
        autoCompleteFirst = false;
    }
    String height = inputTime.getHeight();
    if (height != null) {
        encodeString(responseWriter, HEIGHT, height, autoCompleteFirst);
        autoCompleteFirst = false;
    }
    String highlighterType = inputTime.getHighlighterType();
    if (highlighterType != null) {
        encodeString(responseWriter, "resultHighlighter", highlighterType, autoCompleteFirst);
        autoCompleteFirst = false;
    }
    Integer maxResults = inputTime.getMaxResults();
    if (maxResults != null) {
        encodeString(responseWriter, MAX_RESULTS, maxResults, autoCompleteFirst);
        autoCompleteFirst = false;
    }
    Integer queryDelay = inputTime.getQueryDelay();
    if (queryDelay != null) {
        encodeString(responseWriter, QUERY_DELAY, queryDelay, autoCompleteFirst);
        autoCompleteFirst = false;
    }
    Boolean scrollIntoView = inputTime.getScrollIntoView();
    if (scrollIntoView != null) {
        encodeBoolean(responseWriter, SCROLL_INTO_VIEW, scrollIntoView, autoCompleteFirst);
        autoCompleteFirst = false;
    }
    String showOn = inputTime.getShowOn();
    boolean showOnButton = "button".equals(showOn);
    Map<String, List<ClientBehavior>> clientBehaviorMap = inputTime.getClientBehaviors();
    List<ClientBehavior> valueChangeClientBehaviors = clientBehaviorMap.get(VALUE_CHANGE);
    boolean valueChangeClientBehaviorsNotEmpty = (valueChangeClientBehaviors != null) && !valueChangeClientBehaviors.isEmpty();
    if (showOnButton || valueChangeClientBehaviorsNotEmpty) {
        encodeNonEscapedObject(responseWriter, "after", "", autoCompleteFirst);
        responseWriter.write("{");
        encodeNonEscapedObject(responseWriter, "select", "", true);
        responseWriter.write("function(event){");
        String clientId = inputTime.getClientId(facesContext);
        String inputClientId = clientId.concat(INPUT_SUFFIX);
        EscapedClientId escapedInputClientId = new EscapedClientId(inputClientId);
        boolean selectable = true;
        JavaScriptFragment time = null;
        if (showOnButton) {
            time = new JavaScriptFragment("event.result.text");
        }
        encodeFunctionCall(responseWriter, "LFAI.inputDateTimePickerSelect", 'A', escapedInputClientId, selectable, time, valueChangeClientBehaviorsNotEmpty);
        responseWriter.append(";}");
        responseWriter.write("}");
        autoCompleteFirst = false;
    }
    responseWriter.write("}");
}
Also used : JavaScriptFragment(com.liferay.faces.util.render.JavaScriptFragment) List(java.util.List) EscapedClientId(com.liferay.faces.alloy.render.internal.EscapedClientId) ClientBehavior(javax.faces.component.behavior.ClientBehavior)

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