-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
192 lines (156 loc) · 5.49 KB
/
main.js
File metadata and controls
192 lines (156 loc) · 5.49 KB
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
/*
Written by Ed Moore (ed@ed-moore.net)
*/
const numFilters = 3;
let allData;
let filteredData;
function processJsonFile(){
let jsonText;
console.log("Starting parsing...");
if (!window.File || !window.FileReader || !window.FileList || !window.Blob) {
alert('The File APIs are not fully supported in this browser.');
return;
}
const jsonInput = document.getElementById('jsonFile');
if( !jsonInput ){
alert("Um, I think this is my bad... Please let me know.");
return;
}else if (!jsonInput.files) {
alert("This browser doesn't seem to support the `files` property of file inputs.");
return;
}else if (!jsonInput.files[0]) {
alert("Um, did you select a file?");
return;
}
const jsonFile = jsonInput.files[0];
const reader = new FileReader();
reader.onload = (function (theFile) {
return function (e) {
allData = JSON.parse(e.target.result);
}
})(jsonFile);
reader.readAsText(jsonFile);
$("#step1").removeClass('d-none');
$("#btn-step2").removeClass('disabled').prop('disabled', false);
}
function applyPathing(){
const path = $("#filterPath").val().split('.');
filteredData = allData;
path.forEach(function(k){
filteredData = filteredData[k];
});
if(filteredData === undefined){
alert("Invalid Filter Path");
return;
}
$("#step2").removeClass('d-none');
processJson(filteredData);
}
function processJson(data){
// let fields = getFields('', data[0]);
let fields = getFields(data[0], '.');
let optionsHTML = '<option value="">No Filter</option>\n';
$("#section-select").html('');
$.each(fields, function(i){
$("#section-select").append("<div class='form-check'><input type='checkbox' class='form-check-input' id='select-select" + i + "' value='" + fields[i] + "'><label class='form-check-label' for='select-select" + i + "'>" + fields[i] + "</label></div>");
optionsHTML += "<option>" + fields[i] + "</option>\n";
});
for(let i = 0; i < numFilters; i += 1){
$("#select-filter" + i).html(optionsHTML);
}
bindEvents();
}
/*
const predicateA = (field: string, value: any): boolean => { return field === "country_code" && value === "ar"; };
const predicateB = (field: string, value: any): boolean => { return field === "distributors.id" && (value === 3464 || value === 4056); };
const predicateC = (field: string, value: any): boolean => { return field === "distributors.state" && value === "C"; };
console.log(queryJson(data, [predicateC]));
*/
function filterData(){
let selects = [];
let filters = [];
// Get all the selects
$("#section-select input:checked").each(function(){
selects.push($(this).val());
filters.push((field, value) => { return field === $(this).val(); });
});
if( selects.length == 0 ){
alert("Please complete step 2!");
return;
}
// Get all the filters
$("#section-filter select").each(function(){
const filterField = $(this).val();
if( filterField != '' ){
const filterValue = $("#" + $(this).attr('triggerid')).val();
filters.push((field, value) => {
let match = value.toString().match(new RegExp(filterValue));
return filterField === field && match && value == match[0]; //value.match(new RegExp(filterValue));// === $("#" + $(this).attr('triggerid')).val();
});
}
});
const matches = queryJson(filteredData, filters);
// Remove the fields that aren't required
for (k1 in matches) {
for(k2 = matches[k1].length-1; k2 >= 0; k2--){
if( !selects.includes(matches[k1][k2].key) ){
matches[k1].splice(k2, 1);
}
}
}
console.log(matches);
writeResults(matches);
$("#step5").removeClass('d-none');
}
function writeResults(data){
let str = "";
$("#results tbody, #results thead").html('');
if( data.length == 0 ){
$("#results tbody").append("<tr><td class='text-center'>No results found!</td></tr>");
return;
}
for(k1 in data){
str = "<tr>";
for(k2 in data[k1]){
str += "<th>" + data[k1][k2].key + "</th>";
}
str += "</tr>";
$("#results thead").append(str);
break;
}
for (k1 in data) {
str = "<tr>";
for (k2 in data[k1]) {
str += "<td>" + data[k1][k2].value + "</td>";
}
str += "</tr>";
$("#results tbody").append(str);
}
}
function bindEvents(){
// Step 2
$("#section-select input[type='checkbox']").change(function(){
$("#step3").addClass('d-none');
if( $("#section-select input[type='checkbox']:checked").length > 0 ){
$("#step3").removeClass('d-none');
}
});
// Step 3
$("[trigger]").change(function(){
// Enable text box
if( $(this).val() == '' ){
$("#" + $(this).attr('triggerid')).prop('disabled', true);
}else{
$("#" + $(this).attr('triggerid')).prop('disabled', false);
}
// Display tick?
$("#step4").addClass('d-none');
$("#btn-step5").addClass('disabled').prop('disabled', true);
$("[trigger]").each(function(){
if( $(this).val() != '' ){
$("#step4").removeClass('d-none');
$("#btn-step5").removeClass('disabled').prop('disabled', false);
}
})
});
}