-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_node.js
155 lines (134 loc) · 3.25 KB
/
my_node.js
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
/**
* my_node.js Node class.
*/
var My_Node = function(data)
{
this.id;
this.next; // 2 elements max
this.previous;
this.data;
this.rel_position; // from previous node
this.positions;
this.init(data);
};
$.extend(My_Node.prototype,
{
init: function(id)
{
this.id = typeof(id) == 'undefined' ? -1 : id;
this.next = [undefined,undefined];
this.data={'nodes':[], 'name':'', };
this.data={'nodes':[], 'name':'', };
this.positions = ['down','right','inside'];
return this;
},
getId: function()
{
return this.id;
},
setId: function(id)
{
this.id = id;
return this;
},
addNext: function(node,index)
{
if ((index < this.getNext().length) && (index >= 0))
{
this.next[index] = node;
node.setPrevious(this);
}
else
return -1;
return this;
},
removeNext: function(node)
{
index = $.inArray(node,this.next);
//if ((this.getNext().length <= 1) && (index != -1))
if (index != -1)
delete this.next[index];
else
return -1;
return this;
},
getNext: function()
{
return this.next;
},
getRelPosition: function()
{
return this.rel_position;
},
setRelPosition: function(pos)
{
if ($.inArray(pos,this.getPositions()) != -1)
this.rel_position = pos;
return this;
},
getPositions: function()
{
return this.positions;
},
setPrevious: function(node)
{
this.previous = node;
/*
if (typeof(node) == 'undefined')
this.rel_position = undefined;
else
this.rel_position = ($.inArray(this,node.getNext()) == 0) ?
'down':
'right';
*/
if (typeof(node) == 'undefined')
this.rel_position = undefined;
else
switch($.inArray(this,node.getNext()))
{
case 0: this.rel_position = 'down';break;
case 1: this.rel_position = 'right';break;
case -1: this.rel_position = 'inside';break;
}
return this;
},
getPrevious: function()
{
return this.previous;
},
getData: function(i)
{
return (typeof(i) == 'undefined') ? this.data : this.data[i];
},
setData: function(i,v)
{
this.data[i] = v;
return this;
},
getTotalNextNodes: function()
{
var count=0;
$.each(this.getNext(),function(index,value){
if (! $.isEmptyObject(value))
count++;
});
return count;
},
getFirstNext: function()
{
for (i=0;i<this.getNext().length;i++)
if (typeof(this.getNext()[i]) != 'undefined')
return this.getNext()[i];
return;
},
/*
toString: function()
{
var node="Hi, I'm a node, my id is: "+this.id+"\n";
node+="my previous node says: "+this.previous+"\n";
node+="my next node under me says: "+this.previous+"\n";
node+="my next node to the right says: "+this.previous+"\n";
return node;
},
*/
});