speaker-view.html 24 KB

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