-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha-frame-href.js
More file actions
292 lines (252 loc) · 8.76 KB
/
a-frame-href.js
File metadata and controls
292 lines (252 loc) · 8.76 KB
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
/* globals AFRAME, sessionStorage, THREE, URLSearchParams */
(function () {
// Do not log in production.
var debug = window.location.protocol !== 'https:';
var log = debug ? console.log.bind(console) : function () {};
var registerComponent = function () {
if (typeof AFRAME === 'undefined') {
throw new Error('Component attempted to register before AFRAME ' +
'was available.');
}
/**
* Hyperlink component for A-Frame.
*/
AFRAME.registerComponent('href', {
schema: {
default: ''
},
/**
* Called once when component is attached.
*/
init: function () {
this.handler = this.handler.bind(this);
this.el.addEventListener('click', this.handler);
this.el.addEventListener('gripdown', this.handler);
this.setupHighlight();
},
/**
* Called when component is removed.
*/
remove: function () {
this.el.removeEventListener('click', this.handler);
this.el.removeEventListener('gripdown', this.handler);
},
handler: function () {
var url = this.data;
this.el.emit('navigate', url);
window.location.href = url;
},
setupHighlight: function () {
// Clone mesh and set up highlighter material.
var mesh = this.el.object3DMap.mesh;
if (!mesh) {
return false;
}
var clone = mesh.clone();
clone.material = new THREE.MeshBasicMaterial({
color: 0x0000ff,
transparent: true,
opacity: 0.3
});
clone.scale.set(1.2, 1.2, 1.2);
clone.visible = false;
mesh.parent.add(clone);
// Toggle highlighter on mouse events.
this.el.addEventListener('mouseenter', function () {
// clone.visible = true;
});
this.el.addEventListener('mouseleave', function () {
// clone.visible = false;
});
}
});
};
var initScenesCalled = false;
var supportsVR = navigator.getVRDisplays && navigator.vrEnabled !== false;
var whenScene = function (scene, event, callback) {
// TODO: Return Promises.
if (event === 'loaded' && scene.hasLoaded) {
callback();
return;
}
scene.addEventListener(event, callback);
};
var persistActiveVRDisplaysIDs = function (displays) {
displays = displays || [];
var activeVRDisplaysIDs = JSON.stringify(displays.map(function (display) {
return display.displayId;
}));
sessionStorage.activeVRDisplaysIDs = activeVRDisplaysIDs;
return activeVRDisplaysIDs;
};
var sceneLoaded = function (scene, displays) {
var shouldPresent = false;
if (sessionStorage.vrNavigation === 'true') {
shouldPresent = true;
delete sessionStorage.vrNavigation;
}
// Valid options for auto-presenting in stereo VR mode:
// - `?vr`, `?vr=true`, `?vr=1`, `?vr=2`, etc.
// - `?vr-display-name-filter=oculus`, `?vr-display-name-filter=vive`, etc.
// - `?vr-display-id=1`, `?vr-display-id=2`, etc.
// - `?vr-mode`, `?vr-mode=stereo`
if (displays && 'URLSearchParams' in window) {
var qs = new URLSearchParams(window.location.search);
var qsGetClean = function (key) {
return (qs.get(key) || '').trim();
};
var qsVr = qsGetClean('vr');
var qsVrDisplayId = qsGetClean('vr-display-id');
var qsVrDisplayNameFilter = qsGetClean('vr-display-name-filter');
var qsVrMode = qsGetClean('vr-mode');
if ((qs.has('vr') && qsVr !== 'false' && qsVr !== '0') ||
(qs.has('vr-display-name-filter') && qsVrDisplayNameFilter !== '') ||
(qs.has('vr-display-id') && qsVrDisplayId !== '') ||
(qs.has('vr-mode') && qsVrMode !== 'mono')) {
shouldPresent = true;
var displayId;
if (qs.has('vr')) {
displayId = parseInt(qsVr, 10);
}
if (qs.has('vr-display-id')) {
displayId = parseInt(qsVrDisplayId, 10);
// `VRDisplay#displayId`s are one-based indexed.
if (displayId === 0) {
displayId = 1;
}
}
if (displayId) {
displays = displays.filter(function (display) {
return display.displayId === displayId;
});
persistActiveVRDisplaysIDs(displays);
} else if (qs.has('vr')) {
// Handle `?vr=true`, for example.
persistActiveVRDisplaysIDs(displays);
}
var displayName = qsVrDisplayNameFilter.toLowerCase();
if (displayName) {
displays = displays.filter(function (display) {
return (display.displayName || '').toLowerCase().indexOf(displayName) !== -1;
});
persistActiveVRDisplaysIDs(displays);
}
} else if (qs.has('vr') ||
qs.has('vr-display-name-filter') ||
qs.has('vr-display-id') ||
qs.has('vr-mode')) {
shouldPresent = false;
}
}
// TODO: Add `postMessage` event listener.
if (!scene) {
return;
}
scene.dataset.isLoaded = 'true';
if (!displays || !supportsVR || !shouldPresent) {
return;
}
var toPresent = [];
if (navigator.activeVRDisplays && navigator.activeVRDisplays.length) {
toPresent = navigator.activeVRDisplays;
}
// For navigation.
if (sessionStorage && sessionStorage.activeVRDisplaysIDs) {
var displayIDs = [];
try {
displayIDs = JSON.parse(sessionStorage.activeVRDisplaysIDs);
} catch (e) {
}
toPresent = displayIDs.filter(function (displayID) {
return displayID;
});
}
if (toPresent.length) {
// TODO: Handle entering multiple scenes.
// TODO: Update A-Frame for `<a-scene>`.`enterVR()` to accept an
// explicit `VRDisplay` to present to.
if (scene.enterVR) {
return scene.enterVR(toPresent[0]);
}
}
};
var handleDisplays = function (displays) {
if (!displays.length) { return; }
log('gotDisplays', displays);
return displays;
};
var initScenes = function () {
log('initScenes: called', initScenesCalled);
if (initScenesCalled) {
return;
}
initScenesCalled = true;
var scenes = document.querySelectorAll('a-scene');
if (!scenes.length) {
return;
}
log('initScenes: checking', initScenesCalled);
Array.prototype.forEach.call(scenes, function (scene) {
scene.addEventListener('click', function (e) {
if (e.detail && e.detail.intersectedEl && e.detail.intersectedEl.hasAttribute('href')) {
// Fade out to black (isn't super noticeable because navigation
// happens so quickly).
scene.dataset.isLoaded = 'false';
}
});
whenScene(scene, 'loaded', function () {
log('initScenes: loaded', initScenesCalled);
if (supportsVR) {
// NOTE: This `navigator.getVRDisplays` call is needed by both
// Firefox Nightly and experimental Chromium builds currently.
// And we use it to pass `displays` to `sceneLoaded`, but even
// if we weren't, we still need this call to "initialise" the
// WebVR code path in the aforementioned browsers.
return navigator.getVRDisplays().then(handleDisplays).then(function (displays) {
return sceneLoaded(scene, displays);
});
} else {
return sceneLoaded(scene);
}
});
});
};
var activeVRDisplaysUpdate = function (displays) {
// Polyfilling `navigator.activeVRDisplays` if unavailable.
if (!('activeVRDisplays' in navigator)) {
navigator.activeVRDisplays = displays.filter(function (display) {
return display.isPresenting;
});
}
if (sessionStorage.vrNavigation === 'true') {
return;
}
persistActiveVRDisplaysIDs(navigator.activeVRDisplays);
};
registerComponent();
if (navigator.getVRDisplays && navigator.vrEnabled !== false) {
navigator.getVRDisplays().then(activeVRDisplaysUpdate);
}
window.addEventListener('vrdisplaypresentchange', function (e) {
// Implementation status notes:
// - Firefox: `display` and `reason` are passed in the event instance.
// - Chromium WebVR builds: `display` is supported; `reason` is
// supported, but not yet for `navigation`.
log('"' + e.type + '" event fired');
activeVRDisplaysUpdate();
});
window.addEventListener('load', function (e) {
log(e.type, e);
initScenes();
});
window.addEventListener('beforeunload', function (e) {
log(e.type, e);
sessionStorage.vrNavigation = !!(navigator.activeVRDisplays && navigator.activeVRDisplays.length);
});
window.addEventListener('vrdisplayactivate', function (e) {
log(e.type, e);
});
window.addEventListener('vrdisplaydeactivate', function (e) {
log(e.type, e);
});
})();