-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtreezModelPath.js
364 lines (281 loc) · 9 KB
/
treezModelPath.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import LabeledTreezElement from './../labeledTreezElement.js';
export default class TreezModelPath extends LabeledTreezElement {
constructor(){
super();
this.__label = undefined;
this.__comboBox = undefined;
this.__atomClasses = undefined; //used to identify allowed atoms by their class
this.__atomFunctionNames = undefined; //used to identify allowed atoms by their functions/interface
this.filterDelegate = undefined;
this.__relativeRootAtom = undefined;
this.__relativeRootLabel = undefined;
}
connectedCallback() {
if(!this.__label){
this.className = 'treez-model-path';
let label = document.createElement('label');
this.__label = label;
label.innerText = this.label;
label.className = 'treez-model-path-label';
this.appendChild(label);
let container = document.createElement('div');
container.className='treez-model-path-container';
this.appendChild(container);
let relativeRootLabel = document.createElement('span');
this.__relativeRootLabel = relativeRootLabel;
relativeRootLabel.className='treez-model-path-relative-root-label';
relativeRootLabel.style.display='none';
container.appendChild(relativeRootLabel);
let uniqueOptionsId = 'options-' + this.uniqueId();
let comboBox = document.createElement('select');
this.__comboBox = comboBox;
comboBox.onchange = () => this.__comboBoxChanged();
comboBox.className='treez-model-path-select';
container.appendChild(comboBox);
}
this.update();
}
updateElements(newValue){
this.__updateOptionsAndRelativeRoot();
if(this.__comboBox){
if(newValue){
let comboBoxValue = this.convertToStringValue(newValue);
if(this.__comboBox.value !== comboBoxValue){
this.__comboBox.value = comboBoxValue;
}
} else {
if(this.__comboBox.value !== ''){
this.__comboBox.value = '';
}
}
}
}
updateContentWidth(width){
this.updateWidthFor(this.__comboBox, width);
}
disableElements(booleanValue){
if(booleanValue === undefined){
throw Error('This method expects a boolean argument');
}
if(this.__comboBox){
this.__comboBox.disabled = booleanValue;
}
}
hideElements(booleanValue){
if(booleanValue === undefined){
throw Error('This method expects a boolean argument');
}
if(this.__label){
LabeledTreezElement.hide(this.__label, booleanValue);
LabeledTreezElement.hide(this.__comboBox, booleanValue);
}
}
convertFromStringValue(comboBoxValue){
if(!comboBoxValue){
return null;
}
if(this.isUsingRelativeRoot){
if(comboBoxValue.startsWith('root')){
return comboBoxValue;
}
return this.rootPath + comboBoxValue;
} else {
return comboBoxValue;
}
}
convertToStringValue(absolutePath){
if(!absolutePath){
return null;
}
if(this.isUsingRelativeRoot){
if(absolutePath.includes(this.rootPath)){
return absolutePath.substring(this.rootPath.length);
} else {
return absolutePath;
}
} else {
return absolutePath;
}
}
__comboBoxChanged(){
this.value = this.convertFromStringValue(this.__comboBox.value);
}
__updateOptionsAndRelativeRoot(){
if(!this.root){
return;
}
let modelPaths = [null].concat(this.__getAvailableModelPaths(this.root, this.hasToBeEnabled, this.filterDelegate));
let newComboBoxValue = this.__tryToGetUpdatedModelPath(modelPaths);
let newValue = this.convertFromStringValue(newComboBoxValue);
if(this.value !== newValue){
this.value = newValue;
}
if(this.__comboBox){
this.__removeOptions();
this.__createOptions(modelPaths);
this.__updateRelativeRootLabel();
}
}
__tryToGetUpdatedModelPath(newAvailableModelPaths){
let oldModelPath = this.value;
if(!oldModelPath){
return null;
}
if(newAvailableModelPaths.length === 0){
return null;
}
if(newAvailableModelPaths.length === 1){
return newAvailableModelPaths[0];
}
if(newAvailableModelPaths.indexOf(oldModelPath) >-1){
return oldModelPath;
}
if (oldModelPath.length < this.rootPath.length){
return null;
}
let relativePath = oldModelPath.substring(this.rootPath.length);
if(newAvailableModelPaths.indexOf(relativePath) >-1){
return relativePath;
}
return null;
}
__updateRelativeRootLabel(){
if(this.isUsingRelativeRoot){
this.__relativeRootLabel.textContent = this.rootPath;
this.__relativeRootLabel.style.display='inline-block';
} else {
this.__relativeRootLabel.textContent ='';
this.__relativeRootLabel.style.display='none';
}
}
__removeOptions(){
while (this.__comboBox.firstChild) {
this.__comboBox.removeChild(this.__comboBox.firstChild);
}
}
__createOptions(items){
for(let item of items){
let option = document.createElement('option');
option.innerText = item;
this.__comboBox.appendChild(option);
}
}
__getAvailableModelPaths(atom, hasToBeEnabled, filterDelegate){
let classes = this.__atomClasses;
let functionNames = this.__atomFunctionNames;
let availablePaths = [];
for(let child of atom.children){
if (hasToBeEnabled) {
if (!child.isEnabled) {
continue;
}
}
if(classes){
if(functionNames){
throw new Error('Please specify only atomClasses or atomFunctionNames and not both.');
}
availablePaths = this.__addAvailablePathByClass(availablePaths, child, classes, filterDelegate);
} else {
if(!functionNames){
throw new Error('Either atomClasses or atomFunctionNames already need to be specified to identify allowed atoms.')
}
availablePaths = this.__addAvailablePathByInterface(availablePaths, child, functionNames);
}
availablePaths = availablePaths.concat(this.__getAvailableModelPaths(child, hasToBeEnabled, filterDelegate));
}
return availablePaths;
}
__addAvailablePathByClass(availablePaths, child, classes, filterDelegate){
let rootPath = this.rootPath;
let paths = availablePaths;
for(let clazz of classes){
if (!(child instanceof clazz)) {
continue;
}
if (filterDelegate) {
let passedFilter = filterDelegate(child);
if (!passedFilter) {
continue;
}
}
paths = this.__addPathForChild(paths, child);
}
return paths;
}
__addAvailablePathByInterface(availablePaths, child, functionNames, filterDelegate){
let paths = availablePaths;
let hasInterface = this.__containsAllFunctions(child, functionNames)
if(hasInterface){
if (filterDelegate) {
let passedFilter = filterDelegate(child);
if (passedFilter) {
paths = this.__addPathForChild(paths, child);
}
} else {
paths = this.__addPathForChild(paths, child);
}
}
return paths;
}
__addPathForChild(availablePaths, child){
let rootPath = this.rootPath;
let paths = availablePaths;
let path = child.treePath;
if(this.isUsingRelativeRoot){
let relativePath = path.substring(rootPath.length);
paths.push(relativePath);
} else {
paths.push(path);
}
return paths;
}
__containsAllFunctions(child, functionNames){
for(let functionName of functionNames){
if (!(child[functionName])) {
return false;
}
}
return true;
}
get atomClasses() {
return this.__atomClasses
}
set atomClasses(value) {
this.__atomClasses = value;
this.__updateOptionsAndRelativeRoot();
}
get atomFunctionNames() {
return this.__atomFunctionNames;
}
set atomFunctionNames(value) {
this.__atomFunctionNames = value;
this.__updateOptionsAndRelativeRoot();
}
get relativeRootAtom(){
return this.__relativeRootAtom;
}
get isUsingRelativeRoot(){
return this.relativeRootAtom !== undefined && this.relativeRootAtom !== null;
}
set relativeRootAtom(atom){
this.__relativeRootAtom=atom;
this.__updateOptionsAndRelativeRoot();
}
get root(){
if(this.isUsingRelativeRoot){
return this.relativeRootAtom;
}
if(this.__parentAtom){
return this.__parentAtom.root;
}
return null;
}
get rootPath(){
if(this.root){
return this.root.treePath;
} else {
return '';
}
}
}
window.customElements.define('treez-model-path', TreezModelPath);