<commit_before>/*
 * Copyright (c) 2016, Mirante and/or its affiliates. All rights reserved.
 * Mirante PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package br.net.mirante.singular.form.wicket.mapper.selection;

import br.net.mirante.singular.commons.lambda.IConsumer;
import br.net.mirante.singular.form.mform.*;
import br.net.mirante.singular.form.mform.basic.ui.AtrBasic;
import br.net.mirante.singular.form.mform.basic.view.SViewSelectionBySearchModal;
import br.net.mirante.singular.form.mform.options.SOptionsConfig;
import br.net.mirante.singular.form.mform.util.transformer.Value;
import br.net.mirante.singular.form.wicket.behavior.AjaxUpdateInputBehavior;
import br.net.mirante.singular.form.wicket.component.BFModalWindow;
import br.net.mirante.singular.form.wicket.model.IMInstanciaAwareModel;
import br.net.mirante.singular.util.wicket.ajax.ActionAjaxButton;
import br.net.mirante.singular.util.wicket.bootstrap.layout.BSContainer;
import br.net.mirante.singular.util.wicket.bootstrap.layout.BSControls;
import br.net.mirante.singular.util.wicket.bootstrap.layout.BSGrid;
import br.net.mirante.singular.util.wicket.datatable.BSDataTable;
import br.net.mirante.singular.util.wicket.datatable.BSDataTableBuilder;
import br.net.mirante.singular.util.wicket.datatable.column.BSActionColumn;
import br.net.mirante.singular.util.wicket.modal.BSModalBorder;
import br.net.mirante.singular.util.wicket.resource.Icone;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.behavior.Behavior;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.Response;

import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static br.net.mirante.singular.util.wicket.util.WicketUtils.$b;
import static br.net.mirante.singular.util.wicket.util.WicketUtils.$m;

/**
 * This component is used to represent a selection placed in a modal search.
 */
class SelectInputModalContainer extends BSContainer {

    private BSControls                    formGroup;
    private BSContainer                   modalContainer;
    private IModel<? extends SInstance>   model;
    private SViewSelectionBySearchModal   view;
    private TextField<?>                  valueInput;
    private IMInstanciaAwareModel<String> valueInputModel;

    private IConsumer<IModel<?>> clearModel = m -> m.setObject(null);

    SelectInputModalContainer(String id,
                              BSControls formGroup,
                              BSContainer modalContainer,
                              IModel<? extends SInstance> model,
                              SViewSelectionBySearchModal view,
                              IMInstanciaAwareModel<String> valueInputModel) {
        super(id);
        this.formGroup = formGroup;
        this.modalContainer = modalContainer;
        this.model = model;
        this.view = view;
        this.valueInputModel = valueInputModel;
    }

    public Component build() {

        createValueInput();

        this.appendTag("input", true, "class=\"form-control\"", valueInput);
        this.appendTag("span", true, "class=\"input-group-btn\"", buildSearchButton(model.getObject().getName() + "_modal", Model.of("")));

        /* input-group-sm precisa ser adicionado aqui por que o form-group atual adiciona o form-group-sm */
        formGroup.appendTag("div", true, "class=\"input-group input-group-sm\"", this);

        return valueInput;
    }

    private void createValueInput() {
        valueInput = new TextField<>(model.getObject().getName() + "selection", valueInputModel);
        valueInput.setOutputMarkupId(true);
        valueInput.add($b.attr("readonly", "readonly"));
    }

