summaryrefslogtreecommitdiffhomepage
path: root/html/whiteboard.js
blob: 9018f925e70b4c9ad6f89c4c4910f70fb2db73f2 (plain)
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
// started on main page load
function init() {
	init_board();
}

function init_board() {
	var xhr = new XMLHttpRequest();
	
        const searchParams = (new URL(document.location)).searchParams;
        if (!searchParams.has('id')) {
                redirect_to_new_page();
                return;
        }

	// run on data received back
	xhr.onreadystatechange = function() {
				if (this.readyState == 3) {
					//set_status("Please wait while downloading " + filename + " ...");
					return;
				}
                                if (this.readyState != 4) {
					return;
				}
				if (this.status != 200) {
					//set_status("Server Error while retrieving " + filename + ", status: " + this.status + " " + this.statusText);
                                        return;
                                }

                                var file = new Blob([this.response]);
				reader = new FileReader();
				reader.onload = function() {
					var board = document.getElementById("board");
					board.innerHTML = reader.result;

					// Initialization done. Now we can start modifying.
					board.addEventListener("input", function() {on_modify(); });

					setInterval(function() {checkupdate();}, 2000);
				}
				
			        reader.readAsBinaryString(file);

				//set_status(""); // OK
	}

	var parser = new DOMParser();
	var xmlDocument = parser.parseFromString("<request></request>", "text/xml");
	
	var requestElement = xmlDocument.getElementsByTagName("request")[0];

	var commandElement = xmlDocument.createElement("command");
	commandElement.appendChild(document.createTextNode("getfile"));
	requestElement.appendChild(commandElement);

	var idElement = xmlDocument.createElement("id");
	idElement.appendChild(document.createTextNode(get_id()));
	requestElement.appendChild(idElement);

	xhr.open("POST", "whiteboard.fcgi", true);
	xhr.setRequestHeader("Content-type", "text/xml");
	xhr.responseType = 'blob';
	xhr.send(xmlDocument);

	//set_status("Please wait while server prepares " + filename + " ...");
}

function get_id()
{
        const searchParams = (new URL(document.location)).searchParams;
        return searchParams.get('id');
}

function on_new_page()
{
        redirect_to_new_page();
}

function redirect_to_new_page()
{
	var xhr = new XMLHttpRequest();
	
	// run on data received back
	xhr.onreadystatechange = function() {
				if (this.readyState == 3) {
					//set_status("Please wait while downloading " + filename + " ...");
					return;
				}
                                if (this.readyState != 4) {
					return;
				}
				if (this.status != 200) {
					//set_status("Server Error while retrieving " + filename + ", status: " + this.status + " " + this.statusText);
                                        return;
                                }

                                var id = this.responseText;
                                //alert("location=" + document.location.href);
                                var new_location = document.location.href;
                                var pos = new_location.search("\\?");
                                if (pos >= 0)
                                        new_location = new_location.substring(0, pos);
                                new_location += '?id=' + id;

                                window.location.href = new_location;
				//set_status(""); // OK
	}

	var parser = new DOMParser();
	var xmlDocument = parser.parseFromString("<request></request>", "text/xml");
	
	var requestElement = xmlDocument.getElementsByTagName("request")[0];

	var commandElement = xmlDocument.createElement("command");
	commandElement.appendChild(document.createTextNode("newid"));
	requestElement.appendChild(commandElement);

	xhr.open("POST", "whiteboard.fcgi", true);
	xhr.setRequestHeader("Content-type", "text/xml");
	xhr.send(xmlDocument);

	//set_status("Please wait while server prepares " + filename + " ...");
}

function on_modify()
{
	var xhr = new XMLHttpRequest();
	
	// run on data received back
	xhr.onreadystatechange = function() {
				if (this.readyState == 3) {
					//set_status("Please wait while downloading " + filename + " ...");
					return;
				}
                                if (this.readyState != 4) {
					return;
				}
				if (this.status != 200) {
					//set_status("Server Error while retrieving " + filename + ", status: " + this.status + " " + this.statusText);
                                        return;
                                }

				//set_status(""); // OK
	}

	var parser = new DOMParser();
	var xmlDocument = parser.parseFromString("<request></request>", "text/xml");
	
	var requestElement = xmlDocument.getElementsByTagName("request")[0];

	var commandElement = xmlDocument.createElement("command");
	commandElement.appendChild(document.createTextNode("modify"));
	requestElement.appendChild(commandElement);

	var idElement = xmlDocument.createElement("id");
	idElement.appendChild(document.createTextNode(get_id()));
	requestElement.appendChild(idElement);

	var dataElement = xmlDocument.createElement("data");
	dataElement.appendChild(document.createTextNode(document.getElementById("board").value));
	requestElement.appendChild(dataElement);

	xhr.open("POST", "whiteboard.fcgi", true);
	xhr.setRequestHeader("Content-type", "text/xml");
	xhr.responseType = 'blob';
	xhr.send(xmlDocument);

	//set_status("Please wait while server prepares " + filename + " ...");
}

// checksum of string
function checksum32(s) {
	var result = 0;
	for (var i = 0; i < s.length; i++) {
		result = ((((result >>> 1) | ((result & 1) << 31)) | 0) ^ (s.charCodeAt(i) & 0xFF)) | 0;
	}
	return (result & 0x7FFFFFFF) | 0;
}

// gets called by regular polling
function checkupdate() {
	var xhr = new XMLHttpRequest();
	
	// run on data received back
	xhr.onreadystatechange = function() {
				if (this.readyState == 3) {
					//set_status("Please wait while downloading " + filename + " ...");
					return;
				}
                                if (this.readyState != 4) {
					return;
				}
				if (this.status != 200) {
					//set_status("Server Error while retrieving " + filename + ", status: " + this.status + " " + this.statusText);
                                        return;
                                }

				// no change if response is text/plain
				if (this.getResponseHeader("Content-Type") == "application/octet-stream") {
					var file = new Blob([this.response]);
					reader = new FileReader();
					reader.onload = function() {
						var board = document.getElementById("board");
						board.value = reader.result;
					}
					
					reader.readAsBinaryString(file);
				}

				//set_status(""); // OK
	}

	var parser = new DOMParser();
	var xmlDocument = parser.parseFromString("<request></request>", "text/xml");
	
	var requestElement = xmlDocument.getElementsByTagName("request")[0];

	var commandElement = xmlDocument.createElement("command");
	commandElement.appendChild(document.createTextNode("checkupdate"));
	requestElement.appendChild(commandElement);

	var idElement = xmlDocument.createElement("id");
	idElement.appendChild(document.createTextNode(get_id()));
	requestElement.appendChild(idElement);

	var checksumElement = xmlDocument.createElement("checksum");
	checksumElement.appendChild(document.createTextNode(checksum32(document.getElementById("board").value)));
	requestElement.appendChild(checksumElement);

	xhr.open("POST", "whiteboard.fcgi", true);
	xhr.setRequestHeader("Content-type", "text/xml");
	xhr.responseType = 'blob';
	xhr.send(xmlDocument);

	//set_status("Please wait while server prepares " + filename + " ...");
}