index.jsp 3.95 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<!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>


<c:forEach items="${availableFilters}" var="filter">

	<div class="clearfix filter-block">
		<div class="pull-left filter-name"><spring:message code="filter.${filter.name}" /></div>
		<div class="clearfix pull-left" id="filter-${filter.name}">
			<div class="filter-values pull-left"></div>
			<c:choose>
			<c:when test="${filter.type=='NUMERIC'}">
			<div class="filter-new pull-left">Min: <input class="span1" type="text" /> Max: <input class="span1" type="text" /> <button>+</button></div>	
			</c:when>
			<c:otherwise>
			<div class="filter-new pull-left"><input class="span2" type="text" /><button>+</button></div>	
			</c:otherwise>
			</c:choose>
		</div>	
	</div>

</c:forEach>

<a id="filtersHref" href=""><button>View!</button></a>

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

<script type="text/javascript">
jQuery(document).ready(function() {
//	alert('${jsonString}');
	var filters=$.parseJSON('${jsonString}');
//	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;
			values.forEach(function(i, idx) {
				console.log("Adding: " + i);
				//debugger;
				if ($.isPlainObject(i)) {
					// ranges & ops
					var x="<div class='filtval complex'><span class='deleteTag close'>&times;</span>";
					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'><span class='deleteTag close'>&times;</span>" + i + "</div>";
				}
				c.append(x);
			});
		},
		
		refreshJson: function(newFilter) {
			$("#filtersJson").html(JSON.stringify(newFilter));
			$("#filtersHref").attr("href", "/acn/f?filter="+JSON.stringify(newFilter));
		}
	};
	
	[<c:forEach items="${availableFilters}" var="filter" varStatus="status">${status.index gt 0 ? ',' : ''} "${filter.name}"</c:forEach>  ].forEach(function(name) {
		FF.updateValues("#filter-" + name + " .filter-values", name, filters[name]);
	});
	
	$("body").on("click", ".filter-block .filtval", function(event) {
		event.preventDefault();
		var idx=$(this).index();
		var name=$(this).closest(".clearfix").attr("x-filtername");
		//debugger;
		console.log("Delete! " + idx + " from " + name);
		filters[name].splice(idx, 1);
		if (filters[name].length==0) 
			delete filters[name];
		console.log(filters[name]);
		$(this).remove();
		FF.refreshJson(filters);
	});
	
	$("body").on("click", ".filter-block .filter-new button", function(event) {
		event.preventDefault();
		var name=$(this).closest(".clearfix").attr("x-filtername");
		//debugger;
		var inputs=$(this).parent().children("input");
		var a=null, b=null;
		if (inputs.length==1) {
			// add 1
			a=b=inputs[0].value;
		} else {
			var a=inputs[0].value;
			var b=inputs[1].value;
		}
		var r=null;
		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;
		}
		console.log(r);
		if (r!=null) {
			if (filters[name]==null) {
				filters[name]=[r];
			} else {
				filters[name][filters[name].length]=r;
			}
			inputs[0].value='';
			inputs[0].focus();
			if (inputs[1]) inputs[1].value='';
		}
		console.log(filters);
		FF.refreshJson(filters);
		FF.updateValues("#filter-" + name + " .filter-values", name, filters[name]);
	});
	
	FF.refreshJson(filters);
});
</script>

</body>
</html>