Search in sources :

Example 1 with ThrottlingSettings

use of org.apache.wicket.ajax.attributes.ThrottlingSettings in project midpoint by Evolveum.

the class SearchPanel method initLayout.

private <S extends SearchItem, T extends Serializable> void initLayout() {
    moreDialogModel = new LoadableModel<MoreDialogDto>(false) {

        private static final long serialVersionUID = 1L;

        @Override
        protected MoreDialogDto load() {
            MoreDialogDto dto = new MoreDialogDto();
            dto.setProperties(createPropertiesList());
            return dto;
        }

        @Override
        public MoreDialogDto getObject() {
            if (SearchPanel.this.getModelObject() != null && SearchPanel.this.getModelObject().isTypeChanged()) {
                reset();
            }
            return super.getObject();
        }
    };
    MidpointForm<?> form = new MidpointForm<>(ID_FORM);
    add(form);
    AjaxButton debug = new AjaxButton(ID_DEBUG, createStringResource("SearchPanel.debug")) {

        private static final long serialVersionUID = 1L;

        @Override
        public void onClick(AjaxRequestTarget target) {
            debugPerformed();
        }
    };
    debug.add(new VisibleEnableBehaviour() {

        private static final long serialVersionUID = 1L;

        @Override
        public boolean isVisible() {
            return SearchBoxModeType.ADVANCED.equals(getModelObject().getSearchType()) && queryPlaygroundAccessible;
        }
    });
    form.add(debug);
    PropertyModel<ObjectCollectionSearchItem> collectionModel = new PropertyModel<>(getModel(), Search.F_COLLECTION);
    SearchObjectCollectionPanel collectionPanel = new SearchObjectCollectionPanel(ID_COLLECTION_REF_PANEL, collectionModel);
    form.add(collectionPanel);
    collectionPanel.add(new VisibleBehaviour(() -> collectionModel != null && collectionModel.getObject() != null && getModelObject().isCollectionItemVisible()));
    PropertyModel<ContainerTypeSearchItem> typeModel = new PropertyModel<>(getModel(), Search.F_TYPE);
    SearchTypePanel typePanel = new SearchTypePanel(ID_TYPE_PANEL, typeModel) {

        @Override
        protected void searchPerformed(AjaxRequestTarget target) {
            resetMoreDialogModel();
            SearchPanel.this.searchPerformed(target);
        }
    };
    form.add(typePanel);
    typePanel.add(new VisibleBehaviour(() -> typeModel != null && typeModel.getObject() != null && typeModel.getObject().isVisible() && !SearchBoxModeType.OID.equals(getModelObject().getSearchType())));
    ListView<SearchItem> specialItems = createSpecialItemsPanel(ID_SPECIAL_ITEMS, new PropertyModel<>(getModel(), Search.F_SPECIAL_ITEMS));
    form.add(specialItems);
    ListView<SearchItem> compositedItems = createSpecialItemsPanel(ID_COMPOSITED_SPECIAL_ITEMS, new PropertyModel(getModel(), Search.F_COMPOSITED_SPECIAL_ITEMS + "." + AbstractRoleCompositedSearchItem.F_SEARCH_ITEMS));
    form.add(compositedItems);
    ListView<S> items = new ListView<S>(ID_ITEMS, new PropertyModel<>(getModel(), Search.F_ITEMS)) {

        private static final long serialVersionUID = 1L;

        @Override
        protected void populateItem(ListItem<S> item) {
            AbstractSearchItemPanel searchItem;
            if (item.getModelObject() instanceof FilterSearchItem) {
                searchItem = new SearchFilterPanel(ID_ITEM, (IModel<FilterSearchItem>) item.getModel()) {

                    private static final long serialVersionUID = 1L;

                    @Override
                    protected boolean canRemoveSearchItem() {
                        return super.canRemoveSearchItem() && SearchPanel.this.getModelObject().isCanConfigure();
                    }

                    @Override
                    protected void searchPerformed(AjaxRequestTarget target) {
                        SearchPanel.this.searchPerformed(target);
                    }
                };
            } else {
                searchItem = new SearchPropertyPanel<T>(ID_ITEM, (IModel<PropertySearchItem<T>>) item.getModel()) {

                    private static final long serialVersionUID = 1L;

                    @Override
                    protected boolean canRemoveSearchItem() {
                        return super.canRemoveSearchItem() && SearchPanel.this.getModelObject().isCanConfigure();
                    }

                    @Override
                    protected void searchPerformed(AjaxRequestTarget target) {
                        SearchPanel.this.searchPerformed(target);
                    }
                };
            }
            item.add(searchItem);
        }
    };
    items.add(createVisibleBehaviour(SearchBoxModeType.BASIC));
    form.add(items);
    SearchSpecialItemPanel oidItem = new SearchSpecialItemPanel<String>(ID_OID_ITEM, new PropertyModel<String>(getModel(), Search.F_OID)) {

        @Override
        protected WebMarkupContainer initSearchItemField(String id) {
            TextPanel<String> inputPanel = new TextPanel<String>(id, getModelValue());
            inputPanel.getBaseFormComponent().add(AttributeAppender.append("style", "width: 220px; max-width: 400px !important;"));
            return inputPanel;
        }

        @Override
        protected IModel<String> createLabelModel() {
            return getPageBase().createStringResource("SearchPanel.oid");
        }

        @Override
        protected IModel<String> createHelpModel() {
            return getPageBase().createStringResource("SearchPanel.oid.help");
        }
    };
    oidItem.add(createVisibleBehaviour(SearchBoxModeType.OID));
    form.add(oidItem);
    WebMarkupContainer moreGroup = new WebMarkupContainer(ID_MORE_GROUP);
    moreGroup.add(new VisibleBehaviour(() -> createVisibleBehaviour(SearchBoxModeType.BASIC).isVisible()));
    form.add(moreGroup);
    AjaxLink<Void> more = new AjaxLink<Void>(ID_MORE) {

        private static final long serialVersionUID = 1L;

        @Override
        public void onClick(AjaxRequestTarget target) {
            resetMoreDialogModel();
            Component popover = SearchPanel.this.get(createComponentPath(ID_POPOVER));
            Component button = SearchPanel.this.get(createComponentPath(ID_FORM, ID_MORE_GROUP, ID_MORE));
            togglePopover(target, button, popover, 14);
        }
    };
    more.add(new VisibleEnableBehaviour() {

        private static final long serialVersionUID = 1L;

        @Override
        public boolean isVisible() {
            Search search = getModelObject();
            return !search.getAvailableDefinitions().isEmpty();
        }
    });
    more.setOutputMarkupId(true);
    moreGroup.add(more);
    WebMarkupContainer searchContainer = new WebMarkupContainer(ID_SEARCH_CONTAINER);
    searchContainer.setOutputMarkupId(true);
    form.add(searchContainer);
    final CompositedIconBuilder builder = new CompositedIconBuilder();
    builder.setBasicIcon(GuiStyleConstants.CLASS_ICON_SEARCH, IconCssStyle.IN_ROW_STYLE);
    IconType plusIcon = new IconType();
    plusIcon.setColor("white");
    builder.appendLayerIcon(getIconLabelByModeModel(), plusIcon, LayeredIconCssStyle.BOTTOM_RIGHT_STYLE);
    AjaxCompositedIconSubmitButton searchButtonBeforeDropdown = new AjaxCompositedIconSubmitButton(ID_SEARCH_BUTTON_BEFORE_DROPDOWN, builder.build(), getPageBase().createStringResource("SearchPanel.search")) {

        private static final long serialVersionUID = 1L;

        @Override
        protected void onError(AjaxRequestTarget target) {
            target.add(form);
        }

        @Override
        protected void onSubmit(AjaxRequestTarget target) {
            searchPerformed(target);
        }
    };
    IModel<String> buttonRightPaddingModel = () -> {
        boolean isLongButton = SearchBoxModeType.BASIC.equals(getModelObject().getSearchType()) || SearchBoxModeType.AXIOM_QUERY.equals(getModelObject().getSearchType());
        String style = "padding-right: " + (isLongButton ? "23" : "16") + "px;";
        if (getModelObject().getAllowedSearchType().size() == 1) {
            style = style + "border-top-right-radius: 3px; border-bottom-right-radius: 3px;";
        }
        return style;
    };
    searchButtonBeforeDropdown.add(AttributeAppender.append("style", buttonRightPaddingModel));
    searchButtonBeforeDropdown.add(new VisibleEnableBehaviour() {

        private static final long serialVersionUID = 1L;

        @Override
        public boolean isEnabled() {
            if (SearchBoxModeType.ADVANCED.equals(getModelObject().getSearchType()) || SearchBoxModeType.AXIOM_QUERY.equals(getModelObject().getSearchType())) {
                Search search = getModelObject();
                PrismContext ctx = getPageBase().getPrismContext();
                return search.isAdvancedQueryValid(ctx);
            }
            return true;
        }

        @Override
        public boolean isVisible() {
            Search search = getModelObject();
            if (search.getAllowedSearchType().size() == 1 && SearchBoxModeType.BASIC.equals(search.getAllowedSearchType().get(0))) {
                return !search.getItems().isEmpty() || !search.getAvailableDefinitions().isEmpty();
            }
            return true;
        }
    });
    searchButtonBeforeDropdown.setOutputMarkupId(true);
    searchContainer.add(searchButtonBeforeDropdown);
    form.setDefaultButton(searchButtonBeforeDropdown);
    WebMarkupContainer dropdownButton = new WebMarkupContainer(ID_SEARCH_BUTTON_DROPDOWN);
    dropdownButton.add(new VisibleBehaviour(() -> getModelObject().getAllowedSearchType().size() != 1));
    searchContainer.add(dropdownButton);
    List<InlineMenuItem> searchItems = new ArrayList<>();
    InlineMenuItem searchItem = new InlineMenuItem(createStringResource("SearchPanel.basic")) {

        private static final long serialVersionUID = 1L;

        @Override
        public InlineMenuItemAction initAction() {
            return new InlineMenuItemAction() {

                private static final long serialVersionUID = 1L;

                @Override
                public void onClick(AjaxRequestTarget target) {
                    searchTypeUpdated(target, SearchBoxModeType.BASIC);
                }
            };
        }

        @Override
        public IModel<Boolean> getVisible() {
            return Model.of(getModelObject().isAllowedSearchMode(SearchBoxModeType.BASIC));
        }
    };
    searchItems.add(searchItem);
    searchItem = new InlineMenuItem(createStringResource("SearchPanel.advanced")) {

        private static final long serialVersionUID = 1L;

        @Override
        public InlineMenuItemAction initAction() {
            return new InlineMenuItemAction() {

                private static final long serialVersionUID = 1L;

                @Override
                public void onClick(AjaxRequestTarget target) {
                    searchTypeUpdated(target, SearchBoxModeType.ADVANCED);
                }
            };
        }

        @Override
        public IModel<Boolean> getVisible() {
            return Model.of(getModelObject().isAllowedSearchMode(SearchBoxModeType.ADVANCED));
        }
    };
    searchItems.add(searchItem);
    searchItem = new InlineMenuItem(createStringResource("SearchPanel.fullText")) {

        private static final long serialVersionUID = 1L;

        @Override
        public InlineMenuItemAction initAction() {
            return new InlineMenuItemAction() {

                private static final long serialVersionUID = 1L;

                @Override
                public void onClick(AjaxRequestTarget target) {
                    searchTypeUpdated(target, SearchBoxModeType.FULLTEXT);
                }
            };
        }

        @Override
        public IModel<Boolean> getVisible() {
            return Model.of(isFullTextSearchEnabled() && getModelObject().isAllowedSearchMode(SearchBoxModeType.FULLTEXT));
        }
    };
    searchItems.add(searchItem);
    searchItem = new InlineMenuItem(createStringResource("SearchPanel.oid")) {

        private static final long serialVersionUID = 1L;

        @Override
        public InlineMenuItemAction initAction() {
            return new InlineMenuItemAction() {

                private static final long serialVersionUID = 1L;

                @Override
                public void onClick(AjaxRequestTarget target) {
                    searchTypeUpdated(target, SearchBoxModeType.OID);
                }
            };
        }

        @Override
        public IModel<Boolean> getVisible() {
            return Model.of(getModelObject().isOidSearchEnabled() && getModelObject().isAllowedSearchMode(SearchBoxModeType.OID));
        }
    };
    searchItems.add(searchItem);
    searchItem = new InlineMenuItem(createStringResource("SearchPanel.axiomQuery")) {

        private static final long serialVersionUID = 1L;

        @Override
        public InlineMenuItemAction initAction() {
            return new InlineMenuItemAction() {

                private static final long serialVersionUID = 1L;

                @Override
                public void onClick(AjaxRequestTarget target) {
                    searchTypeUpdated(target, SearchBoxModeType.AXIOM_QUERY);
                }
            };
        }

        @Override
        public IModel<Boolean> getVisible() {
            return Model.of(WebModelServiceUtils.isEnableExperimentalFeature(getPageBase()) && getModelObject().isAllowedSearchMode(SearchBoxModeType.AXIOM_QUERY));
        }
    };
    searchItems.add(searchItem);
    ListView<InlineMenuItem> li = new ListView<InlineMenuItem>(ID_MENU_ITEM, Model.ofList(searchItems)) {

        private static final long serialVersionUID = 1L;

        @Override
        protected void populateItem(ListItem<InlineMenuItem> item) {
            WebMarkupContainer menuItemBody = new MenuLinkPanel(ID_MENU_ITEM_BODY, item.getModel());
            menuItemBody.setRenderBodyOnly(true);
            item.add(menuItemBody);
            menuItemBody.add(new VisibleEnableBehaviour() {

                @Override
                public boolean isVisible() {
                    return Boolean.TRUE.equals(item.getModelObject().getVisible().getObject());
                }
            });
        }
    };
    searchContainer.add(li);
    initPopover();
    WebMarkupContainer fullTextContainer = new WebMarkupContainer(ID_FULL_TEXT_CONTAINER);
    fullTextContainer.add(new VisibleEnableBehaviour() {

        private static final long serialVersionUID = 1L;

        @Override
        public boolean isVisible() {
            return isFullTextSearchEnabled() && getModelObject().getSearchType().equals(SearchBoxModeType.FULLTEXT);
        }
    });
    fullTextContainer.setOutputMarkupId(true);
    form.add(fullTextContainer);
    TextField<String> fullTextInput = new TextField<>(ID_FULL_TEXT_FIELD, new PropertyModel<>(getModel(), Search.F_FULL_TEXT));
    fullTextInput.add(new AjaxFormComponentUpdatingBehavior("blur") {

        private static final long serialVersionUID = 1L;

        @Override
        protected void onUpdate(AjaxRequestTarget target) {
        }
    });
    fullTextInput.add(WebComponentUtil.getSubmitOnEnterKeyDownBehavior("searchSimple"));
    fullTextInput.setOutputMarkupId(true);
    fullTextInput.add(new AttributeAppender("placeholder", createStringResource("SearchPanel.fullTextSearch")));
    fullTextInput.add(createVisibleBehaviour(SearchBoxModeType.FULLTEXT));
    fullTextContainer.add(fullTextInput);
    WebMarkupContainer advancedGroup = new WebMarkupContainer(ID_ADVANCED_GROUP);
    advancedGroup.add(createVisibleBehaviour(SearchBoxModeType.ADVANCED, SearchBoxModeType.AXIOM_QUERY));
    advancedGroup.add(AttributeAppender.append("class", createAdvancedGroupStyle()));
    advancedGroup.setOutputMarkupId(true);
    form.add(advancedGroup);
    Label advancedCheck = new Label(ID_ADVANCED_CHECK);
    advancedCheck.add(AttributeAppender.append("class", createAdvancedGroupLabelStyle()));
    advancedCheck.setOutputMarkupId(true);
    advancedGroup.add(advancedCheck);
    TextArea<?> advancedArea = new TextArea<>(ID_ADVANCED_AREA, new PropertyModel<>(getModel(), Search.F_ADVANCED_QUERY));
    advancedArea.add(new AjaxFormComponentUpdatingBehavior("keyup") {

        @Override
        protected void onUpdate(AjaxRequestTarget target) {
            updateAdvancedArea(advancedArea, target);
        }

        @Override
        protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
            super.updateAjaxAttributes(attributes);
            attributes.setThrottlingSettings(new ThrottlingSettings(ID_ADVANCED_AREA, Duration.ofMillis(500), true));
        }
    });
    advancedArea.add(AttributeAppender.append("placeholder", getPageBase().createStringResource("SearchPanel.insertFilterXml")));
    advancedArea.add(createVisibleBehaviour(SearchBoxModeType.ADVANCED));
    advancedGroup.add(advancedArea);
    TextField<String> queryDslField = new TextField<>(ID_AXIOM_QUERY_FIELD, new PropertyModel<>(getModel(), Search.F_DSL_QUERY));
    queryDslField.add(new AjaxFormComponentUpdatingBehavior("keyup") {

        @Override
        protected void onUpdate(AjaxRequestTarget target) {
            updateQueryDSLArea(advancedCheck, advancedGroup, target);
        }

        @Override
        protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
            super.updateAjaxAttributes(attributes);
            attributes.setThrottlingSettings(new ThrottlingSettings(ID_AXIOM_QUERY_FIELD, Duration.ofMillis(500), true));
            attributes.setChannel(new AjaxChannel("Drop", AjaxChannel.Type.DROP));
        }
    });
    queryDslField.add(AttributeAppender.append("placeholder", getPageBase().createStringResource("SearchPanel.insertAxiomQuery")));
    queryDslField.add(createVisibleBehaviour(SearchBoxModeType.AXIOM_QUERY));
    advancedGroup.add(queryDslField);
    WebMarkupContainer advancedErrorGroup = new WebMarkupContainer(ID_ADVANCED_ERROR_GROUP);
    advancedErrorGroup.setOutputMarkupId(true);
    advancedGroup.add(advancedErrorGroup);
    Label advancedError = new Label(ID_ADVANCED_ERROR, new PropertyModel<String>(getModel(), Search.F_ADVANCED_ERROR));
    advancedError.add(new VisibleEnableBehaviour() {

        private static final long serialVersionUID = 1L;

        @Override
        public boolean isVisible() {
            Search search = getModelObject();
            if (!search.isShowAdvanced()) {
                return false;
            }
            return StringUtils.isNotEmpty(search.getAdvancedError());
        }
    });
    advancedErrorGroup.add(advancedError);
}
Also used : VisibleBehaviour(com.evolveum.midpoint.web.component.util.VisibleBehaviour) TextArea(org.apache.wicket.markup.html.form.TextArea) ArrayList(java.util.ArrayList) Label(org.apache.wicket.markup.html.basic.Label) TextPanel(com.evolveum.midpoint.web.component.input.TextPanel) AbstractRoleCompositedSearchItem(com.evolveum.midpoint.web.page.admin.roles.AbstractRoleCompositedSearchItem) AjaxButton(com.evolveum.midpoint.web.component.AjaxButton) ListView(org.apache.wicket.markup.html.list.ListView) CompositedIconBuilder(com.evolveum.midpoint.gui.impl.component.icon.CompositedIconBuilder) TextField(org.apache.wicket.markup.html.form.TextField) AjaxChannel(org.apache.wicket.ajax.AjaxChannel) AjaxLink(org.apache.wicket.ajax.markup.html.AjaxLink) AjaxFormComponentUpdatingBehavior(org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior) IconType(com.evolveum.midpoint.xml.ns._public.common.common_3.IconType) AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) ThrottlingSettings(org.apache.wicket.ajax.attributes.ThrottlingSettings) WebMarkupContainer(org.apache.wicket.markup.html.WebMarkupContainer) InlineMenuItem(com.evolveum.midpoint.web.component.menu.cog.InlineMenuItem) VisibleEnableBehaviour(com.evolveum.midpoint.web.component.util.VisibleEnableBehaviour) Component(org.apache.wicket.Component) AttributeAppender(org.apache.wicket.behavior.AttributeAppender) IModel(org.apache.wicket.model.IModel) PropertyModel(org.apache.wicket.model.PropertyModel) MidpointForm(com.evolveum.midpoint.web.component.form.MidpointForm) AjaxRequestAttributes(org.apache.wicket.ajax.attributes.AjaxRequestAttributes) MenuLinkPanel(com.evolveum.midpoint.web.component.menu.cog.MenuLinkPanel) InlineMenuItemAction(com.evolveum.midpoint.web.component.menu.cog.InlineMenuItemAction) ListItem(org.apache.wicket.markup.html.list.ListItem) AjaxCompositedIconSubmitButton(com.evolveum.midpoint.web.component.AjaxCompositedIconSubmitButton)

