-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
421 lines (345 loc) · 16 KB
/
index.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import { checkLogin } from './login.js';
import { initLMV, startViewer } from './lmv.js';
import * as vw_stubs from './src/vw_stubs.js';
import * as fac_stubs from './src/fac_stubs.js';
import * as model_stubs from './src/model_stubs.js';
import * as prop_stubs from './src/prop_stubs.js';
import * as doc_stubs from './src/doc_stubs.js';
import * as ev_stubs from './src/ev_stubs.js';
import * as st_stubs from './src/st_stubs.js';
/***************************************************
** FUNC: noFacilitiesAvailable()
** DESC: could be the case that this user doesn't even have a Tandem account, is not
** part of any teams and/or doesn't have access to any facilities.
**********************/
function noFacilitiesAvailable(teams, sharedWithMe) {
// check if we are part of any accounts
if (teams == null) {
Autodesk.Viewing.Private.AlertBox.displayError(
viewer.container,
'Make sure you have an account setup in Autodesk Tandem.',
'No account found',
'img-item-not-found'
);
return true;
}
// first check if any facilities have been shared directly with this user
if (sharedWithMe != null)
return false; // this means we are ok
let foundOne = false;
for (let i=0; i<teams.length; i++) {
if (teams[i].facilities.length) {
foundOne = true;
break;
}
}
if (!foundOne) {
Autodesk.Viewing.Private.AlertBox.displayError(
viewer.container,
'Make sure you are have access to at least one facility in Autodesk Tandem.',
'No facilities found',
'img-item-not-found'
);
return true;
}
return false; // this means we are OK
}
/***************************************************
** FUNC: populateTeamsDropdown()
** DESC: get the list of all teams that we are a part of
**********************/
async function populateTeamsDropdown(app, viewer) {
const acctPicker = document.getElementById('acctPicker');
const preferredTeam = window.localStorage.getItem('tandem-testbed-last-team'); // the last one that was used
// get the list of all teams and then sort them alphabetically
const teams = await app.getTeams();
console.log("Teams:", teams);
const teamNames = [];
for (let i=0; i<teams.length; i++) {
await teams[i].getFacilities(); // need to load the facilties we will be referencing later (will populate DtTeam.facilities property)
teamNames.push(teams[i].name);
}
teamNames.sort((a, b) => a.localeCompare(b)); // Sort alphabetically
// there are also Facilities that are just shared directly with a given user.
// make a "fake" team to be represented in the drop down if we have some of these
const sharedWithMe = await app.getUsersFacilities(); // Facilities we have access to because they've been directly shared with us
if (sharedWithMe != null) {
const fakeTeam = { app: app, name: "** SHARED DIRECTLY **", facilities: sharedWithMe };
teams.push(fakeTeam); // always push to end, regardless of alphabetical place
teamNames.push(fakeTeam.name);
}
// bail out if they don't have access to anything
if (noFacilitiesAvailable(teams, sharedWithMe)) {
return;
}
const safePreferredTeam = teamNames.find(t=>t === preferredTeam) || teamNames[0]; // make sure we can find last used, or else use first
// add all the account names to the acct dropdown picker
for (let i=0; i<teamNames.length; i++) {
const option = document.createElement('option');
option.text = teamNames[i];
option.selected = teamNames[i] == safePreferredTeam; // set initial selection in dropdown
acctPicker.appendChild(option);
}
populateFacilitiesDropdown(app, safePreferredTeam, viewer); // this will load the Facilities on first pass initialization
// this callback will load the Facilities when the dropdown list gets a different selection
acctPicker.onchange = ()=>{
const newTeam = acctPicker.value;
window.localStorage.setItem('tandem-testbed-last-team', newTeam);
populateFacilitiesDropdown(app, newTeam, viewer);
}
acctPicker.style.visibility = 'initial';
}
/***************************************************
** FUNC: populateFacilitiesDropdown()
** DESC: get the list of Facilities for a given team
**********************/
async function populateFacilitiesDropdown(app, teamName, viewer) {
// get the list of all teams
const teams = await app.getTeams();
const curTeam = teams.find(obj => obj.name === teamName);
// load preferred or random facility
const preferredFacilityUrn = window.localStorage.getItem('tandem-testbed-last-facility');
const preferredFacility = curTeam.facilities.find(f=>f.twinId === preferredFacilityUrn) || curTeam.facilities[0];
app.displayFacility(preferredFacility, false, viewer); // initially loaded facility
// setup facility picker UI
await Promise.all(curTeam.facilities.map(f => f.load()));
const facilityPicker = document.getElementById('facilityPicker');
console.log("Facilities for current team:", curTeam.facilities);
curTeam.facilities.sort((a, b) => a.settings.props["Identity Data"]["Building Name"].localeCompare(b.settings.props["Identity Data"]["Building Name"])); // Sort alphabetically
facilityPicker.innerHTML = ''; // clear out any previous options
for (let facility of curTeam.facilities) {
const option = document.createElement('option');
option.text = facility.settings.props["Identity Data"]["Building Name"];
option.selected = facility.twinId == preferredFacility.twinId;
facilityPicker.appendChild(option);
}
// this callback will load the facility that the user picked in the Facility dropdown
facilityPicker.onchange = ()=>{
const newFacility = curTeam.facilities[facilityPicker.selectedIndex];
window.localStorage.setItem('tandem-testbed-last-facility', newFacility.twinId);
app.displayFacility(newFacility, undefined, viewer);
}
facilityPicker.style.visibility = 'initial';
}
/***************************************************
** FUNC: bootstrap()
** DESC: init the Tandem viewer and get the user to login via their Autodesk ID.
**********************/
async function bootstrap() {
// login in the user and set UI elements appropriately (args are HTML elementIDs)
const userLoggedIn = await checkLogin("btn_login", "btn_logout", "btn_userProfile", "viewer");
if (!userLoggedIn)
return; // when user does login, it will go through the bootstrap process again
await initLMV();
// init viewer
const container = document.getElementById('viewer');
const viewer = startViewer(container);
console.log('TandemViewer is up and running');
// init app
const app = new Autodesk.Tandem.DtApp();
window.DT_APP = app;
// setup the account picker dropdown
await populateTeamsDropdown(app, viewer); // this will trigger loading of a current facility
}
/***************************************************
** FUNC: main()
** DESC: load the viewer and final UI elements, then bind all the events to menu items.
**********************/
async function main() {
// init the Viewer and login
await bootstrap();
// bind all the callbacks from the UI to the stub functions
// TBD: not super happy about how this callback mechanism works... I did trial and error
// forever to get these partial HTML snippets to work for the modal input. Will try later
// to make a more elegant mechanism. (JMA - 03/28/22)
let modalFuncCallbackNum = 0;
// Facility Stubs
$("#btn_dumpFacilityInfo").click(fac_stubs.dumpDtFacilityInfo);
$("#btn_dumpAppInfo").click(fac_stubs.dumpDtAppInfo);
$("#btn_dumpDtConstants").click(fac_stubs.dumpDtConstants);
$("#btn_loadFacilityUsageMetrics").click(fac_stubs.loadFacilityUsageMetrics);
// Model stubs
$("#btn_dumpModelInfo").click(model_stubs.dumpDtModelInfo);
$("#btn_getLevels").click(model_stubs.getLevels);
$("#btn_isolateLevel").click(function() {
$('#stubInput_getName').modal('show');
modalFuncCallbackNum = 2;
});
$("#btn_isolateRooms").click(model_stubs.isolateRooms);
$("#btn_showElementsInRoom").click(model_stubs.showElementsInRoom);
$("#btn_getRoomsOfElement").click(model_stubs.getRoomsOfElement);
$("#btn_getModelHistory").click(model_stubs.getDtModelHistory);
$("#btn_getModelUsageMetrics").click(model_stubs.getDtModelUsageMetrics);
$("#btn_dbIdsToPersistentIds").click(model_stubs.dbIdsToExternalIds);
$("#btn_getElementUfClass").click(model_stubs.getElementUfClass);
$("#btn_getElementCustomClass").click(model_stubs.getElementCustomClass);
$("#btn_getElementBounds").click(model_stubs.getElementBounds);
$("#btn_isolateTaggedAssets").click(model_stubs.isolateTaggedAssets);
$("#btn_isolateUnTaggedAssets").click(model_stubs.isolateUnTaggedAssets);
$("#btn_isolateClassifiedAssets").click(model_stubs.isolateClassifiedAssets);
$("#btn_isolateUnClassifiedAssets").click(model_stubs.isolateUnClassifiedAssets);
// Prop Stubs
$("#btn_getQualifiedPropName").click(function() {
$('#stubInput_getPropertyName').modal('show');
modalFuncCallbackNum = 0;
});
$("#btn_getProperties").click(prop_stubs.getDtProperties);
$("#btn_getPropertiesWithHistory").click(prop_stubs.getDtPropertiesWithHistory);
$("#btn_getCommonProperties").click(prop_stubs.getCommonDtProperties);
$("#btn_getPropertySelSet").click(function() {
$('#stubInput_getPropertyName').modal('show');
modalFuncCallbackNum = 1;
});
$("#btn_findElementsWherePropValueEqualsX").click(function() {
$('#stubInput_getPropertyFilter').modal('show');
});
$("#btn_setPropertySelSet").click(function() {
$('#stubInput_setPropertyValue').modal('show');
modalFuncCallbackNum = 0;
});
$("#btn_assignClassification").click(function() {
$('#stubInput_setClassification').modal('show');
});
// Document Stubs
$("#btn_getFacilityDocuments").click(doc_stubs.getFacilityDocuments);
$("#btn_getDocument").click(function() {
$('#stubInput_getURN').modal('show');
modalFuncCallbackNum = 0;
});
$("#btn_deleteDocument").click(function() {
$('#stubInput_getURN').modal('show');
modalFuncCallbackNum = 1;
});
// event stubs
$("#btn_addEventListeners").click(ev_stubs.addEventListeners);
$("#btn_removeEventListeners").click(ev_stubs.removeEventListeners);
// stream stubs
$("#btn_dumpStreamManager").click(st_stubs.dumpStreamManager);
$("#btn_getStreamIds").click(st_stubs.getStreamIds);
$("#btn_getLastReadings").click(st_stubs.getLastReadings);
$("#btn_refreshStreamsLastReadings").click(st_stubs.refreshStreamsLastReadings);
$("#btn_getStreamBulkRollups").click(st_stubs.getStreamBulkRollups);
$("#btn_exportStreamsToJson").click(st_stubs.exportStreamsToJson);
$("#btn_getAllStreamInfos").click(st_stubs.getAllStreamInfos);
$("#btn_getAllStreamInfosFromCache").click(st_stubs.getAllStreamInfosFromCache);
$("#btn_getAllConnectedAttributes").click(st_stubs.getAllConnectedAttributes);
$("#btn_getAttributeCandidates").click(st_stubs.getAttributeCandidates);
$("#btn_getStreamSecrets").click(st_stubs.getStreamSecrets);
$("#btn_getStreamsBulkImportTemplate").click(st_stubs.getStreamsBulkImportTemplate);
$("#btn_createStream").click(function() {
$('#stubInput_getName').modal('show');
modalFuncCallbackNum = 0;
});
$("#btn_deleteStream").click(function() {
$('#stubInput_getInt').modal('show');
modalFuncCallbackNum = 0;
});
$("#btn_resetStreamSecrets").click(function() {
$('#stubInput_getKey').modal('show');
modalFuncCallbackNum = 0;
});
$("#btn_getStreamIngestionUrls").click(st_stubs.getStreamIngestionUrls);
// viewer stubs
$("#btn_addSprites").click(vw_stubs.addSprites);
$("#btn_removeSprites").click(vw_stubs.removeSprites);
$("#btn_getCurrentSelSet").click(vw_stubs.getCurrentSelSet);
$("#btn_isolateCurrentSelSet").click(vw_stubs.isolateCurrentSelSet);
$("#btn_focusCurrentSelSet").click(vw_stubs.focusCurrentSelSet);
$("#btn_showAllObjects").click(vw_stubs.showAllObjects);
$("#btn_getAllElements").click(vw_stubs.getAllElements);
$("#btn_getAllVisibleDbIds").click(vw_stubs.getVisibleDbIds);
$("#btn_selectAllVisibleElements").click(vw_stubs.selectAllVisibleElemnents);
$("#btn_getHiddenElementsByModel").click(vw_stubs.getHiddenElementsByModel);
$("#btn_hideModel").click(vw_stubs.hideModel);
$("#btn_showModel").click(vw_stubs.showModel);
$("#btn_scrapeGeometry").click(vw_stubs.scrapeGeometry);
$("#btn_setThemeColor").click(vw_stubs.setThemeColor);
$("#btn_unsetThemeColor").click(vw_stubs.unsetThemeColor);
$("#btn_clearAllTheming").click(vw_stubs.clearAllTheming);
$("#btn_getSavedViews").click(vw_stubs.getSavedViews);
$("#btn_gotoSavedView").click(function() {
$('#stubInput_getName').modal('show');
modalFuncCallbackNum = 1;
});
// this gets called from above via modal dialog (#btn_getQualifiedPropName, and others)
$('#stubInput_getPropertyName_OK').click(function() {
const propCategory = $("#stubInput_propCategory").val();
const propName = $("#stubInput_propName").val();
if (modalFuncCallbackNum == 0)
prop_stubs.getQualifiedPropName(propCategory, propName);
else if (modalFuncCallbackNum == 1)
prop_stubs.getPropertySelSet(propCategory, propName);
else {
alert("ASSERT: modalFuncCallbackNum not expected.");
}
});
// this gets called from above via modal dialog (#btn_setPropertySelSet, and others)
$('#stubInput_setPropertyValue_OK').click(function() {
const propCategory = $("#stubInput_propCategorySet").val();
const propName = $("#stubInput_propNameSet").val();
const propVal = $("#stubInput_propValSet").val();
if (modalFuncCallbackNum == 0)
prop_stubs.setPropertySelSet(propCategory, propName, propVal);
else {
alert("ASSERT: modalFuncCallbackNum not expected.");
}
});
// this gets called from above via modal dialog (#btn_findElementsWherePropValueEqualsX)
$('#stubInput_getPropertyFilter_OK').click(function() {
const propCategory = $("#stubInput_propCategoryFilter").val();
const propName = $("#stubInput_propNameFilter").val();
const matchStr = $("#stubInput_propValFilter").val();
const isCaseInsensitive = $("#stubInput_propValIsCaseInsensitive").is(":checked");
const isRegEx = $("#stubInput_propValIsRegEx").is(":checked");
const searchVisibleOnly = $("#stubInput_searchVisibleOnly").is(":checked");
prop_stubs.findElementsWherePropValueEqualsX(propCategory, propName, matchStr, isRegEx, searchVisibleOnly, isCaseInsensitive);
});
// this gets called from above via modal dialog (#btn_findElementsWherePropValueEqualsX)
$('#stubInput_setClassification_OK').click(function() {
const classificationStr = $("#stubInput_classificationStr").val();
porp_stubs.assignClassification(classificationStr);
});
$('#stubInput_getURN_OK').click(function() {
const urn = $("#stubInput_urn").val();
if (modalFuncCallbackNum == 0)
doc_stubs.getDocument(urn);
else if (modalFuncCallbackNum == 1)
doc_stubs.deleteDocument(urn);
else {
alert("ASSERT: modalFuncCallbackNum not expected.");
}
});
$('#stubInput_getKey_OK').click(function() {
const key = $("#stubInput_key").val();
if (modalFuncCallbackNum == 0)
st_stubs.resetStreamSecrets(key);
else {
alert("ASSERT: modalFuncCallbackNum not expected.");
}
});
$('#stubInput_getName_OK').click(function() {
const nameStr = $("#stubInput_name").val();
if (modalFuncCallbackNum == 0)
st_stubs.createStream(nameStr);
else if (modalFuncCallbackNum == 1)
vw_stubs.gotoSavedView(nameStr);
else if (modalFuncCallbackNum == 2)
model_stubs.isolateLevel(nameStr);
else {
alert("ASSERT: modalFuncCallbackNum not expected.");
}
});
$('#stubInput_getInt_OK').click(function() {
const rawStr = $("#stubInput_int").val();
if (modalFuncCallbackNum == 0)
st_stubs.deleteStream(parseInt(rawStr));
else {
alert("ASSERT: modalFuncCallbackNum not expected.");
}
});
};
// trigger things when the HTML is loaded
document.addEventListener('DOMContentLoaded', function() {
//console.log("DOMContentLoaded");
main();
});