    private Panel buildSearchButton(String id, IModel<String> filterModel) {

        final BSContainer   panel       = new BSContainer(id);
        final BFModalWindow searchModal = buildModal(id + "__modal", filterModel);

        panel.appendTag("a", true, "class='btn default'", new AjaxLink("search_link") {
            @Override
            public void onClick(AjaxRequestTarget target) {
                searchModal.show(target);
                target.appendJavaScript(getConfigureBackdropScript());
            }

            private String getConfigureBackdropScript() {
                String js = "";
                js += " (function (zindex){ ";
                js += "     $('.modal-backdrop').each(function(index) { ";
                js += "         var zIndex = $(this).css('z-index'); ";
                js += "         $(this).css('z-index', zindex-1+index); ";
                js += "     }); ";
                js += "     $('.modal').each(function(index) { ";
                js += "         var zIndex = $(this).css('z-index'); ";
                js += "         $(this).css('z-index', zindex+index); ";
                js += "     }); ";
                js += " })(10050); ";
                return js;
            }

            @Override
            public void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
                Response response = getResponse();
                response.write("\n");
                response.write("<i class=\"fa fa-search\"></i>");
                super.onComponentTagBody(markupStream, openTag);<commit_msg>[Showcase] Correção temporaria para o problema do searchmodal.<commit_after>/*
 * Copyright (c) 2016, Mirante and/or its affiliates. All rights reserved.
 * Mirante PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package br.net.mirante.singular.form.wicket.mapper.selection;

import br.net.mirante.singular.commons.lambda.IConsumer;
import br.net.mirante.singular.form.mform.*;
import br.net.mirante.singular.form.mform.basic.ui.AtrBasic;
import br.net.mirante.singular.form.mform.basic.view.SViewSelectionBySearchModal;
import br.net.mirante.singular.form.mform.options.SOptionsConfig;
import br.net.mirante.singular.form.mform.util.transformer.Value;
import br.net.mirante.singular.form.wicket.behavior.AjaxUpdateInputBehavior;
import br.net.mirante.singular.form.wicket.component.BFModalWindow;
import br.net.mirante.singular.form.wicket.model.IMInstanciaAwareModel;
import br.net.mirante.singular.util.wicket.ajax.ActionAjaxButton;
import br.net.mirante.singular.util.wicket.bootstrap.layout.BSContainer;
import br.net.mirante.singular.util.wicket.bootstrap.layout.BSControls;
import br.net.mirante.singular.util.wicket.bootstrap.layout.BSGrid;
import br.net.mirante.singular.util.wicket.datatable.BSDataTable;
import br.net.mirante.singular.util.wicket.datatable.BSDataTableBuilder;
import br.net.mirante.singular.util.wicket.datatable.column.BSActionColumn;
import br.net.mirante.singular.util.wicket.modal.BSModalBorder;
import br.net.mirante.singular.util.wicket.resource.Icone;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.behavior.Behavior;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.Response;

import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static br.net.mirante.singular.util.wicket.util.WicketUtils.$b;
import static br.net.mirante.singular.util.wicket.util.WicketUtils.$m;

/**
 * This component is used to represent a selection placed in a modal search.
 */
class SelectInputModalContainer extends BSContainer {

    private BSControls                    formGroup;
    private BSContainer                   modalContainer;
    private IModel<? extends SInstance>   model;
    private SViewSelectionBySearchModal   view;
    private TextField<?>                  valueInput;
    private IMInstanciaAwareModel<String> valueInputModel;

    private IConsumer<IModel<?>> clearModel = m -> m.setObject(null);

    SelectInputModalContainer(String id,
                              BSControls formGroup,
                              BSContainer modalContainer,
                              IModel<? extends SInstance> model,
                              SViewSelectionBySearchModal view,
                              IMInstanciaAwareModel<String> valueInputModel) {
        super(id);
        this.formGroup = formGroup;
        this.modalContainer = modalContainer;
        this.model = model;
        this.view = view;
        this.valueInputModel = valueInputModel;
    }

    public Component build() {

        createValueInput();

        this.appendTag("input", true, "class=\"form-control\"", valueInput);
        this.appendTag("span", true, "class=\"input-group-btn\"", buildSearchButton(model.getObject().getName() + "_modal", Model.of("")));

        /* input-group-sm precisa ser adicionado aqui por que o form-group atual adiciona o form-group-sm */
        formGroup.appendTag("div", true, "class=\"input-group input-group-sm\"", this);

        return valueInput;
    }

    private void createValueInput() {
        valueInput = new TextField<>(model.getObject().getName() + "selection", valueInputModel);
        valueInput.setOutputMarkupId(true);
        valueInput.add($b.attr("readonly", "readonly"));
    }

    private Panel buildSearchButton(String id, IModel<String> filterModel) {

        final BSContainer   panel       = new BSContainer(id);
        final BFModalWindow searchModal = buildModal(id + "__modal", filterModel);

        panel.appendTag("a", true, "class='btn default'", new AjaxLink("search_link") {
            @Override
            public void onClick(AjaxRequestTarget target) {
                target.add(modalContainer);
                searchModal.getModalBorder().setVisible(true);
//                searchModal.show(target);
                target.appendJavaScript(getConfigureBackdropScript());
            }

            private String getConfigureBackdropScript() {
                String js = "";
                js += " (function (zindex){ ";
                js += "     $('.modal-backdrop').each(function(index) { ";
                js += "         $(this).css('z-index', zindex-1+index); ";
                js += "     }); ";
                js += "     $('.modal').each(function(index) { ";
                js += "         $(this).css('z-index', zindex+index); ";
                js += "     }); ";
                js += " })(10050); ";
                return js;
            }

            @Override
            public void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
                Response response = getResponse();
                response.write("\n");
                response.write("<i class=\"fa fa-search\"></i>");
                super.onComponentTagBody(markupStream, openTag);
{'fraction_non_alphanumeric': 0.12130926269613633, 'fraction_numerical': 0.0016871941960519656, 'mean_word_length': 2.8741830065359477, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3529374', 'n_tokens_mistral': 3494, 'n_tokens_neox': 3365, 'n_words': 582}