From 46a8ed79f6afdca1d3e1ccebfb90eb3c1b5feb68 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 5 Dec 2013 15:35:01 +0100 Subject: Major brushup of javascript protocol handler. --- handler.js | 9 +++--- proto.js | 90 ++++++++++++++++++++++++++++++++++++++++++++++-------------- task.js | 16 ++++++++++- view.js | 93 ++++++++++++++++++++++++++++++++------------------------------ 4 files changed, 136 insertions(+), 72 deletions(-) diff --git a/handler.js b/handler.js index 60a6aee..b792df2 100644 --- a/handler.js +++ b/handler.js @@ -7,7 +7,9 @@ function connectEventHandler(e) { document.getElementById("wstask_status").style.backgroundColor = "#40ff40"; document.getElementById("wstask_status").textContent = "TaskProto websocket connection opened "; - socket_task.send("observe 0;"); + + // login("foobar", "hundemad"); +// observe(0); } document.addEventListener("disconnectEvent", disconnectEventHandler, false); @@ -75,8 +77,6 @@ function updateEventHandler(e) if(task == null) return; task.setAttribute(name, value); - - //LogEvent("update handled"); } /////// @@ -88,8 +88,7 @@ function updateEventHandler(e) document.addEventListener("messageEvent", messageEventHandler, false); function messageEventHandler(e) { - LogEvent( - "Event: " + e.detail.time.toLocaleString()+": "+e.detail.message + LogEvent(e.detail.time.toString()+": "+e.detail.message ); } diff --git a/proto.js b/proto.js index 74fa3ab..73d9894 100644 --- a/proto.js +++ b/proto.js @@ -3,26 +3,16 @@ 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]; + // From: https://gist.github.com/2428561 + var parser = document.createElement('a'); + parser.href = document.URL; + + // We open the websocket encrypted if this page came on an + // https:// url itself, otherwise unencrypted + if(parser.protocol == "http:") parser.protocol = "ws:"; + if(parser.protocol == "https:") parser.protocol = "wss:"; + + return parser.href; } var socket_task = new WebSocket(get_appropriate_ws_url(), "lws-task-protocol"); @@ -42,7 +32,7 @@ try { socket_task.onmessage = function got_packet(msg) { var messageEvent = new CustomEvent("messageEvent", { detail: { - message: msg.data, + message: "recv [" + msg.data + "]", time: new Date(), }, bubbles: true, @@ -162,3 +152,61 @@ try { alert('

Error' + exception + '

'); } +function transmit(msg) +{ + //LogEvent(msg); + var messageEvent = new CustomEvent("messageEvent", { + detail: { + message: "send [" + msg + "]", + time: new Date(), + }, + bubbles: true, + cancelable: true + }); + document.dispatchEvent(messageEvent); + + socket_task.send(msg); +} + +function login(user, password) +{ + transmit("login "+user+" "+password); +} + +function logout() +{ + transmit("logout"); +} + +function observe(id) +{ + transmit("observe "+id); +} + +function unobserve(id) +{ + transmit("unobserve "+id); +} + +function create(id, parent) +{ + transmit("create "+id+" "+parent); +} + + +function update(id, name, value) +{ + transmit("update "+id+" "+name+" "+value); +} + +function remove(id) +{ + transmit("remove "+id); +} + +function move(id, parent) +{ + transmit("move "+id+" "+parent); +} + + diff --git a/task.js b/task.js index ac9757f..0367221 100644 --- a/task.js +++ b/task.js @@ -1,6 +1,16 @@ /* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set et sw=2 ts=2: */ +function createId(boardid, taskid) +{ + return "b" + boardid + "_t" + taskid; +} + +function idFromStr(str) +{ + return str.substring(str.search('t') + 1, str.length); +} + var tasks = new Array(); function findTask(id, observeid) @@ -79,8 +89,11 @@ Task.prototype.create = function() task.setAttribute("draggable", true); task.setAttribute("ondragstart", "drag(this, event)"); task.setAttribute("title", this.id); - + + // This is a hack to make it possible to identify the taskid and + // oberveid from the node id alone. task.id = createId(this.observeid, this.id); + /* var observe_button = document.createElement("div"); observe_button.name = "observe_button"; @@ -100,6 +113,7 @@ Task.prototype.create = function() unobserve_button.appendChild(txt_minus); task.appendChild(unobserve_button); */ + this.element.appendChild(this.div_id); var id_txt = document.createTextNode(this.id); this.div_id.appendChild(id_txt); diff --git a/view.js b/view.js index 74e91ff..8bb9f87 100644 --- a/view.js +++ b/view.js @@ -12,16 +12,6 @@ function createTask() } */ -function createId(boardid, taskid) -{ - return "b" + boardid + "_t" + taskid; -} - -function idFromStr(str) -{ - return str.substring(str.search('t') + 1, str.length); -} - function getTask(observeid, id) { @@ -53,7 +43,7 @@ function clear() { } function deleteTask(id) { - socket_task.send("remove " + id + ";"); + remove(id); } function drag(target, e) { @@ -67,19 +57,19 @@ function drop(target, e) { var id = e.dataTransfer.getData('Text'); var task = document.getElementById(id); - socket_task.send("move " + idFromStr(id) + " " + idFromStr(target.id) + ";"); + move(idFromStr(id), idFromStr(target.id)); } function observeMe(target, e) { - e.stopPropagation(); - socket_task.send("observe "+target.title+";"); + e.stopPropagation(); + observe(target.title); } function unobserveMe(target, e) { - e.stopPropagation(); - socket_task.send("unobserve "+target.title+";"); + e.stopPropagation(); + unobserve(target.title); } function showHideChildren(target, e) @@ -99,6 +89,19 @@ function showHideChildren(target, e) } } +function task_submit() { + var data = document.getElementById("input_task_data"); + transmit(data.value); + data.value = ""; +} + +function task_submit_KeyUpHandler(target, e) +{ + if(e.which == 13) { // enter + task_submit(); + } +} + // // Butt ugly.. but hey! it works... // @@ -108,41 +111,41 @@ var oldtxt; var oldtitle; function onKeyUpHandler(target, e) { - if(e.which == 13) { // enter - divtxt.removeChild(target); - oldtxt.nodeValue = 'updating...'; - socket_task.send("update " + updateid + " \""+target.value+"\";"); - } - if(e.which == 27) { // escape - divtxt.removeChild(target); - oldtxt.nodeValue = oldtitle; - } + if(e.which == 13) { // enter + divtxt.removeChild(target); + oldtxt.nodeValue = 'updating...'; + update(updateid, "title", target.value); + } + if(e.which == 27) { // escape + divtxt.removeChild(target); + oldtxt.nodeValue = oldtitle; + } } function onLostFocusHandler(target, e) { - if(target.value == oldtitle) { - divtxt.removeChild(target); - oldtxt.nodeValue = oldtitle; - } + if(target.value == oldtitle) { + divtxt.removeChild(target); + oldtxt.nodeValue = oldtitle; + } } function editTitle(target, e) { - e.stopPropagation(); - updateid = idFromStr(target.id); - if(updateid < 10) return; - var inp = document.createElement("input"); - var txtdiv = document.getElementById(target.id + "_txt"); - divtxt = txtdiv; - oldtxt = txtdiv.firstChild; - oldtitle = oldtxt.nodeValue; - oldtxt.nodeValue = ""; - inp.setAttribute("onkeyup", "onKeyUpHandler(this, event)"); - inp.setAttribute("onblur", "onLostFocusHandler(this, event)"); - inp.setAttribute("style", "border: inherit; padding: inherit; margin: inherit; background: inherit;"); - inp.value = oldtitle; - lineedit = inp; - txtdiv.appendChild(inp); - inp.focus(); + e.stopPropagation(); + updateid = idFromStr(target.id); + if(updateid < 10) return; + var inp = document.createElement("input"); + var txtdiv = document.getElementById(target.id + "_txt"); + divtxt = txtdiv; + oldtxt = txtdiv.firstChild; + oldtitle = oldtxt.nodeValue; + oldtxt.nodeValue = ""; + inp.setAttribute("onkeyup", "onKeyUpHandler(this, event)"); + inp.setAttribute("onblur", "onLostFocusHandler(this, event)"); + inp.setAttribute("style", "border: inherit; padding: inherit; margin: inherit; background: inherit;"); + inp.value = oldtitle; + lineedit = inp; + txtdiv.appendChild(inp); + inp.focus(); } -- cgit v1.2.3