Example 2 with ThrottlingSettings

use of org.apache.wicket.ajax.attributes.ThrottlingSettings in project wicket by apache.

the class AbstractDefaultAjaxBehavior method renderAjaxAttributes.

/**
 * @param component
 * @param attributes
 * @return the attributes as string in JSON format
 */
protected final CharSequence renderAjaxAttributes(final Component component, AjaxRequestAttributes attributes) {
    JSONObject attributesJson = new JSONObject();
    try {
        attributesJson.put(AjaxAttributeName.URL.jsonName(), getCallbackUrl());
        Method method = attributes.getMethod();
        if (Method.POST == method) {
            attributesJson.put(AjaxAttributeName.METHOD.jsonName(), method);
        }
        if (component instanceof Page == false) {
            String componentId = component.getMarkupId();
            attributesJson.put(AjaxAttributeName.MARKUP_ID.jsonName(), componentId);
        }
        String formId = attributes.getFormId();
        if (Strings.isEmpty(formId) == false) {
            attributesJson.put(AjaxAttributeName.FORM_ID.jsonName(), formId);
        }
        if (attributes.isMultipart()) {
            attributesJson.put(AjaxAttributeName.IS_MULTIPART.jsonName(), true);
        }
        String submittingComponentId = attributes.getSubmittingComponentName();
        if (Strings.isEmpty(submittingComponentId) == false) {
            attributesJson.put(AjaxAttributeName.SUBMITTING_COMPONENT_NAME.jsonName(), submittingComponentId);
        }
        CharSequence childSelector = attributes.getChildSelector();
        if (Strings.isEmpty(childSelector) == false) {
            attributesJson.put(AjaxAttributeName.CHILD_SELECTOR.jsonName(), childSelector);
        }
        if (attributes.isSerializeRecursively()) {
            attributesJson.put(AjaxAttributeName.SERIALIZE_RECURSIVELY.jsonName(), true);
        }
        String indicatorId = findIndicatorId();
        if (Strings.isEmpty(indicatorId) == false) {
            attributesJson.put(AjaxAttributeName.INDICATOR_ID.jsonName(), indicatorId);
        }
        for (IAjaxCallListener ajaxCallListener : attributes.getAjaxCallListeners()) {
            if (ajaxCallListener != null) {
                CharSequence initHandler = ajaxCallListener.getInitHandler(component);
                appendListenerHandler(initHandler, attributesJson, AjaxAttributeName.INIT_HANDLER.jsonName(), INIT_HANDLER_FUNCTION_TEMPLATE);
                CharSequence beforeHandler = ajaxCallListener.getBeforeHandler(component);
                appendListenerHandler(beforeHandler, attributesJson, AjaxAttributeName.BEFORE_HANDLER.jsonName(), BEFORE_HANDLER_FUNCTION_TEMPLATE);
                CharSequence beforeSendHandler = ajaxCallListener.getBeforeSendHandler(component);
                appendListenerHandler(beforeSendHandler, attributesJson, AjaxAttributeName.BEFORE_SEND_HANDLER.jsonName(), BEFORE_SEND_HANDLER_FUNCTION_TEMPLATE);
                CharSequence afterHandler = ajaxCallListener.getAfterHandler(component);
                appendListenerHandler(afterHandler, attributesJson, AjaxAttributeName.AFTER_HANDLER.jsonName(), AFTER_HANDLER_FUNCTION_TEMPLATE);
                CharSequence successHandler = ajaxCallListener.getSuccessHandler(component);
                appendListenerHandler(successHandler, attributesJson, AjaxAttributeName.SUCCESS_HANDLER.jsonName(), SUCCESS_HANDLER_FUNCTION_TEMPLATE);
                CharSequence failureHandler = ajaxCallListener.getFailureHandler(component);
                appendListenerHandler(failureHandler, attributesJson, AjaxAttributeName.FAILURE_HANDLER.jsonName(), FAILURE_HANDLER_FUNCTION_TEMPLATE);
                CharSequence completeHandler = ajaxCallListener.getCompleteHandler(component);
                appendListenerHandler(completeHandler, attributesJson, AjaxAttributeName.COMPLETE_HANDLER.jsonName(), COMPLETE_HANDLER_FUNCTION_TEMPLATE);
                CharSequence precondition = ajaxCallListener.getPrecondition(component);
                appendListenerHandler(precondition, attributesJson, AjaxAttributeName.PRECONDITION.jsonName(), PRECONDITION_FUNCTION_TEMPLATE);
                CharSequence doneHandler = ajaxCallListener.getDoneHandler(component);
                appendListenerHandler(doneHandler, attributesJson, AjaxAttributeName.DONE_HANDLER.jsonName(), DONE_HANDLER_FUNCTION_TEMPLATE);
            }
        }
        JSONArray extraParameters = JsonUtils.asArray(attributes.getExtraParameters());
        if (extraParameters.length() > 0) {
            attributesJson.put(AjaxAttributeName.EXTRA_PARAMETERS.jsonName(), extraParameters);
        }
        List<CharSequence> dynamicExtraParameters = attributes.getDynamicExtraParameters();
        if (dynamicExtraParameters != null) {
            for (CharSequence dynamicExtraParameter : dynamicExtraParameters) {
                String func = String.format(DYNAMIC_PARAMETER_FUNCTION_TEMPLATE, dynamicExtraParameter);
                JSONFunction function = new JSONFunction(func);
                attributesJson.append(AjaxAttributeName.DYNAMIC_PARAMETER_FUNCTION.jsonName(), function);
            }
        }
        if (attributes.isAsynchronous() == false) {
            attributesJson.put(AjaxAttributeName.IS_ASYNC.jsonName(), false);
        }
        String[] eventNames = attributes.getEventNames();
        if (eventNames.length == 1) {
            attributesJson.put(AjaxAttributeName.EVENT_NAME.jsonName(), eventNames[0]);
        } else {
            for (String eventName : eventNames) {
                attributesJson.append(AjaxAttributeName.EVENT_NAME.jsonName(), eventName);
            }
        }
        AjaxChannel channel = attributes.getChannel();
        if (channel != null && channel.equals(AjaxChannel.DEFAULT) == false) {
            attributesJson.put(AjaxAttributeName.CHANNEL.jsonName(), channel);
        }
        if (attributes.isPreventDefault()) {
            attributesJson.put(AjaxAttributeName.IS_PREVENT_DEFAULT.jsonName(), true);
        }
        if (AjaxRequestAttributes.EventPropagation.STOP.equals(attributes.getEventPropagation())) {
            attributesJson.put(AjaxAttributeName.EVENT_PROPAGATION.jsonName(), "stop");
        } else if (AjaxRequestAttributes.EventPropagation.STOP_IMMEDIATE.equals(attributes.getEventPropagation())) {
            attributesJson.put(AjaxAttributeName.EVENT_PROPAGATION.jsonName(), "stopImmediate");
        }
        Duration requestTimeout = attributes.getRequestTimeout();
        if (requestTimeout != null) {
            attributesJson.put(AjaxAttributeName.REQUEST_TIMEOUT.jsonName(), requestTimeout.getMilliseconds());
        }
        boolean wicketAjaxResponse = attributes.isWicketAjaxResponse();
        if (wicketAjaxResponse == false) {
            attributesJson.put(AjaxAttributeName.IS_WICKET_AJAX_RESPONSE.jsonName(), false);
        }
        String dataType = attributes.getDataType();
        if (AjaxRequestAttributes.XML_DATA_TYPE.equals(dataType) == false) {
            attributesJson.put(AjaxAttributeName.DATATYPE.jsonName(), dataType);
        }
        ThrottlingSettings throttlingSettings = attributes.getThrottlingSettings();
        if (throttlingSettings != null) {
            JSONObject throttlingSettingsJson = new JSONObject();
            String throttleId = throttlingSettings.getId();
            if (throttleId == null) {
                throttleId = component.getMarkupId();
            }
            throttlingSettingsJson.put(AjaxAttributeName.THROTTLING_ID.jsonName(), throttleId);
            throttlingSettingsJson.put(AjaxAttributeName.THROTTLING_DELAY.jsonName(), throttlingSettings.getDelay().getMilliseconds());
            if (throttlingSettings.getPostponeTimerOnUpdate()) {
                throttlingSettingsJson.put(AjaxAttributeName.THROTTLING_POSTPONE_ON_UPDATE.jsonName(), true);
            }
            attributesJson.put(AjaxAttributeName.THROTTLING.jsonName(), throttlingSettingsJson);
        }
        postprocessConfiguration(attributesJson, component);
    } catch (JSONException e) {
        throw new WicketRuntimeException(e);
    }
    String attributesAsJson = attributesJson.toString();
    return attributesAsJson;
}
Also used : ThrottlingSettings(org.apache.wicket.ajax.attributes.ThrottlingSettings) IAjaxCallListener(org.apache.wicket.ajax.attributes.IAjaxCallListener) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) JSONArray(com.github.openjson.JSONArray) JSONException(com.github.openjson.JSONException) Page(org.apache.wicket.Page) Duration(org.apache.wicket.util.time.Duration) Method(org.apache.wicket.ajax.attributes.AjaxRequestAttributes.Method) JSONFunction(org.apache.wicket.ajax.json.JSONFunction) JSONObject(com.github.openjson.JSONObject)

