reveal.js 13 KB

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