-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.js
More file actions
executable file
·390 lines (355 loc) · 14.4 KB
/
plugin.js
File metadata and controls
executable file
·390 lines (355 loc) · 14.4 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
var sortDescending = true,
sort = false,
sortTerm, header = true,
filter = false,
filterParam;
var filterObject = {},
gResponse;
// This function will read the file using AJAX and send the content to callback function.
function loadJSON(file, callback) {
var requestObject = new XMLHttpRequest();
requestObject.overrideMimeType("application/json");
requestObject.open('GET', file, true);
requestObject.onreadystatechange = function () {
if (requestObject.readyState == 4 && requestObject.status == "200") {
//This will be true only in the beginning, setting the header.
if (header) {
header = false;
buildHeader(requestObject.responseText);
}
gResponse = requestObject.responseText;
callback(requestObject.responseText, false);
}
};
requestObject.send(null);
}
//This is a helper function, to set the attributes to any DOM element in a faster and efficient manner.
function setAttributes(el, attrs) {
for (var key in attrs) {
el.setAttribute(key, attrs[key]);
}
}
//This function will build the header row and search row for the table. As well as excel filter modal on them.
function buildHeader(response) {
var result = JSON.parse(response);
var table = document.getElementById("results");
var head = document.createElement("div");
setAttributes(head, {
"class": "theader"
});
// Building head labels for each column
$.each(result[0], function (key, value) {
var col = document.createElement("div");
setAttributes(col, {
"class": "table_header",
"style": "cursor: pointer;"
});
//Building modal to pop up when clicked on header using DOM.
var divModal = document.createElement("div");
var contentModal = document.createElement("div");
var dialogModal = document.createElement("div");
setAttributes(dialogModal, {
"class": "modal-dialog"
});
setAttributes(contentModal, {
"class": "modal-content "
});
setAttributes(divModal, {
"class": "modal fade ",
"id": key + "Modal",
"role": "dialog",
"tabindex": "-1",
"aria-labelledBy": "example",
"aria-hidden": "true"
});
var modalHeader = document.createElement("div");
setAttributes(modalHeader, {
"class": "modal-header"
});
var headerTitle = document.createElement("h3");
setAttributes(headerTitle, {
"class": "modal-title"
});
var headerText = document.createTextNode("Filter");
headerTitle.appendChild(headerText);
var modalButton = document.createElement("button");
setAttributes(modalButton, {
"type": "button",
"class": "close",
"data-dismiss": "modal",
"aria-label": "Close"
});
var spanClose = document.createElement("span");
setAttributes(spanClose, {
"aria-hidden": "true"
});
spanClose.innerHTML = "×";
modalButton.appendChild(spanClose);
var modalBody = document.createElement("div");
setAttributes(modalBody, {
"class": "modal-body"
});
//This will populate checkbox and labels on the modal, only unique ones using lookup.
var lookup = {};
$(result).each(function (idx, obj) {
$(obj).each(function (term, value) {
if (!(value[key] in lookup)) {
// This lookup would be used to populate only unique values in the modal excel filter.
lookup[value[key]] = 1;
var checkbox = document.createElement('input');
checkbox.type = "checkbox";
checkbox.name = key + "Name";
checkbox.value = value[key];
checkbox.id = key + "id";
var label = document.createElement('label')
label.htmlFor = key + "id";
label.appendChild(document.createTextNode("\u00A0" + value[key]));
modalBody.appendChild(checkbox);
modalBody.appendChild(label);
var br = document.createElement("br");
modalBody.appendChild(br);
}
});
});
var modalFooter = document.createElement("div");
setAttributes(modalFooter, {
"class": "modal-footer"
});
var modalFilter = document.createElement("button");
setAttributes(modalFilter, {
"class": "btn btn-primary",
"type": "submit"
});
var submitText = document.createTextNode("Filter");
modalFilter.appendChild(submitText);
// This will be invoked when filter button is clicked. It will make list of checked checkboxes and store it in filterObj.
modalFilter.addEventListener("click", function () {
var filterObj = {};
$('input[type=checkbox]').each(function () {
if (this.checked)
filterObj[$(this).val()] = 1;
});
// Setting filterParam, so as to reorganize table structure according to this parameter.
filterParam = key;
filterObject = filterObj;
filter = true;
callback(gResponse);
// This will hide the modal.
$('#' + key + 'Modal').modal('hide');
});
modalHeader.appendChild(modalButton);
modalHeader.appendChild(headerTitle);
contentModal.appendChild(modalHeader);
modalFooter.appendChild(modalFilter);
contentModal.appendChild(modalBody);
contentModal.appendChild(modalFooter);
dialogModal.appendChild(contentModal);
divModal.appendChild(dialogModal);
var spanText = document.createElement("span");
var colText = document.createTextNode(key + " ");
spanText.appendChild(colText);
setAttributes(spanText, {
"id": key + "Filter"
});
var spanElement = document.createElement("span");
setAttributes(spanElement, {
"class": "glyphicon glyphicon-sort",
"id": key,
"style": "cursor: pointer;"
});
col.appendChild(spanText);
col.appendChild(spanElement);
head.appendChild(col);
body.appendChild(divModal);
});
table.appendChild(head);
//Building search row for each column.
var divRow = document.createElement("div");
setAttributes(divRow, {
"class": "table_row"
});
//Building search input box for each column
$.each(result[0], function (key, value) {
var divSmall = document.createElement("div");
setAttributes(divSmall, {
"class": "table_small"
});
var tableCell = document.createElement("div");
setAttributes(tableCell, {
"class": "table_cell"
});
var topNav = document.createElement("div");
setAttributes(topNav, {
"class": "topnav"
});
var inputSearch = document.createElement("input");
setAttributes(inputSearch, {
"type": "text",
"placeholder": "Search",
"id": key + "Search"
});
topNav.appendChild(inputSearch);
divSmall.appendChild(tableCell);
divSmall.appendChild(topNav);
divRow.appendChild(divSmall);
});
table.appendChild(divRow);
// Attatching sort and search functionality to each column
$.each(result[0], function (key, value) {
$("#" + key).customSort({
sortTerm: key
});
$("#" + key + "Search").customSearch({
searchTerm: key
});
// Attaching filter functionality to each header text of each column.
$('#' + key + "Filter").on('click', function (ev) {
$('#' + key + 'Modal').modal('show');
})
});
}
// This is the main callback function for displaying the whole data on the screen in tabular format.
function callback(response, search = false) {
var actualJSON = JSON.parse(response);
var content = document.getElementById("results");
// This will remove all the existing tabular structure, so as to append new ones which will match search criteria.
if (search) {
while (document.getElementsByClassName("data")[0]) {
content.removeChild(document.getElementsByClassName("data")[0]);
}
}
// This will remove existing tabular structure, as well as sort the JSON data in ascending or descending order.
if (sort) {
while (document.getElementsByClassName("data")[0]) {
content.removeChild(document.getElementsByClassName("data")[0]);
}
actualJSON.sort(function (one, another) {
if (sortDescending) {
return one[sortTerm] < another[sortTerm];
} else {
return one[sortTerm] > another[sortTerm];
}
});
if (sortDescending) sortDescending = false;
else sortDescending = true;
sort = false;
}
while (document.getElementsByClassName("data")[0]) {
content.removeChild(document.getElementsByClassName("data")[0]);
}
// This loops through whole JSON data and displays it on the screen in tabular format.
for (var i = 0; i < actualJSON.length; i++) {
//Create table using divs
var table = document.getElementById("results");
var div = document.createElement("div");
div.setAttribute("class", "table_row data");
// This lookup is used to skip those rows which do not match filter criteria.
if (filter) {
var tempObj = actualJSON[i];
if (!(tempObj[filterParam] in filterObject))
continue;
}
// This will append each JSON data into each cell of table.
$.each(actualJSON[0], function (key, value) {
var smallDiv = document.createElement("div");
smallDiv.setAttribute("class", "table_small");
var cellDiv = document.createElement("div");
cellDiv.setAttribute("class", "table_cell");
var tempObj = actualJSON[i];
var textNode;
// This is used to replace with no value, if key does not exists.
if (typeof tempObj[key] != 'undefined')
textNode = document.createTextNode(tempObj[key]);
else textNode = document.createTextNode("( no value )");
cellDiv.appendChild(textNode);
smallDiv.appendChild(cellDiv);
div.appendChild(smallDiv);
});
table.appendChild(div);
}
}
(function ($) {
// This is the function used to build whole table, by just giving the JSON file.
$.fn.buildTable = function (options) {
// Default options
var settings = $.extend({
source: "./data.json"
}, options);
loadJSON(settings.source, callback);
};
// This will help in sorting the JSON data.
$.fn.customSort = function (options) {
// Default options
var settings = $.extend({
source: "./data.json",
sortTerm: "youCanOverideHere"
}, options);
// When clicked on sort icon, it will enabled sort and display data on screen in sorted format.
this.click(function () {
sort = true;
sortTerm = settings.sortTerm;
loadJSON(settings.source, callback);
});
};
// This is used for searching within the JSON content.
$.fn.customSearch = function (options) {
// Default options
var settings = $.extend({
source: "./data.json",
searchTerm: "youCanOverideHere"
}, options);
// On keyup, JSON content will be filtered on search criteria.
$(this).keyup(function () {
// If search box gets empty, it will populate whole data back on screen.
if (!this.value) {
var content = document.getElementById("results");
while (document.getElementsByClassName("data")[0]) {
content.removeChild(document.getElementsByClassName("data")[0]);
}
loadJSON(settings.source, callback);
}
});
$(this).autocomplete({
source: function (req, res) {
var regex = new RegExp(req.term, 'i');
$.ajax({
url: settings.source,
dataType: "json",
type: "GET",
data: {
term: req.term
},
success: function (data) {
var obj = [];
res($.map(data, function (item) {
if (req.term.includes(">=") && typeof item[settings.searchTerm] == "number") {
if (item[settings.searchTerm] >= req.term.substring(2)) {
obj.push(item);
}
} else if (req.term.includes("<=") && typeof item[settings.searchTerm] == "number") {
if (item[settings.searchTerm] <= req.term.substring(2)) {
obj.push(item);
}
} else if (req.term.includes("<") && typeof item[settings.searchTerm] == "number") {
if (item[settings.searchTerm] < req.term.substring(1)) {
obj.push(item);
}
} else if (req.term.includes(">") && typeof item[settings.searchTerm] == "number") {
if (item[settings.searchTerm] > req.term.substring(1)) {
obj.push(item);
}
} else if (regex.test(item[settings.searchTerm])) {
obj.push(item);
}
callback(JSON.stringify(obj), true);
}));
},
error: function (xhr) {
}
});
},
select: function (event, ui) {
}
});
};
}(jQuery));