Example 3 with ThrottlingSettings

use of org.apache.wicket.ajax.attributes.ThrottlingSettings in project midpoint by Evolveum.

the class PasswordPanel method initLayout.

private <F extends FocusType> void initLayout(final boolean isReadOnly, PrismObject<F> object) {
    setOutputMarkupId(true);
    final WebMarkupContainer inputContainer = new WebMarkupContainer(ID_INPUT_CONTAINER) {

        private static final long serialVersionUID = 1L;

        @Override
        public boolean isVisible() {
            return passwordInputVisible;
        }
    };
    inputContainer.setOutputMarkupId(true);
    add(inputContainer);
    ValuePolicyType valuePolicy = getValuePolicy(object);
    LoadableModel<List<StringLimitationResult>> limitationsModel = new LoadableModel<>() {

        @Override
        protected List<StringLimitationResult> load() {
            return getLimitationsForActualPassword(valuePolicy, object);
        }
    };
    final PasswordLimitationsPanel validationPanel = new PasswordLimitationsPanel(ID_VALIDATION_PANEL, limitationsModel);
    validationPanel.setOutputMarkupId(true);
    inputContainer.add(validationPanel);
    final PasswordTextField password1 = new SecureModelPasswordTextField(ID_PASSWORD_ONE, new PasswordModel(model)) {

        private static final long serialVersionUID = 1L;

        @Override
        protected void onComponentTag(ComponentTag tag) {
            super.onComponentTag(tag);
            if (clearPasswordInput) {
                tag.remove("value");
            }
        }
    };
    password1.add(AttributeAppender.append("onfocus", "initPasswordValidation({\n" + "container: $('#progress-bar-container'),\n" + "hierarchy: {\n" + "    '0': ['progress-bar-danger', '" + PageBase.createStringResourceStatic(null, "PasswordPanel.strength.veryWeak").getString() + "'],\n" + "    '25': ['progress-bar-danger', '" + PageBase.createStringResourceStatic(null, "PasswordPanel.strength.weak").getString() + "'],\n" + "    '50': ['progress-bar-warning', '" + PageBase.createStringResourceStatic(null, "PasswordPanel.strength.good").getString() + "'],\n" + "    '75': ['progress-bar-success', '" + PageBase.createStringResourceStatic(null, "PasswordPanel.strength.strong").getString() + "'],\n" + "    '100': ['progress-bar-success', '" + PageBase.createStringResourceStatic(null, "PasswordPanel.strength.veryStrong").getString() + "']\n" + "}\n" + "})"));
    password1.setRequired(false);
    password1.setOutputMarkupId(true);
    password1.add(new EmptyOnBlurAjaxFormUpdatingBehaviour());
    inputContainer.add(password1);
    final PasswordTextField password2 = new SecureModelPasswordTextField(ID_PASSWORD_TWO, new PasswordModel(Model.of(new ProtectedStringType())));
    password2.setRequired(false);
    password2.setOutputMarkupId(true);
    inputContainer.add(password2);
    password1.add(new AjaxFormComponentUpdatingBehavior("change") {

        private static final long serialVersionUID = 1L;

        @Override
        protected void onUpdate(AjaxRequestTarget target) {
            boolean required = !StringUtils.isEmpty(password1.getModelObject());
            password2.setRequired(required);
            changePasswordPerformed();
        }
    });
    IModel<String> password2ValidationModel = (IModel<String>) () -> {
        String s1 = password1.getModelObject();
        String s2 = password2.getValue();
        if (StringUtils.isEmpty(s1) || StringUtils.isEmpty(s2)) {
            return "";
        }
        if (!Objects.equals(s1, s2)) {
            return PageBase.createStringResourceStatic(null, "passwordPanel.error").getString();
        }
        return "";
    };
    Label password2ValidationMessage = new Label(ID_PASSWORD_TWO_VALIDATION_MESSAGE, password2ValidationModel);
    password2ValidationMessage.setOutputMarkupId(true);
    inputContainer.add(password2ValidationMessage);
    password1.add(new AjaxFormComponentUpdatingBehavior("keyup input") {

        private static final long serialVersionUID = 1L;

        @Override
        protected void onUpdate(AjaxRequestTarget target) {
            limitationsModel.reset();
            validationPanel.refreshItems(target);
            updatePasswordValidation(target);
            target.add(password2ValidationMessage);
        }

        @Override
        protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
            super.updateAjaxAttributes(attributes);
            attributes.setThrottlingSettings(new ThrottlingSettings(Duration.ofMillis(500), true));
            attributes.setChannel(new AjaxChannel("Drop", AjaxChannel.Type.DROP));
        }
    });
    PasswordValidator pass2Validator = new PasswordValidator(password1);
    password2.add(pass2Validator);
    password2.add(new AjaxFormComponentUpdatingBehavior("keyup input") {

        private static final long serialVersionUID = 1L;

        @Override
        protected void onUpdate(AjaxRequestTarget target) {
            target.add(password2ValidationMessage);
        }

        @Override
        protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
            super.updateAjaxAttributes(attributes);
            attributes.setThrottlingSettings(new ThrottlingSettings(Duration.ofMillis(500), true));
            attributes.setChannel(new AjaxChannel("Drop", AjaxChannel.Type.DROP));
        }
    });
    final WebMarkupContainer linkContainer = new WebMarkupContainer(ID_LINK_CONTAINER) {

        private static final long serialVersionUID = 1L;

        @Override
        public boolean isVisible() {
            return !passwordInputVisible;
        }
    };
    inputContainer.setOutputMarkupId(true);
    linkContainer.setOutputMarkupId(true);
    add(linkContainer);
    final Label passwordSetLabel = new Label(ID_PASSWORD_SET, new ResourceModel("passwordPanel.passwordSet"));
    linkContainer.add(passwordSetLabel);
    final Label passwordRemoveLabel = new Label(ID_PASSWORD_REMOVE, new ResourceModel("passwordPanel.passwordRemoveLabel"));
    passwordRemoveLabel.setVisible(false);
    linkContainer.add(passwordRemoveLabel);
    AjaxLink<Void> link = new AjaxLink<Void>(ID_CHANGE_PASSWORD_LINK) {

        private static final long serialVersionUID = 1L;

        @Override
        public void onClick(AjaxRequestTarget target) {
            clearPasswordInput = true;
            setPasswordInput = false;
            onLinkClick(target);
        }

        @Override
        public boolean isVisible() {
            return !passwordInputVisible && model != null && model.getObject() != null;
        }
    };
    link.add(new VisibleEnableBehaviour() {

        private static final long serialVersionUID = 1L;

        @Override
        public boolean isVisible() {
            return !isReadOnly;
        }
    });
    link.setBody(new ResourceModel("passwordPanel.passwordChange"));
    link.setOutputMarkupId(true);
    linkContainer.add(link);
    final WebMarkupContainer removeButtonContainer = new WebMarkupContainer(ID_REMOVE_BUTTON_CONTAINER);
    AjaxLink<Void> removePassword = new AjaxLink<Void>(ID_REMOVE_PASSWORD_LINK) {

        private static final long serialVersionUID = 1L;

        @Override
        public void onClick(AjaxRequestTarget target) {
            onRemovePassword(model, target);
        }
    };
    removePassword.add(new VisibleEnableBehaviour() {

        private static final long serialVersionUID = 1L;

        @Override
        public boolean isVisible() {
            PageBase pageBase = getPageBase();
            if (pageBase == null) {
                return false;
            }
            if (pageBase instanceof PageUserSelfProfile || pageBase instanceof PageOrgSelfProfile || pageBase instanceof PageRoleSelfProfile || pageBase instanceof PageServiceSelfProfile) {
                return false;
            }
            if (pageBase instanceof PageAdminFocus && !((PageAdminFocus) pageBase).isLoggedInFocusPage() && model.getObject() != null) {
                return true;
            }
            return false;
        }
    });
    removePassword.setBody(new ResourceModel("passwordPanel.passwordRemove"));
    removePassword.setOutputMarkupId(true);
    removeButtonContainer.add(removePassword);
    add(removeButtonContainer);
}
Also used : ThrottlingSettings(org.apache.wicket.ajax.attributes.ThrottlingSettings) ValuePolicyType(com.evolveum.midpoint.xml.ns._public.common.common_3.ValuePolicyType) PageAdminFocus(com.evolveum.midpoint.web.page.admin.PageAdminFocus) StringLimitationResult(com.evolveum.midpoint.model.api.validator.StringLimitationResult) Label(org.apache.wicket.markup.html.basic.Label) WebMarkupContainer(org.apache.wicket.markup.html.WebMarkupContainer) PageRoleSelfProfile(com.evolveum.midpoint.web.page.self.PageRoleSelfProfile) ResourceModel(org.apache.wicket.model.ResourceModel) List(java.util.List) ArrayList(java.util.ArrayList) AjaxChannel(org.apache.wicket.ajax.AjaxChannel) VisibleEnableBehaviour(com.evolveum.midpoint.web.component.util.VisibleEnableBehaviour) AjaxLink(org.apache.wicket.ajax.markup.html.AjaxLink) AjaxFormComponentUpdatingBehavior(org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior) IModel(org.apache.wicket.model.IModel) PageServiceSelfProfile(com.evolveum.midpoint.web.page.self.PageServiceSelfProfile) PasswordTextField(org.apache.wicket.markup.html.form.PasswordTextField) PageBase(com.evolveum.midpoint.gui.api.page.PageBase) AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) AjaxRequestAttributes(org.apache.wicket.ajax.attributes.AjaxRequestAttributes) PageUserSelfProfile(com.evolveum.midpoint.web.page.self.PageUserSelfProfile) ComponentTag(org.apache.wicket.markup.ComponentTag) LoadableModel(com.evolveum.midpoint.gui.api.model.LoadableModel) ProtectedStringType(com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType) PageOrgSelfProfile(com.evolveum.midpoint.web.page.self.PageOrgSelfProfile)

