speaker-view.html 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. <html lang="en">
  2. <head>
  3. <meta charset="utf-8">
  4. <title>reveal.js - Speaker View</title>
  5. <style>
  6. body {
  7. font-family: Helvetica;
  8. font-size: 18px;
  9. }
  10. #current-slide,
  11. #upcoming-slide,
  12. #speaker-controls {
  13. padding: 6px;
  14. box-sizing: border-box;
  15. -moz-box-sizing: border-box;
  16. }
  17. #current-slide iframe,
  18. #upcoming-slide iframe {
  19. width: 100%;
  20. height: 100%;
  21. border: 1px solid #ddd;
  22. }
  23. #current-slide .label,
  24. #upcoming-slide .label {
  25. position: absolute;
  26. top: 10px;
  27. left: 10px;
  28. z-index: 2;
  29. }
  30. #connection-status {
  31. position: absolute;
  32. top: 0;
  33. left: 0;
  34. width: 100%;
  35. height: 100%;
  36. z-index: 20;
  37. padding: 30% 20% 20% 20%;
  38. font-size: 18px;
  39. color: #222;
  40. background: #fff;
  41. text-align: center;
  42. box-sizing: border-box;
  43. line-height: 1.4;
  44. }
  45. .overlay-element {
  46. height: 34px;
  47. line-height: 34px;
  48. padding: 0 10px;
  49. text-shadow: none;
  50. background: rgba( 220, 220, 220, 0.8 );
  51. color: #222;
  52. font-size: 14px;
  53. }
  54. .overlay-element.interactive:hover {
  55. background: rgba( 220, 220, 220, 1 );
  56. }
  57. #current-slide {
  58. position: absolute;
  59. width: 60%;
  60. height: 100%;
  61. top: 0;
  62. left: 0;
  63. padding-right: 0;
  64. }
  65. #upcoming-slide {
  66. position: absolute;
  67. width: 40%;
  68. height: 40%;
  69. right: 0;
  70. top: 0;
  71. }
  72. /* Speaker controls */
  73. #speaker-controls {
  74. position: absolute;
  75. top: 40%;
  76. right: 0;
  77. width: 40%;
  78. height: 60%;
  79. overflow: auto;
  80. font-size: 18px;
  81. }
  82. .speaker-controls-time.hidden,
  83. .speaker-controls-notes.hidden {
  84. display: none;
  85. }
  86. .speaker-controls-time .label,
  87. .speaker-controls-pace .label,
  88. .speaker-controls-notes .label {
  89. text-transform: uppercase;
  90. font-weight: normal;
  91. font-size: 0.66em;
  92. color: #666;
  93. margin: 0;
  94. }
  95. .speaker-controls-time, .speaker-controls-pace {
  96. border-bottom: 1px solid rgba( 200, 200, 200, 0.5 );
  97. margin-bottom: 10px;
  98. padding: 10px 16px;
  99. padding-bottom: 20px;
  100. cursor: pointer;
  101. }
  102. .speaker-controls-time .reset-button {
  103. opacity: 0;
  104. float: right;
  105. color: #666;
  106. text-decoration: none;
  107. }
  108. .speaker-controls-time:hover .reset-button {
  109. opacity: 1;
  110. }
  111. .speaker-controls-time .timer,
  112. .speaker-controls-time .clock {
  113. width: 50%;
  114. }
  115. .speaker-controls-time .timer,
  116. .speaker-controls-time .clock,
  117. .speaker-controls-time .pacing .hours-value,
  118. .speaker-controls-time .pacing .minutes-value,
  119. .speaker-controls-time .pacing .seconds-value {
  120. font-size: 1.9em;
  121. }
  122. .speaker-controls-time .timer {
  123. float: left;
  124. }
  125. .speaker-controls-time .clock {
  126. float: right;
  127. text-align: right;
  128. }
  129. .speaker-controls-time span.mute {
  130. opacity: 0.3;
  131. }
  132. .speaker-controls-time .pacing-title {
  133. margin-top: 5px;
  134. }
  135. .speaker-controls-time .pacing.ahead {
  136. color: blue;
  137. }
  138. .speaker-controls-time .pacing.on-track {
  139. color: green;
  140. }
  141. .speaker-controls-time .pacing.behind {
  142. color: red;
  143. }
  144. .speaker-controls-notes {
  145. padding: 10px 16px;
  146. }
  147. .speaker-controls-notes .value {
  148. margin-top: 5px;
  149. line-height: 1.4;
  150. font-size: 1.2em;
  151. }
  152. /* Layout selector */
  153. #speaker-layout {
  154. position: absolute;
  155. top: 10px;
  156. right: 10px;
  157. color: #222;
  158. z-index: 10;
  159. }
  160. #speaker-layout select {
  161. position: absolute;
  162. width: 100%;
  163. height: 100%;
  164. top: 0;
  165. left: 0;
  166. border: 0;
  167. box-shadow: 0;
  168. cursor: pointer;
  169. opacity: 0;
  170. font-size: 1em;
  171. background-color: transparent;
  172. -moz-appearance: none;
  173. -webkit-appearance: none;
  174. -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
  175. }
  176. #speaker-layout select:focus {
  177. outline: none;
  178. box-shadow: none;
  179. }
  180. .clear {
  181. clear: both;
  182. }
  183. /* Speaker layout: Wide */
  184. body[data-speaker-layout="wide"] #current-slide,
  185. body[data-speaker-layout="wide"] #upcoming-slide {
  186. width: 50%;
  187. height: 45%;
  188. padding: 6px;
  189. }
  190. body[data-speaker-layout="wide"] #current-slide {
  191. top: 0;
  192. left: 0;
  193. }
  194. body[data-speaker-layout="wide"] #upcoming-slide {
  195. top: 0;
  196. left: 50%;
  197. }
  198. body[data-speaker-layout="wide"] #speaker-controls {
  199. top: 45%;
  200. left: 0;
  201. width: 100%;
  202. height: 50%;
  203. font-size: 1.25em;
  204. }
  205. /* Speaker layout: Tall */
  206. body[data-speaker-layout="tall"] #current-slide,
  207. body[data-speaker-layout="tall"] #upcoming-slide {
  208. width: 45%;
  209. height: 50%;
  210. padding: 6px;
  211. }
  212. body[data-speaker-layout="tall"] #current-slide {
  213. top: 0;
  214. left: 0;
  215. }
  216. body[data-speaker-layout="tall"] #upcoming-slide {
  217. top: 50%;
  218. left: 0;
  219. }
  220. body[data-speaker-layout="tall"] #speaker-controls {
  221. padding-top: 40px;
  222. top: 0;
  223. left: 45%;
  224. width: 55%;
  225. height: 100%;
  226. font-size: 1.25em;
  227. }
  228. /* Speaker layout: Notes only */
  229. body[data-speaker-layout="notes-only"] #current-slide,
  230. body[data-speaker-layout="notes-only"] #upcoming-slide {
  231. display: none;
  232. }
  233. body[data-speaker-layout="notes-only"] #speaker-controls {
  234. padding-top: 40px;
  235. top: 0;
  236. left: 0;
  237. width: 100%;
  238. height: 100%;
  239. font-size: 1.25em;
  240. }
  241. @media screen and (max-width: 1080px) {
  242. body[data-speaker-layout="default"] #speaker-controls {
  243. font-size: 16px;
  244. }
  245. }
  246. @media screen and (max-width: 900px) {
  247. body[data-speaker-layout="default"] #speaker-controls {
  248. font-size: 14px;
  249. }
  250. }
  251. @media screen and (max-width: 800px) {
  252. body[data-speaker-layout="default"] #speaker-controls {
  253. font-size: 12px;
  254. }
  255. }
  256. </style>
  257. </head>
  258. <body>
  259. <div id="connection-status">Loading speaker view...</div>
  260. <div id="current-slide"></div>
  261. <div id="upcoming-slide"><span class="overlay-element label">Upcoming</span></div>
  262. <div id="speaker-controls">
  263. <div class="speaker-controls-time">
  264. <h4 class="label">Time <span class="reset-button">Click to Reset</span></h4>
  265. <div class="clock">
  266. <span class="clock-value">0:00 AM</span>
  267. </div>
  268. <div class="timer">
  269. <span class="hours-value">00</span><span class="minutes-value">:00</span><span class="seconds-value">:00</span>
  270. </div>
  271. <div class="clear"></div>
  272. <h4 class="label pacing-title" style="display: none">Pacing – Time to finish current slide</h4>
  273. <div class="pacing" style="display: none">
  274. <span class="hours-value">00</span><span class="minutes-value">:00</span><span class="seconds-value">:00</span>
  275. </div>
  276. </div>
  277. <div class="speaker-controls-notes hidden">
  278. <h4 class="label">Notes</h4>
  279. <div class="value"></div>
  280. </div>
  281. </div>
  282. <div id="speaker-layout" class="overlay-element interactive">
  283. <span class="speaker-layout-label"></span>
  284. <select class="speaker-layout-dropdown"></select>
  285. </div>
  286. <script>
  287. (function() {
  288. var notes,
  289. notesValue,
  290. currentState,
  291. currentSlide,
  292. upcomingSlide,
  293. layoutLabel,
  294. layoutDropdown,
  295. pendingCalls = {},
  296. lastRevealApiCallId = 0,
  297. connected = false;
  298. var SPEAKER_LAYOUTS = {
  299. 'default': 'Default',
  300. 'wide': 'Wide',
  301. 'tall': 'Tall',
  302. 'notes-only': 'Notes only'
  303. };
  304. setupLayout();
  305. var connectionStatus = document.querySelector( '#connection-status' );
  306. var connectionTimeout = setTimeout( function() {
  307. connectionStatus.innerHTML = 'Error connecting to main window.<br>Please try closing and reopening the speaker view.';
  308. }, 5000 );
  309. window.addEventListener( 'message', function( event ) {
  310. clearTimeout( connectionTimeout );
  311. connectionStatus.style.display = 'none';
  312. var data = JSON.parse( event.data );
  313. // The overview mode is only useful to the reveal.js instance
  314. // where navigation occurs so we don't sync it
  315. if( data.state ) delete data.state.overview;
  316. // Messages sent by the notes plugin inside of the main window
  317. if( data && data.namespace === 'reveal-notes' ) {
  318. if( data.type === 'connect' ) {
  319. handleConnectMessage( data );
  320. }
  321. else if( data.type === 'state' ) {
  322. handleStateMessage( data );
  323. }
  324. else if( data.type === 'return' ) {
  325. pendingCalls[data.callId](data.result);
  326. delete pendingCalls[data.callId];
  327. }
  328. }
  329. // Messages sent by the reveal.js inside of the current slide preview
  330. else if( data && data.namespace === 'reveal' ) {
  331. if( /ready/.test( data.eventName ) ) {
  332. // Send a message back to notify that the handshake is complete
  333. window.opener.postMessage( JSON.stringify({ namespace: 'reveal-notes', type: 'connected'} ), '*' );
  334. }
  335. else if( /slidechanged|fragmentshown|fragmenthidden|paused|resumed/.test( data.eventName ) && currentState !== JSON.stringify( data.state ) ) {
  336. window.opener.postMessage( JSON.stringify({ method: 'setState', args: [ data.state ]} ), '*' );
  337. }
  338. }
  339. } );
  340. /**
  341. * Asynchronously calls the Reveal.js API of the main frame.
  342. */
  343. function callRevealApi( methodName, methodArguments, callback ) {
  344. var callId = ++lastRevealApiCallId;
  345. pendingCalls[callId] = callback;
  346. window.opener.postMessage( JSON.stringify( {
  347. namespace: 'reveal-notes',
  348. type: 'call',
  349. callId: callId,
  350. methodName: methodName,
  351. arguments: methodArguments
  352. } ), '*' );
  353. }
  354. /**
  355. * Called when the main window is trying to establish a
  356. * connection.
  357. */
  358. function handleConnectMessage( data ) {
  359. if( connected === false ) {
  360. connected = true;
  361. setupIframes( data );
  362. setupKeyboard();
  363. setupNotes();
  364. setupTimer();
  365. }
  366. }
  367. /**
  368. * Called when the main window sends an updated state.
  369. */
  370. function handleStateMessage( data ) {
  371. // Store the most recently set state to avoid circular loops
  372. // applying the same state
  373. currentState = JSON.stringify( data.state );
  374. // No need for updating the notes in case of fragment changes
  375. if ( data.notes ) {
  376. notes.classList.remove( 'hidden' );
  377. notesValue.style.whiteSpace = data.whitespace;
  378. if( data.markdown ) {
  379. notesValue.innerHTML = marked( data.notes );
  380. }
  381. else {
  382. notesValue.innerHTML = data.notes;
  383. }
  384. }
  385. else {
  386. notes.classList.add( 'hidden' );
  387. }
  388. // Update the note slides
  389. currentSlide.contentWindow.postMessage( JSON.stringify({ method: 'setState', args: [ data.state ] }), '*' );
  390. upcomingSlide.contentWindow.postMessage( JSON.stringify({ method: 'setState', args: [ data.state ] }), '*' );
  391. upcomingSlide.contentWindow.postMessage( JSON.stringify({ method: 'next' }), '*' );
  392. }
  393. // Limit to max one state update per X ms
  394. handleStateMessage = debounce( handleStateMessage, 200 );
  395. /**
  396. * Forward keyboard events to the current slide window.
  397. * This enables keyboard events to work even if focus
  398. * isn't set on the current slide iframe.
  399. *
  400. * Block F5 default handling, it reloads and disconnects
  401. * the speaker notes window.
  402. */
  403. function setupKeyboard() {
  404. document.addEventListener( 'keydown', function( event ) {
  405. if( event.keyCode === 116 || ( event.metaKey && event.keyCode === 82 ) ) {
  406. event.preventDefault();
  407. return false;
  408. }
  409. currentSlide.contentWindow.postMessage( JSON.stringify({ method: 'triggerKey', args: [ event.keyCode ] }), '*' );
  410. } );
  411. }
  412. /**
  413. * Creates the preview iframes.
  414. */
  415. function setupIframes( data ) {
  416. var params = [
  417. 'receiver',
  418. 'progress=false',
  419. 'history=false',
  420. 'transition=none',
  421. 'autoSlide=0',
  422. 'backgroundTransition=none'
  423. ].join( '&' );
  424. var urlSeparator = /\?/.test(data.url) ? '&' : '?';
  425. var hash = '#/' + data.state.indexh + '/' + data.state.indexv;
  426. var currentURL = data.url + urlSeparator + params + '&postMessageEvents=true' + hash;
  427. var upcomingURL = data.url + urlSeparator + params + '&controls=false' + hash;
  428. currentSlide = document.createElement( 'iframe' );
  429. currentSlide.setAttribute( 'width', 1280 );
  430. currentSlide.setAttribute( 'height', 1024 );
  431. currentSlide.setAttribute( 'src', currentURL );
  432. document.querySelector( '#current-slide' ).appendChild( currentSlide );
  433. upcomingSlide = document.createElement( 'iframe' );
  434. upcomingSlide.setAttribute( 'width', 640 );
  435. upcomingSlide.setAttribute( 'height', 512 );
  436. upcomingSlide.setAttribute( 'src', upcomingURL );
  437. document.querySelector( '#upcoming-slide' ).appendChild( upcomingSlide );
  438. }
  439. /**
  440. * Setup the notes UI.
  441. */
  442. function setupNotes() {
  443. notes = document.querySelector( '.speaker-controls-notes' );
  444. notesValue = document.querySelector( '.speaker-controls-notes .value' );
  445. }
  446. function getTimings( callback ) {
  447. callRevealApi( 'getSlidesAttributes', [], function ( slideAttributes ) {
  448. callRevealApi( 'getConfig', [], function ( config ) {
  449. var totalTime = config.totalTime;
  450. var minTimePerSlide = config.minimumTimePerSlide || 0;
  451. var defaultTiming = config.defaultTiming;
  452. if ((defaultTiming == null) && (totalTime == null)) {
  453. callback(null);
  454. return;
  455. }
  456. // Setting totalTime overrides defaultTiming
  457. if (totalTime) {
  458. defaultTiming = 0;
  459. }
  460. var timings = [];
  461. for ( var i in slideAttributes ) {
  462. var slide = slideAttributes[ i ];
  463. var timing = defaultTiming;
  464. if( slide.hasOwnProperty( 'data-timing' )) {
  465. var t = slide[ 'data-timing' ];
  466. timing = parseInt(t);
  467. if( isNaN(timing) ) {
  468. console.warn("Could not parse timing '" + t + "' of slide " + i + "; using default of " + defaultTiming);
  469. timing = defaultTiming;
  470. }
  471. }
  472. timings.push(timing);
  473. }
  474. if ( totalTime ) {
  475. // After we've allocated time to individual slides, we summarize it and
  476. // subtract it from the total time
  477. var remainingTime = totalTime - timings.reduce( function(a, b) { return a + b; }, 0 );
  478. // The remaining time is divided by the number of slides that have 0 seconds
  479. // allocated at the moment, giving the average time-per-slide on the remaining slides
  480. var remainingSlides = (timings.filter( function(x) { return x == 0 }) ).length
  481. var timePerSlide = Math.round( remainingTime / remainingSlides, 0 )
  482. // And now we replace every zero-value timing with that average
  483. timings = timings.map( function(x) { return (x==0 ? timePerSlide : x) } );
  484. }
  485. var slidesUnderMinimum = timings.filter( function(x) { return (x < minTimePerSlide) } ).length
  486. if ( slidesUnderMinimum ) {
  487. message = "The pacing time for " + slidesUnderMinimum + " slide(s) is under the configured minimum of " + minTimePerSlide + " seconds. Check the data-timing attribute on individual slides, or consider increasing the totalTime or minimumTimePerSlide configuration options (or removing some slides).";
  488. alert(message);
  489. }
  490. callback( timings );
  491. } );
  492. } );
  493. }
  494. /**
  495. * Return the number of seconds allocated for presenting
  496. * all slides up to and including this one.
  497. */
  498. function getTimeAllocated( timings, callback ) {
  499. callRevealApi( 'getSlidePastCount', [], function ( currentSlide ) {
  500. var allocated = 0;
  501. for (var i in timings.slice(0, currentSlide + 1)) {
  502. allocated += timings[i];
  503. }
  504. callback( allocated );
  505. } );
  506. }
  507. /**
  508. * Create the timer and clock and start updating them
  509. * at an interval.
  510. */
  511. function setupTimer() {
  512. var start = new Date(),
  513. timeEl = document.querySelector( '.speaker-controls-time' ),
  514. clockEl = timeEl.querySelector( '.clock-value' ),
  515. hoursEl = timeEl.querySelector( '.hours-value' ),
  516. minutesEl = timeEl.querySelector( '.minutes-value' ),
  517. secondsEl = timeEl.querySelector( '.seconds-value' ),
  518. pacingTitleEl = timeEl.querySelector( '.pacing-title' ),
  519. pacingEl = timeEl.querySelector( '.pacing' ),
  520. pacingHoursEl = pacingEl.querySelector( '.hours-value' ),
  521. pacingMinutesEl = pacingEl.querySelector( '.minutes-value' ),
  522. pacingSecondsEl = pacingEl.querySelector( '.seconds-value' );
  523. var timings = null;
  524. getTimings( function ( _timings ) {
  525. timings = _timings;
  526. if (_timings !== null) {
  527. pacingTitleEl.style.removeProperty('display');
  528. pacingEl.style.removeProperty('display');
  529. }
  530. // Update once directly
  531. _updateTimer();
  532. // Then update every second
  533. setInterval( _updateTimer, 1000 );
  534. } );
  535. function _resetTimer() {
  536. if (timings == null) {
  537. start = new Date();
  538. _updateTimer();
  539. }
  540. else {
  541. // Reset timer to beginning of current slide
  542. getTimeAllocated( timings, function ( slideEndTimingSeconds ) {
  543. var slideEndTiming = slideEndTimingSeconds * 1000;
  544. callRevealApi( 'getSlidePastCount', [], function ( currentSlide ) {
  545. var currentSlideTiming = timings[currentSlide] * 1000;
  546. var previousSlidesTiming = slideEndTiming - currentSlideTiming;
  547. var now = new Date();
  548. start = new Date(now.getTime() - previousSlidesTiming);
  549. _updateTimer();
  550. } );
  551. } );
  552. }
  553. }
  554. timeEl.addEventListener( 'click', function() {
  555. _resetTimer();
  556. return false;
  557. } );
  558. function _displayTime( hrEl, minEl, secEl, time) {
  559. var sign = Math.sign(time) == -1 ? "-" : "";
  560. time = Math.abs(Math.round(time / 1000));
  561. var seconds = time % 60;
  562. var minutes = Math.floor( time / 60 ) % 60 ;
  563. var hours = Math.floor( time / ( 60 * 60 )) ;
  564. hrEl.innerHTML = sign + zeroPadInteger( hours );
  565. if (hours == 0) {
  566. hrEl.classList.add( 'mute' );
  567. }
  568. else {
  569. hrEl.classList.remove( 'mute' );
  570. }
  571. minEl.innerHTML = ':' + zeroPadInteger( minutes );
  572. if (hours == 0 && minutes == 0) {
  573. minEl.classList.add( 'mute' );
  574. }
  575. else {
  576. minEl.classList.remove( 'mute' );
  577. }
  578. secEl.innerHTML = ':' + zeroPadInteger( seconds );
  579. }
  580. function _updateTimer() {
  581. var diff, hours, minutes, seconds,
  582. now = new Date();
  583. diff = now.getTime() - start.getTime();
  584. clockEl.innerHTML = now.toLocaleTimeString( 'en-US', { hour12: true, hour: '2-digit', minute:'2-digit' } );
  585. _displayTime( hoursEl, minutesEl, secondsEl, diff );
  586. if (timings !== null) {
  587. _updatePacing(diff);
  588. }
  589. }
  590. function _updatePacing(diff) {
  591. getTimeAllocated( timings, function ( slideEndTimingSeconds ) {
  592. var slideEndTiming = slideEndTimingSeconds * 1000;
  593. callRevealApi( 'getSlidePastCount', [], function ( currentSlide ) {
  594. var currentSlideTiming = timings[currentSlide] * 1000;
  595. var timeLeftCurrentSlide = slideEndTiming - diff;
  596. if (timeLeftCurrentSlide < 0) {
  597. pacingEl.className = 'pacing behind';
  598. }
  599. else if (timeLeftCurrentSlide < currentSlideTiming) {
  600. pacingEl.className = 'pacing on-track';
  601. }
  602. else {
  603. pacingEl.className = 'pacing ahead';
  604. }
  605. _displayTime( pacingHoursEl, pacingMinutesEl, pacingSecondsEl, timeLeftCurrentSlide );
  606. } );
  607. } );
  608. }
  609. }
  610. /**
  611. * Sets up the speaker view layout and layout selector.
  612. */
  613. function setupLayout() {
  614. layoutDropdown = document.querySelector( '.speaker-layout-dropdown' );
  615. layoutLabel = document.querySelector( '.speaker-layout-label' );
  616. // Render the list of available layouts
  617. for( var id in SPEAKER_LAYOUTS ) {
  618. var option = document.createElement( 'option' );
  619. option.setAttribute( 'value', id );
  620. option.textContent = SPEAKER_LAYOUTS[ id ];
  621. layoutDropdown.appendChild( option );
  622. }
  623. // Monitor the dropdown for changes
  624. layoutDropdown.addEventListener( 'change', function( event ) {
  625. setLayout( layoutDropdown.value );
  626. }, false );
  627. // Restore any currently persisted layout
  628. setLayout( getLayout() );
  629. }
  630. /**
  631. * Sets a new speaker view layout. The layout is persisted
  632. * in local storage.
  633. */
  634. function setLayout( value ) {
  635. var title = SPEAKER_LAYOUTS[ value ];
  636. layoutLabel.innerHTML = 'Layout' + ( title ? ( ': ' + title ) : '' );
  637. layoutDropdown.value = value;
  638. document.body.setAttribute( 'data-speaker-layout', value );
  639. // Persist locally
  640. if( supportsLocalStorage() ) {
  641. window.localStorage.setItem( 'reveal-speaker-layout', value );
  642. }
  643. }
  644. /**
  645. * Returns the ID of the most recently set speaker layout
  646. * or our default layout if none has been set.
  647. */
  648. function getLayout() {
  649. if( supportsLocalStorage() ) {
  650. var layout = window.localStorage.getItem( 'reveal-speaker-layout' );
  651. if( layout ) {
  652. return layout;
  653. }
  654. }
  655. // Default to the first record in the layouts hash
  656. for( var id in SPEAKER_LAYOUTS ) {
  657. return id;
  658. }
  659. }
  660. function supportsLocalStorage() {
  661. try {
  662. localStorage.setItem('test', 'test');
  663. localStorage.removeItem('test');
  664. return true;
  665. }
  666. catch( e ) {
  667. return false;
  668. }
  669. }
  670. function zeroPadInteger( num ) {
  671. var str = '00' + parseInt( num );
  672. return str.substring( str.length - 2 );
  673. }
  674. /**
  675. * Limits the frequency at which a function can be called.
  676. */
  677. function debounce( fn, ms ) {
  678. var lastTime = 0,
  679. timeout;
  680. return function() {
  681. var args = arguments;
  682. var context = this;
  683. clearTimeout( timeout );
  684. var timeSinceLastCall = Date.now() - lastTime;
  685. if( timeSinceLastCall > ms ) {
  686. fn.apply( context, args );
  687. lastTime = Date.now();
  688. }
  689. else {
  690. timeout = setTimeout( function() {
  691. fn.apply( context, args );
  692. lastTime = Date.now();
  693. }, ms - timeSinceLastCall );
  694. }
  695. }
  696. }
  697. })();
  698. </script>
  699. </body>
  700. </html>