-
Notifications
You must be signed in to change notification settings - Fork 6
/
TreeItem.js
205 lines (176 loc) · 5.12 KB
/
TreeItem.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
var _TREE_ITEM_ID_MIN = 100;
var _TREE_ITEM_ID_INVALID = -1;
var _TREE_ITEM_ID_CURRENT = _TREE_ITEM_ID_MIN;
// ENUM: Selection Flags
var SF_None = 0;
var SF_Current = 1;
var SF_Selected = 2;
function TreeItem(text, icon, parent){
var __allocateID = function(){
return (_TREE_ITEM_ID_CURRENT++);
};
this.itemID = __allocateID();
this.displayText = text;
this.displayIcon = icon;
this.expanded = false;
this.selectionFlag = SF_None;
this.parentNode = null;
this.subNodes = [];
if(parent){
parent.appendChild(this);
}
}
TreeItem.prototype = {
constructor: TreeItem
, __getItemFromModel: function(){
var item = this, vPath = [];
while(item){
vPath.splice(0, 0, item.itemID);
item = item.parentNode;
}
var model = listModel;
for(var i = 0; i < vPath.length; i++){
var bFound = false;
for(var j = 0; j < model.count; j++){
var node = model.get(j);
if(vPath[i] === node.itemID){
if(i == vPath.length - 1){
return node;
}else{
model = node.subNodes;
bFound = true;
break;
}
}
}
if(!bFound){
// print("Error - Cannot follow the path")
return null;
}
}
}
, level: function(){
var level = 0, item = this;
while(item.parentNode){
item = item.parentNode
level++;
}
return level;
}
, appendChild: function(item){
if(item.parentNode){
item.parentNode.removeChild(item);
}
item.parentNode = this;
this.subNodes.push(item);
var node = this.__getItemFromModel();
if(node){
node.subNodes.append(item);
}
}
, insertChild: function(pos, item){
if(item.parentNode){
item.parentNode.removeChild(item);
}
item.parentNode = this;
this.subNodes.splice(pos, 0, item);
var node = this.__getItemFromModel();
if(node){
node.subNodes.insert(pos, item);
}
}
, removeChild: function(item){
var i = this.subNodes.indexOf(item);
this.subNodes.splice(i, 1);
item.parentNode = null;
var currentSelectionFlag = item.getSelectionFlag();
// 2018.2.13 Clear the flag of the selection when removing the current item
item.setSelectionFlag(SF_None);
// 2018.2.14 Select the next current item after removing the current item
if(currentSelectionFlag === SF_Current){
if(this.subNodes.length > 0){
var nextCurrent = i;
if(nextCurrent >= this.subNodes.length){
nextCurrent = this.subNodes.length - 1;
}
this.subNodes[nextCurrent].setSelectionFlag(SF_Current);
}else{
if(this.itemID !== rootItem.itemID) this.setSelectionFlag(SF_Current);
}
}
var node = this.__getItemFromModel();
if(node){
node.subNodes.remove(i);
}
}
, childItem: function(index){
return this.subNodes[index];
}
, indexOfChildItem: function(item){
for(var i = 0; i < this.subNodes.length; i++){
if(this.subNodes[i].itemID === item.itemID){
return i;
}
}
return -1;
}
, childernCount: function(recursive){
if(!recursive) recursive = false
var count = this.subNodes.length;
if(recursive){
for(var i = 0; i < this.subNodes.length; i++){
count += this.subNodes[i].childernCount(recursive);
}
}
return count;
}, parent: function(){
return this.parentNode;
}
, setText: function(text){
this.displayText = text;
var node = this.__getItemFromModel();
if(node){
node.displayText = text;
}
}
, text: function(){
return this.displayText;
}
, setIcon: function(source){
this.displayIcon = source;
var node = this.__getItemFromModel();
if(node){
node.displayIcon = source;
}
}
, icon: function(){
return this.displayIcon;
}
, setExpanded: function(expanded){
this.expanded = expanded;
var node = this.__getItemFromModel();
if(node){
node.expanded = expanded;
}
}
, isExpanded: function(){
return this.expanded;
}
, setSelectionFlag: function(flag){
this.selectionFlag = flag;
if(flag === SF_Current){
currentItem = this;
}else{
if(currentItem.itemID === this.itemID){
currentItem = null;
}
}
var node = this.__getItemFromModel();
if(node){
node.selectionFlag = flag;
}
}
, getSelectionFlag: function(){
return this.selectionFlag;
}
}