1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 {name
: 'bullet', text
: 'Bullet Physics'},
7 {name
: 'earth', text
: 'Raycasted Earth'},
8 {name
: 'lua', text
: 'Lua Interpreter'},
9 {name
: 'life', text
: 'Game of Life'},
10 {name
: 'voronoi', text
: 'Voronoi Simulation'},
11 {name
: 'smoothlife', text
: 'SmoothLife'},
12 {name
: 'cube', text
: 'Rotating Cube'},
14 var exampleMap
= {}; // Created below.
17 var isChrome
= /Chrome\/([^\s]+)/.test(navigator
.userAgent
);
18 var isMobile
= /Mobi/.test(navigator
.userAgent
);
19 var hasPnacl
= navigator
.mimeTypes
['application/x-pnacl'] !== undefined;
21 if (isChrome
&& !isMobile
) {
24 window
.onpopstate = function(popState
) {
25 if (popState
.state
== null) {
26 updateViewFromLocation();
28 var exampleName
= popState
.state
;
29 loadExample(popState
.state
);
33 // Older version of Chrome?
34 showOldChromeErrorMessage();
37 // Not Chrome, or is mobile Chrome.
38 showNotChromeErrorMessage();
41 function makeExampleList() {
42 var listEl
= document
.querySelector('nav ul');
43 for (var i
= 0; i
< examples
.length
; ++i
) {
44 var example
= examples
[i
];
46 // Populate exampleMap
47 exampleMap
[example
.name
] = example
;
50 var listItemEl
= document
.createElement('li');
51 var anchorEl
= document
.createElement('a');
52 listItemEl
.setAttribute('id', example
.name
);
53 anchorEl
.setAttribute('href', getExampleUrl(example
.name
));
54 anchorEl
.setAttribute('target', 'content');
55 anchorEl
.textContent
= example
.text
;
57 // Listen for clicks and update the nav
58 anchorEl
.addEventListener('click', onLinkClick
.bind(example
), false);
60 listItemEl
.appendChild(anchorEl
);
61 listEl
.appendChild(listItemEl
);
65 function getExampleUrl(exampleName
) {
66 return '/static/' + exampleName
+ '/index.html';
69 function onLinkClick(evt
) {
72 loadExample(this.name
);
75 function updateViewFromLocation() {
76 // Get the location's path.
77 var anchorEl
= document
.createElement('a');
78 anchorEl
.href
= location
.href
;
79 var pathname
= anchorEl
.pathname
;
81 // Load the correct page, based on the demo name.
82 var matches
= pathname
.match(/demo(?:\/(.*))?$/);
85 var matchedExample
= matches
[1];
86 // Strip trailing slash, if any.
87 if (matchedExample
&& matchedExample
.slice(-1) === '/') {
88 matchedExample
= matchedExample
.slice(0, -1);
91 if (matchedExample
in exampleMap
) {
92 replaceState(matchedExample
);
93 loadExample(matchedExample
);
101 function loadExample(exampleName
) {
102 updateTitle(exampleName
);
103 createExampleIframe(exampleName
);
104 updateNav(exampleName
);
107 function updateNav(exampleName
) {
108 var links
= document
.querySelectorAll('li a');
109 for (var l
= 0; l
< links
.length
; l
++) {
110 links
[l
].classList
.remove('active');
113 if (exampleName
!= 'home')
114 document
.querySelector('li#' + exampleName
+ ' a').classList
.add('active');
117 function updateTitle(exampleName
) {
118 var title
= 'PNaCl Demos';
119 if (exampleName
!= 'home') {
120 title
+= ': ' + exampleMap
[exampleName
].text
;
122 document
.title
= title
;
125 function createExampleIframe(exampleName
) {
126 createIframe(getExampleUrl(exampleName
));
129 function createHomeIframe() {
130 createIframe('/static/home/index.html');
133 function createIframe(src
) {
134 var oldIframeEl
= iframeEl
;
136 iframeEl
= document
.createElement('iframe');
137 iframeEl
.setAttribute('frameborder', '0');
138 iframeEl
.setAttribute('width', '100%');
139 iframeEl
.setAttribute('height', '100%');
141 iframeEl
.setAttribute('hidden', '');
142 iframeEl
.onload = function() {
144 oldIframeEl
.parentNode
.removeChild(oldIframeEl
);
145 iframeEl
.removeAttribute('hidden');
147 document
.querySelector('section').appendChild(iframeEl
);
150 function pushState(exampleName
) {
151 window
.history
.pushState(exampleName
, '', '/demo/' + exampleName
);
154 function replaceState(exampleName
) {
155 window
.history
.replaceState(exampleName
, '', '/demo/' + exampleName
);
158 function replaceHomeState() {
159 window
.history
.replaceState('home', '', '/demo');
162 function showOldChromeErrorMessage() {
163 document
.getElementById('old-chrome').removeAttribute('hidden');
166 function showNotChromeErrorMessage() {
167 document
.getElementById('not-chrome').removeAttribute('hidden');