impress.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. /**
  2. * impress.js
  3. *
  4. * impress.js is a presentation tool based on the power of CSS3 transforms and transitions
  5. * in modern browsers and inspired by the idea behind prezi.com.
  6. *
  7. *
  8. * Copyright 2011-2012 Bartek Szopka (@bartaz)
  9. *
  10. * Released under the MIT and GPL Licenses.
  11. *
  12. * ------------------------------------------------
  13. * author: Bartek Szopka
  14. * version: 0.5.3
  15. * url: http://bartaz.github.com/impress.js/
  16. * source: http://github.com/bartaz/impress.js/
  17. */
  18. /*jshint bitwise:true, curly:true, eqeqeq:true, forin:true, latedef:true, newcap:true,
  19. noarg:true, noempty:true, undef:true, strict:true, browser:true */
  20. // You are one of those who like to know how thing work inside?
  21. // Let me show you the cogs that make impress.js run...
  22. (function ( document, window ) {
  23. 'use strict';
  24. // HELPER FUNCTIONS
  25. // `pfx` is a function that takes a standard CSS property name as a parameter
  26. // and returns it's prefixed version valid for current browser it runs in.
  27. // The code is heavily inspired by Modernizr http://www.modernizr.com/
  28. var pfx = (function () {
  29. var style = document.createElement('dummy').style,
  30. prefixes = 'Webkit Moz O ms Khtml'.split(' '),
  31. memory = {};
  32. return function ( prop ) {
  33. if ( typeof memory[ prop ] === "undefined" ) {
  34. var ucProp = prop.charAt(0).toUpperCase() + prop.substr(1),
  35. props = (prop + ' ' + prefixes.join(ucProp + ' ') + ucProp).split(' ');
  36. memory[ prop ] = null;
  37. for ( var i in props ) {
  38. if ( style[ props[i] ] !== undefined ) {
  39. memory[ prop ] = props[i];
  40. break;
  41. }
  42. }
  43. }
  44. return memory[ prop ];
  45. };
  46. })();
  47. // `arraify` takes an array-like object and turns it into real Array
  48. // to make all the Array.prototype goodness available.
  49. var arrayify = function ( a ) {
  50. return [].slice.call( a );
  51. };
  52. // `css` function applies the styles given in `props` object to the element
  53. // given as `el`. It runs all property names through `pfx` function to make
  54. // sure proper prefixed version of the property is used.
  55. var css = function ( el, props ) {
  56. var key, pkey;
  57. for ( key in props ) {
  58. if ( props.hasOwnProperty(key) ) {
  59. pkey = pfx(key);
  60. if ( pkey !== null ) {
  61. el.style[pkey] = props[key];
  62. }
  63. }
  64. }
  65. return el;
  66. };
  67. // `toNumber` takes a value given as `numeric` parameter and tries to turn
  68. // it into a number. If it is not possible it returns 0 (or other value
  69. // given as `fallback`).
  70. var toNumber = function (numeric, fallback) {
  71. return isNaN(numeric) ? (fallback || 0) : Number(numeric);
  72. };
  73. // `byId` returns element with given `id` - you probably have guessed that ;)
  74. var byId = function ( id ) {
  75. return document.getElementById(id);
  76. };
  77. // `$` returns first element for given CSS `selector` in the `context` of
  78. // the given element or whole document.
  79. var $ = function ( selector, context ) {
  80. context = context || document;
  81. return context.querySelector(selector);
  82. };
  83. // `$$` return an array of elements for given CSS `selector` in the `context` of
  84. // the given element or whole document.
  85. var $$ = function ( selector, context ) {
  86. context = context || document;
  87. return arrayify( context.querySelectorAll(selector) );
  88. };
  89. // `triggerEvent` builds a custom DOM event with given `eventName` and `detail` data
  90. // and triggers it on element given as `el`.
  91. var triggerEvent = function (el, eventName, detail) {
  92. var event = document.createEvent("CustomEvent");
  93. event.initCustomEvent(eventName, true, true, detail);
  94. el.dispatchEvent(event);
  95. };
  96. // `translate` builds a translate transform string for given data.
  97. var translate = function ( t ) {
  98. return " translate3d(" + t.x + "px," + t.y + "px," + t.z + "px) ";
  99. };
  100. // `rotate` builds a rotate transform string for given data.
  101. // By default the rotations are in X Y Z order that can be reverted by passing `true`
  102. // as second parameter.
  103. var rotate = function ( r, revert ) {
  104. var rX = " rotateX(" + r.x + "deg) ",
  105. rY = " rotateY(" + r.y + "deg) ",
  106. rZ = " rotateZ(" + r.z + "deg) ";
  107. return revert ? rZ+rY+rX : rX+rY+rZ;
  108. };
  109. // `scale` builds a scale transform string for given data.
  110. var scale = function ( s ) {
  111. return " scale(" + s + ") ";
  112. };
  113. // `perspective` builds a perspective transform string for given data.
  114. var perspective = function ( p ) {
  115. return " perspective(" + p + "px) ";
  116. };
  117. // `getElementFromHash` returns an element located by id from hash part of
  118. // window location.
  119. var getElementFromHash = function () {
  120. // get id from url # by removing `#` or `#/` from the beginning,
  121. // so both "fallback" `#slide-id` and "enhanced" `#/slide-id` will work
  122. return byId( window.location.hash.replace(/^#\/?/,"") );
  123. };
  124. // `computeWindowScale` counts the scale factor between window size and size
  125. // defined for the presentation in the config.
  126. var computeWindowScale = function ( config ) {
  127. var hScale = window.innerHeight / config.height,
  128. wScale = window.innerWidth / config.width,
  129. scale = hScale > wScale ? wScale : hScale;
  130. if (config.maxScale && scale > config.maxScale) {
  131. scale = config.maxScale;
  132. }
  133. if (config.minScale && scale < config.minScale) {
  134. scale = config.minScale;
  135. }
  136. return scale;
  137. };
  138. // CHECK SUPPORT
  139. var body = document.body;
  140. var ua = navigator.userAgent.toLowerCase();
  141. var impressSupported =
  142. // browser should support CSS 3D transtorms
  143. ( pfx("perspective") !== null ) &&
  144. // and `classList` and `dataset` APIs
  145. ( body.classList ) &&
  146. ( body.dataset ) &&
  147. // but some mobile devices need to be blacklisted,
  148. // because their CSS 3D support or hardware is not
  149. // good enough to run impress.js properly, sorry...
  150. ( ua.search(/(iphone)|(ipod)|(android)/) === -1 );
  151. if (!impressSupported) {
  152. // we can't be sure that `classList` is supported
  153. body.className += " impress-not-supported ";
  154. } else {
  155. body.classList.remove("impress-not-supported");
  156. body.classList.add("impress-supported");
  157. }
  158. // GLOBALS AND DEFAULTS
  159. // This is were the root elements of all impress.js instances will be kept.
  160. // Yes, this means you can have more than one instance on a page, but I'm not
  161. // sure if it makes any sense in practice ;)
  162. var roots = {};
  163. // some default config values.
  164. var defaults = {
  165. width: 1024,
  166. height: 768,
  167. maxScale: 1,
  168. minScale: 0,
  169. perspective: 1000,
  170. transitionDuration: 1000
  171. };
  172. // it's just an empty function ... and a useless comment.
  173. var empty = function () { return false; };
  174. // IMPRESS.JS API
  175. // And that's where interesting things will start to happen.
  176. // It's the core `impress` function that returns the impress.js API
  177. // for a presentation based on the element with given id ('impress'
  178. // by default).
  179. var impress = window.impress = function ( rootId ) {
  180. // If impress.js is not supported by the browser return a dummy API
  181. // it may not be a perfect solution but we return early and avoid
  182. // running code that may use features not implemented in the browser.
  183. if (!impressSupported) {
  184. return {
  185. init: empty,
  186. goto: empty,
  187. prev: empty,
  188. next: empty
  189. };
  190. }
  191. rootId = rootId || "impress";
  192. // if given root is already initialized just return the API
  193. if (roots["impress-root-" + rootId]) {
  194. return roots["impress-root-" + rootId];
  195. }
  196. // data of all presentation steps
  197. var stepsData = {};
  198. // element of currently active step
  199. var activeStep = null;
  200. // current state (position, rotation and scale) of the presentation
  201. var currentState = null;
  202. // array of step elements
  203. var steps = null;
  204. // configuration options
  205. var config = null;
  206. // scale factor of the browser window
  207. var windowScale = null;
  208. // root presentation elements
  209. var root = byId( rootId );
  210. var canvas = document.createElement("div");
  211. var initialized = false;
  212. // STEP EVENTS
  213. //
  214. // There are currently two step events triggered by impress.js
  215. // `impress:stepenter` is triggered when the step is shown on the
  216. // screen (the transition from the previous one is finished) and
  217. // `impress:stepleave` is triggered when the step is left (the
  218. // transition to next step just starts).
  219. // reference to last entered step
  220. var lastEntered = null;
  221. // `onStepEnter` is called whenever the step element is entered
  222. // but the event is triggered only if the step is different than
  223. // last entered step.
  224. var onStepEnter = function (step) {
  225. if (lastEntered !== step) {
  226. triggerEvent(step, "impress:stepenter");
  227. lastEntered = step;
  228. }
  229. };
  230. // `onStepLeave` is called whenever the step element is left
  231. // but the event is triggered only if the step is the same as
  232. // last entered step.
  233. var onStepLeave = function (step) {
  234. if (lastEntered === step) {
  235. triggerEvent(step, "impress:stepleave");
  236. lastEntered = null;
  237. }
  238. };
  239. // `initStep` initializes given step element by reading data from its
  240. // data attributes and setting correct styles.
  241. var initStep = function ( el, idx ) {
  242. var data = el.dataset,
  243. step = {
  244. translate: {
  245. x: toNumber(data.x),
  246. y: toNumber(data.y),
  247. z: toNumber(data.z)
  248. },
  249. rotate: {
  250. x: toNumber(data.rotateX),
  251. y: toNumber(data.rotateY),
  252. z: toNumber(data.rotateZ || data.rotate)
  253. },
  254. scale: toNumber(data.scale, 1),
  255. el: el
  256. };
  257. if ( !el.id ) {
  258. el.id = "step-" + (idx + 1);
  259. }
  260. stepsData["impress-" + el.id] = step;
  261. css(el, {
  262. position: "absolute",
  263. transform: "translate(-50%,-50%)" +
  264. translate(step.translate) +
  265. rotate(step.rotate) +
  266. scale(step.scale),
  267. transformStyle: "preserve-3d"
  268. });
  269. };
  270. // `init` API function that initializes (and runs) the presentation.
  271. var init = function () {
  272. if (initialized) { return; }
  273. // First we set up the viewport for mobile devices.
  274. // For some reason iPad goes nuts when it is not done properly.
  275. var meta = $("meta[name='viewport']") || document.createElement("meta");
  276. meta.content = "width=device-width, minimum-scale=1, maximum-scale=1, user-scalable=no";
  277. if (meta.parentNode !== document.head) {
  278. meta.name = 'viewport';
  279. document.head.appendChild(meta);
  280. }
  281. // initialize configuration object
  282. var rootData = root.dataset;
  283. config = {
  284. width: toNumber( rootData.width, defaults.width ),
  285. height: toNumber( rootData.height, defaults.height ),
  286. maxScale: toNumber( rootData.maxScale, defaults.maxScale ),
  287. minScale: toNumber( rootData.minScale, defaults.minScale ),
  288. perspective: toNumber( rootData.perspective, defaults.perspective ),
  289. transitionDuration: toNumber( rootData.transitionDuration, defaults.transitionDuration )
  290. };
  291. windowScale = computeWindowScale( config );
  292. // wrap steps with "canvas" element
  293. arrayify( root.childNodes ).forEach(function ( el ) {
  294. canvas.appendChild( el );
  295. });
  296. root.appendChild(canvas);
  297. // set initial styles
  298. document.documentElement.style.height = "100%";
  299. css(body, {
  300. height: "100%",
  301. overflow: "hidden"
  302. });
  303. var rootStyles = {
  304. position: "absolute",
  305. transformOrigin: "top left",
  306. transition: "all 0s ease-in-out",
  307. transformStyle: "preserve-3d"
  308. };
  309. css(root, rootStyles);
  310. css(root, {
  311. top: "50%",
  312. left: "50%",
  313. transform: perspective( config.perspective/windowScale ) + scale( windowScale )
  314. });
  315. css(canvas, rootStyles);
  316. body.classList.remove("impress-disabled");
  317. body.classList.add("impress-enabled");
  318. // get and init steps
  319. steps = $$(".step", root);
  320. steps.forEach( initStep );
  321. // set a default initial state of the canvas
  322. currentState = {
  323. translate: { x: 0, y: 0, z: 0 },
  324. rotate: { x: 0, y: 0, z: 0 },
  325. scale: 1
  326. };
  327. initialized = true;
  328. triggerEvent(root, "impress:init", { api: roots[ "impress-root-" + rootId ] });
  329. };
  330. // `getStep` is a helper function that returns a step element defined by parameter.
  331. // If a number is given, step with index given by the number is returned, if a string
  332. // is given step element with such id is returned, if DOM element is given it is returned
  333. // if it is a correct step element.
  334. var getStep = function ( step ) {
  335. if (typeof step === "number") {
  336. step = step < 0 ? steps[ steps.length + step] : steps[ step ];
  337. } else if (typeof step === "string") {
  338. step = byId(step);
  339. }
  340. return (step && step.id && stepsData["impress-" + step.id]) ? step : null;
  341. };
  342. // used to reset timeout for `impress:stepenter` event
  343. var stepEnterTimeout = null;
  344. // `goto` API function that moves to step given with `el` parameter (by index, id or element),
  345. // with a transition `duration` optionally given as second parameter.
  346. var goto = function ( el, duration ) {
  347. if ( !initialized || !(el = getStep(el)) ) {
  348. // presentation not initialized or given element is not a step
  349. return false;
  350. }
  351. // Sometimes it's possible to trigger focus on first link with some keyboard action.
  352. // Browser in such a case tries to scroll the page to make this element visible
  353. // (even that body overflow is set to hidden) and it breaks our careful positioning.
  354. //
  355. // So, as a lousy (and lazy) workaround we will make the page scroll back to the top
  356. // whenever slide is selected
  357. //
  358. // If you are reading this and know any better way to handle it, I'll be glad to hear about it!
  359. window.scrollTo(0, 0);
  360. var step = stepsData["impress-" + el.id];
  361. if ( activeStep ) {
  362. activeStep.classList.remove("active");
  363. body.classList.remove("impress-on-" + activeStep.id);
  364. }
  365. el.classList.add("active");
  366. body.classList.add("impress-on-" + el.id);
  367. // compute target state of the canvas based on given step
  368. var target = {
  369. rotate: {
  370. x: -step.rotate.x,
  371. y: -step.rotate.y,
  372. z: -step.rotate.z
  373. },
  374. translate: {
  375. x: -step.translate.x,
  376. y: -step.translate.y,
  377. z: -step.translate.z
  378. },
  379. scale: 1 / step.scale
  380. };
  381. // Check if the transition is zooming in or not.
  382. //
  383. // This information is used to alter the transition style:
  384. // when we are zooming in - we start with move and rotate transition
  385. // and the scaling is delayed, but when we are zooming out we start
  386. // with scaling down and move and rotation are delayed.
  387. var zoomin = target.scale >= currentState.scale;
  388. duration = toNumber(duration, config.transitionDuration);
  389. var delay = (duration / 2);
  390. // if the same step is re-selected, force computing window scaling,
  391. // because it is likely to be caused by window resize
  392. if (el === activeStep) {
  393. windowScale = computeWindowScale(config);
  394. }
  395. var targetScale = target.scale * windowScale;
  396. // trigger leave of currently active element (if it's not the same step again)
  397. if (activeStep && activeStep !== el) {
  398. onStepLeave(activeStep);
  399. }
  400. // Now we alter transforms of `root` and `canvas` to trigger transitions.
  401. //
  402. // And here is why there are two elements: `root` and `canvas` - they are
  403. // being animated separately:
  404. // `root` is used for scaling and `canvas` for translate and rotations.
  405. // Transitions on them are triggered with different delays (to make
  406. // visually nice and 'natural' looking transitions), so we need to know
  407. // that both of them are finished.
  408. css(root, {
  409. // to keep the perspective look similar for different scales
  410. // we need to 'scale' the perspective, too
  411. transform: perspective( config.perspective / targetScale ) + scale( targetScale ),
  412. transitionDuration: duration + "ms",
  413. transitionDelay: (zoomin ? delay : 0) + "ms"
  414. });
  415. css(canvas, {
  416. transform: rotate(target.rotate, true) + translate(target.translate),
  417. transitionDuration: duration + "ms",
  418. transitionDelay: (zoomin ? 0 : delay) + "ms"
  419. });
  420. // Here is a tricky part...
  421. //
  422. // If there is no change in scale or no change in rotation and translation, it means there was actually
  423. // no delay - because there was no transition on `root` or `canvas` elements.
  424. // We want to trigger `impress:stepenter` event in the correct moment, so here we compare the current
  425. // and target values to check if delay should be taken into account.
  426. //
  427. // I know that this `if` statement looks scary, but it's pretty simple when you know what is going on
  428. // - it's simply comparing all the values.
  429. if ( currentState.scale === target.scale ||
  430. (currentState.rotate.x === target.rotate.x && currentState.rotate.y === target.rotate.y &&
  431. currentState.rotate.z === target.rotate.z && currentState.translate.x === target.translate.x &&
  432. currentState.translate.y === target.translate.y && currentState.translate.z === target.translate.z) ) {
  433. delay = 0;
  434. }
  435. // store current state
  436. currentState = target;
  437. activeStep = el;
  438. // And here is where we trigger `impress:stepenter` event.
  439. // We simply set up a timeout to fire it taking transition duration (and possible delay) into account.
  440. //
  441. // I really wanted to make it in more elegant way. The `transitionend` event seemed to be the best way
  442. // to do it, but the fact that I'm using transitions on two separate elements and that the `transitionend`
  443. // event is only triggered when there was a transition (change in the values) caused some bugs and
  444. // made the code really complicated, cause I had to handle all the conditions separately. And it still
  445. // needed a `setTimeout` fallback for the situations when there is no transition at all.
  446. // So I decided that I'd rather make the code simpler than use shiny new `transitionend`.
  447. //
  448. // If you want learn something interesting and see how it was done with `transitionend` go back to
  449. // version 0.5.2 of impress.js: http://github.com/bartaz/impress.js/blob/0.5.2/js/impress.js
  450. window.clearTimeout(stepEnterTimeout);
  451. stepEnterTimeout = window.setTimeout(function() {
  452. onStepEnter(activeStep);
  453. }, duration + delay);
  454. return el;
  455. };
  456. // `prev` API function goes to previous step (in document order)
  457. var prev = function () {
  458. var prev = steps.indexOf( activeStep ) - 1;
  459. prev = prev >= 0 ? steps[ prev ] : steps[ steps.length-1 ];
  460. return goto(prev);
  461. };
  462. // `next` API function goes to next step (in document order)
  463. var next = function () {
  464. var next = steps.indexOf( activeStep ) + 1;
  465. next = next < steps.length ? steps[ next ] : steps[ 0 ];
  466. return goto(next);
  467. };
  468. // Adding some useful classes to step elements.
  469. //
  470. // All the steps that have not been shown yet are given `future` class.
  471. // When the step is entered the `future` class is removed and the `present`
  472. // class is given. When the step is left `present` class is replaced with
  473. // `past` class.
  474. //
  475. // So every step element is always in one of three possible states:
  476. // `future`, `present` and `past`.
  477. //
  478. // There classes can be used in CSS to style different types of steps.
  479. // For example the `present` class can be used to trigger some custom
  480. // animations when step is shown.
  481. root.addEventListener("impress:init", function(){
  482. // STEP CLASSES
  483. steps.forEach(function (step) {
  484. step.classList.add("future");
  485. });
  486. root.addEventListener("impress:stepenter", function (event) {
  487. event.target.classList.remove("past");
  488. event.target.classList.remove("future");
  489. event.target.classList.add("present");
  490. }, false);
  491. root.addEventListener("impress:stepleave", function (event) {
  492. event.target.classList.remove("present");
  493. event.target.classList.add("past");
  494. }, false);
  495. }, false);
  496. // Adding hash change support.
  497. root.addEventListener("impress:init", function(){
  498. // last hash detected
  499. var lastHash = "";
  500. // `#/step-id` is used instead of `#step-id` to prevent default browser
  501. // scrolling to element in hash.
  502. //
  503. // And it has to be set after animation finishes, because in Chrome it
  504. // makes transtion laggy.
  505. // BUG: http://code.google.com/p/chromium/issues/detail?id=62820
  506. root.addEventListener("impress:stepenter", function (event) {
  507. window.location.hash = lastHash = "#/" + event.target.id;
  508. }, false);
  509. window.addEventListener("hashchange", function () {
  510. // When the step is entered hash in the location is updated
  511. // (just few lines above from here), so the hash change is
  512. // triggered and we would call `goto` again on the same element.
  513. //
  514. // To avoid this we store last entered hash and compare.
  515. if (window.location.hash !== lastHash) {
  516. goto( getElementFromHash() );
  517. }
  518. }, false);
  519. // START
  520. // by selecting step defined in url or first step of the presentation
  521. goto(getElementFromHash() || steps[0], 0);
  522. }, false);
  523. body.classList.add("impress-disabled");
  524. // store and return API for given impress.js root element
  525. return (roots[ "impress-root-" + rootId ] = {
  526. init: init,
  527. goto: goto,
  528. next: next,
  529. prev: prev
  530. });
  531. };
  532. // flag that can be used in JS to check if browser have passed the support test
  533. impress.supported = impressSupported;
  534. })(document, window);
  535. // NAVIGATION EVENTS
  536. // As you can see this part is separate from the impress.js core code.
  537. // It's because these navigation actions only need what impress.js provides with
  538. // its simple API.
  539. //
  540. // In future I think about moving it to make them optional, move to separate files
  541. // and treat more like a 'plugins'.
  542. (function ( document, window ) {
  543. 'use strict';
  544. // throttling function calls, by Remy Sharp
  545. // http://remysharp.com/2010/07/21/throttling-function-calls/
  546. var throttle = function (fn, delay) {
  547. var timer = null;
  548. return function () {
  549. var context = this, args = arguments;
  550. clearTimeout(timer);
  551. timer = setTimeout(function () {
  552. fn.apply(context, args);
  553. }, delay);
  554. };
  555. };
  556. // wait for impress.js to be initialized
  557. document.addEventListener("impress:init", function (event) {
  558. // Getting API from event data.
  559. // So you don't event need to know what is the id of the root element
  560. // or anything. `impress:init` event data gives you everything you
  561. // need to control the presentation that was just initialized.
  562. var api = event.detail.api;
  563. // KEYBOARD NAVIGATION HANDLERS
  564. // Prevent default keydown action when one of supported key is pressed.
  565. document.addEventListener("keydown", function ( event ) {
  566. if ( event.keyCode === 9 || ( event.keyCode >= 32 && event.keyCode <= 34 ) || (event.keyCode >= 37 && event.keyCode <= 40) ) {
  567. event.preventDefault();
  568. }
  569. }, false);
  570. // Trigger impress action (next or prev) on keyup.
  571. // Supported keys are:
  572. // [space] - quite common in presentation software to move forward
  573. // [up] [right] / [down] [left] - again common and natural addition,
  574. // [pgdown] / [pgup] - often triggered by remote controllers,
  575. // [tab] - this one is quite controversial, but the reason it ended up on
  576. // this list is quite an interesting story... Remember that strange part
  577. // in the impress.js code where window is scrolled to 0,0 on every presentation
  578. // step, because sometimes browser scrolls viewport because of the focused element?
  579. // Well, the [tab] key by default navigates around focusable elements, so clicking
  580. // it very often caused scrolling to focused element and breaking impress.js
  581. // positioning. I didn't want to just prevent this default action, so I used [tab]
  582. // as another way to moving to next step... And yes, I know that for the sake of
  583. // consistency I should add [shift+tab] as opposite action...
  584. document.addEventListener("keyup", function ( event ) {
  585. if ( event.keyCode === 9 || ( event.keyCode >= 32 && event.keyCode <= 34 ) || (event.keyCode >= 37 && event.keyCode <= 40) ) {
  586. switch( event.keyCode ) {
  587. case 33: // pg up
  588. case 37: // left
  589. case 38: // up
  590. api.prev();
  591. break;
  592. case 9: // tab
  593. case 32: // space
  594. case 34: // pg down
  595. case 39: // right
  596. case 40: // down
  597. api.next();
  598. break;
  599. }
  600. event.preventDefault();
  601. }
  602. }, false);
  603. // delegated handler for clicking on the links to presentation steps
  604. document.addEventListener("click", function ( event ) {
  605. // event delegation with "bubbling"
  606. // check if event target (or any of its parents is a link)
  607. var target = event.target;
  608. while ( (target.tagName !== "A") &&
  609. (target !== document.documentElement) ) {
  610. target = target.parentNode;
  611. }
  612. if ( target.tagName === "A" ) {
  613. var href = target.getAttribute("href");
  614. // if it's a link to presentation step, target this step
  615. if ( href && href[0] === '#' ) {
  616. target = document.getElementById( href.slice(1) );
  617. }
  618. }
  619. if ( api.goto(target) ) {
  620. event.stopImmediatePropagation();
  621. event.preventDefault();
  622. }
  623. }, false);
  624. // delegated handler for clicking on step elements
  625. document.addEventListener("click", function ( event ) {
  626. var target = event.target;
  627. // find closest step element that is not active
  628. while ( !(target.classList.contains("step") && !target.classList.contains("active")) &&
  629. (target !== document.documentElement) ) {
  630. target = target.parentNode;
  631. }
  632. if ( api.goto(target) ) {
  633. event.preventDefault();
  634. }
  635. }, false);
  636. // touch handler to detect taps on the left and right side of the screen
  637. // based on awesome work of @hakimel: https://github.com/hakimel/reveal.js
  638. document.addEventListener("touchstart", function ( event ) {
  639. if (event.touches.length === 1) {
  640. var x = event.touches[0].clientX,
  641. width = window.innerWidth * 0.3,
  642. result = null;
  643. if ( x < width ) {
  644. result = api.prev();
  645. } else if ( x > window.innerWidth - width ) {
  646. result = api.next();
  647. }
  648. if (result) {
  649. event.preventDefault();
  650. }
  651. }
  652. }, false);
  653. // rescale presentation when window is resized
  654. window.addEventListener("resize", throttle(function () {
  655. // force going to active step again, to trigger rescaling
  656. api.goto( document.querySelector(".active"), 500 );
  657. }, 250), false);
  658. }, false);
  659. })(document, window);
  660. // THAT'S ALL FOLKS!
  661. //
  662. // Thanks for reading it all.
  663. // Or thanks for scrolling down and reading the last part.
  664. //
  665. // I've learnt a lot when building impress.js and I hope this code and comments
  666. // will help somebody learn at least some part of it.