This repository was archived by the owner on Nov 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
251 lines (217 loc) · 9.3 KB
/
index.html
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Loddon Capital Works Program Search Widget</title>
<!-- Bootstrap -->
<link href="bootstrap-3.2.0/css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<!-- Select2 -->
<link href="select2-3.5.1/select2.css" rel="stylesheet"/>
<style>
span.select2-match {
font-weight:bold;
text-decoration: none !important;
}
.select2-arrow b {
width: 0%;
background: none !important;
}
</style>
</head>
<body>
<div class="text-center">
<input type='hidden' id="e1" style="width:400px;margin:10px 10px 50px 10px;font-size:18px;"/>
<div>
<div id="t1" class="jsontotable"></div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="bootstrap-3.2.0/js/bootstrap.min.js"></script>
<!-- Select2 -->
<script src="select2-3.5.1/select2.js"></script>
<!-- JSON to table -->
<script src="jquery.jsontotable.min.js"></script>
<script>
// Variables
// Globals
var urlCSVFile = "data/WorksProgram.csv";
var maxNbItemsInDropDown = 7;
var placeHolder = "Type in a road name";
// Initially from the spreadsheet, re-worded for column readability
var headersAll = ["Contract","Current Year / Quarter or Year","Project Type","Description","Road Class","Segment ID","Road Name","Locality","Point From Where Measure","Chainage (Km)","To Chainage (Km) Loddon Bulletin","Proposed or Required Works Loddon Bulletin","Proposed Treatment Code","Cost Code","Ledger ","RH Length(Km) ","RH Width(m)","RH Height (m)","LH Length(Km) ","LH Width(m)","LH Height (m)","Budget","Expenditure","% Activity","Proposed Start","Proposed Finish","Proposed Total Days","Project Ledger Open", "Project Ledger Closed","Patrol Area","Photo","TOTAL DISTANCE","Comments","Additional Works Comments ","Match","No of Grades","Completed Length (Km)"];
// Indices of yellow cells in the spreasheet
var headersToKeepIdx = [1,4,6,7,8,9,10,12];
var quarterIdx = 1;
var quarterLabels = [
"",
"1st July - 30th September 2015",
"1st October - 31st December 2015",
"1st January - 31st March 2016",
"1st April - 30th June 2016",
"FY 2015-2016",
"FY 2016-2017"
];
var chainageFromIdx = 9;
// Work variables
var lines = [], road_name_arr = [], road_name_obj = {};
// Helper functions
// Ordering by position within the matched string, then by lexicographic order
function sortByMatchPosAndLexico(q, a, b) {
var idx_a = a["text"].toLowerCase().indexOf(q.toLowerCase());
var idx_b = b["text"].toLowerCase().indexOf(q.toLowerCase());
if (idx_a > idx_b) {return 1;}
else if (idx_a < idx_b) {return -1;}
else {
if (a["text"]>b["text"]) {return 1;}
else if (a["text"]<b["text"]) {return -1;}
else {return 0;}
}
};
// Presenting row content
// Note: the content of the passed array will be updated (work on a deep clone)
function rowFormat(resultArray){
$.each(resultArray,function(i,v){
// Presentation of the quarter/year column
v[headersAll[quarterIdx]] = v[headersAll[quarterIdx]].replace(/ ?& ?/,"<br>").replace("15 16",'5').replace("16 17",'6').replace(/([0123456])/g,function(match){
return quarterLabels[parseInt(match)];
});
// Other column presentation go here
});
// Ordering results by chainage ascending
return resultArray.sort(function(a,b){
var chs_a = a[headersAll[chainageFromIdx]];
var chs_b = b[headersAll[chainageFromIdx]];
if (!isNaN(parseFloat(chs_a)) && !isNaN(parseFloat(chs_b)))
{
chs_a = parseFloat(chs_a);
chs_b = parseFloat(chs_b);
if (chs_a > chs_b) {return 1;}
else if (chs_a < chs_b) {return -1;}
else {return 0;}
}
else
{
return 0;
}
});
};
// Processing the CSV file into support structures
function processDataInit(allText) {
var allTextLines = allText.split(/\r\n|\n/);
for (var i=0; i<allTextLines.length; i++) {
var data = allTextLines[i].split(',');
if (data.length > 0) {
// Pushing all attributes to keep in an object
var tarr = {};
for (var j=0; j<headersToKeepIdx.length; j++) {
tarr[headersAll[headersToKeepIdx[j]]] = data[headersToKeepIdx[j]];
}
lines.push(tarr);
// Building a dedup'd array of id/text to power select2 drop down
var text_label = data[6];
if (text_label)
{
if (!road_name_obj[text_label])
{
road_name_arr.push({id:i,text:text_label});
// The obj is a convenience structure to check easily if a road name has already been encountered
road_name_obj[text_label]=[i];
}
else
{
// It also allows easy access to the IDs of the records having the same road name
road_name_obj[text_label].push(i);
}
}
}
}
}
// IE8 doesn't understand indexOf
if (!Array.prototype.indexOf)
{
Array.prototype.indexOf = function(elt /*, from*/)
{
var len = this.length >>> 0;
var from = Number(arguments[1]) || 0;
from = (from < 0)
? Math.ceil(from)
: Math.floor(from);
if (from < 0)
from += len;
for (; from < len; from++)
{
if (from in this &&
this[from] === elt)
return from;
}
return -1;
};
};
// jQuery behaviours
$(document).ready(function() {
// Drop down component (select2)
$("#e1").select2({
placeholder: placeHolder,
minimumInputLength: 1,
formatInputTooShort: "",
query: function (query) {
var data = {results: []};
// Subset of array matching the search string (case insensitive)
data.results = $.grep(road_name_arr, function( n, i ) {
return ( road_name_arr[i]["text"].toLowerCase().indexOf(query.term.toLowerCase())>-1 );
}).sort(function(a,b){
return sortByMatchPosAndLexico(query.term,a,b);
}).slice(0,maxNbItemsInDropDown);
query.callback(data);
}
}).on('change',function(data){
// Keep the select control open
$("#e1").select2("open");
// Emptying the table tag
$('#t1').empty();
// A row for column headers
var titleRow = $.grep(headersAll, function( n, i ) {
return ( headersToKeepIdx.indexOf(i)>-1 );
});
// Transforming this column header structure into an proper object (suitable for JSON to HTML templating)
var a = {};
$.each(titleRow,function(idx,val){
a[val]=val;
})
// Grep of line records whose index has been tracked in the road_name_obj
var jsonResultArr = $.grep(lines, function( n, i ) {
return ( road_name_obj[data.added.text].indexOf(i)>-1 );
});
// Perform a deep copy of the result for intervention directly in the values
var jsonResultArrClone = new Array(jsonResultArr.length);
jsonResultArrClone = $.map(jsonResultArrClone,function(val,idx){
return $.extend({},jsonResultArr[idx]);
});
// Formatting for proper presentation
jsonResultArrClone = rowFormat(jsonResultArrClone);
// Render the JSON as an HTML table
$.jsontotable($.merge($.merge( [], [a]),jsonResultArrClone), { id: '#t1', header: true, className: 'table table-striped table-hover table-bordered' });
}).on('select2-close',function(){
$("#e1").select2("open");
}).select2("open");
// Requesting the CSV file containing the work programs
$.ajax({
type: "GET",
url: urlCSVFile,
dataType: "text",
success: function(data) {
processDataInit(data);
}
});
});
</script>
</body>
</html>