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


document.addEventListener("connectEvent", connectEventHandler, false);
function connectEventHandler(e)
{
	document.getElementById("wsnode_status").style.backgroundColor = "#40ff40";
	document.getElementById("wsnode_status").textContent = "NodeProto websocket connection opened ";

	//login("foobar", "hundemad");
	//subscribe(0);
}

document.addEventListener("disconnectEvent", disconnectEventHandler, false);
function disconnectEventHandler(e)
{
	document.getElementById("wsnode_status").style.backgroundColor = "#ff4040";
	document.getElementById("wsnode_status").textContent = "NodeProto websocket connection CLOSED ";
}

document.addEventListener("removeEvent", removeEventHandler, false);
function removeEventHandler(e)
{
	var subscribeid = e.detail.subscribeid;
	var id = e.detail.id;

	var node = findNode(id, subscribeid);
	if(node.parent)
	{
		node.parent.removeChild(node);
	}
}

document.addEventListener("moveEvent", moveEventHandler, false);
function moveEventHandler(e)
{
	var subscribeid = e.detail.subscribeid;
	var id = e.detail.id;
	var parentid = e.detail.parentid;
	var insertBeforeId = e.detail.insertBeforeId;

	var node = findNode(id, subscribeid);
	var new_parent = findNode(parentid, subscribeid);
	if(node != null && new_parent != null)
	{
		new_parent.addChild(node, insertBeforeId);
	}
}

document.addEventListener("createEvent", createEventHandler, false);
function createEventHandler(e)
{
	var subscribeid = e.detail.subscribeid;
	var id = e.detail.id;
	var parentid = e.detail.parentid;
	var insertBeforeId = e.detail.insertBeforeId;

	var node = new Node(id, subscribeid);
	node.create();

	if(parentid == -1)
	{
		nodes.push(node);
		var board = getBoard(subscribeid);
		board.appendChild(node.element);
		return;
	}

	var parent = findNode(parentid, subscribeid);
	if(parent != null)
	{
		parent.addChild(node, insertBeforeId);
	}
}

document.addEventListener("updateEvent", updateEventHandler, false);
function updateEventHandler(e)
{
	var subscribeid = e.detail.subscribeid;
	var id = e.detail.id;
	var name = e.detail.name;
	var value = e.detail.value;

	var node = findNode(id, subscribeid);
	if(node == null)
	{
		return;
	}

	node.setAttribute(name, value);
}

///////
//////
/////
////
///
//

document.addEventListener("messageEvent", messageEventHandler, false);
function messageEventHandler(e)
{
	LogEvent(e.detail.time.toString()+": "+e.detail.message);
}

// log event in console
function LogEvent(msg)
{
	var log = document.getElementById("log");
	log.textContent += msg + "\n";
	var ot = log.scrollHeight - log.clientHeight;
	if(ot > 0)
	{
		log.scrollTop = ot;
	}
}