Skip to content

Commit 0774390

Browse files
feat(update): update entities when component updates
1 parent 67bb6c2 commit 0774390

File tree

3 files changed

+69
-35
lines changed

3 files changed

+69
-35
lines changed

dist/aframe-dialog-popup-component.js

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,12 @@ AFRAME.registerComponent('dialog-popup', {
179179
}
180180
},
181181
multiple: true,
182-
dialogPlane: null,
183-
openIcon: null,
184-
closeIcon: null,
182+
dialogPlaneEl: null,
183+
openIconEl: null,
184+
closeIconEl: null,
185+
titleEl: null,
186+
bodyEl: null,
187+
imageEl: null,
185188
hasImage: false,
186189

187190
/**
@@ -206,8 +209,18 @@ AFRAME.registerComponent('dialog-popup', {
206209
*/
207210
remove: function remove() {
208211
var openOn = this.data.openOn;
209-
this.openIcon.removeEventListener(openOn, this.toggleDialogOpen.bind(this));
210-
this.closeIcon.removeEventListener(openOn, this.toggleDialogOpen.bind(this));
212+
this.openIconEl.removeEventListener(openOn, this.toggleDialogOpen.bind(this));
213+
this.closeIconEl.removeEventListener(openOn, this.toggleDialogOpen.bind(this));
214+
},
215+
216+
/**
217+
* When this component is updated, re-calculate title, body, image, and
218+
* dialog plane to incorporate changes.
219+
*/
220+
update: function update() {
221+
this.generateTitle();
222+
this.generateBody();
223+
this.generateImage();
211224
},
212225

213226
/**
@@ -216,9 +229,9 @@ AFRAME.registerComponent('dialog-popup', {
216229
toggleDialogOpen: function toggleDialogOpen() {
217230
this.isOpen = !this.isOpen;
218231

219-
if (this.dialogPlane) {
232+
if (this.dialogPlaneEl) {
220233
this.positionDialogPlane();
221-
this.dialogPlane.setAttribute('visible', this.isOpen);
234+
this.dialogPlaneEl.setAttribute('visible', this.isOpen);
222235
}
223236
},
224237

@@ -251,7 +264,7 @@ AFRAME.registerComponent('dialog-popup', {
251264
}
252265

253266
openIcon.addEventListener(openOn, this.toggleDialogOpen.bind(this));
254-
this.openIcon = openIcon;
267+
this.openIconEl = openIcon;
255268
return openIcon;
256269
},
257270

@@ -282,7 +295,7 @@ AFRAME.registerComponent('dialog-popup', {
282295
src: src
283296
});
284297
closeIcon.addEventListener(openOn, this.toggleDialogOpen.bind(this));
285-
this.closeIcon = closeIcon;
298+
this.closeIconEl = closeIcon;
286299
return closeIcon;
287300
},
288301

@@ -299,7 +312,7 @@ AFRAME.registerComponent('dialog-popup', {
299312
height = _this$data3.dialogBoxHeight,
300313
padding = _this$data3.dialogBoxPadding,
301314
imageHeight = _this$data3.imageHeight;
302-
var title = document.createElement('a-entity');
315+
var title = this.titleEl || document.createElement('a-entity');
303316
title.setAttribute('id', "".concat(this.el.getAttribute('id'), "--title"));
304317
title.setAttribute('text', {
305318
value: value.substring(0, wrapCount),
@@ -321,6 +334,7 @@ AFRAME.registerComponent('dialog-popup', {
321334
y: y,
322335
z: 0.01
323336
});
337+
this.titleEl = title;
324338
return title;
325339
},
326340

@@ -337,7 +351,7 @@ AFRAME.registerComponent('dialog-popup', {
337351
height = _this$data4.dialogBoxHeight,
338352
padding = _this$data4.dialogBoxPadding,
339353
imageHeight = _this$data4.imageHeight;
340-
var body = document.createElement('a-entity');
354+
var body = this.bodyEl || document.createElement('a-entity');
341355
body.setAttribute('id', "".concat(this.el.getAttribute('id'), "--title"));
342356
body.setAttribute('text', {
343357
value: value,
@@ -359,6 +373,7 @@ AFRAME.registerComponent('dialog-popup', {
359373
y: y,
360374
z: 0.01
361375
});
376+
this.bodyEl = body;
362377
return body;
363378
},
364379

@@ -376,7 +391,7 @@ AFRAME.registerComponent('dialog-popup', {
376391
return null;
377392
}
378393

379-
var image = document.createElement('a-image');
394+
var image = this.imageEl || document.createElement('a-image');
380395
image.setAttribute('id', "".concat(this.el.getAttribute('id'), "--image"));
381396
image.setAttribute('src', src);
382397
image.setAttribute('width', width);
@@ -387,6 +402,7 @@ AFRAME.registerComponent('dialog-popup', {
387402
z: 0.01
388403
});
389404
this.hasImage = true;
405+
this.imageEl = image;
390406
return image;
391407
},
392408

@@ -399,7 +415,7 @@ AFRAME.registerComponent('dialog-popup', {
399415
height = _this$data6.dialogBoxHeight,
400416
padding = _this$data6.dialogBoxPadding,
401417
color = _this$data6.dialogBoxColor;
402-
var plane = document.createElement('a-entity');
418+
var plane = this.dialogPlaneEl || document.createElement('a-entity');
403419
plane.setAttribute('id', "".concat(this.el.getAttribute('id'), "--dialog-plane"));
404420
plane.setAttribute('visible', false);
405421
plane.setAttribute('geometry', {
@@ -419,13 +435,13 @@ AFRAME.registerComponent('dialog-popup', {
419435
plane.appendChild(this.generateCloseIcon());
420436
plane.appendChild(this.generateTitle());
421437
plane.appendChild(this.generateBody());
422-
this.dialogPlane = plane;
438+
this.dialogPlaneEl = plane;
423439
return plane;
424440
},
425441
positionDialogPlane: function positionDialogPlane() {
426-
if (this.dialogPlane) {
427-
var vector = this.dialogPlane.object3D.parent.worldToLocal(this.cameraEl.object3D.getWorldPosition());
428-
this.dialogPlane.object3D.lookAt(vector);
442+
if (this.dialogPlaneEl) {
443+
var vector = this.dialogPlaneEl.object3D.parent.worldToLocal(this.cameraEl.object3D.getWorldPosition());
444+
this.dialogPlaneEl.object3D.lookAt(vector);
429445
}
430446
},
431447
spawnEntities: function spawnEntities() {

0 commit comments

Comments
 (0)