Ver Fonte

'slidechanged' event, added Reveal.addEventListener/Reveal.removeEventListener api methods

Hakim El Hattab há 12 anos atrás
pai
commit
19b67aab13
4 ficheiros alterados com 59 adições e 16 exclusões
  1. 16 2
      README.md
  2. 8 2
      index.html
  3. 35 12
      js/reveal.js
  4. 0 0
      js/reveal.min.js

+ 16 - 2
README.md

@@ -68,16 +68,27 @@ The Reveal class provides a minimal JavaScript API for controlling its navigatio
 
 ### States
 
-If you set ``data-state="someState"`` on a slide ``<section>``, "someState" will be applied as a class on the document element when that slide is opened. This allows you to apply broad style changes to the page based on the active slide.
+If you set ``data-state="somestate"`` on a slide ``<section>``, "somestate" will be applied as a class on the document element when that slide is opened. This allows you to apply broad style changes to the page based on the active slide.
 
 Furthermore you can also listen to these changes in state via JavaScript:
 
 ```
-document.addEventListener( 'someState', function() {
+Reveal.addEventListener( 'somestate', function() {
 	// TODO: Sprinkle magic
 }, false );
 ```
 
+### Slide change event
+
+An 'slidechanged' event is fired each time the slide is changed (regardless of state). The event object holds the index of the current slide.
+
+```
+Reveal.addEventListener( 'slidechanged', function( event ) {
+	// event.indexh & event.indexv
+} );
+```
+
+
 ## Examples
 
 * http://lab.hakim.se/reveal-js/ (original)
@@ -104,6 +115,9 @@ document.addEventListener( 'someState', function() {
 #### 1.3 (master)
 - Revised keyboard shortcuts, including ESC for overview, N for next, P for previous. Thanks [mahemoff](https://github.com/mahemoff)
 - Added support for looped presentations via config
+- Fixed IE9 fallback
+- Added event binding methods (Reveal.addEventListener, Reveal.removeEventListener)
+- Added 'slidechanged' event
 
 #### 1.2
 

+ 8 - 2
index.html

@@ -148,7 +148,7 @@
 					<p>
 						Additionally custom events can be triggered on a per slide basis by binding to the <code>data-state</code> name.
 					</p>
-					<pre><code contenteditable style="font-size: 18px; margin-top: 20px;">document.addEventListener( 'customevent', function() {
+					<pre><code contenteditable style="font-size: 18px; margin-top: 20px;">Reveal.addEventListener( 'customevent', function() {
 	alert( '"customevent" has fired' );
 } );
 					</code></pre>
@@ -261,10 +261,16 @@ linkify( 'a' );
 				query[ a.split( '=' ).shift() ] = a.split( '=' ).pop();
 			} );
 
-			document.addEventListener( 'customevent', function() {
+			// Fires when a slide with data-state=customevent is activated
+			Reveal.addEventListener( 'customevent', function() {
 				alert( '"customevent" has fired' );
 			} );
 
+			// Fires each time a new slide is activated
+			Reveal.addEventListener( 'slidechanged', function( event ) {
+				// event.indexh & event.indexv
+			} );
+
 			Reveal.initialize({
 				// Display controls in the bottom right corner
 				controls: true,

+ 35 - 12
js/reveal.js

@@ -78,13 +78,13 @@ var Reveal = (function(){
 		dom.controlsDown = document.querySelector( '#reveal .controls .down' );
 
 		// Bind all view events
-		document.addEventListener('keydown', onDocumentKeyDown, false);
-		document.addEventListener('touchstart', onDocumentTouchStart, false);
-		window.addEventListener('hashchange', onWindowHashChange, false);
-		dom.controlsLeft.addEventListener('click', preventAndForward( navigateLeft ), false);
-		dom.controlsRight.addEventListener('click', preventAndForward( navigateRight ), false);
-		dom.controlsUp.addEventListener('click', preventAndForward( navigateUp ), false);
-		dom.controlsDown.addEventListener('click', preventAndForward( navigateDown ), false);
+		document.addEventListener( 'keydown', onDocumentKeyDown, false );
+		document.addEventListener( 'touchstart', onDocumentTouchStart, false );
+		window.addEventListener( 'hashchange', onWindowHashChange, false );
+		dom.controlsLeft.addEventListener( 'click', preventAndForward( navigateLeft ), false );
+		dom.controlsRight.addEventListener( 'click', preventAndForward( navigateRight ), false );
+		dom.controlsUp.addEventListener( 'click', preventAndForward( navigateUp ), false );
+		dom.controlsDown.addEventListener( 'click', preventAndForward( navigateDown ), false );
 
 		// Copy options over to our config object
 		extend( config, options );
@@ -504,10 +504,8 @@ var Reveal = (function(){
 
 			document.documentElement.classList.add( state[i] );
 
-			// Dispatch custom event
-			var event = document.createEvent( "HTMLEvents" );
-			event.initEvent( state[i], true, true );
-			document.dispatchEvent( event );
+			// Dispatch custom event matching the state's name
+			dispatchEvent( state[i] );
 		}
 
 		// Clean up the remaints of the previous state
@@ -529,6 +527,12 @@ var Reveal = (function(){
 		
 		clearTimeout( writeURLTimeout );
 		writeURLTimeout = setTimeout( writeURL, 1500 );
+
+		// Dispatch an event notifying observers of the change in slide
+		dispatchEvent( 'slidechanged', {
+			'indexh': indexh, 
+			'indexv': indexv
+		} );
 	}
 
 	/**
@@ -596,6 +600,17 @@ var Reveal = (function(){
 		}
 	}
 
+	/**
+	 * Dispatches an event of the specified type from the 
+	 * #reveal DOM element.
+	 */
+	function dispatchEvent( type, properties ) {
+		var event = document.createEvent( "HTMLEvents", 1, 2 );
+		event.initEvent( type, true, true );
+		extend( event, properties );
+		dom.wrapper.dispatchEvent( event );
+	}
+
 	/**
 	 * Navigate to the next slide fragment.
 	 * 
@@ -736,7 +751,15 @@ var Reveal = (function(){
 		navigateLeft: navigateLeft,
 		navigateRight: navigateRight,
 		navigateUp: navigateUp,
-		navigateDown: navigateDown
+		navigateDown: navigateDown,
+
+		// Forward event binding to the reveal DOM element
+		addEventListener: function( type, listener, useCapture ) {
+			( dom.wrapper || document.querySelector( '#reveal' ) ).addEventListener( type, listener, useCapture );
+		},
+		removeEventListener: function( type, listener, useCapture ) {
+			( dom.wrapper || document.querySelector( '#reveal' ) ).removeEventListener( type, listener, useCapture );
+		}
 	};
 	
 })();

Diff do ficheiro suprimidas por serem muito extensas
+ 0 - 0
js/reveal.min.js


Alguns ficheiros não foram mostrados porque muitos ficheiros mudaram neste diff