summaryrefslogtreecommitdiff
path: root/proto.js
blob: 0baf22d48bbd9e3717aafd986dbf64528f7727b0 (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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set et sw=2 ts=2: */

var BrowserDetect = {
  init: function () {
    this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
    this.version = this.searchVersion(navigator.userAgent)
      || this.searchVersion(navigator.appVersion)
      || "an unknown version";
    this.OS = this.searchString(this.dataOS) || "an unknown OS";
  },
  searchString: function (data) {
    for (var i=0;i<data.length;i++)  {
      var dataString = data[i].string;
      var dataProp = data[i].prop;
      this.versionSearchString = data[i].versionSearch || data[i].identity;
      if (dataString) {
        if (dataString.indexOf(data[i].subString) != -1)
          return data[i].identity;
      }
      else if (dataProp)
        return data[i].identity;
    }
  },
  searchVersion: function (dataString) {
    var index = dataString.indexOf(this.versionSearchString);
    if (index == -1) return;
    return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
  },
  dataBrowser: [ { string: navigator.userAgent, subString: "Chrome", identity: "Chrome" },
                 { string: navigator.userAgent, subString: "OmniWeb", versionSearch: "OmniWeb/",
                   identity: "OmniWeb" },
                 { string: navigator.vendor, subString: "Apple", identity: "Safari",
                   versionSearch: "Version" },
                 { prop: window.opera, identity: "Opera", versionSearch: "Version" },
                 { string: navigator.vendor, subString: "iCab", identity: "iCab" },
                 { string: navigator.vendor, subString: "KDE", identity: "Konqueror" },
                 { string: navigator.userAgent, subString: "Firefox", identity: "Firefox" },
                 { string: navigator.vendor, subString: "Camino", identity: "Camino" },
                 // for newer Netscapes (6+)
                 { string: navigator.userAgent, subString: "Netscape", identity: "Netscape" },
                 { string: navigator.userAgent, subString: "MSIE", identity: "Explorer",
                   versionSearch: "MSIE" },
                 { string: navigator.userAgent, subString: "Gecko", identity: "Mozilla",
                   versionSearch: "rv" } ,
                 // for older Netscapes (4-)
                 { string: navigator.userAgent, subString: "Mozilla", identity: "Netscape",
                   versionSearch: "Mozilla" }
  ],
  dataOS : [ { string: navigator.platform, subString: "Win", identity: "Windows" },
             { string: navigator.platform, subString: "Mac", identity: "Mac" },
             { string: navigator.userAgent, subString: "iPhone", identity: "iPhone/iPod" },
             { string: navigator.platform, subString: "Linux", identity: "Linux" }
  ]

};

//document.cursor = crosshair;

BrowserDetect.init();

function get_appropriate_ws_url()
{
    var pcol;
    var u = document.URL;

  /*
   * We open the websocket encrypted if this page came on an
   * https:// url itself, otherwise unencrypted
   */

  if (u.substring(0, 5) == "https") {
    pcol = "wss://";
    u = u.substr(8);
  } else {
    pcol = "ws://";
    if (u.substring(0, 4) == "http")
      u = u.substr(7);
  }

  u = u.split('/');

  return pcol + u[0];
}


//document.getElementById("create_task").style.display = '';

/* lws-task protocol */

var down = 0;
var no_last = 1;
var last_x = 0, last_y = 0;
var ctx;
var socket_task;
var socket_observe;
var color = "#000000";
var dragged = "";

var board = document.getElementById("board");
board.setAttribute("ondrop", "dropInBody(event)");
board.setAttribute("ondragover", "return false");
board.setAttribute("ondragenter", "return dragenter(this)");
board.setAttribute("ondragleave", "return dragleave(this)");


if (BrowserDetect.browser == "Firefox") {
  socket_task = new MozWebSocket(get_appropriate_ws_url(), "lws-task-protocol");
  socket_observe = new MozWebSocket(get_appropriate_ws_url(), "lws-observe-protocol");
} else {
  socket_task = new WebSocket(get_appropriate_ws_url(),  "lws-task-protocol");
  socket_observe = new WebSocket(get_appropriate_ws_url(),  "lws-observe-protocol");
}

try {
  socket_task.onopen = function() {
    document.getElementById("wstask_status").style.backgroundColor = "#40ff40";
    document.getElementById("wstask_status").textContent = "TaskProto websocket connection opened ";
    //    document.getElementById("box").style.display = "none";
  } 

  socket_task.onmessage = function got_packet(msg) {
    document.getElementById("wstask_lastmsg").textContent = msg.data;

    var msgs = new Array();
    var idx = 0;
    msgs[idx] = '';
    var c = 0;
    var instring = false;
    for(c = 0; c < msg.data.length; c++) {
      if(msg.data[c] == '"' && c > 0 && msg.data[c - 1] != '\\') instring = !instring;
      if(msg.data[c] == ';' && instring == false) {
        idx++;
        msgs[idx] = '';
      }
      if(msg.data[c] != ';' || instring == true) msgs[idx] += msg.data[c];
    }

    f = 0;
    while (f < msgs.length - 1) {
      var msg = new Array();
      instring = false;
      idx = 0;
      msg[idx] = '';
      for(c = 0; c < msgs[f].length; c++) {
        if(msgs[f][c] == '"' && c > 0 && msgs[f][c - 1] != '\\') {
          instring = !instring;
          continue;
        }
        if(msgs[f][c] == ' ' && instring == false) {
          msg[idx] = msg[idx].replace("\\\\","\\").replace("\\\"","\"");
          idx++;
          msg[idx] = '';
        }
        if(msgs[f][c] != ' ' || instring == true) msg[idx] += msgs[f][c];
      }

      var cmd = msg[0];

      if(cmd == "del") {
        var id = msg[1];
        var task = document.getElementById("task_" + id);
        //todo: remove from parent not board
        board.removeChild(task);
      }
      else if(cmd == "move") {
        var id = msg[1];
	      var parent_id = msg[2];
//        var left = msg[2];
//        var top = msg[3];
        var task = document.getElementById("task_" + id);
        if(parent_id != -1) {
          var parent_task = document.getElementById("task_" + parent_id);
	        parent_task.appendChild(task);
        }
        else {
          board.appendChild(task);
        }
//	task.parentNode.removeChild(task);
//	board.removeChild(task);
//	parent_task.appendChild(task);
//        task.style.left = left + "px";
//        task.style.top = top + "px";
      }
      else if(cmd == "add") {
        var id = msg[1];
        var title = msg[2];
        var description = msg[3];
	var parent_id = msg[4];

        var task = document.createElement("div");
        task.name = "task";
        task.setAttribute("class", "task");
        task.setAttribute("ondrop", "drop(this, event)");
        task.setAttribute("ondragenter", "return dragenter(this)");
        task.setAttribute("ondragleave", "return dragleave(this)");
        task.setAttribute("ondragover", "return false");
        task.setAttribute("draggable", true);
        task.setAttribute("ondragstart", "drag(this, event)");

	task.id = "task_" + id;

        var taskText = document.createTextNode(title + ": " + description + " :" + task.id);
        taskText.id = "txt";
	task.appendChild(taskText);
	
//        task.style.position = "absolute";
//        task.style.left = left + "px";
//        task.style.top = top + "px"; 
//        task.setAttribute("onMouseDown", "dragged = '" + id + "';");

//        var dlButton = document.createElement("input");
//        dlButton.type = "button";
//        dlButton.value = "Remove";
//        dlButton.onclick = "deleteTask(document.getElementById(" + task.id + ").id";
//        dlButton.setAttribute("onclick", "deleteTask(" + id +")");
//        task.appendChild(dlButton);

//        board.appendChild(task);
      	
        var parent_task = document.getElementById("task_" + parent_id);

	if(parent_task) {
          parent_task.appendChild(task);
	}
	else {
          board.appendChild(task);
	}
      }
      else if(cmd == "update") {
        var id = msg[1];
        var title = msg[2];
        var description = msg[3];
       
        var task = document.getElementById("task_" + id);

	var taskText = document.createTextNode(title + ": " + description + " :" + task.id);
	task.appendChild(taskText);
      }
      f++;
//      document.getElementById("box").style.top = i[3] + "px";
//      document.getElementById("box").style.left = i[2] + "px";
//      document.getElementById("box").style.display = "block";
    }
  }

  socket_task.onclose = function(){
    document.getElementById("wstask_status").style.backgroundColor = "#ff4040";
    document.getElementById("wstask_status").textContent = "TaskProto websocket connection CLOSED ";
  }
} catch(exception) {
  alert('<p>Error' + exception + '</p>');
}

try {
  socket_observe.onopen = function() {
    document.getElementById("wsobserve_status").style.backgroundColor = "#40ff40";
    document.getElementById("wsobserve_status").textContent = "ObserveProto websocket connection opened ";
    //    document.getElementById("box").style.display = "none";
  } 

  socket_observe.onmessage = function got_packet(msg) {
    document.getElementById("wsobserve_lastmsg").textContent = msg.data;

    var msgs = new Array();
    var idx = 0;
    msgs[idx] = '';
    var c = 0;
    var instring = false;
    for(c = 0; c < msg.data.length; c++) {
      if(msg.data[c] == '"' && c > 0 && msg.data[c - 1] != '\\') instring = !instring;
      if(msg.data[c] == ';' && instring == false) {
        idx++;
        msgs[idx] = '';
      }
      if(msg.data[c] != ';' || instring == true) msgs[idx] += msg.data[c];
    }

    f = 0;
    while (f < msgs.length - 1) {
      var msg = new Array();
      instring = false;
      idx = 0;
      msg[idx] = '';
      for(c = 0; c < msgs[f].length; c++) {
        if(msgs[f][c] == '"' && c > 0 && msgs[f][c - 1] != '\\') {
          instring = !instring;
          continue;
        }
        if(msgs[f][c] == ' ' && instring == false) {
          msg[idx] = msg[idx].replace("\\\\","\\").replace("\\\"","\"");
          idx++;
          msg[idx] = '';
        }
        if(msgs[f][c] != ' ' || instring == true) msg[idx] += msgs[f][c];
      }

      var cmd = msg[0];

      if(cmd == "del") {
        var id = msg[1];
        var task = document.getElementById("task_" + id);
        //todo: remove from parent not board
        board.removeChild(task);
      }
      else if(cmd == "move") {
        var id = msg[1];
	      var parent_id = msg[2];
//        var left = msg[2];
//        var top = msg[3];
        var task = document.getElementById("task_" + id);
        if(parent_id != -1) {
          var parent_observe = document.getElementById("task_" + parent_id);
	        parent_observe.appendChild(task);
        }
        else {
          board.appendChild(task);
        }
//	task.parentNode.removeChild(task);
//	board.removeChild(task);
//	parent_observe.appendChild(task);
//        task.style.left = left + "px";
//        task.style.top = top + "px";
      }
      else if(cmd == "add") {
        var id = msg[1];
        var title = msg[2];
        var description = msg[3];
	var parent_id = msg[4];

        var task = document.createElement("div");
        task.name = "task";
        task.setAttribute("class", "task");
        task.setAttribute("ondrop", "drop(this, event)");
        task.setAttribute("ondragenter", "return dragenter(this)");
        task.setAttribute("ondragleave", "return dragleave(this)");
        task.setAttribute("ondragover", "return false");
        task.setAttribute("draggable", true);
        task.setAttribute("ondragstart", "drag(this, event)");

	task.id = "task_" + id;

        var taskText = document.createTextNode(title + ": " + description + " :" + task.id);
        taskText.id = "txt";
	task.appendChild(taskText);
	
//        task.style.position = "absolute";
//        task.style.left = left + "px";
//        task.style.top = top + "px"; 
//        task.setAttribute("onMouseDown", "dragged = '" + id + "';");

//        var dlButton = document.createElement("input");
//        dlButton.type = "button";
//        dlButton.value = "Remove";
//        dlButton.onclick = "deleteTask(document.getElementById(" + task.id + ").id";
//        dlButton.setAttribute("onclick", "deleteTask(" + id +")");
//        task.appendChild(dlButton);

//        board.appendChild(task);
      	
        var parent_observe = document.getElementById("task_" + parent_id);

	if(parent_observe) {
          parent_observe.appendChild(task);
	}
	else {
          board.appendChild(task);
	}
      }
      else if(cmd == "update") {
        var id = msg[1];
        var title = msg[2];
        var description = msg[3];
       
        var task = document.getElementById("task_" + id);

	var taskText = document.createTextNode(title + ": " + description + " :" + task.id);
	task.appendChild(taskText);
      }
      f++;
//      document.getElementById("box").style.top = i[3] + "px";
//      document.getElementById("box").style.left = i[2] + "px";
//      document.getElementById("box").style.display = "block";
    }
  }

  socket_observe.onclose = function(){
    document.getElementById("wsobserve_status").style.backgroundColor = "#ff4040";
    document.getElementById("wsobserve_status").textContent = "ObserveProto websocket connection CLOSED ";
  }
} catch(exception) {
  alert('<p>Error' + exception + '</p>');
}

//var canvas = document.createElement('canvas');
//canvas.height = 600;
//canvas.width = 800;
//ctx = canvas.getContext("2d");

//document.addEventListener('mousemove', ev_mousemove, false);
//document.addEventListener('mouseup', ev_mouseup, false);
/*
canvas.addEventListener('mousedown', ev_mousedown, false);
//cancas.addEventListener('dragend', ev_mousemove, false);
*/

//offsetX = offsetY = 0;
//element = canvas;
//if (element.offsetParent) {
//  do {
//    offsetX += element.offsetLeft;
//    offsetY += element.offsetTop;
//  } while ((element = element.offsetParent));
//}
 
function update_color() {
  color = document.getElementById("color").value;
}

//function ev_mouseup(ev) {
//  dragged = '';
//}

//function ev_mousemove (ev)
//{
 // if(dragged == '') return;
//
//  var x, y;
//  x = ev.clientX;
//  y = ev.clientY;

//  socket_observe.send("move " + dragged + " " + x + " " + y);
//}

function task_submit() {
  var data = document.getElementById("input_task_data").value;
  socket_task.send(data);
}

function observe_submit() {
  var data = document.getElementById("input_observe_data").value;
  socket_observe.send(data);
}

function clear() {
  document.getElementById("input_data").value = "";
}

function deleteTask(id) {
  socket_task.send("del " + id + ";");
}

function drag(target, e) {
  e.target.style.opacity = '0.4';
  e.dataTransfer.setData('Text', target.id);
  e.stopPropagation(); // <--- this fixes the drag target problem
}

function drop(target, e) {
  this.style.opacity = '1.0';
  e.preventDefault();
  e.stopPropagation();

  var id = e.dataTransfer.getData('Text');
  var task = document.getElementById(id);
//  task.parentNode.removeChild(task);
//  alert("move " + id.substring(5, id.length) + " " + target.id.substring(5,target.id.length) + ";");
  socket_task.send("move " + id.substring(5, id.length) + " " + target.id.substring(5, target.id.length) + ";");
      
//  target.appendChild(task);
}

function dragenter(obj) {
  obj.style.backgroundColor = 'red';
  return false;
}

function dragleave(obj) {
  obj.style.backgroundColor = 'grey';
  return false;
}

function dropInBody(e) {
  e.preventDefault();
  e.stopPropagation();

  var id = e.dataTransfer.getData('Text');
  var task = document.getElementById(id);
//  task.parentNode.removeChild(task);
//  alert("move " + id.substring(5, id.length) + " " + target.id.substring(5,target.id.length) + ";");
  socket_task.send("move " + id.substring(5, id.length) + " -1;");
      
//  target.appendChild(task);
}