filter.jsp 8.45 KB
Newer Older
Matija Obreza's avatar
Matija Obreza committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<!DOCTYPE html>

<%@include file="/WEB-INF/jsp/init.jsp"%>

<html>
<head>
<title><spring:message code="filters.page.title" /></title>
</head>
<body>
	<h1><spring:message code="filters.view" /></h1>

<div id="allfilters">
	<c:if test="${crop ne null}">
	<div class="filter-block row">
		<div class="col-lg-3">
			<spring:message code="filter.crop" />
		</div>
		<div class="col-lg-9">
			<c:out value="${crop.getName(pageContext.response.locale)}" />
		</div>
	</div>
	</c:if>
	
<c:forEach items="${selectedFilters}" var="filter">

	<div class="clearfix filter-block" x-filtername="${filter.key}">
		<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">
			<c:choose>
37
38
39
40
41
42
43
44
45
46
47
48
				<c:when test="${filter.filterType=='LIST'}">
					<div class="">
						<c:forEach items="${filter.options}" var="option">
						<div><label><input type="checkbox" value="${option.value}" /> <c:out value="${option.name}" /></label> <span class="pull-right"><c:out value="${option.count}" /></span></div>
						</c:forEach>
					</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 class="span2 form-control autocomplete-filter" x-source="${filter.autocompleteUrl}" type="text" /><button class="btn notimportant">+</button></span></div>	
					</div>
				</c:when>
Matija Obreza's avatar
Matija Obreza committed
49
50
51
52
				<c:when test="${filter.dataType=='NUMERIC'}">
					<div class="form-group input-group"><span class="input-group-btn"><input class="span1 form-control" type="text" /><input class="span1 form-control" type="text" /><button class="btn notimportant">+</button></span></div>	
				</c:when>
 				<c:when test="${filter.dataType=='BOOLEAN'}">
53
54
55
56
57
					<div class="">
						<div><label><input type="checkbox" value="true"> <spring:message code="boolean.true" /></label></div>
						<div><label><input type="checkbox" value="false"> <spring:message code="boolean.false" /></label></div>
						<div><label><input type="checkbox" value="null"> <spring:message code="boolean.null" /></label></div>
					</div>
Matija Obreza's avatar
Matija Obreza committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
				</c:when>
				<c:otherwise>
					<div class="form-group input-group"><span class="input-group-btn"><input class="span2 form-control" type="text" /><button class="btn notimportant">+</button></span></div>	
				</c:otherwise>
			</c:choose>
		</div>	
		<div class="col-lg-4 filter-values" id="filter-${filter.name.replace(':', '_')}"></div>
	</div>
</c:forEach>

<div class="row">
	<div class="col-lg-offset-3 col-lg-9">
		<a id="filtersHref" href=""><button class="btn btn-green pull-left">View!</button></a>
		<c:url var="pickUrl" value="/explore/pick">
			<c:param name="pick">${pick}</c:param>
			<c:param name="crop"><c:out value="${crop.shortName}" /></c:param>
		</c:url>
		<a href="${pickUrl}"><button class="btn btn-green pull-left">Change filters</button></a>
	</div>
</div>

</div>

<div id="filtersJson">${jsonObject}</div>


