Commit c2cd8802 authored by igoshin's avatar igoshin Committed by Matija Obreza
Browse files

#12579 enabled ajax call for additional filters

parent 65b7522c
......@@ -16,6 +16,7 @@
package org.genesys2.server.servlet.controller;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
......@@ -38,6 +39,7 @@ import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
......@@ -275,7 +277,7 @@ public class ExplorerController extends BaseController {
@RequestMapping("/explore")
public String viewFiltered(ModelMap model, @RequestParam(value = "page", required = false, defaultValue = "1") int page,
@RequestParam(value = "filter", required = true, defaultValue = "{}") String jsonFilter,
@RequestParam(value = "pick", required = false) String jsonPick) {
@RequestParam(value = "pick", required = false,defaultValue = "[]") String jsonPick) {
String[] selectedFilters =new String[0];
......@@ -316,7 +318,7 @@ public class ExplorerController extends BaseController {
model.addAttribute("filters",filters);
model.addAttribute("pick",pick);
model.addAttribute("jsonPick", jsonTree.toString());
jsonPick= jsonTree.toString();
_logger.info("Clean jsonPick: " + jsonTree);
} catch (IOException e) {
_logger.error("Invalid JSON for pick", e);
......@@ -330,7 +332,6 @@ public class ExplorerController extends BaseController {
_logger.debug(jsonTree.toString());
if (model.containsAttribute("crop")) {
// jsonTree.remove("crop");
model.addAttribute("jsonFilter", jsonTree.toString());
// Replace any other crop filters...
......@@ -350,20 +351,71 @@ public class ExplorerController extends BaseController {
_logger.info("Got: " + accessions);
List<GenesysFilterService.GenesysFilter> additionalFilters = filterService.selectFilters(crop,selectedFilters);
List<GenesysFilterService.GenesysFilter> availableFilters =filterService.listAvailableFilters();
List<GenesysFilterService.GenesysFilter> allFilters=new ArrayList<>();
allFilters.addAll(availableFilters);
allFilters.addAll(additionalFilters);
model.addAttribute("crops", cropService.list(getLocale()));
model.addAttribute("pagedData", accessions);
model.addAttribute("jsonPick", jsonPick);
model.addAttribute("pagedData", accessions);
model.addAttribute("availableFilters", availableFilters);
model.addAttribute("additionalFilters", additionalFilters);
model.addAttribute("allFilters", allFilters);
return "/accession/explore_new";
}
@RequestMapping(value = "/additional-filter", method = RequestMethod.GET)
public String getAdditionalFilters(ModelMap model, @RequestParam(value = "filter", required = true, defaultValue = "{}") String jsonFilter,
@RequestParam(value = "shortName", required = true) String shortName,
@RequestParam(value = "pick", required = true) String filterPick) throws IOException {
Crop crop = cropService.getCrop(shortName);
if (crop == null) {
throw new ResourceNotFoundException("No crop " + shortName);
}
String[] selectedFilters = new String[0];
Map<String, List<String>> filters = mapper.readValue(jsonFilter, Map.class);
Map<String, String> pick = new HashMap<>();
for (String key : filters.keySet()) {
pick.put(key, key);
}
if (StringUtils.isNotBlank(filterPick)) {
selectedFilters = mapper.readValue(filterPick, selectedFilters.getClass());
}
List<GenesysFilterService.GenesysFilter> additionalFilters = filterService.selectFilters(crop, selectedFilters);
model.addAttribute("filters", filters);
model.addAttribute("pick", pick);
model.addAttribute("additionalFilters", additionalFilters);
return "/accession/additional-filter";
}
@RequestMapping(value = "/modal", method = RequestMethod.GET)
public String getModelWindow(ModelMap model,@RequestParam(value = "shortName", required = true) String shortName,
@RequestParam(value = "pick", required = false,defaultValue = "[]") String jsonPick) {
Crop crop = cropService.getCrop(shortName);
if (crop != null) {
List<ParameterCategory> categories = traitService.listCategories();
Map<ParameterCategory, List<Parameter>> descriptors = traitService.mapTraits(crop, categories);
Map<Long, List<Method>> methods = traitService.mapMethods(crop);
model.addAttribute("crop", crop);
model.addAttribute("categories", categories);
model.addAttribute("descriptors", descriptors);
model.addAttribute("methods", methods);
}
model.addAttribute("jsonPick", jsonPick);
return "/accession/modal";
}
private String[] convertToPickString(List<ParameterCategory> categories, Map<ParameterCategory, List<Parameter>> descriptors, Map<Long, List<Method>> methods) {
List<String> s = new ArrayList<>();
......
<%@ page contentType="charset=UTF-8" pageEncoding="UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<c:forEach items="${additionalFilters}" var="filter">
<c:set var="normalizedKey" value="${fn:replace(filter.key,':', '_')}"/>
<c:set var="filterKey" value="${filter.key}"/>
<div class="clearfix filter-block" style="display:${pick[filterKey] eq filterKey ? '' : 'none'}"
id="${normalizedKey}_filter" key="${normalizedKey}">
<div class="col-lg-3">
<c:if test="${not filter.core}">
<a href="<c:url value="/descriptors/${filter.key}" />"><c:out value="${filter.title}" /></a>
</c:if>
<c:if test="${filter.core}">
<spring:message code="filter.${filter.name}" />
</c:if>
</div>
<div class="col-lg-5 filter-new" style="display:none">
<c:choose>
<c:when test="${filter.filterType=='LIST'}">
<div class="">
<c:forEach items="${filter.options}" var="option">
<div>
<label>
<input class="filter-list" id="${option.value}_input" ${fn:contains(filters[filter.key], option.value)?'checked':''} norm-key="${normalizedKey}" i-key="${normalizedKey}${option.value}" type="checkbox" value="${option.value}" />
<spring:message code="${option.name}"/>
</label>
</div>
</c:forEach>
</div>
<div style="margin-top: 3px">
<button class="btn btn-primary apply"><spring:message code="filter.apply"/></button>
</div>
</c:when>
<c:when test="${filter.filterType=='I18NLIST'}">
<div class="">
<c:forEach items="${filter.options}" var="option">
<div>
<label>
<input class="filter-list" id="${option.value}_input" ${fn:contains(filters[filter.key], option.value)?'checked':''} norm-key="${normalizedKey}" i-key="${normalizedKey}${option.value}" type="checkbox" value="${option.value}" />
<spring:message code="${option.name}"/>
</label>
</div>
</c:forEach>
<div style="margin-top: 3px">
<button class="btn btn-primary apply"><spring:message code="filter.apply"/></button>
</div>
</div>
</c:when>
<c:when test="${filter.filterType=='AUTOCOMPLETE'}">
<div class="ui-front">
<div class="form-group input-group">
<span class="input-group-btn">
<input id="${normalizedKey}_input" class="span2 form-control autocomplete-filter" x-source="${filter.autocompleteUrl}" type="text"/>
<button class="btn notimportant filter-auto" norm-key="${normalizedKey}" >+</button>
</span>
</div>
<div style="margin-top: 3px">
<button class="btn btn-primary apply"><spring:message code="filter.apply"/></button>
</div>
</div>
</c:when>
<c:when test="${filter.dataType=='NUMERIC'}">
<div class="form-group input-group">
<span class="input-group-btn">
<input id="${normalizedKey}_input_1" class="span5 form-control" type="text"/>
<input id="${normalizedKey}_input_2" class="span5 form-control" type="text"/>
<button class="btn notimportant filter-range" norm-key="${normalizedKey}">+</button>
</span>
</div>
<div style="margin-top: 3px">
<button class="btn btn-primary apply"><spring:message code="filter.apply"/></button>
</div>
</c:when>
<c:when test="${filter.dataType=='BOOLEAN'}">
<div class="">
<div><label><input type="checkbox" ${fn:contains(filters[filter.key], 'true')?'checked':''} class="filter-bool" i-key="${normalizedKey}true" id="${normalizedKey}" value="true"/><spring:message code="boolean.true"/></label></div>
<div><label><input type="checkbox" ${fn:contains(filters[filter.key], 'false')?'checked':''} class="filter-bool" i-key="${normalizedKey}false" id="${normalizedKey}" value="false"/><spring:message code="boolean.false"/></label></div>
<div><label><input type="checkbox" ${fn:contains(filters[filter.key], 'null')?'checked':''} class="filter-bool" i-key="${normalizedKey}null" id="${normalizedKey}" value="null"/><spring:message code="boolean.null"/></label></div>
</div>
<div style="margin-top: 3px">
<button class="btn btn-primary apply"><spring:message code="filter.apply"/></button>
</div>
</c:when>
<c:otherwise>
<div class="ui-front">
<div class="form-group input-group">
<span class="input-group-btn">
<input class="span2 form-control" id="${normalizedKey}_input" type="text" />
<button class="btn notimportant filter-auto" norm-key="${normalizedKey}">+</button>
</span>
</div>
<div style="margin-top: 3px">
<button class="btn btn-primary apply"><spring:message code="filter.apply"/></button>
</div>
</div>
</c:otherwise>
</c:choose>
</div>
<div>
<a href="#" style="float: right" class="close-fil glyphicon glyphicon-remove"></a>
<a href="#" style="float: right" class="edit-fil glyphicon glyphicon-pencil"></a>
</div>
<div class="col-lg-4 filter-values" id="${normalizedKey}_value">
<c:forEach items="${filters[filter.key]}" var="value">
<c:set var="string" value="${value}"/>
<c:if test="${fn:contains(value, 'range')}">
<c:set var="string" value="${fn:replace(value,'{range=[','Between ')}"/>
<c:set var="string" value="${fn:replace(string,',',' and ')}"/>
<c:set var="string" value="${fn:replace(string,']}','')}"/>
</c:if>
<c:if test="${fn:contains(value, 'min')}">
<c:set var="string" value="${fn:replace(value,'{min=','More than ')}"/>
<c:set var="string" value="${fn:replace(string,'}','')}"/>
</c:if>
<c:if test="${fn:contains(value, 'max')}">
<c:set var="string" value="${fn:replace(value,'{max=','Less than ')}"/>
<c:set var="string" value="${fn:replace(string,'}','')}"/>
</c:if>
<div class="filtval complex" x-key="${normalizedKey}${value}" id="${normalizedKey}">${string}</div>
</c:forEach>
</div>
</div>
</c:forEach>
......@@ -63,18 +63,16 @@
</li>
<c:if test="${crop != null}">
<li class="dropdown form-horizontal" id="menu2">
<a class="" data-toggle="modal" data-target="#myModal">
<spring:message code="filter.additional"/>
<b class="glyphicon-plus"></b>
</a>
</li>
</c:if>
<li class="dropdown form-horizontal" id="menu2" style="display:${fn:contains(jsonPick,'crop')?'':'none'}">
<a class="" data-toggle="modal" data-target="#myModal">
<spring:message code="filter.additional"/>
<b class="glyphicon-plus"></b>
</a>
</li>
</ul>
<c:forEach items="${allFilters}" var="filter">
<c:forEach items="${availableFilters}" var="filter">
<c:set var="normalizedKey" value="${fn:replace(filter.key,':', '_')}"/>
<c:set var="filterKey" value="${filter.key}"/>
......@@ -99,7 +97,7 @@
<c:forEach items="${filter.options}" var="option">
<div>
<label>
<input id="${option.value}_input" ${fn:contains(filters[filter.key], option.value)?'checked':''} i-key="${normalizedKey}${option.value}" type="checkbox" value="${option.value}" onclick="filterList('${normalizedKey}','${option.value}')"/>
<input class="filter-list" id="${option.value}_input" ${fn:contains(filters[filter.key], option.value)?'checked':''} norm-key="${normalizedKey}" i-key="${normalizedKey}${option.value}" type="checkbox" value="${option.value}" />
<spring:message code="${option.name}"/>
</label>
</div>
......@@ -114,7 +112,7 @@
<c:forEach items="${filter.options}" var="option">
<div>
<label>
<input id="${option.value}_input" ${fn:contains(filters[filter.key], option.value)?'checked':''} i-key="${normalizedKey}${option.value}" type="checkbox" value="${option.value}" onclick="filterList('${normalizedKey}','${option.value}')"/>
<input class="filter-list" id="${option.value}_input" ${fn:contains(filters[filter.key], option.value)?'checked':''} norm-key="${normalizedKey}" i-key="${normalizedKey}${option.value}" type="checkbox" value="${option.value}" />
<spring:message code="${option.name}"/>
</label>
</div>
......@@ -130,7 +128,7 @@
<div class="form-group input-group">
<span class="input-group-btn">
<input id="${normalizedKey}_input" class="span2 form-control autocomplete-filter" x-source="${filter.autocompleteUrl}" type="text"/>
<button class="btn notimportant" onclick="filterAuto('${normalizedKey}')">+</button>
<button class="btn notimportant filter-auto" norm-key="${normalizedKey}" >+</button>
</span>
</div>
<div style="margin-top: 3px">
......@@ -144,7 +142,7 @@
<span class="input-group-btn">
<input id="${normalizedKey}_input_1" class="span5 form-control" type="text"/>
<input id="${normalizedKey}_input_2" class="span5 form-control" type="text"/>
<button class="btn notimportant" onclick="filterRange('${normalizedKey}')">+</button>
<button class="btn notimportant filter-range" norm-key="${normalizedKey}">+</button>
</span>
</div>
<div style="margin-top: 3px">
......@@ -153,9 +151,9 @@
</c:when>
<c:when test="${filter.dataType=='BOOLEAN'}">
<div class="">
<div><label><input type="checkbox" ${fn:contains(filters[filter.key], 'true')?'checked':''} class="bool-data" i-key="${normalizedKey}true" id="${normalizedKey}" value="true"><spring:message code="boolean.true"/></label></div>
<div><label><input type="checkbox" ${fn:contains(filters[filter.key], 'false')?'checked':''} class="bool-data" i-key="${normalizedKey}false" id="${normalizedKey}" value="false"><spring:message code="boolean.false"/></label></div>
<div><label><input type="checkbox" ${fn:contains(filters[filter.key], 'null')?'checked':''} class="bool-data" i-key="${normalizedKey}null" id="${normalizedKey}" value="null"><spring:message code="boolean.null"/></label></div>
<div><label><input type="checkbox" ${fn:contains(filters[filter.key], 'true')?'checked':''} class="filter-bool" i-key="${normalizedKey}true" id="${normalizedKey}" value="true"><spring:message code="boolean.true"/></label></div>
<div><label><input type="checkbox" ${fn:contains(filters[filter.key], 'false')?'checked':''} class="filter-bool" i-key="${normalizedKey}false" id="${normalizedKey}" value="false"><spring:message code="boolean.false"/></label></div>
<div><label><input type="checkbox" ${fn:contains(filters[filter.key], 'null')?'checked':''} class="filter-bool" i-key="${normalizedKey}null" id="${normalizedKey}" value="null"><spring:message code="boolean.null"/></label></div>
</div>
<div style="margin-top: 3px">
<button class="btn btn-primary apply"><spring:message code="filter.apply"/></button>
......@@ -177,15 +175,17 @@
</div>
</c:when>
<c:otherwise>
<div class="ui-front">
<div class="form-group input-group">
<span class="input-group-btn">
<input class="span2 form-control" id="${normalizedKey}_input" type="text"/>
<button class="btn notimportant" onclick="filterAuto('${normalizedKey}')">+</button>
<button class="btn notimportant filter-auto" norm-key="${normalizedKey}">+</button>
</span>
</div>
<div style="margin-top: 3px">
<button class="btn btn-primary apply"><spring:message code="filter.apply"/></button>
</div>
</div>
</c:otherwise>
</c:choose>
</div>
......@@ -224,33 +224,34 @@
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="myModalLabel"><spring:message code="filter.additional"/></h4>
</div>
<div class="modal-body" style="overflow: auto;height: 200px">
<c:forEach items="${categories}" var="category">
<c:if test="${descriptors[category].size() gt 0}">
<h2><c:out value="${crop.getName(pageContext.request.locale)}:" /> <c:out value="${category.getName(pageContext.request.locale)}" /></h2>
<c:forEach items="${descriptors[category]}" var="descriptor">
<c:if test="${methods[descriptor.id].size() gt 0}">
<div class="row filter-block">
<div class="col-lg-3">
<div dir="ltr"><c:out value="${descriptor.title}" /></div>
</div>
<div class="col-lg-9">
<c:forEach items="${methods[descriptor.id]}" var="method">
<div><label><input id="gm_${method.id}_id" type="checkbox" style="margin-right: 1em;" onclick="enableFilter('gm_${method.id}')" value="gm:${method.id}" /><span dir="ltr"><c:out value="${method.method}" /></span></label></div>
</c:forEach>
</div>
</div>
</c:if>
</c:forEach>
</c:if>
</c:forEach>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal"><spring:message code="filter.apply"/></button>
</div>
<%--<div class="modal-header">--%>
<%--<h4 class="modal-title" id="myModalLabel"><spring:message code="filter.additional"/></h4>--%>
<%--</div>--%>
<%--<div class="modal-body" style="overflow: auto;height: 200px">--%>
<%--<c:forEach items="${categories}" var="category">--%>
<%--<c:if test="${descriptors[category].size() gt 0}">--%>
<%--<h2><c:out value="${crop.getName(pageContext.request.locale)}:" /> <c:out value="${category.getName(pageContext.request.locale)}" /></h2>--%>
<%--<c:forEach items="${descriptors[category]}" var="descriptor">--%>
<%--<c:if test="${methods[descriptor.id].size() gt 0}">--%>
<%--<div class="row filter-block">--%>
<%--<div class="col-lg-3">--%>
<%--<div dir="ltr"><c:out value="${descriptor.title}" /></div>--%>
<%--</div>--%>
<%--<div class="col-lg-9">--%>
<%--<c:forEach items="${methods[descriptor.id]}" var="method">--%>
<%--<c:set value="gm:${method.id}" var="gm_value"/>--%>
<%--<div><label><input class="additional" ${fn:contains(jsonPick, gm_value)?'checked':''} id="gm_${method.id}_id" type="checkbox" style="margin-right: 1em;" value="gm:${method.id}" /><span dir="ltr"><c:out value="${method.method}" /></span></label></div>--%>
<%--</c:forEach>--%>
<%--</div>--%>
<%--</div>--%>
<%--</c:if>--%>
<%--</c:forEach>--%>
<%--</c:if>--%>
<%--</c:forEach>--%>
<%--</div>--%>
<%--<div class="modal-footer">--%>
<%--<button type="button" class="btn btn-primary" data-dismiss="modal"><spring:message code="filter.apply"/></button>--%>
<%--</div>--%>
</div>
</div>
</div>
......@@ -293,7 +294,7 @@
<script type="text/javascript">
var jsonData = ${fn:replace(jsonFilter,'_',':')};
var filterPick = [];
var filterPick = ${jsonPick};
var page=${pagedData.number};
......@@ -305,7 +306,9 @@
function submitJson() {
for (var key in jsonData) {
filterPick.push(key);
if(!filterPick.indexOf(key)>-1){
filterPick.push(key);
}
}
var filter = JSON.stringify(jsonData);
......@@ -351,12 +354,12 @@
}
}
function appendHtml(key, value) {
function appendHtml(key, value,element) {
var valueId = "#" + key + "_value";
var summaryViewId="#"+key+"_summary";
var div = "<div class='filtval complex' x-key='" + key + value + "' id='" + key + "'>" + value + "</div>"
$(valueId).append(div);
$(summaryViewId).append(div);
var div = "<div class='filtval complex' x-key='" + key + value + "' id='" + key + "'>" + value + "</div>";
$(element).parent().parent().parent().parent().parent().find(valueId).append(div);
$(element).parent().parent().parent().parent().parent().find(summaryViewId).append(div);
}
function collectData(key,value) {
......@@ -390,24 +393,28 @@
}
if (array.length == 0) {
filterPick.splice(filterPick.indexOf(key),1);
delete jsonData[key];
}
}
}
function filterAuto(key) {
function filterAuto(element) {
var key=$(element).attr("norm-key");
var inputId = "#" + key + "_input";
var value = $(inputId).val();
var value = $(element).parent().find(inputId).val();
if (existInJson(value, key)) {
appendHtml(key, value);
appendHtml(key, value,element);
jsonData[key.replace("_",":")] = collectData(key,value);
$(inputId).val('');
}
}
function filterRange(key) {
function filterRange(element) {
var key= $(element).attr("norm-key");
var inputId1 = "#" + key + "_input_1";
var inputId2 = "#" + key + "_input_2";
......@@ -454,7 +461,7 @@
value = "Between " + inputValue1 + " and " + inputValue2;
}
appendHtml(key, value);
appendHtml(key, value,element);
jsonData[key.replace("_",":")] = jsonValue;
$(inputId1).val('');
......@@ -462,16 +469,18 @@
}
}
function filterList(key, option) {
function filterList(element) {
var key = $(element).attr("norm-key");
var option = $(element).val();
var inputId = "#" + option + "_input";
var value = $(inputId).val();
var value = $(element).parent().find(inputId).val();
var checked = $(inputId).is(":checked");
var checked = $(element).parent().find(inputId).is(":checked");
if (checked && existInJson(value, key)) {
jsonData[key.replace("_",":")] = collectData(key,value);
appendHtml(key, value);
jsonData[key.replace("_", ":")] = collectData(key, value);
appendHtml(key, value, element);
} else {
var xkey = key + value;
......@@ -490,25 +499,103 @@
if(value!=''){
appendHtml(key, value);
jsonData[key] = collectData(key, value);
$("#menu2").show();
}else{
$("#menu2").hide();
}
}
$( document ).ready(function() {
$.ajax("/additional-filter", {
type: 'GET',
data: "filter=" + JSON.stringify(jsonData) + "&shortName=" + jsonData.crop+"&pick="+JSON.stringify(filterPick),
success: function (data) {
$("#allfilters").append(data);
},
error: function (error) {
console.log(error);
}
});
$("#menu2").on("click", function (event) {
$.ajax("/modal", {
type: 'GET',
data: "shortName=" + jsonData.crop + "&pick=" + JSON.stringify(filterPick),
success: function (data) {
$(".modal-content").empty();
$(".modal-content").append(data);
},
error: function (error) {
console.log(error);
}
});
});
$("body").on("click",".additional", function (event) {
var id = $(this).attr("id");
var filterId = "#" + id.replace("_id", "_filter");
var filter = id.replace("gm_", "gm:").replace("_id", "");
var pick=[];
if ($(this).is(":checked")&&!filterPick.indexOf(filter)>-1) {
jsonData[filter] = "";
pick.push(filter);
if(!filterPick.indexOf(filter)>-1) {
filterPick.push(filter);
}
$.ajax("/additional-filter", {
type: 'GET',
data: "filter=" + JSON.stringify(jsonData) + "&shortName=" + jsonData.crop + "&pick=" + JSON.stringify(pick),
success: function (data) {
delete jsonData[filter];
filterPick.splice(filterPick.indexOf(filter),1);
$("#allfilters").append(data);