-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadWrite.js
More file actions
143 lines (128 loc) · 3.23 KB
/
readWrite.js
File metadata and controls
143 lines (128 loc) · 3.23 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
let myMap = false;
let mapJSON = '';
let listElems = [];
// Nic tu nie ma, precz
var SaveToJSON = function()
{
if(!myMap) return;
let mapObj = {};
mapObj.bitRate = myMap[0].bitRate;
mapObj.rows = myMap.length;
mapObj.addrArr = [];
mapObj.author = $('#author').val();
mapObj.description = $('#description').val();
for(let i = 0; i < myMap.length; i++)
{
mapObj.addrArr.push(BinaryToAddress(myMap[i].sumBin));
}
mapJSON = JSON.stringify(mapObj);
AJAX("jsonSave.php", JSONSavedCallback, "captcha=" + grecaptcha.getResponse() + "&JSON=" + mapJSON + "&sanitizeXSS=true");
return mapJSON;
}
var ReadFromJSON = function(id)
{
AJAX("jsonRead.php", JSONReadCallback, "id=" + id);
}
//No serio mówię (piszę)
var RenderFromJson = function()
{
if (!mapJSON) return;
if (mapJSON[0] == "<")
{
console.log("token <");
console.log(mapJSON);
return;
}
let mapObj = JSON.parse(mapJSON);
myMap = MapFromBinary(mapObj.rows, mapObj.bitRate, mapObj.addrArr);
}
var NewMap = function()
{
$("#map").html("");
let height = $("#height").val();
height = (height > 200) ? 200 : Math.round(height);
height = (height < 1) ? 1 : height;
let width = $("#i128")[0].checked ? 128 : 32;
let isFilled = $("#black")[0].checked;
$("#json").text("");
myMap = RenderNewMap(height, width, isFilled);
}
var MapFromBinary = function(rows, columns, addrArr)
{
$("#map").html("");
$("#json").html('<span style="font-size: 20px">Adresy:</span> ' + addrArr);
return RenderMapFromBinary(rows, columns, addrArr);
}
function AJAX(url, callback, str)
{
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function()
{
if (this.readyState == 4 && this.status == 200) callback(this);
};
xhttp.open("POST", url, true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send(str);
}
function JSONSavedCallback(xhttp)
{
mapJSON = xhttp.responseText;
if (!mapJSON)
{
console.log("Response is empty");
return;
}
if (mapJSON == "nocaptcha")
{
alert("Nie bądź robotem");
return;
}
if (mapJSON[0] == "E" && mapJSON[1] == ":")
{
console.log(mapJSON);
return;
}
grecaptcha.reset();
//$("#json").text("length: " + mapJSON.length + mapJSON);
}
function JSONReadCallback(xhttp)
{
mapJSON = xhttp.responseText;
if (!mapJSON)
{
console.log("Response is empty");
return;
}
RenderFromJson();
window.scrollTo(0, 500);
}
function ListDisplayCallback(xhttp)
{
listJSON = xhttp.responseText;
if (!listJSON)
{
console.log("Response is empty");
return;
}
if (listJSON == "keyword not set")
{
console.log(listJSON);
return;
}
if (listJSON[0] == "E" && listJSON[1] == ":")
{
console.log(listJSON);
return;
}
$("#list").html("");
list = JSON.parse(listJSON);
for (let i = 0; i < list.length; i++)
{
listElems.push(new ListElement(list[i]));
}
}
var ListDisplay = function(keyword = "")
{
AJAX("listRead.php", ListDisplayCallback, "keyword=" + keyword);
}
ListDisplay();