summaryrefslogtreecommitdiff
path: root/munia.html
blob: dfe59e7406367a41da961b42a77d7267edb4f5d9 (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
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Munia</title>
</head>
<body id="body">
<select id="color" onchange="update_color();">
  <option value="#000000">Black</option>
  <option value="#0000ff">Blue</option>
  <option value="#20ff20">Green</option>
  <option value="#802020">Dark Red</option>
</select>
<div id="wslm_status">Not initialized</div>
<div id="wslm_lastmsg"></div>
<div id="wslm_drawing"></div>
<div id="box" style="position: absolute;">Hello World</div>
<input type="text" id="input_data" value="Do magic here" onfocus="clear()"/>
<input type="button" value="submit" onclick="submit()"/>
</div>
<script>
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" }
  ]

};
</script>
<script>
//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-mirror protocol */

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

// alert(BrowserDetect.browser);
if (BrowserDetect.browser == "Firefox") {
  socket_lm = new MozWebSocket(get_appropriate_ws_url(), "lws-mirror-protocol");
} else {
  socket_lm = new WebSocket(get_appropriate_ws_url(),  "lws-mirror-protocol");
}

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

  socket_lm.onmessage = function got_packet(msg) {
//    alert(msg.data);
//    document.getElementById("wslm_lastmsg").textContent = msg.data;
    
    j = msg.data.split(';');
    f = 0;
    while (f < j.length - 1) {
      i = j[f].split(' ');
      if (i[0] == 'd') {
        ctx.strokeStyle = i[1];
        ctx.beginPath();
        ctx.moveTo(+(i[2]), +(i[3]));
        ctx.lineTo(+(i[4]), +(i[5]));
        ctx.stroke();
      }
      else if (i[0] == 'c') {
        ctx.strokeStyle = i[1];
        ctx.beginPath();
        ctx.arc(+(i[2]), +(i[3]), +(i[4]), 0, Math.PI*2, true); 
        ctx.stroke();
      }
      else if (i[0] == "del") {
//        alert("Deleting task");
        var task = document.getElementById("task_" + i[1]);
//        task.style.display = "none";
        document.body.removeChild(task);
      }
      else if (i[0] == "move") {
//        alert("Moving task");
        var task = document.getElementById("task_" + i[1]);
        task.style.left = i[2] + "px";
        task.style.top = i[3] + "px";
      }
      else if (i[0] == "add") {
        var task = document.createElement("div");
        task.name = "task";
        task.id = "task_" + i[1];

        var taskText = document.createTextNode(i[2] + ": " + i[3] + " :" + task.id);
        task.appendChild(taskText);

        task.style.position = "absolute";
        task.style.left = i[4] + "px";
        task.style.top = i[5] + "px"; 
        task.style.backgroundColor = "grey";
        task.style.borderStyle= "solid";
        task.style.borderWidth= "medium";
        task.style.borderRadius= "15px";
        task.style.padding = "4px";
	task.setAttribute("onMouseDown", "dragged = '" + i[1] + "';");

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

        document.body.appendChild(task);
      }
      f++;
//      document.getElementById("box").style.top = i[3] + "px";
//      document.getElementById("box").style.left = i[2] + "px";
//      document.getElementById("box").style.display = "block";
    }
  }

  socket_lm.onclose = function(){
    document.getElementById("wslm_status").style.backgroundColor = "#ff4040";
    document.getElementById("wslm_status").textContent = " 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.getElementById('wslm_drawing').appendChild(canvas);

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_lm.send("move " + dragged + " " + x + " " + y);
}

function submit() {
  var data = document.getElementById("input_data").value;
  alert(data);
  socket_lm.send(data);
}

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

function deleteTask(id) {
//  alert(id);
  socket_lm.send("del " + id + ";");
}

</script>

</body>
</html>