-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
184 lines (152 loc) · 5.86 KB
/
script.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
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
var db;
function indexedDBOk() {
return "indexedDB" in window;
}
function isEmpty(obj) {
for (var key in obj) {
if (obj.hasOwnProperty(key))
return false;
}
return true;
}
document.addEventListener("DOMContentLoaded", function () {
if (!indexedDBOk) return;
var openRequest = indexedDB.open("idarticle_answer", 1);
openRequest.onupgradeneeded = function (e) {
var thisDB = e.target.result;
if (!thisDB.objectStoreNames.contains("answer"))
thisDB.createObjectStore("answer", {
autoIncrement: true
});
}
openRequest.onsuccess = function (e) {
db = e.target.result;
document.querySelector("#add").addEventListener("click", addanswer, false);
document.querySelector("#getAll").addEventListener("click", getanswers, false);
document.querySelector("#deleteAll").addEventListener("click", deleteAll, false);
document.querySelector("#up").addEventListener("click", synchronize, false);
}
openRequest.onerror = function (e) {
alert('ERROR');
}
}, false);
function addanswer(e) {
var name = document.querySelector("#name").value;
var age = document.querySelector("#age").value;
if (age < 18) {
alert("You are not old enough to vote");
return false;
}
if (isEmpty(name)) {
alert("Name cannot be empty");
return false;
}
if (document.getElementById('odpA').checked)
var odp = document.querySelector("#odpA").value;
else if (document.getElementById('odpB').checked)
var odp = document.querySelector("#odpB").value;
else if (document.getElementById('odpC').checked)
var odp = document.querySelector("#odpC").value;
else if (document.getElementById('odpD').checked)
var odp = document.querySelector("#odpD").value;
var transaction = db.transaction(["answer"], "readwrite");
var store = transaction.objectStore("answer");
var answer = {
name: name,
age: age,
odp: odp,
}
var request = store.add(answer);
request.onerror = function (e) {
console.log("Error", e.target.error.name);
}
request.onsuccess = function (e) {
alert("Vote added");
console.log("Vote added");
}
}
function getanswers(e) {
var s = "";
var firstAnswer = 0;
var secondAnswer = 0;
var thirdAnswer = 0;
var fourthAnswer = 0;
var countAnswer = 0;
db.transaction(["answer"], "readonly").objectStore("answer").openCursor().onsuccess = function (e) {
var cursor = e.target.result;
if (cursor) {
s += "ID " + cursor.key + " ";
for (var field in cursor.value) {
s += field + "=" + cursor.value[field] + " ";
}
switch (cursor.value["odp"]) {
case 'A':
++firstAnswer;
++countAnswer;
break;
case 'B':
++secondAnswer;
++countAnswer;
break;
case 'C':
++thirdAnswer;
++countAnswer;
break;
case 'D':
++fourthAnswer;
++countAnswer;
break;
}
s += cursor.value["odp"] + " " + secondAnswer;
s += "<br>";
cursor.continue();
}
var trumpVotes = Math.round((100 * firstAnswer) / countAnswer);
var warrenVotes = Math.round((100 * secondAnswer) / countAnswer);
var sandersVotes = Math.round((100 * thirdAnswer) / countAnswer);
var bidenVotes = Math.round((100 * fourthAnswer) / countAnswer);
if (countAnswer == 0) {
trumpVotes = 0;
warrenVotes = 0;
sandersVotes = 0;
bidenVotes = 0;
}
var graphTrump = "Trump " + trumpVotes + "% <canvas id='myCanvas' width='" + (firstAnswer * 100) + "' height='100' style='border:1px solid #c3c3c3; background-color: #3D9970;'></canvas>";
var graphWarren = "Warren " + warrenVotes + "% <canvas id='myCanvas' width='" + (secondAnswer * 100) + "' height='100' style='border:1px solid #c3c3c3; background-color: #85144b;'></canvas>";
var graphSanders = "Sanders " + sandersVotes + "% <canvas id='myCanvas' width='" + (thirdAnswer * 100) + "' height='100' style='border:1px solid #c3c3c3; background-color: #FF851B;'></canvas>";
var graphBiden = "Biden " + bidenVotes + "% <canvas id='myCanvas' width='" + (fourthAnswer * 100) + "' height='100' style='border:1px solid #c3c3c3; background-color: #001f3f;'></canvas>";
document.getElementById('trump').innerHTML = graphTrump;
document.getElementById('warren').innerHTML = graphSanders;
document.getElementById('sanders').innerHTML = graphWarren;
document.getElementById('biden').innerHTML = graphBiden;
document.getElementById('fadingImg').style.visibility = 'hidden';
}
}
function deleteAll() {
firstAnswer = 0;
secondAnswer = 0;
thirdAnswer = 0;
fourthAnswer = 0;
countAnswer = 0;
var req = indexedDB.deleteDatabase("idarticle_answer");
location.reload();
}
function synchronize(e) {
var s = "";
var transaction = db.transaction(["answer"], "readwrite").objectStore("answer").openCursor().onsuccess = function (e) {
var cursor = e.target.result;
if (cursor) {
for (var field in cursor.value) {
s += cursor.value[field] + ":";
}
s += "#";
cursor.continue();
}
var req = indexedDB.deleteDatabase("idarticle_answer");
document.getElementById('hid').value = s;
document.getElementById('akt').submit();
}
}
function removeImg() {
document.getElementById('fadeImgSecond').style.visibility = 'hidden';
}