JJ Allaire | efa6ad4 | 2016-01-30 13:12:05 -0500 | [diff] [blame] | 1 | (function() { |
| 2 | |
| 3 | // don't emit events from inside the previews themselves |
| 4 | if( window.location.search.match( /receiver/gi ) ) { return; } |
| 5 | |
| 6 | var socket = io.connect( window.location.origin ), |
| 7 | socketId = Math.random().toString().slice( 2 ); |
| 8 | |
| 9 | console.log( 'View slide notes at ' + window.location.origin + '/notes/' + socketId ); |
| 10 | |
| 11 | window.open( window.location.origin + '/notes/' + socketId, 'notes-' + socketId ); |
| 12 | |
| 13 | /** |
| 14 | * Posts the current slide data to the notes window |
| 15 | */ |
| 16 | function post() { |
| 17 | |
| 18 | var slideElement = Reveal.getCurrentSlide(), |
| 19 | notesElement = slideElement.querySelector( 'aside.notes' ); |
| 20 | |
| 21 | var messageData = { |
| 22 | notes: '', |
| 23 | markdown: false, |
| 24 | socketId: socketId, |
| 25 | state: Reveal.getState() |
| 26 | }; |
| 27 | |
| 28 | // Look for notes defined in a slide attribute |
| 29 | if( slideElement.hasAttribute( 'data-notes' ) ) { |
| 30 | messageData.notes = slideElement.getAttribute( 'data-notes' ); |
| 31 | } |
| 32 | |
| 33 | // Look for notes defined in an aside element |
| 34 | if( notesElement ) { |
| 35 | messageData.notes = notesElement.innerHTML; |
| 36 | messageData.markdown = typeof notesElement.getAttribute( 'data-markdown' ) === 'string'; |
| 37 | } |
| 38 | |
| 39 | socket.emit( 'statechanged', messageData ); |
| 40 | |
| 41 | } |
| 42 | |
| 43 | // When a new notes window connects, post our current state |
| 44 | socket.on( 'new-subscriber', function( data ) { |
| 45 | post(); |
| 46 | } ); |
| 47 | |
| 48 | // When the state changes from inside of the speaker view |
| 49 | socket.on( 'statechanged-speaker', function( data ) { |
| 50 | Reveal.setState( data.state ); |
| 51 | } ); |
| 52 | |
| 53 | // Monitor events that trigger a change in state |
| 54 | Reveal.addEventListener( 'slidechanged', post ); |
| 55 | Reveal.addEventListener( 'fragmentshown', post ); |
| 56 | Reveal.addEventListener( 'fragmenthidden', post ); |
| 57 | Reveal.addEventListener( 'overviewhidden', post ); |
| 58 | Reveal.addEventListener( 'overviewshown', post ); |
| 59 | Reveal.addEventListener( 'paused', post ); |
| 60 | Reveal.addEventListener( 'resumed', post ); |
| 61 | |
| 62 | // Post the initial state |
| 63 | post(); |
| 64 | |
| 65 | }()); |