-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestAccessor.js
127 lines (112 loc) · 3.73 KB
/
restAccessor.js
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
function postAndUpdate(){
var titles = document.forms["form"].elements[0].value;
titles = titles.replace(/\n/g,"~");
postAjax('http://localhost:8080/review?sort=' + getSortingParameter() + "&filter=" + getFilterParameter(), titles , function (data){updateOutput(data);});
}
function postAjax(url, data, success) {
var params = typeof data == 'string' ? data : Object.keys(data).map(
function(k){ return encodeURIComponent(k) + '=' + encodeURIComponent(data[k]) }
).join('&');
var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xhr.open('POST', url);
xhr.onreadystatechange = function() {
if (xhr.readyState>3 && xhr.status==200) { success(xhr.responseText); }
};
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
xhr.send(params);
return xhr;
}
function updateOutput(data){
var spinner = document.getElementById("spinner");
spinner.style.display = "block";
console.log(ascOrDesc());
getColumnHeaders();
clearOutputTable();
printColumnHeaders();
var responseObj = JSON.parse(data);
var columnHeaders = getColumnHeaders();
for (var i = 0 ; i < responseObj.length ; i++){
var row = outputTable.insertRow(i + 1);
for (columnIdx in columnHeaders){
row.insertCell(columnIdx).innerHTML = responseObj[i][columnHeaders[columnIdx]];
}
}
spinner.style.display = "none";
}
function getAdditionalInfo(parameterName, review){
if(document.getElementById(parameterName + "Box").checked){
return " (" + review[parameterName] + ")"
} else {
return ""
}
}
function getSortingParameter(){
return document.getElementById("sorting").value + ":" + ascOrDesc();
}
function ascOrDesc(){
if (document.getElementById("asc").checked){
return "asc";
} else {
return "desc";
}
}
function getFilterParameter(){
return document.getElementById("filtering").value + ":" + greaterThanOrLessThan() + ":" + document.getElementById("filterValue").value
}
function greaterThanOrLessThan(){
if (document.getElementById("greaterThan").checked){
return "gt"
} else {
return "lt"
}
}
function getColumnHeaders(){
var columnHeaders = ["title", "imdbRating"];
var optionalHeaders = document.getElementsByClassName("info");
for (let item of optionalHeaders){
if(item.checked){
columnHeaders.push(item.id.replace("Box",""));
}
}
return columnHeaders;
}
function printColumnHeaders(){
var columnHeaders = getColumnHeaders();
var outputTable = document.getElementById("outputTable");
var headerRow = outputTable.insertRow(0);
for (columnIdx in columnHeaders){
var headerCell = document.createElement("TH");
headerCell.innerHTML = upperFirstLetter(columnHeaders[columnIdx]);
headerRow.appendChild(headerCell);
}
}
function upperFirstLetter(string){
return string.charAt(0).toUpperCase() + string.slice(1);
}
function clearOutputTable(){
var Parent = document.getElementById("outputTable");
while(Parent.hasChildNodes()){
Parent.removeChild(Parent.firstChild);
}
}
function empty(data){
if(typeof(data) == 'number' || typeof(data) == 'boolean'){
return false;
}
if(typeof(data) == 'undefined' || data === null){
return true;
}
if(typeof(data.length) != 'undefined'){
return data.length == 0;
}
var count = 0;
for(var i in data){
if(data.hasOwnProperty(i)){
count ++;
}
}
return count == 0;
}
var spinner = document.getElementById("spinner");
spinner.style.display = "none";