-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathtreeSelector.html
More file actions
385 lines (345 loc) · 9.22 KB
/
treeSelector.html
File metadata and controls
385 lines (345 loc) · 9.22 KB
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<!DOCTYPE html>
<!--
Copyright 2018 SAS Institute Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!--
The tree selector is based on https://github.com/daweilv/treejs/tree/master
MIT License
Copyright (c) 2018 david.lv
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-->
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script type="text/javascript" src="../util/messagingUtil.js"></script>
<script type="text/javascript" src="../util/contentUtil.js"></script>
<script src="./TreeSelector/tree.min.js"></script>
<style>
html {
overflow: hidden;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
font-family: arial;
font-size: 13px;
overflow-y: auto;
/* Enable vertical scrolling */
}
div {
position: relative;
}
#container {
position: relative;
font-family: "Albany AMT";
}
.treejs {
font-size: 10pt;
}
/* Hide scrollbar by default */
::-webkit-scrollbar {
width: 8px;
background-color: #f5f5f5;
}
/* Make scrollbar visible when needed */
::-webkit-scrollbar-thumb {
border-radius: 3px;
background-color: #888888;
}
/* Make scrollbar track visible when needed */
::-webkit-scrollbar-track {
border-radius: 3px;
background-color: #f5f5f5;
}
</style>
</head>
<body>
<div id="container"></div>
<script>
"use strict";
function onDataReceived(resultData) {
if (!resultData) return;
let resultName = resultData.resultName;
let selections = va.contentUtil.initializeSelections(resultData); // good practice to remove eventual brush columns
const treeData = createTreeData(resultData);
const myTree = new Tree('#container', {
data: treeData,
// only expand level 1 node
closeDepth: 1,
// Just a placeholder: not currently used
beforeLoad: function (rawData) {
function formatData() {
// do some format
}
return formatData(rawData);
},
loaded: function () {
// do something or set values after Tree loaded callback
// do not use arrow function `()=>` , if you use `this`, use function instead.
// this context bind current tree instance
this.collapseAll();
},
onChange: function () {
//console.log("values", this.values);
//console.log("selectedNodes", this.selectedNodes);
//console.log("disables", this.disables);
//console.log("disabledNodes", this.disabledNodes);
let selectedRows = this.selectedNodes.filter((e) => e.hasOwnProperty("row")).map((e) => e.row); // returns an array of selected rows, e.g.: [3,4,5]
va.messagingUtil.postSelectionMessage(resultName, selectedRows);
},
});
}
// Example of message "resultData" received from VA
/*
{
"version": "1",
"resultName": "dd30",
"rowCount": 14,
"availableRowCount": 14,
"data": [
[
"Asia",
"Acura",
" MDX"
],
[
"Asia",
"Acura",
" TL 4dr"
],
[
"Asia",
"Honda",
" Accord EX 2dr"
],
[
"Asia",
"Honda",
" Civic DX 2dr"
],
[
"Asia",
"Nissan",
" Murano SL"
],
[
"Europe",
"Audi",
" A4 3.0 4dr"
],
[
"Europe",
"Audi",
" A6 2.7 Turbo Quattro 4dr"
],
[
"Europe",
"BMW",
" X3 3.0i"
],
[
"Europe",
"Jaguar",
" S-Type 3.0 4dr"
],
[
"USA",
"Chevrolet",
" Silverado SS"
],
[
"USA",
"Chevrolet",
" Tahoe LT"
],
[
"USA",
"Chevrolet",
" TrailBlazer LT"
],
[
"USA",
"Ford",
" Explorer XLT V6"
],
[
"USA",
"Ford",
" F-150 Regular Cab XL"
]
],
"columns": [
{
"name": "bi39",
"label": "Origin",
"type": "string"
},
{
"name": "bi40",
"label": "Make",
"type": "string"
},
{
"name": "bi41",
"label": "Model",
"type": "string"
}
]
};
*/
// Example of "treeData" structure equivalent to "resultData" above
// Note: the attrribute "row" did not exist before and was added to support sending selections back to VA
/*
[
{
id: 1,
text: 'Asia',
children: [
{
id: 2,
text: 'Acura',
children: [
{id: 3, text: ' MDX', row: 0},
{id: 4, text: ' TL 4dr', row: 1},
],
},
{
id: 5,
text: 'Honda',
children: [
{id: 6, text: ' Accord EX 2dr', row: 2},
{id: 7, text: ' Civic DX 2dr', row: 3},
],
},
{
id: 8,
text: 'Nissan',
children: [
{id: 9, text: ' Murano SL', row: 4},
],
},
],
},
{
id: 10,
text: 'Europe',
children: [
{
id: 11,
text: 'Audi',
children: [
{id: 12, text: ' A4 3.0 4dr', row: 5},
{id: 13, text: ' A6 2.7 Turbo Quattro 4dr', row: 6},
],
},
{
id: 14,
text: 'BMW',
children: [
{id: 15, text: ' X3 3.0i', row: 7},
],
},
{
id: 16,
text: 'Jaguar',
children: [
{id: 17, text: ' S-Type 3.0 4dr', row: 8},
],
},
],
},
{
id: 18,
text: 'USA',
children: [
{
id: 19,
text: 'Chevrolet',
children: [
{id: 20, text: ' Silverado SS', row: 9},
{id: 21, text: ' Tahoe LT', row: 10},
{id: 22, text: ' TrailBlazer LT', row: 11},
],
},
{
id: 23,
text: 'Ford',
children: [
{id: 24, text: ' Explorer XLT V6', row: 12},
{id: 25, text: ' F-150 Regular Cab XL', row: 13},
],
},
],
},
];
*/
// Takes the "resultData" message received from VA and creates the equivalent "treeData" structure that the tree selector object needs
function createTreeData(resultData) {
// Only use columns that are categories in the tree selector
// determine the index of the columns to be used
const columns = resultData.columns.map((e, i) => { return { type: e.type, index: i } }).filter(e => e.type != "number").map((e) => e.index);
// create copy of the original data with only the columns to be used in the tree selector
const data = [];
resultData.data.forEach((elem, i) => {
const row = elem.filter((e, col) => columns.includes(col));
data.push(row);
});
// Eliminate leaf hiearchy levels with missing value to support unbalanced hiearchies
data.forEach((row) => {
// identify the last non-missing hiearchy level
let index = row.findLastIndex((element) => element != "(missing)");
// eliminate the missing value levels (after the last non-missing)
if (index >= 0) row.splice(index + 1);
});
// Create the equivalent treeData structure
// solution based on: https://stackoverflow.com/questions/48467764/how-to-convert-array-of-arrays-into-deep-nested-tree-view
let treeData = [];
let id = 0;
data.forEach(function (row, rowNumber) {
row.reduce(function (tree, key, currIndex, rowArray) {
let temp = tree.find(({ text }) => key === text);
if (!temp) {
temp = { id: ++id, text: key, children: [] };
// add row information only for last level of hierarchy to make it easier to retrieve the row number to send to VA
if (currIndex == rowArray.length - 1) temp.row = rowNumber;
tree.push(temp);
}
return temp.children;
}, treeData);
});
return treeData;
}
document.addEventListener("DOMContentLoaded", function (event) {
va.messagingUtil.setOnDataReceivedCallback(onDataReceived);
});
</script>
</body>
</html>