-
Notifications
You must be signed in to change notification settings - Fork 1
/
aframe-open-page-iframe.js
87 lines (77 loc) · 2.47 KB
/
aframe-open-page-iframe.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
AFRAME.registerComponent('open-page-iframe', {
schema: {
event: { type: "string", default: "click" },
url: { type: "string", default: "" }
},
init() {
let data = this.data;
let el = this.el;
if (data.event && data.url) {
el.addEventListener(data.event, this.openIframe.bind(this));
}
this.mountStyles();
},
mountStyles() {
let styles = document.querySelector(this.modalStyleSelector);
if (styles === null) {
let template = `<style id="${this.modalStyleSelector}">
${this.modalSelector}.page__modal {
position: fixed;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
width: 90vw;
height: 70vh;
}
${this.modalSelector}.page__modal .page__modal-header {
width: 100%;
display: flex;
flex-direction: row-reverse;
}
${this.modalSelector}.page__modal iframe {
width: 100%;
height: 100%;
}
</style>`;
document.body.insertAdjacentHTML('beforeend', template);
}
},
getSceneEl() {
return this.el.sceneEl;
},
openIframe() {
this.usingVRMode = this.getSceneEl().is('vr-mode');
this.getSceneEl().exitVR();
let modal = this.mountHTML();
modal.focus();
},
closeIframe() {
this.clearGarbage();
if (this.usingVRMode) {
this.getSceneEl().enterVR();
}
this.getSceneEl().focus();
},
get modalSelector() {
return '#a_open_page_iframe';
},
get modalStyleSelector() {
return '#a_open_page_css';
},
clearGarbage() {
document.querySelectorAll(this.modalSelector).forEach(item => item.remove());
},
mountHTML() {
this.clearGarbage();
let template = `<div id="a_open_page_iframe" class="page__modal">
<div class="page__modal-header">
<button class="close">back to VR</button>
</div>
<iframe src="${this.data.url}" frameborder="0" frameborder="0"></iframe>
</div>`;
document.body.insertAdjacentHTML('beforeend', template);
let modal = document.querySelector(this.modalSelector);
modal.querySelector('.close').addEventListener('click', this.closeIframe.bind(this));
return modal;
},
});