-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathexamples.js
76 lines (68 loc) · 2.37 KB
/
examples.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
/* global $ */
(function () {
// Remove non-mobile examples on mobile.
if (settings.isMobile) {
var nonMobileExamples = document.querySelectorAll('[data-supports-mobile="false"]');
for (var i = 0; i < nonMobileExamples.length; i++) {
nonMobileExamples[i].parentNode.removeChild(nonMobileExamples[i]);
}
}
// Hook up arrow keys.
document.body.addEventListener('keyup', function (e) {
var currentLink;
var left = e.keyCode === 37;
var right = e.keyCode === 39;
if (!left && !right) { return; }
currentLink = getCurrentNavLink();
if (!currentLink) { return; }
clickNavLink(left ? getPrevNavLink() : getNextNavLink());
});
// Hook up navigation arrows.
var examplePrev = document.querySelector('#examplePrev');
var exampleNext = document.querySelector('#exampleNext');
examplePrev.addEventListener('click', function () {
window.location.href = getDestNavLink(true);
});
exampleNext.addEventListener('click', function () {
window.location.href = getDestNavLink(false);
});
// Inspector.
var exampleInspector = document.querySelector('#exampleInspector');
if (exampleInspector) {
var exampleIframe = document.querySelector('#exampleIframe');
var exampleViewsource = document.querySelector('#exampleViewSource');
exampleInspector.addEventListener('click', function () {
// <ctrl> + <alt> + i.
exampleIframe.contentWindow.postMessage('INJECT_AFRAME_INSPECTOR', '*');
exampleInspector.style.display = 'none';
exampleViewsource.style.display = 'none';
});
}
function clickNavLink (link) {
var currentLink = getCurrentNavLink();
if (!currentLink || currentLink === link) { return; }
currentLink.classList.remove('current');
currentLink.classList.remove('click');
// Click link.
link.classList.add('click');
link.click();
currentLink = link;
currentLink.classList.add('current');
currentLink.classList.remove('click');
}
function getCurrentNavLink () {
return document.querySelector('.examples-subnav li[data-current="true"]');
}
function getDestNavLink (prev) {
var link = getCurrentNavLink()
var newLink = prev ? link.previousElementSibling : link.nextElementSibling;
if (!newLink) { newLink = document.querySelector('.examples-subnav > li:first-child'); }
return newLink.querySelector('a').getAttribute('href');
}
function getPrevNavLink () {
return getDestNavLink(true);
}
function getNextNavLink () {
return getDestNavLink(false);
}
})();