<content tag="javascript">	
85
<script type="text/javascript" src="<c:url value="/html/js/jquery-ui.min.js" />"></script>
Matija Obreza's avatar
Matija Obreza committed
86
87
88
89
90
91
92
<script type="text/javascript">
jQuery(document).ready(function() {
//	alert('${jsonString}');
	var pick=[];
	var filters={};
	try {
		filters=$.parseJSON('${jsonString}');
93
	} catch (e) {console.error(e);}
Matija Obreza's avatar
Matija Obreza committed
94
95
	try {
		pick=$.parseJSON('${pick}');
96
	} catch (e) {console.error(e);}
Matija Obreza's avatar
Matija Obreza committed
97
98
99
100
101
102
103
104
105
106
//	debugger;
	console.log("Filters: " +JSON.stringify(filters));
	
	var FF = {
		updateValues: function(id, filterName, values) {
			var c=$(id);
			//c.closest(".clearfix").attr("x-filtername", filterName)
			//alert(c.data("filtername"));
			c.empty();
			if (values==null) return;
107
108
109
110
111
112
			var checkboxes=c.prev().find("input[type=checkbox]")
			if (checkboxes.length>0) {
				values.forEach(function(i, idx) {
					checkboxes.each(function(){if (this.value==''+i) this.checked=true;});
				});
			}
Matija Obreza's avatar
Matija Obreza committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
			values.forEach(function(i, idx) {
				console.log("Adding: " + i);
				//debugger;
				if ($.isPlainObject(i)) {
					// ranges & ops
					var x="<div class='filtval complex'>";
					if (i["range"]!=null) {
						x+="Between " + i["range"][0] + " and " + i["range"][1];
					} else if (i["min"]!=null) {
						x+="More than " + i["min"];
					} else if (i["max"]!=null) {
						x+="Less than " + i["max"];
					}
					x+="</div>";
				} else {
					var x="<div class='filtval discrete'>" + i + "</div>";
				}
				c.append(x);
			});
		},
		
		refreshJson: function(newFilter) {
			$("#filtersJson").html(JSON.stringify(newFilter));
136
			$("#filtersHref").attr("href", "<c:url value="/explore" />?<c:if test="${crop ne null}">crop=${crop.shortName}&</c:if>filter="+JSON.stringify(newFilter)+"&pick="+JSON.stringify(pick));
Matija Obreza's avatar
Matija Obreza committed
137
138
139
140
141
142
		},
		
		addFilterValue: function(element) {
			var name=$(element).closest(".clearfix").attr("x-filtername").replace('_',':');
			//debugger;
			var a=null, b=null;
143
144
			var inputs=$(element).parent().children("input[type=text]");
			var checkboxes=$(element).parent().children("input[type=checkbox]");
Matija Obreza's avatar
Matija Obreza committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
			var r=null;
			if (inputs.length==1) {
				// add 1
				a=inputs[0].value.trim();
				if (a!='') r=a;
			} else if (inputs.length==2) {
				a=inputs[0].value.trim();
				b=inputs[1].value.trim();

				if (a=='' && b=='') {
				} else if (a==b) {
					r=a;
				} else if (b=='') {
					r={"min":parseFloat(a)};
					if (isNaN(r["min"])) r=null;
				} else if (a=='') {
					r={"max":parseFloat(b)};
					if (isNaN(r["max"])) r=null;
				} else {
					r={"range":[parseFloat(a),parseFloat(b)]};
					if (isNaN(r["range"][0]) || isNaN(r["range"][1])) r=null;
				}
167
168
169
170
171
172
			} else if (checkboxes.length==1) {
				a=checkboxes[0].value;
				if (a=='true' || a=='false' || a=='null')
					r = (a == 'true' ? true : a=='null' ? 'null' : false);
				else
					r=a;
Matija Obreza's avatar
Matija Obreza committed
173
174
175
176
177
178
179
180
181
182
			}
			
			console.log(r);
			if (r!=null) {
				r = (r=='null' ? null : r);
				if (filters[name]==null) {
					filters[name]=[r];
				} else {
					filters[name][filters[name].length]=r;
				}
183
184
				if (inputs[0] && inputs[0].type=="text") { inputs[0].value=''; inputs[0].focus(); }
				if (inputs[1] && inputs[1].type=="text") inputs[1].value='';
Matija Obreza's avatar
Matija Obreza committed
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
			}
			console.log(filters);
			filters=FF.cleanup(filters);
			console.log(filters);
			
			FF.refreshJson(filters);
			FF.updateValues("#filter-" + name.replace(":", "_") + ".filter-values", name, filters[name]);
		},
		
		cleanup: function(filt) {
			var newFilt={};
			for (var prop in filt) { 
				console.log("Yada: " + prop); 
				var nv=newFilt[prop]=[];
				for (var i=0; i<filt[prop].length; i++) {
					var pos=nv.indexOf(filt[prop][i]);
					if (pos<0) nv[nv.length]=filt[prop][i];
				}
			}
			return newFilt;
		}
	};
	
208
	[<c:forEach items="${selectedFilters}" var="filter" varStatus="status">${status.index gt 0 ? ',' : ''} "${filter.name.replace(':','_')}"</c:forEach>  ].forEach(function(name) {
Matija Obreza's avatar
Matija Obreza committed
209
210
211
212
213
214
215
216
217
		FF.updateValues("#filter-" + name + ".filter-values", name, filters[name]);
	});
	
	$("body").on("click", ".filtval", function(event) {
		event.preventDefault();
		var idx=$(this).index();
		var name=$(this).closest(".clearfix").attr("x-filtername");
		//debugger;
		console.log("Delete! " + idx + " from " + name);
218
219
220
221
222
223
		if (filters[name]) {
			filters[name].splice(idx, 1);
			if (filters[name].length==0) 
				delete filters[name];
			console.log(filters[name]);
		}
Matija Obreza's avatar
Matija Obreza committed
224
225
226
227
228
229
230
231
		$(this).remove();
		FF.refreshJson(filters);
	});
	
	$("body").on("click", ".filter-new button", function(event) {
		event.preventDefault();
		FF.addFilterValue(this);
	});
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
	
	$("body").on("click", ".filter-new input[type=checkbox]", function(event) {
//		event.preventDefault();
		if (this.checked) {
			FF.addFilterValue(this);
		} else {
			var name=$(this).closest(".clearfix").attr("x-filtername");
			//debugger;
			console.log("Delete! " + this.value + " from " + name);
			if (filters[name]) {
				filters[name].splice(filters[name].indexOf(this.value), 1);
				if (filters[name].length==0) 
					delete filters[name];
			}
			FF.refreshJson(filters);
			FF.updateValues("#filter-" + name.replace(":", "_") + ".filter-values", name, filters[name]);
		}
	});
Matija Obreza's avatar
Matija Obreza committed
250
251
252
253
254
255
256
257

	$("body").on("keydown", ".filter-new input", function(event) {
		if (event.keyCode==13) {
			event.preventDefault();
			FF.addFilterValue(this);
		}
	});
	
258
259
260
261
262
	$(".autocomplete-filter").each(function() {
		var t=$(this); 
		t.autocomplete({ delay: 200, minLength: 3, source: t.attr('x-source'),  messages: { noResults: '', results: function() {} } });
	});
	
Matija Obreza's avatar
Matija Obreza committed
263
264
265
266
267
268
	FF.refreshJson(filters);
});
</script>
</content>
</body>
</html>