Aggregations

ThrottlingSettings (org.apache.wicket.ajax.attributes.ThrottlingSettings)3 VisibleEnableBehaviour (com.evolveum.midpoint.web.component.util.VisibleEnableBehaviour)2 ArrayList (java.util.ArrayList)2 AjaxChannel (org.apache.wicket.ajax.AjaxChannel)2 AjaxRequestTarget (org.apache.wicket.ajax.AjaxRequestTarget)2 AjaxRequestAttributes (org.apache.wicket.ajax.attributes.AjaxRequestAttributes)2 AjaxFormComponentUpdatingBehavior (org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior)2 AjaxLink (org.apache.wicket.ajax.markup.html.AjaxLink)2 WebMarkupContainer (org.apache.wicket.markup.html.WebMarkupContainer)2 Label (org.apache.wicket.markup.html.basic.Label)2 IModel (org.apache.wicket.model.IModel)2 LoadableModel (com.evolveum.midpoint.gui.api.model.LoadableModel)1 PageBase (com.evolveum.midpoint.gui.api.page.PageBase)1 CompositedIconBuilder (com.evolveum.midpoint.gui.impl.component.icon.CompositedIconBuilder)1 StringLimitationResult (com.evolveum.midpoint.model.api.validator.StringLimitationResult)1 AjaxButton (com.evolveum.midpoint.web.component.AjaxButton)1 AjaxCompositedIconSubmitButton (com.evolveum.midpoint.web.component.AjaxCompositedIconSubmitButton)1 MidpointForm (com.evolveum.midpoint.web.component.form.MidpointForm)1 TextPanel (com.evolveum.midpoint.web.component.input.TextPanel)1 InlineMenuItem (com.evolveum.midpoint.web.component.menu.cog.InlineMenuItem)1