Skip to content

Commit 717f1f3

Browse files
committed
Use component event handlers insted of bound a-scene methods
1 parent c1ed0f7 commit 717f1f3

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

src/components/scene/vr-mode-ui.js

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,14 @@ module.exports.Component = registerComponent('vr-mode-ui', {
2424

2525
if (utils.getUrlParameter('ui') === 'false') { return; }
2626

27-
this.enterVR = bind(sceneEl.enterVR, sceneEl);
28-
this.exitVR = bind(sceneEl.exitVR, sceneEl);
2927
this.insideLoader = false;
3028
this.enterVREl = null;
3129
this.orientationModalEl = null;
30+
this.bindMethods();
3231

3332
// Hide/show VR UI when entering/exiting VR mode.
34-
sceneEl.addEventListener('enter-vr', bind(this.updateEnterVRInterface, this));
35-
sceneEl.addEventListener('exit-vr', bind(this.updateEnterVRInterface, this));
33+
sceneEl.addEventListener('enter-vr', this.updateEnterVRInterface);
34+
sceneEl.addEventListener('exit-vr', this.updateEnterVRInterface);
3635

3736
window.addEventListener('message', function (event) {
3837
if (event.data.type === 'loaderReady') {
@@ -42,8 +41,22 @@ module.exports.Component = registerComponent('vr-mode-ui', {
4241
});
4342

4443
// Modal that tells the user to change orientation if in portrait.
45-
window.addEventListener('orientationchange',
46-
bind(this.toggleOrientationModalIfNeeded, this));
44+
window.addEventListener('orientationchange', this.toggleOrientationModalIfNeeded);
45+
},
46+
47+
bindMethods: function () {
48+
this.onEnterVRButtonClick = bind(this.onEnterVRButtonClick, this);
49+
this.onModalClick = bind(this.onModalClick, this);
50+
this.toggleOrientationModalIfNeeded = bind(this.toggleOrientationModalIfNeeded, this);
51+
this.updateEnterVRInterface = bind(this.updateEnterVRInterface, this);
52+
},
53+
54+
onModalClick: function () {
55+
this.el.exitVR();
56+
},
57+
58+
onEnterVRButtonClick: function () {
59+
this.el.enterVR();
4760
},
4861

4962
update: function () {
@@ -55,10 +68,10 @@ module.exports.Component = registerComponent('vr-mode-ui', {
5568
if (this.enterVREl || this.orientationModalEl) { return; }
5669

5770
// Add UI if enabled and not already present.
58-
this.enterVREl = createEnterVRButton(this.enterVR);
71+
this.enterVREl = createEnterVRButton(this.onEnterVRButtonClick);
5972
sceneEl.appendChild(this.enterVREl);
6073

61-
this.orientationModalEl = createOrientationModal(this.exitVR);
74+
this.orientationModalEl = createOrientationModal(this.onModalClick);
6275
sceneEl.appendChild(this.orientationModalEl);
6376

6477
this.updateEnterVRInterface();
@@ -105,10 +118,10 @@ module.exports.Component = registerComponent('vr-mode-ui', {
105118
*
106119
* Structure: <div><button></div>
107120
*
108-
* @param {function} enterVRHandler
121+
* @param {function} onClick - click event handler
109122
* @returns {Element} Wrapper <div>.
110123
*/
111-
function createEnterVRButton (enterVRHandler) {
124+
function createEnterVRButton (onClick) {
112125
var vrButton;
113126
var wrapper;
114127

@@ -124,16 +137,18 @@ function createEnterVRButton (enterVRHandler) {
124137
// Insert elements.
125138
wrapper.appendChild(vrButton);
126139
vrButton.addEventListener('click', function (evt) {
127-
enterVRHandler();
140+
onClick();
128141
});
129142
return wrapper;
130143
}
131144

132145
/**
133-
* Create a modal that tells mobile users to orient the phone to landscape.
134-
* Add a close button that if clicked, exits VR and closes the modal.
146+
* Creates a modal dialog to request the user to switch to landscape orientation.
147+
* *
148+
* @param {function} onClick - click event handler
149+
* @returns {Element} Wrapper <div>.
135150
*/
136-
function createOrientationModal (exitVRHandler) {
151+
function createOrientationModal (onClick) {
137152
var modal = document.createElement('div');
138153
modal.className = ORIENTATION_MODAL_CLASS;
139154
modal.classList.add(HIDDEN_CLASS);
@@ -144,7 +159,7 @@ function createOrientationModal (exitVRHandler) {
144159
exit.innerHTML = 'Exit VR';
145160

146161
// Exit VR on close.
147-
exit.addEventListener('click', exitVRHandler);
162+
exit.addEventListener('click', onClick);
148163

149164
modal.appendChild(exit);
150165

0 commit comments

Comments
 (0)