@combeenation/3d-viewer 12.4.1 → 12.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -9
- package/dist/lib-cjs/api/classes/animationInterface.d.ts +8 -8
- package/dist/lib-cjs/api/classes/animationInterface.js +2 -2
- package/dist/lib-cjs/api/classes/dottedPath.d.ts +79 -79
- package/dist/lib-cjs/api/classes/dottedPath.js +166 -166
- package/dist/lib-cjs/api/classes/element.d.ts +153 -153
- package/dist/lib-cjs/api/classes/element.js +702 -702
- package/dist/lib-cjs/api/classes/event.d.ts +401 -401
- package/dist/lib-cjs/api/classes/event.js +424 -424
- package/dist/lib-cjs/api/classes/eventBroadcaster.d.ts +26 -26
- package/dist/lib-cjs/api/classes/eventBroadcaster.js +49 -49
- package/dist/lib-cjs/api/classes/fuzzyMap.d.ts +7 -7
- package/dist/lib-cjs/api/classes/fuzzyMap.js +21 -21
- package/dist/lib-cjs/api/classes/parameter.d.ts +410 -410
- package/dist/lib-cjs/api/classes/parameter.js +642 -642
- package/dist/lib-cjs/api/classes/parameterObservable.d.ts +36 -36
- package/dist/lib-cjs/api/classes/parameterObservable.js +72 -72
- package/dist/lib-cjs/api/classes/parameterizable.d.ts +15 -15
- package/dist/lib-cjs/api/classes/parameterizable.js +102 -102
- package/dist/lib-cjs/api/classes/placementAnimation.d.ts +45 -45
- package/dist/lib-cjs/api/classes/placementAnimation.js +176 -176
- package/dist/lib-cjs/api/classes/variant.d.ts +261 -261
- package/dist/lib-cjs/api/classes/variant.js +872 -872
- package/dist/lib-cjs/api/classes/variantInstance.d.ts +53 -53
- package/dist/lib-cjs/api/classes/variantInstance.js +125 -125
- package/dist/lib-cjs/api/classes/variantParameterizable.d.ts +17 -17
- package/dist/lib-cjs/api/classes/variantParameterizable.js +86 -86
- package/dist/lib-cjs/api/classes/viewer.d.ts +215 -215
- package/dist/lib-cjs/api/classes/viewer.js +708 -708
- package/dist/lib-cjs/api/classes/viewerError.d.ts +43 -43
- package/dist/lib-cjs/api/classes/viewerError.js +55 -55
- package/dist/lib-cjs/api/classes/viewerLight.d.ts +66 -66
- package/dist/lib-cjs/api/classes/viewerLight.js +344 -344
- package/dist/lib-cjs/api/internal/lensRendering.d.ts +8 -8
- package/dist/lib-cjs/api/internal/lensRendering.js +11 -11
- package/dist/lib-cjs/api/internal/sceneSetup.d.ts +13 -13
- package/dist/lib-cjs/api/internal/sceneSetup.js +227 -227
- package/dist/lib-cjs/api/manager/animationManager.d.ts +30 -30
- package/dist/lib-cjs/api/manager/animationManager.js +126 -126
- package/dist/lib-cjs/api/manager/gltfExportManager.d.ts +80 -80
- package/dist/lib-cjs/api/manager/gltfExportManager.js +300 -299
- package/dist/lib-cjs/api/manager/gltfExportManager.js.map +1 -1
- package/dist/lib-cjs/api/manager/sceneManager.d.ts +33 -33
- package/dist/lib-cjs/api/manager/sceneManager.js +128 -128
- package/dist/lib-cjs/api/manager/tagManager.d.ts +118 -118
- package/dist/lib-cjs/api/manager/tagManager.js +530 -530
- package/dist/lib-cjs/api/manager/textureLoadManager.d.ts +22 -22
- package/dist/lib-cjs/api/manager/textureLoadManager.js +107 -107
- package/dist/lib-cjs/api/manager/variantInstanceManager.d.ts +106 -106
- package/dist/lib-cjs/api/manager/variantInstanceManager.js +290 -290
- package/dist/lib-cjs/api/store/specStorage.d.ts +32 -32
- package/dist/lib-cjs/api/store/specStorage.js +65 -65
- package/dist/lib-cjs/api/util/babylonHelper.d.ts +238 -238
- package/dist/lib-cjs/api/util/babylonHelper.js +825 -825
- package/dist/lib-cjs/api/util/debugHelper.d.ts +9 -9
- package/dist/lib-cjs/api/util/debugHelper.js +93 -93
- package/dist/lib-cjs/api/util/deviceHelper.d.ts +9 -9
- package/dist/lib-cjs/api/util/deviceHelper.js +28 -28
- package/dist/lib-cjs/api/util/geometryHelper.d.ts +17 -17
- package/dist/lib-cjs/api/util/geometryHelper.js +112 -112
- package/dist/lib-cjs/api/util/globalTypes.d.ts +490 -490
- package/dist/lib-cjs/api/util/globalTypes.js +1 -1
- package/dist/lib-cjs/api/util/resourceHelper.d.ts +58 -58
- package/dist/lib-cjs/api/util/resourceHelper.js +214 -214
- package/dist/lib-cjs/api/util/sceneLoaderHelper.d.ts +58 -58
- package/dist/lib-cjs/api/util/sceneLoaderHelper.js +228 -228
- package/dist/lib-cjs/api/util/stringHelper.d.ts +13 -13
- package/dist/lib-cjs/api/util/stringHelper.js +32 -32
- package/dist/lib-cjs/api/util/structureHelper.d.ts +9 -9
- package/dist/lib-cjs/api/util/structureHelper.js +57 -57
- package/dist/lib-cjs/buildinfo.json +3 -3
- package/dist/lib-cjs/commonjs.tsconfig.tsbuildinfo +1 -1
- package/dist/lib-cjs/index.d.ts +63 -63
- package/dist/lib-cjs/index.js +128 -128
- package/package.json +93 -92
- package/src/api/classes/animationInterface.ts +10 -10
- package/src/api/classes/dottedPath.ts +181 -181
- package/src/api/classes/element.ts +766 -766
- package/src/api/classes/event.ts +457 -457
- package/src/api/classes/eventBroadcaster.ts +52 -52
- package/src/api/classes/fuzzyMap.ts +21 -21
- package/src/api/classes/parameter.ts +686 -686
- package/src/api/classes/parameterObservable.ts +73 -73
- package/src/api/classes/parameterizable.ts +87 -87
- package/src/api/classes/placementAnimation.ts +162 -162
- package/src/api/classes/variant.ts +965 -965
- package/src/api/classes/variantInstance.ts +123 -123
- package/src/api/classes/variantParameterizable.ts +83 -83
- package/src/api/classes/viewer.ts +751 -751
- package/src/api/classes/viewerError.ts +63 -63
- package/src/api/classes/viewerLight.ts +335 -335
- package/src/api/internal/debugViewer.ts +90 -90
- package/src/api/internal/lensRendering.ts +9 -9
- package/src/api/internal/sceneSetup.ts +208 -208
- package/src/api/manager/animationManager.ts +143 -143
- package/src/api/manager/gltfExportManager.ts +337 -334
- package/src/api/manager/sceneManager.ts +134 -134
- package/src/api/manager/tagManager.ts +572 -572
- package/src/api/manager/textureLoadManager.ts +107 -107
- package/src/api/manager/variantInstanceManager.ts +306 -306
- package/src/api/store/specStorage.ts +68 -68
- package/src/api/util/babylonHelper.ts +915 -915
- package/src/api/util/debugHelper.ts +121 -121
- package/src/api/util/deviceHelper.ts +31 -31
- package/src/api/util/geometryHelper.ts +142 -142
- package/src/api/util/globalTypes.ts +566 -566
- package/src/api/util/resourceHelper.ts +201 -201
- package/src/api/util/sceneLoaderHelper.ts +247 -247
- package/src/api/util/stringHelper.ts +30 -30
- package/src/api/util/structureHelper.ts +62 -62
- package/src/buildinfo.json +3 -3
- package/src/dev.ts +70 -70
- package/src/index.ts +116 -116
- package/src/types.d.ts +49 -49
|
@@ -1,291 +1,291 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.VariantInstanceManager = void 0;
|
|
13
|
-
const dottedPath_1 = require("../classes/dottedPath");
|
|
14
|
-
const event_1 = require("../classes/event");
|
|
15
|
-
const eventBroadcaster_1 = require("../classes/eventBroadcaster");
|
|
16
|
-
const parameter_1 = require("../classes/parameter");
|
|
17
|
-
const variant_1 = require("../classes/variant");
|
|
18
|
-
const variantInstance_1 = require("../classes/variantInstance");
|
|
19
|
-
const specStorage_1 = require("../store/specStorage");
|
|
20
|
-
const babylonHelper_1 = require("../util/babylonHelper");
|
|
21
|
-
const transformNode_1 = require("@babylonjs/core/Meshes/transformNode");
|
|
22
|
-
const lodash_es_1 = require("lodash-es");
|
|
23
|
-
/**
|
|
24
|
-
* There's usually no need to instantiate a {@link VariantInstanceManager} yourself as it is provided by each
|
|
25
|
-
* {@link Viewer} instance via its {@link Viewer.variantInstances}
|
|
26
|
-
*/
|
|
27
|
-
class VariantInstanceManager extends eventBroadcaster_1.EventBroadcaster {
|
|
28
|
-
/**
|
|
29
|
-
* Constructor.
|
|
30
|
-
*/
|
|
31
|
-
constructor(viewer, rootVariant) {
|
|
32
|
-
super();
|
|
33
|
-
this.viewer = viewer;
|
|
34
|
-
this.rootVariant = rootVariant;
|
|
35
|
-
this.variantInstancePromises = new Map();
|
|
36
|
-
this.variantInstances = new Map();
|
|
37
|
-
this.variantInstanceDefinitions = new Map();
|
|
38
|
-
this.parameters = {};
|
|
39
|
-
this.queues = {};
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Creates a {@link VariantInstanceManager} based on given root Variant.
|
|
43
|
-
*/
|
|
44
|
-
static create(viewer) {
|
|
45
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
46
|
-
const rootVariant = yield variant_1.Variant.createRoot(specStorage_1.SpecStorage.getSpec(), viewer);
|
|
47
|
-
return new VariantInstanceManager(viewer, rootVariant);
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Gets all instances.
|
|
52
|
-
*/
|
|
53
|
-
get all() {
|
|
54
|
-
return [...this.variantInstances.values()];
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Gets all instance definitions.
|
|
58
|
-
*/
|
|
59
|
-
get allDefinitions() {
|
|
60
|
-
return [...this.variantInstanceDefinitions.values()];
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Gets whether all definitions have been created.
|
|
64
|
-
*/
|
|
65
|
-
get areAllDefinitionsCreated() {
|
|
66
|
-
const createdDefinitions = this.allDefinitions.filter(definition => this.all.filter(instance => instance.name === definition.name).length > 0);
|
|
67
|
-
return this.allDefinitions.length === createdDefinitions.length;
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Gets all {@link VariantInstance}s with given {@link Variant}'s name.
|
|
71
|
-
*/
|
|
72
|
-
allWithVariantName(name) {
|
|
73
|
-
return this.all.filter(instance => instance.variant.name === name);
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Checks whether a {@link VariantInstance} is either registered or created.
|
|
77
|
-
*/
|
|
78
|
-
has(name) {
|
|
79
|
-
return this.variantInstances.has(name) || this.variantInstanceDefinitions.has(name);
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Retrieves a {@link VariantInstance}.
|
|
83
|
-
* If the instance has been created, the created instance will be returned.
|
|
84
|
-
* If the instance is registered, the instance will be created and returned.
|
|
85
|
-
* if the instance has already been requested and is currently pending, the dedicated promise will be returned.
|
|
86
|
-
* If none of these apply, an Error is thrown.
|
|
87
|
-
*
|
|
88
|
-
* @emits {@link Event.VARIANT_INSTANCE_CREATED}
|
|
89
|
-
*/
|
|
90
|
-
get(name) {
|
|
91
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
92
|
-
// 1) see if we have an instance of that name already and return it if found
|
|
93
|
-
if (this.variantInstances.has(name)) {
|
|
94
|
-
return this.variantInstances.get(name); // cannot be null, hence the exclamation mark
|
|
95
|
-
}
|
|
96
|
-
// 2) try to get promise from list of requested instances and return it if found
|
|
97
|
-
if (this.variantInstancePromises.has(name)) {
|
|
98
|
-
return this.variantInstancePromises.get(name);
|
|
99
|
-
}
|
|
100
|
-
// 3) see if there's a definition of that name, create an instance from it and store the related promise
|
|
101
|
-
if (this.variantInstanceDefinitions.has(name)) {
|
|
102
|
-
const definition = this.variantInstanceDefinitions.get(name); // cannot be null
|
|
103
|
-
const variantInstancePromise = this.createFromDefinition(definition);
|
|
104
|
-
this.variantInstancePromises.set(name, variantInstancePromise);
|
|
105
|
-
// remove promise after it has been finished
|
|
106
|
-
variantInstancePromise.then(() => this.variantInstancePromises.delete(name));
|
|
107
|
-
return variantInstancePromise;
|
|
108
|
-
}
|
|
109
|
-
// if we are here, no instance definition of that name exists. throw error.
|
|
110
|
-
throw Error(`VariantInstance with name "${name}" neither created nor configured.`);
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Creates and registers a unique {@link VariantInstance}.
|
|
115
|
-
*
|
|
116
|
-
* @emits {@link Event.VARIANT_INSTANCE_CREATED}
|
|
117
|
-
*/
|
|
118
|
-
create(dottedPath, name, parameters) {
|
|
119
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
120
|
-
const variant = dottedPath_1.DottedPath.create(dottedPath).path;
|
|
121
|
-
const definition = {
|
|
122
|
-
name: this.ensureUniqueName(name ? name : variant),
|
|
123
|
-
variant: variant,
|
|
124
|
-
parameters: parameters,
|
|
125
|
-
};
|
|
126
|
-
return yield this.createFromDefinition(definition);
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
/**
|
|
130
|
-
* Registers a unique {@link VariantInstanceDefinition}.
|
|
131
|
-
*
|
|
132
|
-
* @emits {@link Event.VARIANT_INSTANCE_REGISTERED}
|
|
133
|
-
*/
|
|
134
|
-
register(instanceDefinition) {
|
|
135
|
-
const name = instanceDefinition.name ? instanceDefinition.name : dottedPath_1.DottedPath.create(instanceDefinition.variant).path;
|
|
136
|
-
instanceDefinition.name = this.ensureUniqueName(name);
|
|
137
|
-
this.variantInstanceDefinitions.set(instanceDefinition.name, instanceDefinition);
|
|
138
|
-
this.broadcastEvent(event_1.Event.VARIANT_INSTANCE_REGISTERED, instanceDefinition);
|
|
139
|
-
return instanceDefinition;
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Commits the {@link ParameterBag} to all registered and created {@link VariantInstance}s.
|
|
143
|
-
*/
|
|
144
|
-
commitParameters(parameters = {}, onlyVisible = true) {
|
|
145
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
146
|
-
this.parameters = (0, lodash_es_1.merge)(this.parameters, parameters);
|
|
147
|
-
this.allDefinitions.forEach(definition => (definition.parameters = (0, lodash_es_1.merge)(definition.parameters, this.parameters)));
|
|
148
|
-
let instances = this.all;
|
|
149
|
-
if (onlyVisible) {
|
|
150
|
-
instances = this.all.filter(instance => instance.variant.visible);
|
|
151
|
-
}
|
|
152
|
-
const promises = instances.map(instance => instance.variant.commitParameters(this.parameters));
|
|
153
|
-
yield Promise.all(promises);
|
|
154
|
-
return this;
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
/**
|
|
158
|
-
* Show one {@link VariantInstance}. If `exclusive` is set to `true` it will hide all other
|
|
159
|
-
* instances handled by this {@link VariantInstanceManager}. If the function is called synchronously,
|
|
160
|
-
* the calls end up in a queue according to the FIFO principle (the last one wins).
|
|
161
|
-
*/
|
|
162
|
-
show(name, exclusive = false) {
|
|
163
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
164
|
-
const promise = new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
|
|
165
|
-
if (exclusive) {
|
|
166
|
-
this.allDefinitions
|
|
167
|
-
.filter(definition => definition.name !== name)
|
|
168
|
-
.forEach(definition => {
|
|
169
|
-
const parameters = {};
|
|
170
|
-
parameters[parameter_1.Parameter.VISIBLE] = false;
|
|
171
|
-
definition.parameters = (0, lodash_es_1.merge)(definition.parameters, parameters);
|
|
172
|
-
});
|
|
173
|
-
yield Promise.all(this.all
|
|
174
|
-
.filter(_instance => _instance.name !== name)
|
|
175
|
-
.map(_instance => {
|
|
176
|
-
return _instance.variant.hide();
|
|
177
|
-
}));
|
|
178
|
-
}
|
|
179
|
-
const instance = yield this.get(name);
|
|
180
|
-
yield instance.variant.show();
|
|
181
|
-
resolve(instance);
|
|
182
|
-
}));
|
|
183
|
-
if (!this.queues['show']) {
|
|
184
|
-
this.queues['show'] = [];
|
|
185
|
-
}
|
|
186
|
-
this.queues['show'].push(promise);
|
|
187
|
-
for (let i = 0; i < this.queues['show'].length; i++) {
|
|
188
|
-
const instance = yield this.queues['show'][i];
|
|
189
|
-
if (exclusive) {
|
|
190
|
-
if (i + 1 < this.queues['show'].length) {
|
|
191
|
-
yield instance.variant.hide();
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
this.queues['show'] = [];
|
|
196
|
-
return promise;
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* Clones and registers a new {@link VariantInstance} with a new, unique name.
|
|
201
|
-
*
|
|
202
|
-
* @param tagMapping Object for renaming tags on the clone, whereas the "key" is the tag name of orignal variant
|
|
203
|
-
* instance and the "value" is the new tag name that should be created from it.\
|
|
204
|
-
* Check the docs of the tag managers [renaming](https://3dviewer.docs.combeenation.com/pages/documentation/Tag-Manager.html#uniqueness-of-node-and-tag-names)
|
|
205
|
-
* chapter for further details.
|
|
206
|
-
*
|
|
207
|
-
* @emits {@link Event.VARIANT_INSTANCE_CLONED}
|
|
208
|
-
*/
|
|
209
|
-
clone(name, newName, parameters, tagMapping) {
|
|
210
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
211
|
-
if (!this.variantInstances.has(name)) {
|
|
212
|
-
throw Error(`VariantInstance with name "${name}" does not exist.`);
|
|
213
|
-
}
|
|
214
|
-
if (this.viewer.tagManager.parameters.size > 0) {
|
|
215
|
-
// visible state of nodes should be updated by tag manager if it is in use
|
|
216
|
-
// this logic is programmed in the elements `addParameterObservers` function and will be triggered by setting the
|
|
217
|
-
// variants visibility to true
|
|
218
|
-
parameters = Object.assign({ [parameter_1.Parameter.VISIBLE]: true }, (parameters !== null && parameters !== void 0 ? parameters : {}));
|
|
219
|
-
}
|
|
220
|
-
const variantInstance = this.variantInstances.get(name);
|
|
221
|
-
const definition = {
|
|
222
|
-
name: this.ensureUniqueName(newName ? newName : name),
|
|
223
|
-
variant: variantInstance.variant.id,
|
|
224
|
-
parameters: parameters,
|
|
225
|
-
};
|
|
226
|
-
const variantInstanceClone = yield variantInstance_1.VariantInstance.createLiving(variantInstance.variant, definition.name, definition.parameters);
|
|
227
|
-
this.variantInstances.set(definition.name, variantInstanceClone);
|
|
228
|
-
(0, babylonHelper_1.reportDuplicateNodeNames)((0, babylonHelper_1.duplicateNodeNames)(this.viewer.scene.getNodes(), n => n instanceof transformNode_1.TransformNode));
|
|
229
|
-
this.viewer.tagManager.mapNodesAndTags(variantInstanceClone.variant.elementNodesFlat, tagMapping !== null && tagMapping !== void 0 ? tagMapping : {});
|
|
230
|
-
yield this.viewer.tagManager.applyExistingParameterValuesTo([
|
|
231
|
-
...variantInstanceClone.variant.elementNodesFlat,
|
|
232
|
-
...variantInstanceClone.variant.elementAbstractMeshesFlat.map(n => n.material).filter(m => !!m),
|
|
233
|
-
]);
|
|
234
|
-
variantInstance.broadcastEvent(event_1.Event.VARIANT_INSTANCE_CLONED, variantInstanceClone);
|
|
235
|
-
return variantInstanceClone;
|
|
236
|
-
});
|
|
237
|
-
}
|
|
238
|
-
/**
|
|
239
|
-
* Destroys the instance by removing the {@link Variant} and de-registering the instance.
|
|
240
|
-
*
|
|
241
|
-
* @emits {@link Event.VARIANT_INSTANCE_DESTROYED}
|
|
242
|
-
*/
|
|
243
|
-
destroy(name) {
|
|
244
|
-
if (!this.variantInstances.has(name)) {
|
|
245
|
-
throw Error(`VariantInstance with name "${name}" does not exist.`);
|
|
246
|
-
}
|
|
247
|
-
const variantInstance = this.variantInstances.get(name);
|
|
248
|
-
variantInstance.variant.destroy();
|
|
249
|
-
this.variantInstances.delete(name);
|
|
250
|
-
variantInstance.broadcastEvent(event_1.Event.VARIANT_INSTANCE_DESTROYED, variantInstance);
|
|
251
|
-
return this;
|
|
252
|
-
}
|
|
253
|
-
/**
|
|
254
|
-
* Creates and registers a unique {@link VariantInstance} from `definition`.
|
|
255
|
-
*
|
|
256
|
-
* @emits {@link Event.VARIANT_INSTANCE_CREATED}
|
|
257
|
-
*/
|
|
258
|
-
createFromDefinition(definition) {
|
|
259
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
260
|
-
if (!definition.name) {
|
|
261
|
-
throw new Error(`Given VariantInstanceDefinition ${definition.variant} is missing a name property.`);
|
|
262
|
-
}
|
|
263
|
-
if (this.variantInstances.has(definition.name)) {
|
|
264
|
-
throw new Error(`VariantInstance with name "${definition.name}" is already created.`);
|
|
265
|
-
}
|
|
266
|
-
if (!this.rootVariant) {
|
|
267
|
-
throw new Error(`Viewer has not been properly bootstrapped (rootVariant missing).`);
|
|
268
|
-
}
|
|
269
|
-
const variant = yield this.rootVariant.getDescendant(definition.variant);
|
|
270
|
-
const variantInstance = yield variantInstance_1.VariantInstance.createLiving(variant, definition.name, definition.parameters);
|
|
271
|
-
this.variantInstances.set(definition.name, variantInstance);
|
|
272
|
-
this.broadcastEvent(event_1.Event.VARIANT_INSTANCE_CREATED, variantInstance);
|
|
273
|
-
return variantInstance;
|
|
274
|
-
});
|
|
275
|
-
}
|
|
276
|
-
/**
|
|
277
|
-
* Creates a unique instance name based on already registered instance names on the {@link VariantInstanceManager}.
|
|
278
|
-
* Appends an `_i` to the name where `i = 0` and increments as long as the name is not registered.
|
|
279
|
-
*/
|
|
280
|
-
ensureUniqueName(name) {
|
|
281
|
-
let instanceName = name;
|
|
282
|
-
let i = 1;
|
|
283
|
-
while (this.has(instanceName)) {
|
|
284
|
-
instanceName = `${name}_${i}`;
|
|
285
|
-
i++;
|
|
286
|
-
}
|
|
287
|
-
return instanceName;
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
exports.VariantInstanceManager = VariantInstanceManager;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.VariantInstanceManager = void 0;
|
|
13
|
+
const dottedPath_1 = require("../classes/dottedPath");
|
|
14
|
+
const event_1 = require("../classes/event");
|
|
15
|
+
const eventBroadcaster_1 = require("../classes/eventBroadcaster");
|
|
16
|
+
const parameter_1 = require("../classes/parameter");
|
|
17
|
+
const variant_1 = require("../classes/variant");
|
|
18
|
+
const variantInstance_1 = require("../classes/variantInstance");
|
|
19
|
+
const specStorage_1 = require("../store/specStorage");
|
|
20
|
+
const babylonHelper_1 = require("../util/babylonHelper");
|
|
21
|
+
const transformNode_1 = require("@babylonjs/core/Meshes/transformNode");
|
|
22
|
+
const lodash_es_1 = require("lodash-es");
|
|
23
|
+
/**
|
|
24
|
+
* There's usually no need to instantiate a {@link VariantInstanceManager} yourself as it is provided by each
|
|
25
|
+
* {@link Viewer} instance via its {@link Viewer.variantInstances}
|
|
26
|
+
*/
|
|
27
|
+
class VariantInstanceManager extends eventBroadcaster_1.EventBroadcaster {
|
|
28
|
+
/**
|
|
29
|
+
* Constructor.
|
|
30
|
+
*/
|
|
31
|
+
constructor(viewer, rootVariant) {
|
|
32
|
+
super();
|
|
33
|
+
this.viewer = viewer;
|
|
34
|
+
this.rootVariant = rootVariant;
|
|
35
|
+
this.variantInstancePromises = new Map();
|
|
36
|
+
this.variantInstances = new Map();
|
|
37
|
+
this.variantInstanceDefinitions = new Map();
|
|
38
|
+
this.parameters = {};
|
|
39
|
+
this.queues = {};
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Creates a {@link VariantInstanceManager} based on given root Variant.
|
|
43
|
+
*/
|
|
44
|
+
static create(viewer) {
|
|
45
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
46
|
+
const rootVariant = yield variant_1.Variant.createRoot(specStorage_1.SpecStorage.getSpec(), viewer);
|
|
47
|
+
return new VariantInstanceManager(viewer, rootVariant);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Gets all instances.
|
|
52
|
+
*/
|
|
53
|
+
get all() {
|
|
54
|
+
return [...this.variantInstances.values()];
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Gets all instance definitions.
|
|
58
|
+
*/
|
|
59
|
+
get allDefinitions() {
|
|
60
|
+
return [...this.variantInstanceDefinitions.values()];
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Gets whether all definitions have been created.
|
|
64
|
+
*/
|
|
65
|
+
get areAllDefinitionsCreated() {
|
|
66
|
+
const createdDefinitions = this.allDefinitions.filter(definition => this.all.filter(instance => instance.name === definition.name).length > 0);
|
|
67
|
+
return this.allDefinitions.length === createdDefinitions.length;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Gets all {@link VariantInstance}s with given {@link Variant}'s name.
|
|
71
|
+
*/
|
|
72
|
+
allWithVariantName(name) {
|
|
73
|
+
return this.all.filter(instance => instance.variant.name === name);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Checks whether a {@link VariantInstance} is either registered or created.
|
|
77
|
+
*/
|
|
78
|
+
has(name) {
|
|
79
|
+
return this.variantInstances.has(name) || this.variantInstanceDefinitions.has(name);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Retrieves a {@link VariantInstance}.
|
|
83
|
+
* If the instance has been created, the created instance will be returned.
|
|
84
|
+
* If the instance is registered, the instance will be created and returned.
|
|
85
|
+
* if the instance has already been requested and is currently pending, the dedicated promise will be returned.
|
|
86
|
+
* If none of these apply, an Error is thrown.
|
|
87
|
+
*
|
|
88
|
+
* @emits {@link Event.VARIANT_INSTANCE_CREATED}
|
|
89
|
+
*/
|
|
90
|
+
get(name) {
|
|
91
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
92
|
+
// 1) see if we have an instance of that name already and return it if found
|
|
93
|
+
if (this.variantInstances.has(name)) {
|
|
94
|
+
return this.variantInstances.get(name); // cannot be null, hence the exclamation mark
|
|
95
|
+
}
|
|
96
|
+
// 2) try to get promise from list of requested instances and return it if found
|
|
97
|
+
if (this.variantInstancePromises.has(name)) {
|
|
98
|
+
return this.variantInstancePromises.get(name);
|
|
99
|
+
}
|
|
100
|
+
// 3) see if there's a definition of that name, create an instance from it and store the related promise
|
|
101
|
+
if (this.variantInstanceDefinitions.has(name)) {
|
|
102
|
+
const definition = this.variantInstanceDefinitions.get(name); // cannot be null
|
|
103
|
+
const variantInstancePromise = this.createFromDefinition(definition);
|
|
104
|
+
this.variantInstancePromises.set(name, variantInstancePromise);
|
|
105
|
+
// remove promise after it has been finished
|
|
106
|
+
variantInstancePromise.then(() => this.variantInstancePromises.delete(name));
|
|
107
|
+
return variantInstancePromise;
|
|
108
|
+
}
|
|
109
|
+
// if we are here, no instance definition of that name exists. throw error.
|
|
110
|
+
throw Error(`VariantInstance with name "${name}" neither created nor configured.`);
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Creates and registers a unique {@link VariantInstance}.
|
|
115
|
+
*
|
|
116
|
+
* @emits {@link Event.VARIANT_INSTANCE_CREATED}
|
|
117
|
+
*/
|
|
118
|
+
create(dottedPath, name, parameters) {
|
|
119
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
120
|
+
const variant = dottedPath_1.DottedPath.create(dottedPath).path;
|
|
121
|
+
const definition = {
|
|
122
|
+
name: this.ensureUniqueName(name ? name : variant),
|
|
123
|
+
variant: variant,
|
|
124
|
+
parameters: parameters,
|
|
125
|
+
};
|
|
126
|
+
return yield this.createFromDefinition(definition);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Registers a unique {@link VariantInstanceDefinition}.
|
|
131
|
+
*
|
|
132
|
+
* @emits {@link Event.VARIANT_INSTANCE_REGISTERED}
|
|
133
|
+
*/
|
|
134
|
+
register(instanceDefinition) {
|
|
135
|
+
const name = instanceDefinition.name ? instanceDefinition.name : dottedPath_1.DottedPath.create(instanceDefinition.variant).path;
|
|
136
|
+
instanceDefinition.name = this.ensureUniqueName(name);
|
|
137
|
+
this.variantInstanceDefinitions.set(instanceDefinition.name, instanceDefinition);
|
|
138
|
+
this.broadcastEvent(event_1.Event.VARIANT_INSTANCE_REGISTERED, instanceDefinition);
|
|
139
|
+
return instanceDefinition;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Commits the {@link ParameterBag} to all registered and created {@link VariantInstance}s.
|
|
143
|
+
*/
|
|
144
|
+
commitParameters(parameters = {}, onlyVisible = true) {
|
|
145
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
146
|
+
this.parameters = (0, lodash_es_1.merge)(this.parameters, parameters);
|
|
147
|
+
this.allDefinitions.forEach(definition => (definition.parameters = (0, lodash_es_1.merge)(definition.parameters, this.parameters)));
|
|
148
|
+
let instances = this.all;
|
|
149
|
+
if (onlyVisible) {
|
|
150
|
+
instances = this.all.filter(instance => instance.variant.visible);
|
|
151
|
+
}
|
|
152
|
+
const promises = instances.map(instance => instance.variant.commitParameters(this.parameters));
|
|
153
|
+
yield Promise.all(promises);
|
|
154
|
+
return this;
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Show one {@link VariantInstance}. If `exclusive` is set to `true` it will hide all other
|
|
159
|
+
* instances handled by this {@link VariantInstanceManager}. If the function is called synchronously,
|
|
160
|
+
* the calls end up in a queue according to the FIFO principle (the last one wins).
|
|
161
|
+
*/
|
|
162
|
+
show(name, exclusive = false) {
|
|
163
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
164
|
+
const promise = new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
|
|
165
|
+
if (exclusive) {
|
|
166
|
+
this.allDefinitions
|
|
167
|
+
.filter(definition => definition.name !== name)
|
|
168
|
+
.forEach(definition => {
|
|
169
|
+
const parameters = {};
|
|
170
|
+
parameters[parameter_1.Parameter.VISIBLE] = false;
|
|
171
|
+
definition.parameters = (0, lodash_es_1.merge)(definition.parameters, parameters);
|
|
172
|
+
});
|
|
173
|
+
yield Promise.all(this.all
|
|
174
|
+
.filter(_instance => _instance.name !== name)
|
|
175
|
+
.map(_instance => {
|
|
176
|
+
return _instance.variant.hide();
|
|
177
|
+
}));
|
|
178
|
+
}
|
|
179
|
+
const instance = yield this.get(name);
|
|
180
|
+
yield instance.variant.show();
|
|
181
|
+
resolve(instance);
|
|
182
|
+
}));
|
|
183
|
+
if (!this.queues['show']) {
|
|
184
|
+
this.queues['show'] = [];
|
|
185
|
+
}
|
|
186
|
+
this.queues['show'].push(promise);
|
|
187
|
+
for (let i = 0; i < this.queues['show'].length; i++) {
|
|
188
|
+
const instance = yield this.queues['show'][i];
|
|
189
|
+
if (exclusive) {
|
|
190
|
+
if (i + 1 < this.queues['show'].length) {
|
|
191
|
+
yield instance.variant.hide();
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
this.queues['show'] = [];
|
|
196
|
+
return promise;
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Clones and registers a new {@link VariantInstance} with a new, unique name.
|
|
201
|
+
*
|
|
202
|
+
* @param tagMapping Object for renaming tags on the clone, whereas the "key" is the tag name of orignal variant
|
|
203
|
+
* instance and the "value" is the new tag name that should be created from it.\
|
|
204
|
+
* Check the docs of the tag managers [renaming](https://3dviewer.docs.combeenation.com/pages/documentation/Tag-Manager.html#uniqueness-of-node-and-tag-names)
|
|
205
|
+
* chapter for further details.
|
|
206
|
+
*
|
|
207
|
+
* @emits {@link Event.VARIANT_INSTANCE_CLONED}
|
|
208
|
+
*/
|
|
209
|
+
clone(name, newName, parameters, tagMapping) {
|
|
210
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
211
|
+
if (!this.variantInstances.has(name)) {
|
|
212
|
+
throw Error(`VariantInstance with name "${name}" does not exist.`);
|
|
213
|
+
}
|
|
214
|
+
if (this.viewer.tagManager.parameters.size > 0) {
|
|
215
|
+
// visible state of nodes should be updated by tag manager if it is in use
|
|
216
|
+
// this logic is programmed in the elements `addParameterObservers` function and will be triggered by setting the
|
|
217
|
+
// variants visibility to true
|
|
218
|
+
parameters = Object.assign({ [parameter_1.Parameter.VISIBLE]: true }, (parameters !== null && parameters !== void 0 ? parameters : {}));
|
|
219
|
+
}
|
|
220
|
+
const variantInstance = this.variantInstances.get(name);
|
|
221
|
+
const definition = {
|
|
222
|
+
name: this.ensureUniqueName(newName ? newName : name),
|
|
223
|
+
variant: variantInstance.variant.id,
|
|
224
|
+
parameters: parameters,
|
|
225
|
+
};
|
|
226
|
+
const variantInstanceClone = yield variantInstance_1.VariantInstance.createLiving(variantInstance.variant, definition.name, definition.parameters);
|
|
227
|
+
this.variantInstances.set(definition.name, variantInstanceClone);
|
|
228
|
+
(0, babylonHelper_1.reportDuplicateNodeNames)((0, babylonHelper_1.duplicateNodeNames)(this.viewer.scene.getNodes(), n => n instanceof transformNode_1.TransformNode));
|
|
229
|
+
this.viewer.tagManager.mapNodesAndTags(variantInstanceClone.variant.elementNodesFlat, tagMapping !== null && tagMapping !== void 0 ? tagMapping : {});
|
|
230
|
+
yield this.viewer.tagManager.applyExistingParameterValuesTo([
|
|
231
|
+
...variantInstanceClone.variant.elementNodesFlat,
|
|
232
|
+
...variantInstanceClone.variant.elementAbstractMeshesFlat.map(n => n.material).filter(m => !!m),
|
|
233
|
+
]);
|
|
234
|
+
variantInstance.broadcastEvent(event_1.Event.VARIANT_INSTANCE_CLONED, variantInstanceClone);
|
|
235
|
+
return variantInstanceClone;
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Destroys the instance by removing the {@link Variant} and de-registering the instance.
|
|
240
|
+
*
|
|
241
|
+
* @emits {@link Event.VARIANT_INSTANCE_DESTROYED}
|
|
242
|
+
*/
|
|
243
|
+
destroy(name) {
|
|
244
|
+
if (!this.variantInstances.has(name)) {
|
|
245
|
+
throw Error(`VariantInstance with name "${name}" does not exist.`);
|
|
246
|
+
}
|
|
247
|
+
const variantInstance = this.variantInstances.get(name);
|
|
248
|
+
variantInstance.variant.destroy();
|
|
249
|
+
this.variantInstances.delete(name);
|
|
250
|
+
variantInstance.broadcastEvent(event_1.Event.VARIANT_INSTANCE_DESTROYED, variantInstance);
|
|
251
|
+
return this;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Creates and registers a unique {@link VariantInstance} from `definition`.
|
|
255
|
+
*
|
|
256
|
+
* @emits {@link Event.VARIANT_INSTANCE_CREATED}
|
|
257
|
+
*/
|
|
258
|
+
createFromDefinition(definition) {
|
|
259
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
260
|
+
if (!definition.name) {
|
|
261
|
+
throw new Error(`Given VariantInstanceDefinition ${definition.variant} is missing a name property.`);
|
|
262
|
+
}
|
|
263
|
+
if (this.variantInstances.has(definition.name)) {
|
|
264
|
+
throw new Error(`VariantInstance with name "${definition.name}" is already created.`);
|
|
265
|
+
}
|
|
266
|
+
if (!this.rootVariant) {
|
|
267
|
+
throw new Error(`Viewer has not been properly bootstrapped (rootVariant missing).`);
|
|
268
|
+
}
|
|
269
|
+
const variant = yield this.rootVariant.getDescendant(definition.variant);
|
|
270
|
+
const variantInstance = yield variantInstance_1.VariantInstance.createLiving(variant, definition.name, definition.parameters);
|
|
271
|
+
this.variantInstances.set(definition.name, variantInstance);
|
|
272
|
+
this.broadcastEvent(event_1.Event.VARIANT_INSTANCE_CREATED, variantInstance);
|
|
273
|
+
return variantInstance;
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Creates a unique instance name based on already registered instance names on the {@link VariantInstanceManager}.
|
|
278
|
+
* Appends an `_i` to the name where `i = 0` and increments as long as the name is not registered.
|
|
279
|
+
*/
|
|
280
|
+
ensureUniqueName(name) {
|
|
281
|
+
let instanceName = name;
|
|
282
|
+
let i = 1;
|
|
283
|
+
while (this.has(instanceName)) {
|
|
284
|
+
instanceName = `${name}_${i}`;
|
|
285
|
+
i++;
|
|
286
|
+
}
|
|
287
|
+
return instanceName;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
exports.VariantInstanceManager = VariantInstanceManager;
|
|
291
291
|
//# sourceMappingURL=variantInstanceManager.js.map
|
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
export declare class SpecStorage {
|
|
2
|
-
protected spec: StructureJson;
|
|
3
|
-
static instance: SpecStorage | undefined;
|
|
4
|
-
/**
|
|
5
|
-
* Constructor.
|
|
6
|
-
*/
|
|
7
|
-
protected constructor(spec: StructureJson);
|
|
8
|
-
/**
|
|
9
|
-
* Creates {@link SpecStorage} from spec.
|
|
10
|
-
*/
|
|
11
|
-
static createFromSpec(structureJson: StructureJson): SpecStorage;
|
|
12
|
-
/**
|
|
13
|
-
* Gets the {@link SpecStorage} instance.
|
|
14
|
-
*/
|
|
15
|
-
static getInstance(): SpecStorage;
|
|
16
|
-
/**
|
|
17
|
-
* Gets the spec from the {@link SpecStorage}.
|
|
18
|
-
*/
|
|
19
|
-
static getSpec(): StructureJson;
|
|
20
|
-
/**
|
|
21
|
-
* Destroys the current instance to allow reinstantiating the SpecStorage.
|
|
22
|
-
*/
|
|
23
|
-
static destroy(): void;
|
|
24
|
-
/**
|
|
25
|
-
* Gets spec from key.
|
|
26
|
-
*/
|
|
27
|
-
static get<T>(key?: string): T;
|
|
28
|
-
/**
|
|
29
|
-
* Sets value for spec for given key.
|
|
30
|
-
*/
|
|
31
|
-
static set<T>(key: string, value: T): typeof SpecStorage;
|
|
32
|
-
}
|
|
1
|
+
export declare class SpecStorage {
|
|
2
|
+
protected spec: StructureJson;
|
|
3
|
+
static instance: SpecStorage | undefined;
|
|
4
|
+
/**
|
|
5
|
+
* Constructor.
|
|
6
|
+
*/
|
|
7
|
+
protected constructor(spec: StructureJson);
|
|
8
|
+
/**
|
|
9
|
+
* Creates {@link SpecStorage} from spec.
|
|
10
|
+
*/
|
|
11
|
+
static createFromSpec(structureJson: StructureJson): SpecStorage;
|
|
12
|
+
/**
|
|
13
|
+
* Gets the {@link SpecStorage} instance.
|
|
14
|
+
*/
|
|
15
|
+
static getInstance(): SpecStorage;
|
|
16
|
+
/**
|
|
17
|
+
* Gets the spec from the {@link SpecStorage}.
|
|
18
|
+
*/
|
|
19
|
+
static getSpec(): StructureJson;
|
|
20
|
+
/**
|
|
21
|
+
* Destroys the current instance to allow reinstantiating the SpecStorage.
|
|
22
|
+
*/
|
|
23
|
+
static destroy(): void;
|
|
24
|
+
/**
|
|
25
|
+
* Gets spec from key.
|
|
26
|
+
*/
|
|
27
|
+
static get<T>(key?: string): T;
|
|
28
|
+
/**
|
|
29
|
+
* Sets value for spec for given key.
|
|
30
|
+
*/
|
|
31
|
+
static set<T>(key: string, value: T): typeof SpecStorage;
|
|
32
|
+
}
|