From 39396a2660150770c3fd14aac8b4be0dcbae8497 Mon Sep 17 00:00:00 2001 From: Jonas Suhr Christensen Date: Sat, 10 Mar 2012 14:56:34 +0100 Subject: Added new task hierarchy based protocol. Drag and drop works somehow - maybe some weirdness in html5 drag and drop. --- munia.html | 2 +- proto.js | 127 ++++++++++++++++++++++++++++++++++++------------------ src/msgparser.cc | 36 ++++++++++------ src/msgparser.h | 9 ++-- src/muniad.cc | 9 ++-- src/task.cc | 7 +-- src/task.h | 31 ++++++++++++- src/task_proto.cc | 18 +++++--- 8 files changed, 161 insertions(+), 78 deletions(-) diff --git a/munia.html b/munia.html index 2dc3099..2c736df 100644 --- a/munia.html +++ b/munia.html @@ -31,7 +31,7 @@ background-color:yellow;
Not initialized
- +
diff --git a/proto.js b/proto.js index c57e133..5d855b0 100644 --- a/proto.js +++ b/proto.js @@ -150,44 +150,67 @@ try { if(cmd == "del") { var id = msg[1]; var task = document.getElementById("task_" + id); - document.body.removeChild(task); + //todo: remove from parent not body + document.body.removeChild(task); } else if(cmd == "move") { var id = msg[1]; - var left = msg[2]; - var top = msg[3]; + var parent_id = msg[2]; +// var left = msg[2]; +// var top = msg[3]; var task = document.getElementById("task_" + id); - task.style.left = left + "px"; - task.style.top = top + "px"; + var parent_task = document.getElementById("task_" + parent_id); +// task.parentNode.removeChild(task); + parent_task.appendChild(task); +// document.body.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 left = msg[4]; - var top = msg[5]; + var parent_id = msg[4]; +// var left = msg[4]; +// var top = msg[5]; var task = document.createElement("div"); task.name = "task"; task.setAttribute("class", "task"); - task.id = "task_" + id; + task.setAttribute("ondrop", "drop(this, event)"); + task.setAttribute("ondragenter", "return false"); + 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); task.appendChild(taskText); - task.style.position = "absolute"; - task.style.left = left + "px"; - task.style.top = top + "px"; - task.setAttribute("onMouseDown", "dragged = '" + 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"; +// 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); +// dlButton.setAttribute("onclick", "deleteTask(" + id +")"); +// task.appendChild(dlButton); document.body.appendChild(task); + + var parent_task = document.getElementById("task_" + parent_id); + + if(parent_task) { + parent_task.appendChild(task); + } + else { + document.body.appendChild(task); + } } f++; // document.getElementById("box").style.top = i[3] + "px"; @@ -204,45 +227,45 @@ try { alert('

Error' + exception + '

'); } -var canvas = document.createElement('canvas'); -canvas.height = 600; -canvas.width = 800; -ctx = canvas.getContext("2d"); +//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); +//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)); -} +//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; +//function ev_mouseup(ev) { +// dragged = ''; +//} - var x, y; - x = ev.clientX; - y = ev.clientY; +//function ev_mousemove (ev) +//{ + // if(dragged == '') return; +// +// var x, y; +// x = ev.clientX; +// y = ev.clientY; - socket_lm.send("move " + dragged + " " + x + " " + y); -} +// socket_lm.send("move " + dragged + " " + x + " " + y); +//} function submit() { var data = document.getElementById("input_data").value; @@ -256,3 +279,21 @@ function clear() { function deleteTask(id) { socket_lm.send("del " + id + ";"); } + +function drag(target, e) { + e.dataTransfer.setData('Text', target.id); +} + +function drop(target, 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_lm.send("move " + id.substring(5, id.length) + " " + target.id.substring(5, target.id.length) + ";"); + +// target.appendChild(task); +} + diff --git a/src/msgparser.cc b/src/msgparser.cc index 1ea2f31..f3bafe1 100644 --- a/src/msgparser.cc +++ b/src/msgparser.cc @@ -32,6 +32,7 @@ #include #include +/* inline void parse_add(msg_t& m, std::string data) { int offset = 0; std::string title = data.substr(offset, data.find(' ', offset) - offset); @@ -73,6 +74,7 @@ inline void parse_move(msg_t& m, std::string data) { m.move.x = x; m.move.y = y; } +*/ MsgVector parse_msg(std::string data) { @@ -165,14 +167,15 @@ MsgVector parse_msg(std::string data) { switch(m.cmd) { case cmd::add: { - if(t.size() != 4+1) { + if(t.size() != 3+1) { printf("Wrong number of parameters\n"); continue; } sprintf(m.add.title, "%s", t[1].c_str()); sprintf(m.add.desc, "%s", t[2].c_str()); - m.add.x = atoi(t[3].c_str()); - m.add.y = atoi(t[4].c_str()); + m.add.parent_id = atoi(t[3].c_str()); +// m.add.x = atoi(t[3].c_str()); +// m.add.y = atoi(t[4].c_str()); // printf("addcmd: %s %s %d %d\n", m.add.title, m.add.desc, m.add.x, m.add.y); break; } @@ -185,13 +188,14 @@ MsgVector parse_msg(std::string data) { break; } case cmd::move: { - if(t.size() != 3+1) { + if(t.size() != 2+1) { printf("Wrong number of parameters\n"); continue; } m.move.id = atoi(t[1].c_str()); - m.move.x = atoi(t[2].c_str()); - m.move.y = atoi(t[3].c_str()); + m.move.parent_id = atoi(t[2].c_str()); +// m.move.x = atoi(t[2].c_str()); +// m.move.y = atoi(t[3].c_str()); break; } case cmd::update: { @@ -217,8 +221,9 @@ msg_t create_msg(cmd::cmd_t type, task_t t) { switch(type) { case cmd::add: { m.add.id = t.id; - m.add.x = t.x; - m.add.y = t.y; + m.add.parent_id = t.parent_id; +// m.add.x = t.x; +// m.add.y = t.y; // m.add.title = t.title.c_str(); // m.add.desc = t.desc.c_str(); snprintf(m.add.title, sizeof(m.add.title), "%s", t.title.c_str()); @@ -231,8 +236,9 @@ msg_t create_msg(cmd::cmd_t type, task_t t) { } case cmd::move: { m.move.id = t.id; - m.move.x = t.x; - m.move.y = t.y; +// m.move.x = t.x; +// m.move.y = t.y; + m.move.parent_id = t.parent_id; break; } case cmd::update: { @@ -258,10 +264,14 @@ std::string msg_tostring(msg_t m) { case cmd::add: { // printf("msg: %d, %d, %d, %s, %s\n", m.add.id, m.add.x, m.add.y, m.add.title, m.add.desc); // snprintf(buf, BUF_SIZE, "add %d %s %s %d %d;", - asprintf(&buf, "add %d \"%s\" \"%s\" %d %d;", +// asprintf(&buf, "add %d \"%s\" \"%s\" %d %d;", +// m.add.id, +// m.add.title, m.add.desc, +// m.add.x, m.add.y); + asprintf(&buf, "add %d \"%s\" \"%s\" %d;", m.add.id, m.add.title, m.add.desc, - m.add.x, m.add.y); + m.add.parent_id); break; } case cmd::del: { @@ -271,7 +281,7 @@ std::string msg_tostring(msg_t m) { } case cmd::move: { // snprintf(buf, BUF_SIZE, "move %d %d %d;", m.move.id, m.move.x, m.move.y); - asprintf(&buf, "move %d %d %d;", m.move.id, m.move.x, m.move.y); + asprintf(&buf, "move %d %d;", m.move.id, m.move.parent_id); break; } case cmd::update: { diff --git a/src/msgparser.h b/src/msgparser.h index fa853fa..58f68da 100644 --- a/src/msgparser.h +++ b/src/msgparser.h @@ -45,8 +45,9 @@ namespace cmd { typedef struct { int id; - int x; - int y; + int parent_id; +// int x; +// int y; char title[256]; char desc[256]; } add_t; @@ -55,8 +56,7 @@ typedef struct { } del_t; typedef struct { int id; - int x; - int y; + int parent_id; } move_t; typedef struct { int id; @@ -74,7 +74,6 @@ typedef struct msg_t { move_t move; update_t update; }; - } msg_types; diff --git a/src/muniad.cc b/src/muniad.cc index ef76ff1..ab69456 100644 --- a/src/muniad.cc +++ b/src/muniad.cc @@ -82,9 +82,10 @@ int main(int argc, char **argv) char interface_name[128] = ""; const char * interface = NULL; - fprintf(stderr, "libwebsockets test server\n" - "(C) Copyright 2010-2011 Andy Green " - "licensed under LGPL2.1\n"); + fprintf(stderr, "Munia test server\n" + "(C) Copyright 2012 Bent Bisballe Nyeng (deva@aasimon.org)\n" + " & Jonas Suhr Christensen (jsc@umbraculum.org)\n" + "Licensed under LGPL2.1\n"); while(n >= 0) { n = getopt_long(argc, argv, "ci:khsp:", options, NULL); @@ -105,7 +106,7 @@ int main(int argc, char **argv) interface = interface_name; break; case 'h': - fprintf(stderr, "Usage: test-server [--port=

] [--ssl]\n"); + fprintf(stderr, "Usage: muniad [--port=

] [--ssl]\n"); exit(1); } } diff --git a/src/task.cc b/src/task.cc index 24e94a1..9940fc6 100644 --- a/src/task.cc +++ b/src/task.cc @@ -35,11 +35,12 @@ int current_id_count() { } task_t create_task(std::string title, std::string desc, - int x, int y) { + /*int x, int y,*/ int parent_id) { task_t t; - t.x = x; - t.y = y; + t.parent_id = parent_id; +// t.x = x; +// t.y = y; t.title = title; t.desc = desc; t.id = id_count; id_count++; diff --git a/src/task.h b/src/task.h index 4365c1c..43541bc 100644 --- a/src/task.h +++ b/src/task.h @@ -32,6 +32,16 @@ #include /* +Task: + id + subtasks +// tags + title + description +// primary_assignment +// secondary_assignment + + Protocol: Server -> client: @@ -51,18 +61,35 @@ x and y are integers as strings id are an integer as a string */ - +/* typedef struct { int x, y; int id; std::string title; std::string desc; } task_t; +*/ +/* +protocol: + add_task title description parent_id + del_task id + update_task id title description + move_task id parent + copy_task id parent +*/ + +typedef struct { + int id; + int parent_id; + std::string title; + std::string desc; +} task_t; + typedef std::list TaskList; extern TaskList tasklist; task_t create_task(std::string title, std::string desc, - int x, int y); + /*int x, int y*/ int parent_id); #endif/*__MUNIA_TASK_H__*/ diff --git a/src/task_proto.cc b/src/task_proto.cc index 4bcc3a8..717ca3e 100644 --- a/src/task_proto.cc +++ b/src/task_proto.cc @@ -184,9 +184,11 @@ int callback_lws_task(struct libwebsocket_context * context, case cmd::add: { printf("Handling add cmd\n"); +// task_t t = create_task(m.add.title, m.add.desc, +// m.add.x, m.add.y); task_t t = create_task(m.add.title, m.add.desc, - m.add.x, m.add.y); - tasklist.push_back(t); + m.add.parent_id); + tasklist.push_back(t); // buf_len = sprintf(buf, "add %d %s %s %d %d;", // t.id, t.title.c_str(), t.desc.c_str(), // t.x, t.y); @@ -202,6 +204,7 @@ int callback_lws_task(struct libwebsocket_context * context, printf("Delete\n"); printf("Deleting task with id %d\n", m.del.id); + // todo: delete all children recursively task_t del_task; bool id_found = false; TaskList::iterator it; @@ -228,21 +231,22 @@ int callback_lws_task(struct libwebsocket_context * context, case cmd::move: { printf("Move\n"); - printf("Moving task with id %d to (%d,%d)\n", m.move.id, m.move.x, m.move.y); + printf("Moving task with id %d to %d\n", m.move.id, m.move.parent_id); bool id_found = false; TaskList::iterator it; - int x = m.move.x / 300 * 300; +// int x = m.move.x / 300 * 300; task_t moved_task; for(it = tasklist.begin(); it != tasklist.end(); it++) { task_t &t = *it; if(t.id == m.move.id) { id_found = true; - t.x = x; - t.y = m.move.y; - moved_task = t; +// t.x = x; +// t.y = m.move.y; + t.parent_id = m.move.parent_id; + moved_task = t; break; } } -- cgit v1.2.3