summaryrefslogtreecommitdiff
path: root/task.js
blob: 03672211c2e031d4cdc7f0ef7a27cc10c9199261 (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
/* -*- 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)
{
    for(var i = 0; i < tasks.length; i++) {
	var task = tasks[i];
	var child = task.findTask(id, observeid);
	if(child != null) return child;
    }

    return null;
}

function Task(id, observeid)
{
    this.id = id;
    this.observeid = observeid;
    this.children = new Array();
    this.attributes = {};
    this.parent = null;

    // Elements:
    this.element = document.createElement("div");
    this.div_id = document.createElement("span");
    this.div_title = document.createElement("span");
}

Task.prototype.dump = function()
{
    alert(this.id);
}

Task.prototype.findTask = function(id, observeid)
{
    if(this.observeid != observeid) return null;

    if(this.id == id) return this;

    for(var i = 0; i < this.children.length; i++) {
	var task = this.children[i];
	var child = task.findTask(id, observeid);
	if(child != null) return child;
    }

    return null;
}
    
Task.prototype.addChild = function(task)
{
    if(task.parent != null) task.parent.removeChild(task);
    this.children.push(task);
    task.parent = this;
    this.element.appendChild(task.element);
}

Task.prototype.removeChild = function(task)
{
    this.children = this.children.filter(
	function(e) {
	    return e.id != task.id;
	});
    task.parent = null;
    this.element.removeChild(task.element);
}

Task.prototype.create = function()
{
    var task = this.element;

    task.name = "task";
    task.setAttribute("class", "task");
    task.setAttribute("ondblclick", "editTitle(this, event)");
    //task.setAttribute("onclick", "showHideChildren(this, event)");
    task.setAttribute("ondrop", "drop(this, event)");
    task.setAttribute("ondragover", "return false");
    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";
    observe_button.setAttribute("onclick", "observeMe(this, event)");
    observe_button.setAttribute("title", this.id);
    observe_button.setAttribute("style", "float: left; display: inline-box; width:14px; height: 14px; border: solid green 2px; cursor: pointer;");
    var txt_plus = document.createTextNode("+");
    observe_button.appendChild(txt_plus);
    task.appendChild(observe_button);
    
    var unobserve_button = document.createElement("div");
    unobserve_button.name = "unobserve_button";
    unobserve_button.setAttribute("onclick", "unobserveMe(this, event)");
    unobserve_button.setAttribute("title", this.id);
    unobserve_button.setAttribute("style", "float: left; display: inline-box; width:14px; height: 14px; border: solid red 2px; cursor: pointer;");
    var txt_minus = document.createTextNode("-");
    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);

    this.element.appendChild(this.div_title);

    this.setAttribute("title", "(missing title)");
}

Task.prototype.setAttribute = function(name, value)
{
    this.attributes[name] = value;
    
    if(name == "title") {
	if(this.div_title.firstChild != null) {
	    this.div_title.removeChild(this.div_title.firstChild);
	}
	var title_txt = document.createTextNode(value);
	this.div_title.appendChild(title_txt);
    }
}