reveal.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /**
  2. * Copyright (C) 2011 Hakim El Hattab, http://hakim.se
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. *
  22. *
  23. * #############################################################################
  24. *
  25. *
  26. * Reveal.js is an easy to use HTML based slideshow enhanced by
  27. * sexy CSS 3D transforms.
  28. *
  29. * Slides are given unique hash based URL's so that they can be
  30. * opened directly.
  31. *
  32. * Public facing methods:
  33. * - Reveal.initialize( { ... options ... } );
  34. * - Reveal.navigateTo( indexh, indexv );
  35. * - Reveal.navigateLeft();
  36. * - Reveal.navigateRight();
  37. * - Reveal.navigateUp();
  38. * - Reveal.navigateDown();
  39. *
  40. *
  41. * version 0.1:
  42. * - First release
  43. *
  44. * version 0.2:
  45. * - Refactored code and added inline documentation
  46. * - Slides now have unique URL's
  47. * - A basic API to invoke navigation was added
  48. *
  49. * version 0.3:
  50. * - Added licensing terms
  51. * - Fixed broken links on touch devices
  52. *
  53. * version 1.0:
  54. * - Added controls
  55. * - Added initialization options
  56. * - Reveal views in fragments
  57. * - Revamped, darker, theme
  58. * - Tweaked markup styles (a, em, strong, b, i, blockquote, q, pre, ul, ol)
  59. * - Support for themes at initialization (default/linear/concave)
  60. * - Code highlighting via highlight.js
  61. *
  62. * TODO:
  63. * - Touch/swipe interactions
  64. * - Presentation overview via keyboard shortcut
  65. *
  66. * @author Hakim El Hattab | http://hakim.se
  67. * @version 1.1
  68. */
  69. var Reveal = (function(){
  70. var HORIZONTAL_SLIDES_SELECTOR = '#main>section',
  71. VERTICAL_SLIDES_SELECTOR = 'section.present>section',
  72. // The horizontal and verical index of the currently active slide
  73. indexh = 0,
  74. indexv = 0,
  75. // Configurations options, including;
  76. // > {Boolean} controls
  77. // > {String} theme
  78. // > {Boolean} rollingLinks
  79. config = {},
  80. // Cached references to DOM elements
  81. dom = {};
  82. /**
  83. * Starts up the slideshow by applying configuration
  84. * options and binding various events.
  85. */
  86. function initialize( options ) {
  87. // Cache references to DOM elements
  88. dom.controls = document.querySelector( '.controls' );
  89. dom.controlsLeft = document.querySelector( '.controls .left' );
  90. dom.controlsRight = document.querySelector( '.controls .right' );
  91. dom.controlsUp = document.querySelector( '.controls .up' );
  92. dom.controlsDown = document.querySelector( '.controls .down' );
  93. // Bind all view events
  94. document.addEventListener('keydown', onDocumentKeyDown, false);
  95. document.addEventListener('touchstart', onDocumentTouchStart, false);
  96. window.addEventListener('hashchange', onWindowHashChange, false);
  97. dom.controlsLeft.addEventListener('click', preventAndForward( navigateLeft ), false);
  98. dom.controlsRight.addEventListener('click', preventAndForward( navigateRight ), false);
  99. dom.controlsUp.addEventListener('click', preventAndForward( navigateUp ), false);
  100. dom.controlsDown.addEventListener('click', preventAndForward( navigateDown ), false);
  101. // Fall back on default options
  102. config.rollingLinks = options.rollingLinks === undefined ? true : options.rollingLinks;
  103. config.controls = options.controls === undefined ? false : options.controls;
  104. config.theme = options.theme === undefined ? 'default' : options.theme;
  105. if( config.controls ) {
  106. dom.controls.style.display = 'block';
  107. }
  108. if( config.theme !== 'default' ) {
  109. document.body.classList.add( config.theme );
  110. }
  111. if( config.rollingLinks ) {
  112. // Add some 3D magic to our anchors
  113. linkify();
  114. }
  115. // Read the initial hash
  116. readURL();
  117. }
  118. /**
  119. * Prevents an events defaults behavior calls the
  120. * specified delegate.
  121. *
  122. * @param {Function} delegate The method to call
  123. * after the wrapper has been executed
  124. */
  125. function preventAndForward( delegate ) {
  126. return function( event ) {
  127. event.preventDefault();
  128. delegate.call();
  129. }
  130. }
  131. /**
  132. * Handler for the document level 'keydown' event.
  133. *
  134. * @param {Object} event
  135. */
  136. function onDocumentKeyDown( event ) {
  137. // FFT: Use document.querySelector( ':focus' ) === null
  138. // instead of checking contentEditable?
  139. if( event.keyCode >= 37 && event.keyCode <= 40 && event.target.contentEditable === 'inherit' ) {
  140. switch( event.keyCode ) {
  141. case 37: navigateLeft(); break; // left
  142. case 39: navigateRight(); break; // right
  143. case 38: navigateUp(); break; // up
  144. case 40: navigateDown(); break; // down
  145. }
  146. slide();
  147. event.preventDefault();
  148. }
  149. }
  150. /**
  151. * Handler for the document level 'touchstart' event.
  152. *
  153. * This enables very basic tap interaction for touch
  154. * devices. Added mainly for performance testing of 3D
  155. * transforms on iOS but was so happily surprised with
  156. * how smoothly it runs so I left it in here. Apple +1
  157. *
  158. * @param {Object} event
  159. */
  160. function onDocumentTouchStart( event ) {
  161. // We're only interested in one point taps
  162. if (event.touches.length === 1) {
  163. // Never prevent taps on anchors and images
  164. if( event.target.tagName.toLowerCase() === 'a' || event.target.tagName.toLowerCase() === 'img' ) {
  165. return;
  166. }
  167. event.preventDefault();
  168. var point = {
  169. x: event.touches[0].clientX,
  170. y: event.touches[0].clientY
  171. };
  172. // Define the extent of the areas that may be tapped
  173. // to navigate
  174. var wt = window.innerWidth * 0.3;
  175. var ht = window.innerHeight * 0.3;
  176. if( point.x < wt ) {
  177. navigateLeft();
  178. }
  179. else if( point.x > window.innerWidth - wt ) {
  180. navigateRight();
  181. }
  182. else if( point.y < ht ) {
  183. navigateUp();
  184. }
  185. else if( point.y > window.innerHeight - ht ) {
  186. navigateDown();
  187. }
  188. slide();
  189. }
  190. }
  191. /**
  192. * Handler for the window level 'hashchange' event.
  193. *
  194. * @param {Object} event
  195. */
  196. function onWindowHashChange( event ) {
  197. readURL();
  198. }
  199. /**
  200. * Wrap all links in 3D goodness.
  201. */
  202. function linkify() {
  203. var supports3DTransforms = document.body.style['webkitPerspective'] !== undefined ||
  204. document.body.style['MozPerspective'] !== undefined ||
  205. document.body.style['perspective'] !== undefined;
  206. if( supports3DTransforms ) {
  207. var nodes = document.querySelectorAll( 'section a:not(.image)' );
  208. for( var i = 0, len = nodes.length; i < len; i++ ) {
  209. var node = nodes[i];
  210. if( !node.className || !node.className.match( /roll/g ) ) {
  211. node.className += ' roll';
  212. node.innerHTML = '<span data-title="'+ node.text +'">' + node.innerHTML + '</span>';
  213. }
  214. };
  215. }
  216. }
  217. /**
  218. * Updates one dimension of slides by showing the slide
  219. * with the specified index.
  220. *
  221. * @param {String} selector A CSS selector that will fetch
  222. * the group of slides we are working with
  223. * @param {Number} index The index of the slide that should be
  224. * shown
  225. *
  226. * @return {Number} The index of the slide that is now shown,
  227. * might differ from the passed in index if it was out of
  228. * bounds.
  229. */
  230. function updateSlides( selector, index ) {
  231. // Select all slides and convert the NodeList result to
  232. // an array
  233. var slides = Array.prototype.slice.call( document.querySelectorAll( selector ) );
  234. if( slides.length ) {
  235. // Enforce max and minimum index bounds
  236. index = Math.max(Math.min(index, slides.length - 1), 0);
  237. slides[index].setAttribute('class', 'present');
  238. // Any element previous to index is given the 'past' class
  239. slides.slice(0, index).map(function(element){
  240. element.setAttribute('class', 'past');
  241. });
  242. // Any element subsequent to index is given the 'future' class
  243. slides.slice(index + 1).map(function(element){
  244. element.setAttribute('class', 'future');
  245. });
  246. }
  247. else {
  248. // Since there are no slides we can't be anywhere beyond the
  249. // zeroth index
  250. index = 0;
  251. }
  252. return index;
  253. }
  254. /**
  255. * Updates the visual slides to represent the currently
  256. * set indices.
  257. */
  258. function slide() {
  259. indexh = updateSlides( HORIZONTAL_SLIDES_SELECTOR, indexh );
  260. indexv = updateSlides( VERTICAL_SLIDES_SELECTOR, indexv );
  261. updateControls();
  262. writeURL();
  263. }
  264. /**
  265. * Updates the state and link pointers of the controls.
  266. */
  267. function updateControls() {
  268. var routes = availableRoutes();
  269. // Remove the 'enabled' class from all directions
  270. [ dom.controlsLeft, dom.controlsRight, dom.controlsUp, dom.controlsDown ].forEach( function( node ) {
  271. node.classList.remove( 'enabled' );
  272. } )
  273. if( routes.left ) dom.controlsLeft.classList.add( 'enabled' );
  274. if( routes.right ) dom.controlsRight.classList.add( 'enabled' );
  275. if( routes.up ) dom.controlsUp.classList.add( 'enabled' );
  276. if( routes.down ) dom.controlsDown.classList.add( 'enabled' );
  277. }
  278. /**
  279. * Determine what available routes there are for navigation.
  280. *
  281. * @return {Object} containing four booleans: left/right/up/down
  282. */
  283. function availableRoutes() {
  284. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  285. var verticalSlides = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
  286. return {
  287. left: indexh > 0,
  288. right: indexh < horizontalSlides.length - 1,
  289. up: indexv > 0,
  290. down: indexv < verticalSlides.length - 1
  291. };
  292. }
  293. /**
  294. * Reads the current URL (hash) and navigates accordingly.
  295. */
  296. function readURL() {
  297. // Break the hash down to separate components
  298. var bits = window.location.hash.slice(2).split('/');
  299. // Read the index components of the hash
  300. indexh = bits[0] ? parseInt( bits[0] ) : 0;
  301. indexv = bits[1] ? parseInt( bits[1] ) : 0;
  302. navigateTo( indexh, indexv );
  303. }
  304. /**
  305. * Updates the page URL (hash) to reflect the current
  306. * state.
  307. */
  308. function writeURL() {
  309. var url = '/';
  310. // Only include the minimum possible number of components in
  311. // the URL
  312. if( indexh > 0 || indexv > 0 ) url += indexh;
  313. if( indexv > 0 ) url += '/' + indexv;
  314. window.location.hash = url;
  315. }
  316. /**
  317. * Navigate to the next slide fragment.
  318. *
  319. * @return {Boolean} true if there was a next fragment,
  320. * false otherwise
  321. */
  322. function nextFragment() {
  323. // Vertical slides:
  324. if( document.querySelector( VERTICAL_SLIDES_SELECTOR + '.present' ) ) {
  325. var verticalFragments = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR + '.present .fragment:not(.visible)' );
  326. if( verticalFragments.length ) {
  327. verticalFragments[0].classList.add( 'visible' );
  328. return true;
  329. }
  330. }
  331. // Horizontal slides:
  332. else {
  333. var horizontalFragments = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.present .fragment:not(.visible)' );
  334. if( horizontalFragments.length ) {
  335. horizontalFragments[0].classList.add( 'visible' );
  336. return true;
  337. }
  338. }
  339. return false;
  340. }
  341. /**
  342. * Navigate to the previous slide fragment.
  343. *
  344. * @return {Boolean} true if there was a previous fragment,
  345. * false otherwise
  346. */
  347. function previousFragment() {
  348. // Vertical slides:
  349. if( document.querySelector( VERTICAL_SLIDES_SELECTOR + '.present' ) ) {
  350. var verticalFragments = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR + '.present .fragment.visible' );
  351. if( verticalFragments.length ) {
  352. verticalFragments[ verticalFragments.length - 1 ].classList.remove( 'visible' );
  353. return true;
  354. }
  355. }
  356. // Horizontal slides:
  357. else {
  358. var horizontalFragments = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.present .fragment.visible' );
  359. if( horizontalFragments.length ) {
  360. horizontalFragments[ horizontalFragments.length - 1 ].classList.remove( 'visible' );
  361. return true;
  362. }
  363. }
  364. return false;
  365. }
  366. /**
  367. * Triggers a navigation to the specified indices.
  368. *
  369. * @param {Number} h The horizontal index of the slide to show
  370. * @param {Number} v The vertical index of the slide to show
  371. */
  372. function navigateTo( h, v ) {
  373. indexh = h === undefined ? indexh : h;
  374. indexv = v === undefined ? indexv : v;
  375. slide();
  376. }
  377. function navigateLeft() {
  378. // Prioritize hiding fragments
  379. if( previousFragment() === false ) {
  380. indexh --;
  381. indexv = 0;
  382. slide();
  383. }
  384. }
  385. function navigateRight() {
  386. // Prioritize revealing fragments
  387. if( nextFragment() === false ) {
  388. indexh ++;
  389. indexv = 0;
  390. slide();
  391. }
  392. }
  393. function navigateUp() {
  394. // Prioritize hiding fragments
  395. if( previousFragment() === false ) {
  396. indexv --;
  397. slide();
  398. }
  399. }
  400. function navigateDown() {
  401. // Prioritize revealing fragments
  402. if( nextFragment() === false ) {
  403. indexv ++;
  404. slide();
  405. }
  406. }
  407. // Expose some methods publicly
  408. return {
  409. initialize: initialize,
  410. navigateTo: navigateTo,
  411. navigateLeft: navigateLeft,
  412. navigateRight: navigateRight,
  413. navigateUp: navigateUp,
  414. navigateDown: navigateDown
  415. };
  416. })();