summaryrefslogtreecommitdiff
path: root/src/ws/node.js
blob: e9d68b61ee2a7e60c779b23e80cb4ccb040b6c58 (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
/* -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set et sw=2 ts=2: */

function createId(boardid, nodeid)
{
	return "b" + boardid + "_t" + nodeid;
}

function idFromStr(str)
{
	return str.substring(str.search('t') + 1, str.length);
}

var nodes = new Array();

function findNode(id, subscribeid)
{
	for(var i = 0; i < nodes.length; i++)
	{
		var node = nodes[i];
		var child = node.findNode(id, subscribeid);
		if(child != null)
		{
			return child;
		}
	}

	return null;
}

function Node(id, subscribeid)
{
	this.id = id;
	this.subscribeid = subscribeid;
	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");
}

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

Node.prototype.findNode = function(id, subscribeid)
{
	if(this.subscribeid != subscribeid)
	{
		return null;
	}

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

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

	return null;
};

Node.prototype.addChild = function(node, insertBeforeId)
{
	if(node.parent != null)
	{
		node.parent.removeChild(node);
	}

	node.parent = this;

	inserted = false;
	for(i = 0; i < this.children.length; ++i)
	{
		if(this.children[i].id == insertBeforeId)
		{
			this.children.splice(i - 1, 0, node);
			this.element.insertBefore(node.element, this.element.childNodes[i + 2]);
			inserted = true;
			break;
		}
	}

	if(inserted == false)
	{
		this.children.push(node);
		this.element.appendChild(node.element);
	}
};

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

Node.prototype.create = function()
{
	var node = this.element;

	node.name = "node";
	node.setAttribute("class", "node");
	node.setAttribute("ondblclick", "editTitle(this, event)");
	//node.setAttribute("onclick", "showHideChildren(this, event)");
	node.setAttribute("ondrop", "drop(this, event)");
	node.setAttribute("ondragover", "return false");
	node.setAttribute("draggable", true);
	node.setAttribute("ondragstart", "drag(this, event)");
	node.setAttribute("ondragend", "dragEnd(event)");
	node.setAttribute("nodeid", this.id);

	// This is a hack to make it possible to identify the nodeid and
	// oberveid from the node id alone.
	node.id = createId(this.subscribeid, this.id);

/*
	var subscribe_button = document.createElement("div");
	subscribe_button.name = "subscribe_button";
	subscribe_button.setAttribute("onclick", "subscribeMe(this, event)");
	subscribe_button.setAttribute("nodeid", this.id);
	subscribe_button.setAttribute("style", "float: left; display: inline-box; width:14px; height: 14px; border: solid green 2px; cursor: pointer;");
	var txt_plus = document.createTextNode("+");
	subscribe_button.appendChild(txt_plus);
	node.appendChild(subscribe_button);

	var unsubscribe_button = document.createElement("div");
	unsubscribe_button.name = "unsubscribe_button";
	unsubscribe_button.setAttribute("onclick", "unsubscribeMe(this, event)");
	unsubscribe_button.setAttribute("nodeid", this.id);
	unsubscribe_button.setAttribute("style", "float: left; display: inline-box; width:14px; height: 14px; border: solid red 2px; cursor: pointer;");
	var txt_minus = document.createTextNode("-");
	unsubscribe_button.appendChild(txt_minus);
	node.appendChild(unsubscribe_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)");
};

Node.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);
	}

	if(name == "dragged")
	{
		if(value == "true")
		{
			// Make transparent and non-draggable
			this.element.style.opacity = "0.3";
			// TODO: Apply recursively?
			this.element.setAttribute("draggable", false);
		}
		else
		{
			// Make opaque and draggable again
			this.element.style.opacity = "1.0";
			// TODO: Apply recursively?
			this.element.setAttribute("draggable", true);
		}
	}
};