diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2012-05-05 10:42:47 +0200 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2012-05-05 10:42:47 +0200 |
commit | 7da71469707ae9394e457f1d212b192aa3970707 (patch) | |
tree | 1b62b9544081986e21d09961cdc1ab60f958ee40 | |
parent | f4b130c25db123b28594a0f4abe739635e507faa (diff) |
Update javascript to wrk with the new protocol.
-rw-r--r-- | munia.html | 5 | ||||
-rw-r--r-- | proto.js | 607 |
2 files changed, 307 insertions, 305 deletions
@@ -51,12 +51,9 @@ body { <body id="body"> <div id="wstask_status">TaskProto not initialized</div> <div id="wsobserve_status">ObserveProto not initialized</div> -TaskProto: <input type="text" id="input_task_data" value="add title description -1" onfocus="clear()"/> +TaskProto: <input type="text" id="input_task_data" value="observe 0" onfocus="clear()"/> <input type="button" value="submit" onclick="task_submit()"/><br/> -ObserveProto: <input type="text" id="input_observe_data" value="observe 0" onfocus="clear()"/> -<input type="button" value="submit" onclick="observe_submit()"/><br/> TaskMessages: <span id="wstask_lastmsg"></span><br/> -ObserveMessages: <span id="wsobserve_lastmsg"></span> </div> <div id="board" class="board"></div> <script type="text/javascript" charset="utf-8" src="/proto.js"></script> @@ -64,23 +64,23 @@ 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); - } + /* + * 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('/'); + u = u.split('/'); - return pcol + u[0]; + return pcol + u[0]; } @@ -105,293 +105,298 @@ 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"); + 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"); + 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++; + 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] = ''; - } - 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; + 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]; } - 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); + f = 0; + while (f < msgs.length - 1) { + var msg = new Array(); + instring = false; + idx = 0; + + // strip padding and trailing whitespace. + var msgstr = msgs[f].replace(/^\s+||\s+$/g,''); + + msg[idx] = ''; + for(c = 0; c < msgstr.length; c++) { + if(msgstr[c] == '"' && c > 0 && msgstr[c - 1] != '\\') { + instring = !instring; + continue; + } + if(msgstr[c] == ' ' && instring == false) { + msg[idx] = msg[idx].replace("\\\\","\\").replace("\\\"","\""); + idx++; + msg[idx] = ''; + } + if(msgstr[c] != ' ' || instring == true) msg[idx] += msgstr[c]; + } + + var observeid = msg[0]; + var cmd = msg[1]; + + if(cmd == "del") { + var id = msg[2]; + var task = document.getElementById("task_" + id); + //todo: remove from parent not board + board.removeChild(task); + } + else if(cmd == "move") { + var id = msg[2]; + var parent_id = 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 == "create") { + var id = msg[2]; + var parent_id = msg[3]; + // var title = msg[3]; + // var description = 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); + var taskText = document.createTextNode('[' + 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[2]; + var title = msg[3]; + var description = 'N/A';//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"; } - 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 "; - } + 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>'); + alert('<p>Error' + exception + '</p>'); } /* -try { + 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"; + 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]; - } + 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]; - } + 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]; + 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]; + 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)"); + 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"; + 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); + // 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); + parent_observe.appendChild(task); } else { - board.appendChild(task); + 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); + } + 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"; - } + } + 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 "; + document.getElementById("wsobserve_status").style.backgroundColor = "#ff4040"; + document.getElementById("wsobserve_status").textContent = "ObserveProto websocket connection CLOSED "; } -} catch(exception) { + } catch(exception) { alert('<p>Error' + exception + '</p>'); -} + } */ //var canvas = document.createElement('canvas'); @@ -402,9 +407,9 @@ try { //document.addEventListener('mousemove', ev_mousemove, false); //document.addEventListener('mouseup', ev_mouseup, false); /* -canvas.addEventListener('mousedown', ev_mousedown, false); -//cancas.addEventListener('dragend', ev_mousemove, false); -*/ + canvas.addEventListener('mousedown', ev_mousedown, false); + //cancas.addEventListener('dragend', ev_mousemove, false); + */ //offsetX = offsetY = 0; //element = canvas; @@ -414,9 +419,9 @@ canvas.addEventListener('mousedown', ev_mousedown, false); // offsetY += element.offsetTop; // } while ((element = element.offsetParent)); //} - + function update_color() { - color = document.getElementById("color").value; + color = document.getElementById("color").value; } //function ev_mouseup(ev) { @@ -425,7 +430,7 @@ function update_color() { //function ev_mousemove (ev) //{ - // if(dragged == '') return; +// if(dragged == '') return; // // var x, y; // x = ev.clientX; @@ -435,63 +440,63 @@ function update_color() { //} function task_submit() { - var data = document.getElementById("input_task_data").value; - socket_task.send(data); + var data = document.getElementById("input_task_data").value; + socket_task.send(data); } /* -function observe_submit() { + function observe_submit() { var data = document.getElementById("input_observe_data").value; socket_observe.send(data); -} + } */ function clear() { - document.getElementById("input_data").value = ""; + document.getElementById("input_data").value = ""; } function deleteTask(id) { - socket_task.send("del " + 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 + 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); + 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; + obj.style.backgroundColor = 'red'; + return false; } function dragleave(obj) { - obj.style.backgroundColor = 'grey'; - return false; + 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); + 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); } |