reveal.js 120 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475
  1. /*!
  2. * reveal.js
  3. * http://lab.hakim.se/reveal-js
  4. * MIT licensed
  5. *
  6. * Copyright (C) 2015 Hakim El Hattab, http://hakim.se
  7. */
  8. (function( root, factory ) {
  9. if( typeof define === 'function' && define.amd ) {
  10. // AMD. Register as an anonymous module.
  11. define( function() {
  12. root.Reveal = factory();
  13. return root.Reveal;
  14. } );
  15. } else if( typeof exports === 'object' ) {
  16. // Node. Does not work with strict CommonJS.
  17. module.exports = factory();
  18. } else {
  19. // Browser globals.
  20. root.Reveal = factory();
  21. }
  22. }( this, function() {
  23. 'use strict';
  24. var Reveal;
  25. var SLIDES_SELECTOR = '.slides section',
  26. HORIZONTAL_SLIDES_SELECTOR = '.slides>section',
  27. VERTICAL_SLIDES_SELECTOR = '.slides>section.present>section',
  28. HOME_SLIDE_SELECTOR = '.slides>section:first-of-type',
  29. // Configurations defaults, can be overridden at initialization time
  30. config = {
  31. // The "normal" size of the presentation, aspect ratio will be preserved
  32. // when the presentation is scaled to fit different resolutions
  33. width: 960,
  34. height: 700,
  35. // Factor of the display size that should remain empty around the content
  36. margin: 0.1,
  37. // Bounds for smallest/largest possible scale to apply to content
  38. minScale: 0.2,
  39. maxScale: 1.5,
  40. // Display controls in the bottom right corner
  41. controls: true,
  42. // Display a presentation progress bar
  43. progress: true,
  44. // Display the page number of the current slide
  45. slideNumber: false,
  46. // Push each slide change to the browser history
  47. history: false,
  48. // Enable keyboard shortcuts for navigation
  49. keyboard: true,
  50. // Optional function that blocks keyboard events when retuning false
  51. keyboardCondition: null,
  52. // Enable the slide overview mode
  53. overview: true,
  54. // Vertical centering of slides
  55. center: true,
  56. // Enables touch navigation on devices with touch input
  57. touch: true,
  58. // Loop the presentation
  59. loop: false,
  60. // Change the presentation direction to be RTL
  61. rtl: false,
  62. // Turns fragments on and off globally
  63. fragments: true,
  64. // Flags if the presentation is running in an embedded mode,
  65. // i.e. contained within a limited portion of the screen
  66. embedded: false,
  67. // Flags if we should show a help overlay when the questionmark
  68. // key is pressed
  69. help: true,
  70. // Flags if it should be possible to pause the presentation (blackout)
  71. pause: true,
  72. // Number of milliseconds between automatically proceeding to the
  73. // next slide, disabled when set to 0, this value can be overwritten
  74. // by using a data-autoslide attribute on your slides
  75. autoSlide: 0,
  76. // Stop auto-sliding after user input
  77. autoSlideStoppable: true,
  78. // Enable slide navigation via mouse wheel
  79. mouseWheel: false,
  80. // Apply a 3D roll to links on hover
  81. rollingLinks: false,
  82. // Hides the address bar on mobile devices
  83. hideAddressBar: true,
  84. // Opens links in an iframe preview overlay
  85. previewLinks: false,
  86. // Exposes the reveal.js API through window.postMessage
  87. postMessage: true,
  88. // Dispatches all reveal.js events to the parent window through postMessage
  89. postMessageEvents: false,
  90. // Focuses body when page changes visiblity to ensure keyboard shortcuts work
  91. focusBodyOnPageVisibilityChange: true,
  92. // Transition style
  93. transition: 'slide', // none/fade/slide/convex/concave/zoom
  94. // Transition speed
  95. transitionSpeed: 'default', // default/fast/slow
  96. // Transition style for full page slide backgrounds
  97. backgroundTransition: 'fade', // none/fade/slide/convex/concave/zoom
  98. // Parallax background image
  99. parallaxBackgroundImage: '', // CSS syntax, e.g. "a.jpg"
  100. // Parallax background size
  101. parallaxBackgroundSize: '', // CSS syntax, e.g. "3000px 2000px"
  102. // Amount of pixels to move the parallax background per slide step
  103. parallaxBackgroundHorizontal: null,
  104. parallaxBackgroundVertical: null,
  105. // Number of slides away from the current that are visible
  106. viewDistance: 3,
  107. // Script dependencies to load
  108. dependencies: []
  109. },
  110. // Flags if reveal.js is loaded (has dispatched the 'ready' event)
  111. loaded = false,
  112. // Flags if the overview mode is currently active
  113. overview = false,
  114. // The horizontal and vertical index of the currently active slide
  115. indexh,
  116. indexv,
  117. // The previous and current slide HTML elements
  118. previousSlide,
  119. currentSlide,
  120. previousBackground,
  121. // Slides may hold a data-state attribute which we pick up and apply
  122. // as a class to the body. This list contains the combined state of
  123. // all current slides.
  124. state = [],
  125. // The current scale of the presentation (see width/height config)
  126. scale = 1,
  127. // CSS transform that is currently applied to the slides container,
  128. // split into two groups
  129. slidesTransform = { layout: '', overview: '' },
  130. // Cached references to DOM elements
  131. dom = {},
  132. // Features supported by the browser, see #checkCapabilities()
  133. features = {},
  134. // Client is a mobile device, see #checkCapabilities()
  135. isMobileDevice,
  136. // Throttles mouse wheel navigation
  137. lastMouseWheelStep = 0,
  138. // Delays updates to the URL due to a Chrome thumbnailer bug
  139. writeURLTimeout = 0,
  140. // Flags if the interaction event listeners are bound
  141. eventsAreBound = false,
  142. // The current auto-slide duration
  143. autoSlide = 0,
  144. // Auto slide properties
  145. autoSlidePlayer,
  146. autoSlideTimeout = 0,
  147. autoSlideStartTime = -1,
  148. autoSlidePaused = false,
  149. // Holds information about the currently ongoing touch input
  150. touch = {
  151. startX: 0,
  152. startY: 0,
  153. startSpan: 0,
  154. startCount: 0,
  155. captured: false,
  156. threshold: 40
  157. },
  158. // Holds information about the keyboard shortcuts
  159. keyboardShortcuts = {
  160. 'N , SPACE': 'Next slide',
  161. 'P': 'Previous slide',
  162. '← , H': 'Navigate left',
  163. '→ , L': 'Navigate right',
  164. '↑ , K': 'Navigate up',
  165. '↓ , J': 'Navigate down',
  166. 'Home': 'First slide',
  167. 'End': 'Last slide',
  168. 'B , .': 'Pause',
  169. 'F': 'Fullscreen',
  170. 'ESC, O': 'Slide overview'
  171. };
  172. /**
  173. * Starts up the presentation if the client is capable.
  174. */
  175. function initialize( options ) {
  176. checkCapabilities();
  177. if( !features.transforms2d && !features.transforms3d ) {
  178. document.body.setAttribute( 'class', 'no-transforms' );
  179. // Since JS won't be running any further, we need to load all
  180. // images that were intended to lazy load now
  181. var images = document.getElementsByTagName( 'img' );
  182. for( var i = 0, len = images.length; i < len; i++ ) {
  183. var image = images[i];
  184. if( image.getAttribute( 'data-src' ) ) {
  185. image.setAttribute( 'src', image.getAttribute( 'data-src' ) );
  186. image.removeAttribute( 'data-src' );
  187. }
  188. }
  189. // If the browser doesn't support core features we won't be
  190. // using JavaScript to control the presentation
  191. return;
  192. }
  193. // Cache references to key DOM elements
  194. dom.wrapper = document.querySelector( '.reveal' );
  195. dom.slides = document.querySelector( '.reveal .slides' );
  196. // Force a layout when the whole page, incl fonts, has loaded
  197. window.addEventListener( 'load', layout, false );
  198. var query = Reveal.getQueryHash();
  199. // Do not accept new dependencies via query config to avoid
  200. // the potential of malicious script injection
  201. if( typeof query['dependencies'] !== 'undefined' ) delete query['dependencies'];
  202. // Copy options over to our config object
  203. extend( config, options );
  204. extend( config, query );
  205. // Hide the address bar in mobile browsers
  206. hideAddressBar();
  207. // Loads the dependencies and continues to #start() once done
  208. load();
  209. }
  210. /**
  211. * Inspect the client to see what it's capable of, this
  212. * should only happens once per runtime.
  213. */
  214. function checkCapabilities() {
  215. features.transforms3d = 'WebkitPerspective' in document.body.style ||
  216. 'MozPerspective' in document.body.style ||
  217. 'msPerspective' in document.body.style ||
  218. 'OPerspective' in document.body.style ||
  219. 'perspective' in document.body.style;
  220. features.transforms2d = 'WebkitTransform' in document.body.style ||
  221. 'MozTransform' in document.body.style ||
  222. 'msTransform' in document.body.style ||
  223. 'OTransform' in document.body.style ||
  224. 'transform' in document.body.style;
  225. features.requestAnimationFrameMethod = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame;
  226. features.requestAnimationFrame = typeof features.requestAnimationFrameMethod === 'function';
  227. features.canvas = !!document.createElement( 'canvas' ).getContext;
  228. features.touch = !!( 'ontouchstart' in window );
  229. // Transitions in the overview are disabled in desktop and
  230. // mobile Safari due to lag
  231. features.overviewTransitions = !/Version\/[\d\.]+.*Safari/.test( navigator.userAgent );
  232. isMobileDevice = /(iphone|ipod|ipad|android)/gi.test( navigator.userAgent );
  233. }
  234. /**
  235. * Loads the dependencies of reveal.js. Dependencies are
  236. * defined via the configuration option 'dependencies'
  237. * and will be loaded prior to starting/binding reveal.js.
  238. * Some dependencies may have an 'async' flag, if so they
  239. * will load after reveal.js has been started up.
  240. */
  241. function load() {
  242. var scripts = [],
  243. scriptsAsync = [],
  244. scriptsToPreload = 0;
  245. // Called once synchronous scripts finish loading
  246. function proceed() {
  247. if( scriptsAsync.length ) {
  248. // Load asynchronous scripts
  249. head.js.apply( null, scriptsAsync );
  250. }
  251. start();
  252. }
  253. function loadScript( s ) {
  254. head.ready( s.src.match( /([\w\d_\-]*)\.?js$|[^\\\/]*$/i )[0], function() {
  255. // Extension may contain callback functions
  256. if( typeof s.callback === 'function' ) {
  257. s.callback.apply( this );
  258. }
  259. if( --scriptsToPreload === 0 ) {
  260. proceed();
  261. }
  262. });
  263. }
  264. for( var i = 0, len = config.dependencies.length; i < len; i++ ) {
  265. var s = config.dependencies[i];
  266. // Load if there's no condition or the condition is truthy
  267. if( !s.condition || s.condition() ) {
  268. if( s.async ) {
  269. scriptsAsync.push( s.src );
  270. }
  271. else {
  272. scripts.push( s.src );
  273. }
  274. loadScript( s );
  275. }
  276. }
  277. if( scripts.length ) {
  278. scriptsToPreload = scripts.length;
  279. // Load synchronous scripts
  280. head.js.apply( null, scripts );
  281. }
  282. else {
  283. proceed();
  284. }
  285. }
  286. /**
  287. * Starts up reveal.js by binding input events and navigating
  288. * to the current URL deeplink if there is one.
  289. */
  290. function start() {
  291. // Make sure we've got all the DOM elements we need
  292. setupDOM();
  293. // Listen to messages posted to this window
  294. setupPostMessage();
  295. // Prevent iframes from scrolling the slides out of view
  296. setupIframeScrollPrevention();
  297. // Resets all vertical slides so that only the first is visible
  298. resetVerticalSlides();
  299. // Updates the presentation to match the current configuration values
  300. configure();
  301. // Read the initial hash
  302. readURL();
  303. // Update all backgrounds
  304. updateBackground( true );
  305. // Notify listeners that the presentation is ready but use a 1ms
  306. // timeout to ensure it's not fired synchronously after #initialize()
  307. setTimeout( function() {
  308. // Enable transitions now that we're loaded
  309. dom.slides.classList.remove( 'no-transition' );
  310. loaded = true;
  311. dispatchEvent( 'ready', {
  312. 'indexh': indexh,
  313. 'indexv': indexv,
  314. 'currentSlide': currentSlide
  315. } );
  316. }, 1 );
  317. // Special setup and config is required when printing to PDF
  318. if( isPrintingPDF() ) {
  319. removeEventListeners();
  320. // The document needs to have loaded for the PDF layout
  321. // measurements to be accurate
  322. if( document.readyState === 'complete' ) {
  323. setupPDF();
  324. }
  325. else {
  326. window.addEventListener( 'load', setupPDF );
  327. }
  328. }
  329. }
  330. /**
  331. * Finds and stores references to DOM elements which are
  332. * required by the presentation. If a required element is
  333. * not found, it is created.
  334. */
  335. function setupDOM() {
  336. // Prevent transitions while we're loading
  337. dom.slides.classList.add( 'no-transition' );
  338. // Background element
  339. dom.background = createSingletonNode( dom.wrapper, 'div', 'backgrounds', null );
  340. // Progress bar
  341. dom.progress = createSingletonNode( dom.wrapper, 'div', 'progress', '<span></span>' );
  342. dom.progressbar = dom.progress.querySelector( 'span' );
  343. // Arrow controls
  344. createSingletonNode( dom.wrapper, 'aside', 'controls',
  345. '<div class="navigate-left"></div>' +
  346. '<div class="navigate-right"></div>' +
  347. '<div class="navigate-up"></div>' +
  348. '<div class="navigate-down"></div>' );
  349. // Slide number
  350. dom.slideNumber = createSingletonNode( dom.wrapper, 'div', 'slide-number', '' );
  351. // Overlay graphic which is displayed during the paused mode
  352. createSingletonNode( dom.wrapper, 'div', 'pause-overlay', null );
  353. // Cache references to elements
  354. dom.controls = document.querySelector( '.reveal .controls' );
  355. dom.theme = document.querySelector( '#theme' );
  356. dom.wrapper.setAttribute( 'role', 'application' );
  357. // There can be multiple instances of controls throughout the page
  358. dom.controlsLeft = toArray( document.querySelectorAll( '.navigate-left' ) );
  359. dom.controlsRight = toArray( document.querySelectorAll( '.navigate-right' ) );
  360. dom.controlsUp = toArray( document.querySelectorAll( '.navigate-up' ) );
  361. dom.controlsDown = toArray( document.querySelectorAll( '.navigate-down' ) );
  362. dom.controlsPrev = toArray( document.querySelectorAll( '.navigate-prev' ) );
  363. dom.controlsNext = toArray( document.querySelectorAll( '.navigate-next' ) );
  364. dom.statusDiv = createStatusDiv();
  365. }
  366. /**
  367. * Creates a hidden div with role aria-live to announce the
  368. * current slide content. Hide the div off-screen to make it
  369. * available only to Assistive Technologies.
  370. */
  371. function createStatusDiv() {
  372. var statusDiv = document.getElementById( 'aria-status-div' );
  373. if( !statusDiv ) {
  374. statusDiv = document.createElement( 'div' );
  375. statusDiv.style.position = 'absolute';
  376. statusDiv.style.height = '1px';
  377. statusDiv.style.width = '1px';
  378. statusDiv.style.overflow ='hidden';
  379. statusDiv.style.clip = 'rect( 1px, 1px, 1px, 1px )';
  380. statusDiv.setAttribute( 'id', 'aria-status-div' );
  381. statusDiv.setAttribute( 'aria-live', 'polite' );
  382. statusDiv.setAttribute( 'aria-atomic','true' );
  383. dom.wrapper.appendChild( statusDiv );
  384. }
  385. return statusDiv;
  386. }
  387. /**
  388. * Configures the presentation for printing to a static
  389. * PDF.
  390. */
  391. function setupPDF() {
  392. var slideSize = getComputedSlideSize( window.innerWidth, window.innerHeight );
  393. // Dimensions of the PDF pages
  394. var pageWidth = Math.floor( slideSize.width * ( 1 + config.margin ) ),
  395. pageHeight = Math.floor( slideSize.height * ( 1 + config.margin ) );
  396. // Dimensions of slides within the pages
  397. var slideWidth = slideSize.width,
  398. slideHeight = slideSize.height;
  399. // Let the browser know what page size we want to print
  400. injectStyleSheet( '@page{size:'+ pageWidth +'px '+ pageHeight +'px; margin: 0;}' );
  401. // Limit the size of certain elements to the dimensions of the slide
  402. injectStyleSheet( '.reveal section>img, .reveal section>video, .reveal section>iframe{max-width: '+ slideWidth +'px; max-height:'+ slideHeight +'px}' );
  403. document.body.classList.add( 'print-pdf' );
  404. document.body.style.width = pageWidth + 'px';
  405. document.body.style.height = pageHeight + 'px';
  406. // Slide and slide background layout
  407. toArray( dom.wrapper.querySelectorAll( SLIDES_SELECTOR ) ).forEach( function( slide ) {
  408. // Vertical stacks are not centred since their section
  409. // children will be
  410. if( slide.classList.contains( 'stack' ) === false ) {
  411. // Center the slide inside of the page, giving the slide some margin
  412. var left = ( pageWidth - slideWidth ) / 2,
  413. top = ( pageHeight - slideHeight ) / 2;
  414. var contentHeight = getAbsoluteHeight( slide );
  415. var numberOfPages = Math.max( Math.ceil( contentHeight / pageHeight ), 1 );
  416. // Center slides vertically
  417. if( numberOfPages === 1 && config.center || slide.classList.contains( 'center' ) ) {
  418. top = Math.max( ( pageHeight - contentHeight ) / 2, 0 );
  419. }
  420. // Position the slide inside of the page
  421. slide.style.left = left + 'px';
  422. slide.style.top = top + 'px';
  423. slide.style.width = slideWidth + 'px';
  424. // TODO Backgrounds need to be multiplied when the slide
  425. // stretches over multiple pages
  426. var background = slide.querySelector( '.slide-background' );
  427. if( background ) {
  428. background.style.width = pageWidth + 'px';
  429. background.style.height = ( pageHeight * numberOfPages ) + 'px';
  430. background.style.top = -top + 'px';
  431. background.style.left = -left + 'px';
  432. }
  433. }
  434. } );
  435. // Show all fragments
  436. toArray( dom.wrapper.querySelectorAll( SLIDES_SELECTOR + ' .fragment' ) ).forEach( function( fragment ) {
  437. fragment.classList.add( 'visible' );
  438. } );
  439. }
  440. /**
  441. * This is an unfortunate necessity. Iframes can trigger the
  442. * parent window to scroll, for example by focusing an input.
  443. * This scrolling can not be prevented by hiding overflow in
  444. * CSS so we have to resort to repeatedly checking if the
  445. * browser has decided to offset our slides :(
  446. */
  447. function setupIframeScrollPrevention() {
  448. if( dom.slides.querySelector( 'iframe' ) ) {
  449. setInterval( function() {
  450. if( dom.wrapper.scrollTop !== 0 || dom.wrapper.scrollLeft !== 0 ) {
  451. dom.wrapper.scrollTop = 0;
  452. dom.wrapper.scrollLeft = 0;
  453. }
  454. }, 500 );
  455. }
  456. }
  457. /**
  458. * Creates an HTML element and returns a reference to it.
  459. * If the element already exists the existing instance will
  460. * be returned.
  461. */
  462. function createSingletonNode( container, tagname, classname, innerHTML ) {
  463. // Find all nodes matching the description
  464. var nodes = container.querySelectorAll( '.' + classname );
  465. // Check all matches to find one which is a direct child of
  466. // the specified container
  467. for( var i = 0; i < nodes.length; i++ ) {
  468. var testNode = nodes[i];
  469. if( testNode.parentNode === container ) {
  470. return testNode;
  471. }
  472. }
  473. // If no node was found, create it now
  474. var node = document.createElement( tagname );
  475. node.classList.add( classname );
  476. if( typeof innerHTML === 'string' ) {
  477. node.innerHTML = innerHTML;
  478. }
  479. container.appendChild( node );
  480. return node;
  481. }
  482. /**
  483. * Creates the slide background elements and appends them
  484. * to the background container. One element is created per
  485. * slide no matter if the given slide has visible background.
  486. */
  487. function createBackgrounds() {
  488. var printMode = isPrintingPDF();
  489. // Clear prior backgrounds
  490. dom.background.innerHTML = '';
  491. dom.background.classList.add( 'no-transition' );
  492. // Iterate over all horizontal slides
  493. toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ).forEach( function( slideh ) {
  494. var backgroundStack;
  495. if( printMode ) {
  496. backgroundStack = createBackground( slideh, slideh );
  497. }
  498. else {
  499. backgroundStack = createBackground( slideh, dom.background );
  500. }
  501. // Iterate over all vertical slides
  502. toArray( slideh.querySelectorAll( 'section' ) ).forEach( function( slidev ) {
  503. if( printMode ) {
  504. createBackground( slidev, slidev );
  505. }
  506. else {
  507. createBackground( slidev, backgroundStack );
  508. }
  509. backgroundStack.classList.add( 'stack' );
  510. } );
  511. } );
  512. // Add parallax background if specified
  513. if( config.parallaxBackgroundImage ) {
  514. dom.background.style.backgroundImage = 'url("' + config.parallaxBackgroundImage + '")';
  515. dom.background.style.backgroundSize = config.parallaxBackgroundSize;
  516. // Make sure the below properties are set on the element - these properties are
  517. // needed for proper transitions to be set on the element via CSS. To remove
  518. // annoying background slide-in effect when the presentation starts, apply
  519. // these properties after short time delay
  520. setTimeout( function() {
  521. dom.wrapper.classList.add( 'has-parallax-background' );
  522. }, 1 );
  523. }
  524. else {
  525. dom.background.style.backgroundImage = '';
  526. dom.wrapper.classList.remove( 'has-parallax-background' );
  527. }
  528. }
  529. /**
  530. * Creates a background for the given slide.
  531. *
  532. * @param {HTMLElement} slide
  533. * @param {HTMLElement} container The element that the background
  534. * should be appended to
  535. */
  536. function createBackground( slide, container ) {
  537. var data = {
  538. background: slide.getAttribute( 'data-background' ),
  539. backgroundSize: slide.getAttribute( 'data-background-size' ),
  540. backgroundImage: slide.getAttribute( 'data-background-image' ),
  541. backgroundVideo: slide.getAttribute( 'data-background-video' ),
  542. backgroundIframe: slide.getAttribute( 'data-background-iframe' ),
  543. backgroundColor: slide.getAttribute( 'data-background-color' ),
  544. backgroundRepeat: slide.getAttribute( 'data-background-repeat' ),
  545. backgroundPosition: slide.getAttribute( 'data-background-position' ),
  546. backgroundTransition: slide.getAttribute( 'data-background-transition' )
  547. };
  548. var element = document.createElement( 'div' );
  549. // Carry over custom classes from the slide to the background
  550. element.className = 'slide-background ' + slide.className.replace( /present|past|future/, '' );
  551. if( data.background ) {
  552. // Auto-wrap image urls in url(...)
  553. if( /^(http|file|\/\/)/gi.test( data.background ) || /\.(svg|png|jpg|jpeg|gif|bmp)$/gi.test( data.background ) ) {
  554. slide.setAttribute( 'data-background-image', data.background );
  555. }
  556. else {
  557. element.style.background = data.background;
  558. }
  559. }
  560. // Create a hash for this combination of background settings.
  561. // This is used to determine when two slide backgrounds are
  562. // the same.
  563. if( data.background || data.backgroundColor || data.backgroundImage || data.backgroundVideo || data.backgroundIframe ) {
  564. element.setAttribute( 'data-background-hash', data.background +
  565. data.backgroundSize +
  566. data.backgroundImage +
  567. data.backgroundVideo +
  568. data.backgroundIframe +
  569. data.backgroundColor +
  570. data.backgroundRepeat +
  571. data.backgroundPosition +
  572. data.backgroundTransition );
  573. }
  574. // Additional and optional background properties
  575. if( data.backgroundSize ) element.style.backgroundSize = data.backgroundSize;
  576. if( data.backgroundColor ) element.style.backgroundColor = data.backgroundColor;
  577. if( data.backgroundRepeat ) element.style.backgroundRepeat = data.backgroundRepeat;
  578. if( data.backgroundPosition ) element.style.backgroundPosition = data.backgroundPosition;
  579. if( data.backgroundTransition ) element.setAttribute( 'data-background-transition', data.backgroundTransition );
  580. container.appendChild( element );
  581. // If backgrounds are being recreated, clear old classes
  582. slide.classList.remove( 'has-dark-background' );
  583. slide.classList.remove( 'has-light-background' );
  584. // If this slide has a background color, add a class that
  585. // signals if it is light or dark. If the slide has no background
  586. // color, no class will be set
  587. var computedBackgroundColor = window.getComputedStyle( element ).backgroundColor;
  588. if( computedBackgroundColor ) {
  589. var rgb = colorToRgb( computedBackgroundColor );
  590. // Ignore fully transparent backgrounds. Some browsers return
  591. // rgba(0,0,0,0) when reading the computed background color of
  592. // an element with no background
  593. if( rgb && rgb.a !== 0 ) {
  594. if( colorBrightness( computedBackgroundColor ) < 128 ) {
  595. slide.classList.add( 'has-dark-background' );
  596. }
  597. else {
  598. slide.classList.add( 'has-light-background' );
  599. }
  600. }
  601. }
  602. return element;
  603. }
  604. /**
  605. * Registers a listener to postMessage events, this makes it
  606. * possible to call all reveal.js API methods from another
  607. * window. For example:
  608. *
  609. * revealWindow.postMessage( JSON.stringify({
  610. * method: 'slide',
  611. * args: [ 2 ]
  612. * }), '*' );
  613. */
  614. function setupPostMessage() {
  615. if( config.postMessage ) {
  616. window.addEventListener( 'message', function ( event ) {
  617. var data = event.data;
  618. // Make sure we're dealing with JSON
  619. if( typeof data === 'string' && data.charAt( 0 ) === '{' && data.charAt( data.length - 1 ) === '}' ) {
  620. data = JSON.parse( data );
  621. // Check if the requested method can be found
  622. if( data.method && typeof Reveal[data.method] === 'function' ) {
  623. Reveal[data.method].apply( Reveal, data.args );
  624. }
  625. }
  626. }, false );
  627. }
  628. }
  629. /**
  630. * Applies the configuration settings from the config
  631. * object. May be called multiple times.
  632. */
  633. function configure( options ) {
  634. var numberOfSlides = dom.wrapper.querySelectorAll( SLIDES_SELECTOR ).length;
  635. dom.wrapper.classList.remove( config.transition );
  636. // New config options may be passed when this method
  637. // is invoked through the API after initialization
  638. if( typeof options === 'object' ) extend( config, options );
  639. // Force linear transition based on browser capabilities
  640. if( features.transforms3d === false ) config.transition = 'linear';
  641. dom.wrapper.classList.add( config.transition );
  642. dom.wrapper.setAttribute( 'data-transition-speed', config.transitionSpeed );
  643. dom.wrapper.setAttribute( 'data-background-transition', config.backgroundTransition );
  644. dom.controls.style.display = config.controls ? 'block' : 'none';
  645. dom.progress.style.display = config.progress ? 'block' : 'none';
  646. if( config.rtl ) {
  647. dom.wrapper.classList.add( 'rtl' );
  648. }
  649. else {
  650. dom.wrapper.classList.remove( 'rtl' );
  651. }
  652. if( config.center ) {
  653. dom.wrapper.classList.add( 'center' );
  654. }
  655. else {
  656. dom.wrapper.classList.remove( 'center' );
  657. }
  658. // Exit the paused mode if it was configured off
  659. if( config.pause === false ) {
  660. resume();
  661. }
  662. if( config.mouseWheel ) {
  663. document.addEventListener( 'DOMMouseScroll', onDocumentMouseScroll, false ); // FF
  664. document.addEventListener( 'mousewheel', onDocumentMouseScroll, false );
  665. }
  666. else {
  667. document.removeEventListener( 'DOMMouseScroll', onDocumentMouseScroll, false ); // FF
  668. document.removeEventListener( 'mousewheel', onDocumentMouseScroll, false );
  669. }
  670. // Rolling 3D links
  671. if( config.rollingLinks ) {
  672. enableRollingLinks();
  673. }
  674. else {
  675. disableRollingLinks();
  676. }
  677. // Iframe link previews
  678. if( config.previewLinks ) {
  679. enablePreviewLinks();
  680. }
  681. else {
  682. disablePreviewLinks();
  683. enablePreviewLinks( '[data-preview-link]' );
  684. }
  685. // Remove existing auto-slide controls
  686. if( autoSlidePlayer ) {
  687. autoSlidePlayer.destroy();
  688. autoSlidePlayer = null;
  689. }
  690. // Generate auto-slide controls if needed
  691. if( numberOfSlides > 1 && config.autoSlide && config.autoSlideStoppable && features.canvas && features.requestAnimationFrame ) {
  692. autoSlidePlayer = new Playback( dom.wrapper, function() {
  693. return Math.min( Math.max( ( Date.now() - autoSlideStartTime ) / autoSlide, 0 ), 1 );
  694. } );
  695. autoSlidePlayer.on( 'click', onAutoSlidePlayerClick );
  696. autoSlidePaused = false;
  697. }
  698. // When fragments are turned off they should be visible
  699. if( config.fragments === false ) {
  700. toArray( dom.slides.querySelectorAll( '.fragment' ) ).forEach( function( element ) {
  701. element.classList.add( 'visible' );
  702. element.classList.remove( 'current-fragment' );
  703. } );
  704. }
  705. sync();
  706. }
  707. /**
  708. * Binds all event listeners.
  709. */
  710. function addEventListeners() {
  711. eventsAreBound = true;
  712. window.addEventListener( 'hashchange', onWindowHashChange, false );
  713. window.addEventListener( 'resize', onWindowResize, false );
  714. if( config.touch ) {
  715. dom.wrapper.addEventListener( 'touchstart', onTouchStart, false );
  716. dom.wrapper.addEventListener( 'touchmove', onTouchMove, false );
  717. dom.wrapper.addEventListener( 'touchend', onTouchEnd, false );
  718. // Support pointer-style touch interaction as well
  719. if( window.navigator.pointerEnabled ) {
  720. // IE 11 uses un-prefixed version of pointer events
  721. dom.wrapper.addEventListener( 'pointerdown', onPointerDown, false );
  722. dom.wrapper.addEventListener( 'pointermove', onPointerMove, false );
  723. dom.wrapper.addEventListener( 'pointerup', onPointerUp, false );
  724. }
  725. else if( window.navigator.msPointerEnabled ) {
  726. // IE 10 uses prefixed version of pointer events
  727. dom.wrapper.addEventListener( 'MSPointerDown', onPointerDown, false );
  728. dom.wrapper.addEventListener( 'MSPointerMove', onPointerMove, false );
  729. dom.wrapper.addEventListener( 'MSPointerUp', onPointerUp, false );
  730. }
  731. }
  732. if( config.keyboard ) {
  733. document.addEventListener( 'keydown', onDocumentKeyDown, false );
  734. document.addEventListener( 'keypress', onDocumentKeyPress, false );
  735. }
  736. if( config.progress && dom.progress ) {
  737. dom.progress.addEventListener( 'click', onProgressClicked, false );
  738. }
  739. if( config.focusBodyOnPageVisibilityChange ) {
  740. var visibilityChange;
  741. if( 'hidden' in document ) {
  742. visibilityChange = 'visibilitychange';
  743. }
  744. else if( 'msHidden' in document ) {
  745. visibilityChange = 'msvisibilitychange';
  746. }
  747. else if( 'webkitHidden' in document ) {
  748. visibilityChange = 'webkitvisibilitychange';
  749. }
  750. if( visibilityChange ) {
  751. document.addEventListener( visibilityChange, onPageVisibilityChange, false );
  752. }
  753. }
  754. // Listen to both touch and click events, in case the device
  755. // supports both
  756. var pointerEvents = [ 'touchstart', 'click' ];
  757. // Only support touch for Android, fixes double navigations in
  758. // stock browser
  759. if( navigator.userAgent.match( /android/gi ) ) {
  760. pointerEvents = [ 'touchstart' ];
  761. }
  762. pointerEvents.forEach( function( eventName ) {
  763. dom.controlsLeft.forEach( function( el ) { el.addEventListener( eventName, onNavigateLeftClicked, false ); } );
  764. dom.controlsRight.forEach( function( el ) { el.addEventListener( eventName, onNavigateRightClicked, false ); } );
  765. dom.controlsUp.forEach( function( el ) { el.addEventListener( eventName, onNavigateUpClicked, false ); } );
  766. dom.controlsDown.forEach( function( el ) { el.addEventListener( eventName, onNavigateDownClicked, false ); } );
  767. dom.controlsPrev.forEach( function( el ) { el.addEventListener( eventName, onNavigatePrevClicked, false ); } );
  768. dom.controlsNext.forEach( function( el ) { el.addEventListener( eventName, onNavigateNextClicked, false ); } );
  769. } );
  770. }
  771. /**
  772. * Unbinds all event listeners.
  773. */
  774. function removeEventListeners() {
  775. eventsAreBound = false;
  776. document.removeEventListener( 'keydown', onDocumentKeyDown, false );
  777. document.removeEventListener( 'keypress', onDocumentKeyPress, false );
  778. window.removeEventListener( 'hashchange', onWindowHashChange, false );
  779. window.removeEventListener( 'resize', onWindowResize, false );
  780. dom.wrapper.removeEventListener( 'touchstart', onTouchStart, false );
  781. dom.wrapper.removeEventListener( 'touchmove', onTouchMove, false );
  782. dom.wrapper.removeEventListener( 'touchend', onTouchEnd, false );
  783. // IE11
  784. if( window.navigator.pointerEnabled ) {
  785. dom.wrapper.removeEventListener( 'pointerdown', onPointerDown, false );
  786. dom.wrapper.removeEventListener( 'pointermove', onPointerMove, false );
  787. dom.wrapper.removeEventListener( 'pointerup', onPointerUp, false );
  788. }
  789. // IE10
  790. else if( window.navigator.msPointerEnabled ) {
  791. dom.wrapper.removeEventListener( 'MSPointerDown', onPointerDown, false );
  792. dom.wrapper.removeEventListener( 'MSPointerMove', onPointerMove, false );
  793. dom.wrapper.removeEventListener( 'MSPointerUp', onPointerUp, false );
  794. }
  795. if ( config.progress && dom.progress ) {
  796. dom.progress.removeEventListener( 'click', onProgressClicked, false );
  797. }
  798. [ 'touchstart', 'click' ].forEach( function( eventName ) {
  799. dom.controlsLeft.forEach( function( el ) { el.removeEventListener( eventName, onNavigateLeftClicked, false ); } );
  800. dom.controlsRight.forEach( function( el ) { el.removeEventListener( eventName, onNavigateRightClicked, false ); } );
  801. dom.controlsUp.forEach( function( el ) { el.removeEventListener( eventName, onNavigateUpClicked, false ); } );
  802. dom.controlsDown.forEach( function( el ) { el.removeEventListener( eventName, onNavigateDownClicked, false ); } );
  803. dom.controlsPrev.forEach( function( el ) { el.removeEventListener( eventName, onNavigatePrevClicked, false ); } );
  804. dom.controlsNext.forEach( function( el ) { el.removeEventListener( eventName, onNavigateNextClicked, false ); } );
  805. } );
  806. }
  807. /**
  808. * Extend object a with the properties of object b.
  809. * If there's a conflict, object b takes precedence.
  810. */
  811. function extend( a, b ) {
  812. for( var i in b ) {
  813. a[ i ] = b[ i ];
  814. }
  815. }
  816. /**
  817. * Converts the target object to an array.
  818. */
  819. function toArray( o ) {
  820. return Array.prototype.slice.call( o );
  821. }
  822. /**
  823. * Utility for deserializing a value.
  824. */
  825. function deserialize( value ) {
  826. if( typeof value === 'string' ) {
  827. if( value === 'null' ) return null;
  828. else if( value === 'true' ) return true;
  829. else if( value === 'false' ) return false;
  830. else if( value.match( /^\d+$/ ) ) return parseFloat( value );
  831. }
  832. return value;
  833. }
  834. /**
  835. * Measures the distance in pixels between point a
  836. * and point b.
  837. *
  838. * @param {Object} a point with x/y properties
  839. * @param {Object} b point with x/y properties
  840. */
  841. function distanceBetween( a, b ) {
  842. var dx = a.x - b.x,
  843. dy = a.y - b.y;
  844. return Math.sqrt( dx*dx + dy*dy );
  845. }
  846. /**
  847. * Applies a CSS transform to the target element.
  848. */
  849. function transformElement( element, transform ) {
  850. element.style.WebkitTransform = transform;
  851. element.style.MozTransform = transform;
  852. element.style.msTransform = transform;
  853. element.style.transform = transform;
  854. }
  855. /**
  856. * Applies CSS transforms to the slides container. The container
  857. * is transformed from two separate sources: layout and the overview
  858. * mode.
  859. */
  860. function transformSlides( transforms ) {
  861. // Pick up new transforms from arguments
  862. if( typeof transforms.layout === 'string' ) slidesTransform.layout = transforms.layout;
  863. if( typeof transforms.overview === 'string' ) slidesTransform.overview = transforms.overview;
  864. // Apply the transforms to the slides container
  865. if( slidesTransform.layout ) {
  866. transformElement( dom.slides, slidesTransform.layout + ' ' + slidesTransform.overview );
  867. }
  868. else {
  869. transformElement( dom.slides, slidesTransform.overview );
  870. }
  871. }
  872. /**
  873. * Injects the given CSS styles into the DOM.
  874. */
  875. function injectStyleSheet( value ) {
  876. var tag = document.createElement( 'style' );
  877. tag.type = 'text/css';
  878. if( tag.styleSheet ) {
  879. tag.styleSheet.cssText = value;
  880. }
  881. else {
  882. tag.appendChild( document.createTextNode( value ) );
  883. }
  884. document.getElementsByTagName( 'head' )[0].appendChild( tag );
  885. }
  886. /**
  887. * Measures the distance in pixels between point a and point b.
  888. *
  889. * @param {String} color The string representation of a color,
  890. * the following formats are supported:
  891. * - #000
  892. * - #000000
  893. * - rgb(0,0,0)
  894. */
  895. function colorToRgb( color ) {
  896. var hex3 = color.match( /^#([0-9a-f]{3})$/i );
  897. if( hex3 && hex3[1] ) {
  898. hex3 = hex3[1];
  899. return {
  900. r: parseInt( hex3.charAt( 0 ), 16 ) * 0x11,
  901. g: parseInt( hex3.charAt( 1 ), 16 ) * 0x11,
  902. b: parseInt( hex3.charAt( 2 ), 16 ) * 0x11
  903. };
  904. }
  905. var hex6 = color.match( /^#([0-9a-f]{6})$/i );
  906. if( hex6 && hex6[1] ) {
  907. hex6 = hex6[1];
  908. return {
  909. r: parseInt( hex6.substr( 0, 2 ), 16 ),
  910. g: parseInt( hex6.substr( 2, 2 ), 16 ),
  911. b: parseInt( hex6.substr( 4, 2 ), 16 )
  912. };
  913. }
  914. var rgb = color.match( /^rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i );
  915. if( rgb ) {
  916. return {
  917. r: parseInt( rgb[1], 10 ),
  918. g: parseInt( rgb[2], 10 ),
  919. b: parseInt( rgb[3], 10 )
  920. };
  921. }
  922. var rgba = color.match( /^rgba\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\,\s*([\d]+|[\d]*.[\d]+)\s*\)$/i );
  923. if( rgba ) {
  924. return {
  925. r: parseInt( rgba[1], 10 ),
  926. g: parseInt( rgba[2], 10 ),
  927. b: parseInt( rgba[3], 10 ),
  928. a: parseFloat( rgba[4] )
  929. };
  930. }
  931. return null;
  932. }
  933. /**
  934. * Calculates brightness on a scale of 0-255.
  935. *
  936. * @param color See colorStringToRgb for supported formats.
  937. */
  938. function colorBrightness( color ) {
  939. if( typeof color === 'string' ) color = colorToRgb( color );
  940. if( color ) {
  941. return ( color.r * 299 + color.g * 587 + color.b * 114 ) / 1000;
  942. }
  943. return null;
  944. }
  945. /**
  946. * Retrieves the height of the given element by looking
  947. * at the position and height of its immediate children.
  948. */
  949. function getAbsoluteHeight( element ) {
  950. var height = 0;
  951. if( element ) {
  952. var absoluteChildren = 0;
  953. toArray( element.childNodes ).forEach( function( child ) {
  954. if( typeof child.offsetTop === 'number' && child.style ) {
  955. // Count # of abs children
  956. if( window.getComputedStyle( child ).position === 'absolute' ) {
  957. absoluteChildren += 1;
  958. }
  959. height = Math.max( height, child.offsetTop + child.offsetHeight );
  960. }
  961. } );
  962. // If there are no absolute children, use offsetHeight
  963. if( absoluteChildren === 0 ) {
  964. height = element.offsetHeight;
  965. }
  966. }
  967. return height;
  968. }
  969. /**
  970. * Returns the remaining height within the parent of the
  971. * target element.
  972. *
  973. * remaining height = [ configured parent height ] - [ current parent height ]
  974. */
  975. function getRemainingHeight( element, height ) {
  976. height = height || 0;
  977. if( element ) {
  978. var newHeight, oldHeight = element.style.height;
  979. // Change the .stretch element height to 0 in order find the height of all
  980. // the other elements
  981. element.style.height = '0px';
  982. newHeight = height - element.parentNode.offsetHeight;
  983. // Restore the old height, just in case
  984. element.style.height = oldHeight + 'px';
  985. return newHeight;
  986. }
  987. return height;
  988. }
  989. /**
  990. * Checks if this instance is being used to print a PDF.
  991. */
  992. function isPrintingPDF() {
  993. return ( /print-pdf/gi ).test( window.location.search );
  994. }
  995. /**
  996. * Hides the address bar if we're on a mobile device.
  997. */
  998. function hideAddressBar() {
  999. if( config.hideAddressBar && isMobileDevice ) {
  1000. // Events that should trigger the address bar to hide
  1001. window.addEventListener( 'load', removeAddressBar, false );
  1002. window.addEventListener( 'orientationchange', removeAddressBar, false );
  1003. }
  1004. }
  1005. /**
  1006. * Causes the address bar to hide on mobile devices,
  1007. * more vertical space ftw.
  1008. */
  1009. function removeAddressBar() {
  1010. setTimeout( function() {
  1011. window.scrollTo( 0, 1 );
  1012. }, 10 );
  1013. }
  1014. /**
  1015. * Dispatches an event of the specified type from the
  1016. * reveal DOM element.
  1017. */
  1018. function dispatchEvent( type, args ) {
  1019. var event = document.createEvent( 'HTMLEvents', 1, 2 );
  1020. event.initEvent( type, true, true );
  1021. extend( event, args );
  1022. dom.wrapper.dispatchEvent( event );
  1023. // If we're in an iframe, post each reveal.js event to the
  1024. // parent window. Used by the notes plugin
  1025. if( config.postMessageEvents && window.parent !== window.self ) {
  1026. window.parent.postMessage( JSON.stringify({ namespace: 'reveal', eventName: type, state: getState() }), '*' );
  1027. }
  1028. }
  1029. /**
  1030. * Wrap all links in 3D goodness.
  1031. */
  1032. function enableRollingLinks() {
  1033. if( features.transforms3d && !( 'msPerspective' in document.body.style ) ) {
  1034. var anchors = dom.wrapper.querySelectorAll( SLIDES_SELECTOR + ' a' );
  1035. for( var i = 0, len = anchors.length; i < len; i++ ) {
  1036. var anchor = anchors[i];
  1037. if( anchor.textContent && !anchor.querySelector( '*' ) && ( !anchor.className || !anchor.classList.contains( anchor, 'roll' ) ) ) {
  1038. var span = document.createElement('span');
  1039. span.setAttribute('data-title', anchor.text);
  1040. span.innerHTML = anchor.innerHTML;
  1041. anchor.classList.add( 'roll' );
  1042. anchor.innerHTML = '';
  1043. anchor.appendChild(span);
  1044. }
  1045. }
  1046. }
  1047. }
  1048. /**
  1049. * Unwrap all 3D links.
  1050. */
  1051. function disableRollingLinks() {
  1052. var anchors = dom.wrapper.querySelectorAll( SLIDES_SELECTOR + ' a.roll' );
  1053. for( var i = 0, len = anchors.length; i < len; i++ ) {
  1054. var anchor = anchors[i];
  1055. var span = anchor.querySelector( 'span' );
  1056. if( span ) {
  1057. anchor.classList.remove( 'roll' );
  1058. anchor.innerHTML = span.innerHTML;
  1059. }
  1060. }
  1061. }
  1062. /**
  1063. * Bind preview frame links.
  1064. */
  1065. function enablePreviewLinks( selector ) {
  1066. var anchors = toArray( document.querySelectorAll( selector ? selector : 'a' ) );
  1067. anchors.forEach( function( element ) {
  1068. if( /^(http|www)/gi.test( element.getAttribute( 'href' ) ) ) {
  1069. element.addEventListener( 'click', onPreviewLinkClicked, false );
  1070. }
  1071. } );
  1072. }
  1073. /**
  1074. * Unbind preview frame links.
  1075. */
  1076. function disablePreviewLinks() {
  1077. var anchors = toArray( document.querySelectorAll( 'a' ) );
  1078. anchors.forEach( function( element ) {
  1079. if( /^(http|www)/gi.test( element.getAttribute( 'href' ) ) ) {
  1080. element.removeEventListener( 'click', onPreviewLinkClicked, false );
  1081. }
  1082. } );
  1083. }
  1084. /**
  1085. * Opens a preview window for the target URL.
  1086. */
  1087. function showPreview( url ) {
  1088. closeOverlay();
  1089. dom.overlay = document.createElement( 'div' );
  1090. dom.overlay.classList.add( 'overlay' );
  1091. dom.overlay.classList.add( 'overlay-preview' );
  1092. dom.wrapper.appendChild( dom.overlay );
  1093. dom.overlay.innerHTML = [
  1094. '<header>',
  1095. '<a class="close" href="#"><span class="icon"></span></a>',
  1096. '<a class="external" href="'+ url +'" target="_blank"><span class="icon"></span></a>',
  1097. '</header>',
  1098. '<div class="spinner"></div>',
  1099. '<div class="viewport">',
  1100. '<iframe src="'+ url +'"></iframe>',
  1101. '</div>'
  1102. ].join('');
  1103. dom.overlay.querySelector( 'iframe' ).addEventListener( 'load', function( event ) {
  1104. dom.overlay.classList.add( 'loaded' );
  1105. }, false );
  1106. dom.overlay.querySelector( '.close' ).addEventListener( 'click', function( event ) {
  1107. closeOverlay();
  1108. event.preventDefault();
  1109. }, false );
  1110. dom.overlay.querySelector( '.external' ).addEventListener( 'click', function( event ) {
  1111. closeOverlay();
  1112. }, false );
  1113. setTimeout( function() {
  1114. dom.overlay.classList.add( 'visible' );
  1115. }, 1 );
  1116. }
  1117. /**
  1118. * Opens a overlay window with help material.
  1119. */
  1120. function showHelp() {
  1121. if( config.help ) {
  1122. closeOverlay();
  1123. dom.overlay = document.createElement( 'div' );
  1124. dom.overlay.classList.add( 'overlay' );
  1125. dom.overlay.classList.add( 'overlay-help' );
  1126. dom.wrapper.appendChild( dom.overlay );
  1127. var html = '<p class="title">Keyboard Shortcuts</p><br/>';
  1128. html += '<table><th>KEY</th><th>ACTION</th>';
  1129. for( var key in keyboardShortcuts ) {
  1130. html += '<tr><td>' + key + '</td><td>' + keyboardShortcuts[ key ] + '</td></tr>';
  1131. }
  1132. html += '</table>';
  1133. dom.overlay.innerHTML = [
  1134. '<header>',
  1135. '<a class="close" href="#"><span class="icon"></span></a>',
  1136. '</header>',
  1137. '<div class="viewport">',
  1138. '<div class="viewport-inner">'+ html +'</div>',
  1139. '</div>'
  1140. ].join('');
  1141. dom.overlay.querySelector( '.close' ).addEventListener( 'click', function( event ) {
  1142. closeOverlay();
  1143. event.preventDefault();
  1144. }, false );
  1145. setTimeout( function() {
  1146. dom.overlay.classList.add( 'visible' );
  1147. }, 1 );
  1148. }
  1149. }
  1150. /**
  1151. * Closes any currently open overlay.
  1152. */
  1153. function closeOverlay() {
  1154. if( dom.overlay ) {
  1155. dom.overlay.parentNode.removeChild( dom.overlay );
  1156. dom.overlay = null;
  1157. }
  1158. }
  1159. /**
  1160. * Applies JavaScript-controlled layout rules to the
  1161. * presentation.
  1162. */
  1163. function layout() {
  1164. if( dom.wrapper && !isPrintingPDF() ) {
  1165. var size = getComputedSlideSize();
  1166. var slidePadding = 20; // TODO Dig this out of DOM
  1167. // Layout the contents of the slides
  1168. layoutSlideContents( config.width, config.height, slidePadding );
  1169. dom.slides.style.width = size.width + 'px';
  1170. dom.slides.style.height = size.height + 'px';
  1171. // Determine scale of content to fit within available space
  1172. scale = Math.min( size.presentationWidth / size.width, size.presentationHeight / size.height );
  1173. // Respect max/min scale settings
  1174. scale = Math.max( scale, config.minScale );
  1175. scale = Math.min( scale, config.maxScale );
  1176. // Don't apply any scaling styles if scale is 1
  1177. if( scale === 1 ) {
  1178. dom.slides.style.zoom = '';
  1179. dom.slides.style.left = '';
  1180. dom.slides.style.top = '';
  1181. dom.slides.style.bottom = '';
  1182. dom.slides.style.right = '';
  1183. transformSlides( { layout: '' } );
  1184. }
  1185. else {
  1186. // Prefer zooming in desktop Chrome so that content remains crisp
  1187. if( !isMobileDevice && /chrome/i.test( navigator.userAgent ) && typeof dom.slides.style.zoom !== 'undefined' ) {
  1188. dom.slides.style.zoom = scale;
  1189. transformSlides( { layout: '' } );
  1190. }
  1191. // Apply scale transform as a fallback
  1192. else {
  1193. dom.slides.style.left = '50%';
  1194. dom.slides.style.top = '50%';
  1195. dom.slides.style.bottom = 'auto';
  1196. dom.slides.style.right = 'auto';
  1197. transformSlides( { layout: 'translate(-50%, -50%) scale('+ scale +')' } );
  1198. }
  1199. }
  1200. // Select all slides, vertical and horizontal
  1201. var slides = toArray( dom.wrapper.querySelectorAll( SLIDES_SELECTOR ) );
  1202. for( var i = 0, len = slides.length; i < len; i++ ) {
  1203. var slide = slides[ i ];
  1204. // Don't bother updating invisible slides
  1205. if( slide.style.display === 'none' ) {
  1206. continue;
  1207. }
  1208. if( config.center || slide.classList.contains( 'center' ) ) {
  1209. // Vertical stacks are not centred since their section
  1210. // children will be
  1211. if( slide.classList.contains( 'stack' ) ) {
  1212. slide.style.top = 0;
  1213. }
  1214. else {
  1215. slide.style.top = Math.max( ( ( size.height - getAbsoluteHeight( slide ) ) / 2 ) - slidePadding, 0 ) + 'px';
  1216. }
  1217. }
  1218. else {
  1219. slide.style.top = '';
  1220. }
  1221. }
  1222. updateProgress();
  1223. updateParallax();
  1224. }
  1225. }
  1226. /**
  1227. * Applies layout logic to the contents of all slides in
  1228. * the presentation.
  1229. */
  1230. function layoutSlideContents( width, height, padding ) {
  1231. // Handle sizing of elements with the 'stretch' class
  1232. toArray( dom.slides.querySelectorAll( 'section > .stretch' ) ).forEach( function( element ) {
  1233. // Determine how much vertical space we can use
  1234. var remainingHeight = getRemainingHeight( element, height );
  1235. // Consider the aspect ratio of media elements
  1236. if( /(img|video)/gi.test( element.nodeName ) ) {
  1237. var nw = element.naturalWidth || element.videoWidth,
  1238. nh = element.naturalHeight || element.videoHeight;
  1239. var es = Math.min( width / nw, remainingHeight / nh );
  1240. element.style.width = ( nw * es ) + 'px';
  1241. element.style.height = ( nh * es ) + 'px';
  1242. }
  1243. else {
  1244. element.style.width = width + 'px';
  1245. element.style.height = remainingHeight + 'px';
  1246. }
  1247. } );
  1248. }
  1249. /**
  1250. * Calculates the computed pixel size of our slides. These
  1251. * values are based on the width and height configuration
  1252. * options.
  1253. */
  1254. function getComputedSlideSize( presentationWidth, presentationHeight ) {
  1255. var size = {
  1256. // Slide size
  1257. width: config.width,
  1258. height: config.height,
  1259. // Presentation size
  1260. presentationWidth: presentationWidth || dom.wrapper.offsetWidth,
  1261. presentationHeight: presentationHeight || dom.wrapper.offsetHeight
  1262. };
  1263. // Reduce available space by margin
  1264. size.presentationWidth -= ( size.presentationWidth * config.margin );
  1265. size.presentationHeight -= ( size.presentationHeight * config.margin );
  1266. // Slide width may be a percentage of available width
  1267. if( typeof size.width === 'string' && /%$/.test( size.width ) ) {
  1268. size.width = parseInt( size.width, 10 ) / 100 * size.presentationWidth;
  1269. }
  1270. // Slide height may be a percentage of available height
  1271. if( typeof size.height === 'string' && /%$/.test( size.height ) ) {
  1272. size.height = parseInt( size.height, 10 ) / 100 * size.presentationHeight;
  1273. }
  1274. return size;
  1275. }
  1276. /**
  1277. * Stores the vertical index of a stack so that the same
  1278. * vertical slide can be selected when navigating to and
  1279. * from the stack.
  1280. *
  1281. * @param {HTMLElement} stack The vertical stack element
  1282. * @param {int} v Index to memorize
  1283. */
  1284. function setPreviousVerticalIndex( stack, v ) {
  1285. if( typeof stack === 'object' && typeof stack.setAttribute === 'function' ) {
  1286. stack.setAttribute( 'data-previous-indexv', v || 0 );
  1287. }
  1288. }
  1289. /**
  1290. * Retrieves the vertical index which was stored using
  1291. * #setPreviousVerticalIndex() or 0 if no previous index
  1292. * exists.
  1293. *
  1294. * @param {HTMLElement} stack The vertical stack element
  1295. */
  1296. function getPreviousVerticalIndex( stack ) {
  1297. if( typeof stack === 'object' && typeof stack.setAttribute === 'function' && stack.classList.contains( 'stack' ) ) {
  1298. // Prefer manually defined start-indexv
  1299. var attributeName = stack.hasAttribute( 'data-start-indexv' ) ? 'data-start-indexv' : 'data-previous-indexv';
  1300. return parseInt( stack.getAttribute( attributeName ) || 0, 10 );
  1301. }
  1302. return 0;
  1303. }
  1304. /**
  1305. * Displays the overview of slides (quick nav) by scaling
  1306. * down and arranging all slide elements.
  1307. */
  1308. function activateOverview() {
  1309. // Only proceed if enabled in config
  1310. if( config.overview && !isOverview() ) {
  1311. overview = true;
  1312. dom.wrapper.classList.add( 'overview' );
  1313. dom.wrapper.classList.remove( 'overview-deactivating' );
  1314. if( features.overviewTransitions ) {
  1315. setTimeout( function() {
  1316. dom.wrapper.classList.add( 'overview-animated' );
  1317. }, 1 );
  1318. }
  1319. // Don't auto-slide while in overview mode
  1320. cancelAutoSlide();
  1321. // Move the backgrounds element into the slide container to
  1322. // that the same scaling is applied
  1323. dom.slides.appendChild( dom.background );
  1324. // Clicking on an overview slide navigates to it
  1325. toArray( dom.wrapper.querySelectorAll( SLIDES_SELECTOR ) ).forEach( function( slide ) {
  1326. if( !slide.classList.contains( 'stack' ) ) {
  1327. slide.addEventListener( 'click', onOverviewSlideClicked, true );
  1328. }
  1329. } );
  1330. updateSlidesVisibility();
  1331. layoutOverview();
  1332. updateOverview();
  1333. layout();
  1334. // Notify observers of the overview showing
  1335. dispatchEvent( 'overviewshown', {
  1336. 'indexh': indexh,
  1337. 'indexv': indexv,
  1338. 'currentSlide': currentSlide
  1339. } );
  1340. }
  1341. }
  1342. /**
  1343. * Uses CSS transforms to position all slides in a grid for
  1344. * display inside of the overview mode.
  1345. */
  1346. function layoutOverview() {
  1347. var margin = 70;
  1348. var slideWidth = config.width + margin,
  1349. slideHeight = config.height + margin;
  1350. // Reverse in RTL mode
  1351. if( config.rtl ) {
  1352. slideWidth = -slideWidth;
  1353. }
  1354. // Layout slides
  1355. toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ).forEach( function( hslide, h ) {
  1356. hslide.setAttribute( 'data-index-h', h );
  1357. transformElement( hslide, 'translate3d(' + ( h * slideWidth ) + 'px, 0, 0)' );
  1358. if( hslide.classList.contains( 'stack' ) ) {
  1359. toArray( hslide.querySelectorAll( 'section' ) ).forEach( function( vslide, v ) {
  1360. vslide.setAttribute( 'data-index-h', h );
  1361. vslide.setAttribute( 'data-index-v', v );
  1362. transformElement( vslide, 'translate3d(0, ' + ( v * slideHeight ) + 'px, 0)' );
  1363. } );
  1364. }
  1365. } );
  1366. // Layout slide backgrounds
  1367. toArray( dom.background.childNodes ).forEach( function( hbackground, h ) {
  1368. transformElement( hbackground, 'translate3d(' + ( h * slideWidth ) + 'px, 0, 0)' );
  1369. toArray( hbackground.querySelectorAll( '.slide-background' ) ).forEach( function( vbackground, v ) {
  1370. transformElement( vbackground, 'translate3d(0, ' + ( v * slideHeight ) + 'px, 0)' );
  1371. } );
  1372. } );
  1373. }
  1374. /**
  1375. * Moves the overview viewport to the current slides.
  1376. * Called each time the current slide changes.
  1377. */
  1378. function updateOverview() {
  1379. var margin = 70;
  1380. var slideWidth = config.width + margin,
  1381. slideHeight = config.height + margin;
  1382. // Reverse in RTL mode
  1383. if( config.rtl ) {
  1384. slideWidth = -slideWidth;
  1385. }
  1386. transformSlides( {
  1387. overview: [
  1388. 'translateX('+ ( -indexh * slideWidth ) +'px)',
  1389. 'translateY('+ ( -indexv * slideHeight ) +'px)',
  1390. 'translateZ('+ ( window.innerWidth < 400 ? -1000 : -2500 ) +'px)'
  1391. ].join( ' ' )
  1392. } );
  1393. }
  1394. /**
  1395. * Exits the slide overview and enters the currently
  1396. * active slide.
  1397. */
  1398. function deactivateOverview() {
  1399. // Only proceed if enabled in config
  1400. if( config.overview ) {
  1401. overview = false;
  1402. dom.wrapper.classList.remove( 'overview' );
  1403. dom.wrapper.classList.remove( 'overview-animated' );
  1404. // Temporarily add a class so that transitions can do different things
  1405. // depending on whether they are exiting/entering overview, or just
  1406. // moving from slide to slide
  1407. dom.wrapper.classList.add( 'overview-deactivating' );
  1408. setTimeout( function () {
  1409. dom.wrapper.classList.remove( 'overview-deactivating' );
  1410. }, 1 );
  1411. // Move the background element back out
  1412. dom.wrapper.appendChild( dom.background );
  1413. // Clean up changes made to slides
  1414. toArray( dom.wrapper.querySelectorAll( SLIDES_SELECTOR ) ).forEach( function( slide ) {
  1415. transformElement( slide, '' );
  1416. slide.removeEventListener( 'click', onOverviewSlideClicked, true );
  1417. } );
  1418. // Clean up changes made to backgrounds
  1419. toArray( dom.background.querySelectorAll( '.slide-background' ) ).forEach( function( background ) {
  1420. transformElement( background, '' );
  1421. } );
  1422. transformSlides( { overview: '' } );
  1423. slide( indexh, indexv );
  1424. layout();
  1425. cueAutoSlide();
  1426. // Notify observers of the overview hiding
  1427. dispatchEvent( 'overviewhidden', {
  1428. 'indexh': indexh,
  1429. 'indexv': indexv,
  1430. 'currentSlide': currentSlide
  1431. } );
  1432. }
  1433. }
  1434. /**
  1435. * Toggles the slide overview mode on and off.
  1436. *
  1437. * @param {Boolean} override Optional flag which overrides the
  1438. * toggle logic and forcibly sets the desired state. True means
  1439. * overview is open, false means it's closed.
  1440. */
  1441. function toggleOverview( override ) {
  1442. if( typeof override === 'boolean' ) {
  1443. override ? activateOverview() : deactivateOverview();
  1444. }
  1445. else {
  1446. isOverview() ? deactivateOverview() : activateOverview();
  1447. }
  1448. }
  1449. /**
  1450. * Checks if the overview is currently active.
  1451. *
  1452. * @return {Boolean} true if the overview is active,
  1453. * false otherwise
  1454. */
  1455. function isOverview() {
  1456. return overview;
  1457. }
  1458. /**
  1459. * Checks if the current or specified slide is vertical
  1460. * (nested within another slide).
  1461. *
  1462. * @param {HTMLElement} slide [optional] The slide to check
  1463. * orientation of
  1464. */
  1465. function isVerticalSlide( slide ) {
  1466. // Prefer slide argument, otherwise use current slide
  1467. slide = slide ? slide : currentSlide;
  1468. return slide && slide.parentNode && !!slide.parentNode.nodeName.match( /section/i );
  1469. }
  1470. /**
  1471. * Handling the fullscreen functionality via the fullscreen API
  1472. *
  1473. * @see http://fullscreen.spec.whatwg.org/
  1474. * @see https://developer.mozilla.org/en-US/docs/DOM/Using_fullscreen_mode
  1475. */
  1476. function enterFullscreen() {
  1477. var element = document.body;
  1478. // Check which implementation is available
  1479. var requestMethod = element.requestFullScreen ||
  1480. element.webkitRequestFullscreen ||
  1481. element.webkitRequestFullScreen ||
  1482. element.mozRequestFullScreen ||
  1483. element.msRequestFullscreen;
  1484. if( requestMethod ) {
  1485. requestMethod.apply( element );
  1486. }
  1487. }
  1488. /**
  1489. * Enters the paused mode which fades everything on screen to
  1490. * black.
  1491. */
  1492. function pause() {
  1493. if( config.pause ) {
  1494. var wasPaused = dom.wrapper.classList.contains( 'paused' );
  1495. cancelAutoSlide();
  1496. dom.wrapper.classList.add( 'paused' );
  1497. if( wasPaused === false ) {
  1498. dispatchEvent( 'paused' );
  1499. }
  1500. }
  1501. }
  1502. /**
  1503. * Exits from the paused mode.
  1504. */
  1505. function resume() {
  1506. var wasPaused = dom.wrapper.classList.contains( 'paused' );
  1507. dom.wrapper.classList.remove( 'paused' );
  1508. cueAutoSlide();
  1509. if( wasPaused ) {
  1510. dispatchEvent( 'resumed' );
  1511. }
  1512. }
  1513. /**
  1514. * Toggles the paused mode on and off.
  1515. */
  1516. function togglePause( override ) {
  1517. if( typeof override === 'boolean' ) {
  1518. override ? pause() : resume();
  1519. }
  1520. else {
  1521. isPaused() ? resume() : pause();
  1522. }
  1523. }
  1524. /**
  1525. * Checks if we are currently in the paused mode.
  1526. */
  1527. function isPaused() {
  1528. return dom.wrapper.classList.contains( 'paused' );
  1529. }
  1530. /**
  1531. * Toggles the auto slide mode on and off.
  1532. *
  1533. * @param {Boolean} override Optional flag which sets the desired state.
  1534. * True means autoplay starts, false means it stops.
  1535. */
  1536. function toggleAutoSlide( override ) {
  1537. if( typeof override === 'boolean' ) {
  1538. override ? resumeAutoSlide() : pauseAutoSlide();
  1539. }
  1540. else {
  1541. autoSlidePaused ? resumeAutoSlide() : pauseAutoSlide();
  1542. }
  1543. }
  1544. /**
  1545. * Checks if the auto slide mode is currently on.
  1546. */
  1547. function isAutoSliding() {
  1548. return !!( autoSlide && !autoSlidePaused );
  1549. }
  1550. /**
  1551. * Steps from the current point in the presentation to the
  1552. * slide which matches the specified horizontal and vertical
  1553. * indices.
  1554. *
  1555. * @param {int} h Horizontal index of the target slide
  1556. * @param {int} v Vertical index of the target slide
  1557. * @param {int} f Optional index of a fragment within the
  1558. * target slide to activate
  1559. * @param {int} o Optional origin for use in multimaster environments
  1560. */
  1561. function slide( h, v, f, o ) {
  1562. // Remember where we were at before
  1563. previousSlide = currentSlide;
  1564. // Query all horizontal slides in the deck
  1565. var horizontalSlides = dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  1566. // If no vertical index is specified and the upcoming slide is a
  1567. // stack, resume at its previous vertical index
  1568. if( v === undefined && !isOverview() ) {
  1569. v = getPreviousVerticalIndex( horizontalSlides[ h ] );
  1570. }
  1571. // If we were on a vertical stack, remember what vertical index
  1572. // it was on so we can resume at the same position when returning
  1573. if( previousSlide && previousSlide.parentNode && previousSlide.parentNode.classList.contains( 'stack' ) ) {
  1574. setPreviousVerticalIndex( previousSlide.parentNode, indexv );
  1575. }
  1576. // Remember the state before this slide
  1577. var stateBefore = state.concat();
  1578. // Reset the state array
  1579. state.length = 0;
  1580. var indexhBefore = indexh || 0,
  1581. indexvBefore = indexv || 0;
  1582. // Activate and transition to the new slide
  1583. indexh = updateSlides( HORIZONTAL_SLIDES_SELECTOR, h === undefined ? indexh : h );
  1584. indexv = updateSlides( VERTICAL_SLIDES_SELECTOR, v === undefined ? indexv : v );
  1585. // Update the visibility of slides now that the indices have changed
  1586. updateSlidesVisibility();
  1587. layout();
  1588. // Apply the new state
  1589. stateLoop: for( var i = 0, len = state.length; i < len; i++ ) {
  1590. // Check if this state existed on the previous slide. If it
  1591. // did, we will avoid adding it repeatedly
  1592. for( var j = 0; j < stateBefore.length; j++ ) {
  1593. if( stateBefore[j] === state[i] ) {
  1594. stateBefore.splice( j, 1 );
  1595. continue stateLoop;
  1596. }
  1597. }
  1598. document.documentElement.classList.add( state[i] );
  1599. // Dispatch custom event matching the state's name
  1600. dispatchEvent( state[i] );
  1601. }
  1602. // Clean up the remains of the previous state
  1603. while( stateBefore.length ) {
  1604. document.documentElement.classList.remove( stateBefore.pop() );
  1605. }
  1606. // Update the overview if it's currently active
  1607. if( isOverview() ) {
  1608. updateOverview();
  1609. }
  1610. // Find the current horizontal slide and any possible vertical slides
  1611. // within it
  1612. var currentHorizontalSlide = horizontalSlides[ indexh ],
  1613. currentVerticalSlides = currentHorizontalSlide.querySelectorAll( 'section' );
  1614. // Store references to the previous and current slides
  1615. currentSlide = currentVerticalSlides[ indexv ] || currentHorizontalSlide;
  1616. // Show fragment, if specified
  1617. if( typeof f !== 'undefined' ) {
  1618. navigateFragment( f );
  1619. }
  1620. // Dispatch an event if the slide changed
  1621. var slideChanged = ( indexh !== indexhBefore || indexv !== indexvBefore );
  1622. if( slideChanged ) {
  1623. dispatchEvent( 'slidechanged', {
  1624. 'indexh': indexh,
  1625. 'indexv': indexv,
  1626. 'previousSlide': previousSlide,
  1627. 'currentSlide': currentSlide,
  1628. 'origin': o
  1629. } );
  1630. }
  1631. else {
  1632. // Ensure that the previous slide is never the same as the current
  1633. previousSlide = null;
  1634. }
  1635. // Solves an edge case where the previous slide maintains the
  1636. // 'present' class when navigating between adjacent vertical
  1637. // stacks
  1638. if( previousSlide ) {
  1639. previousSlide.classList.remove( 'present' );
  1640. previousSlide.setAttribute( 'aria-hidden', 'true' );
  1641. // Reset all slides upon navigate to home
  1642. // Issue: #285
  1643. if ( dom.wrapper.querySelector( HOME_SLIDE_SELECTOR ).classList.contains( 'present' ) ) {
  1644. // Launch async task
  1645. setTimeout( function () {
  1646. var slides = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.stack') ), i;
  1647. for( i in slides ) {
  1648. if( slides[i] ) {
  1649. // Reset stack
  1650. setPreviousVerticalIndex( slides[i], 0 );
  1651. }
  1652. }
  1653. }, 0 );
  1654. }
  1655. }
  1656. // Handle embedded content
  1657. if( slideChanged || !previousSlide ) {
  1658. stopEmbeddedContent( previousSlide );
  1659. startEmbeddedContent( currentSlide );
  1660. }
  1661. // Announce the current slide contents, for screen readers
  1662. dom.statusDiv.textContent = currentSlide.textContent;
  1663. updateControls();
  1664. updateProgress();
  1665. updateBackground();
  1666. updateParallax();
  1667. updateSlideNumber();
  1668. // Update the URL hash
  1669. writeURL();
  1670. cueAutoSlide();
  1671. }
  1672. /**
  1673. * Syncs the presentation with the current DOM. Useful
  1674. * when new slides or control elements are added or when
  1675. * the configuration has changed.
  1676. */
  1677. function sync() {
  1678. // Subscribe to input
  1679. removeEventListeners();
  1680. addEventListeners();
  1681. // Force a layout to make sure the current config is accounted for
  1682. layout();
  1683. // Reflect the current autoSlide value
  1684. autoSlide = config.autoSlide;
  1685. // Start auto-sliding if it's enabled
  1686. cueAutoSlide();
  1687. // Re-create the slide backgrounds
  1688. createBackgrounds();
  1689. // Write the current hash to the URL
  1690. writeURL();
  1691. sortAllFragments();
  1692. updateControls();
  1693. updateProgress();
  1694. updateBackground( true );
  1695. updateSlideNumber();
  1696. updateSlidesVisibility();
  1697. formatEmbeddedContent();
  1698. if( isOverview() ) {
  1699. layoutOverview();
  1700. }
  1701. }
  1702. /**
  1703. * Resets all vertical slides so that only the first
  1704. * is visible.
  1705. */
  1706. function resetVerticalSlides() {
  1707. var horizontalSlides = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  1708. horizontalSlides.forEach( function( horizontalSlide ) {
  1709. var verticalSlides = toArray( horizontalSlide.querySelectorAll( 'section' ) );
  1710. verticalSlides.forEach( function( verticalSlide, y ) {
  1711. if( y > 0 ) {
  1712. verticalSlide.classList.remove( 'present' );
  1713. verticalSlide.classList.remove( 'past' );
  1714. verticalSlide.classList.add( 'future' );
  1715. verticalSlide.setAttribute( 'aria-hidden', 'true' );
  1716. }
  1717. } );
  1718. } );
  1719. }
  1720. /**
  1721. * Sorts and formats all of fragments in the
  1722. * presentation.
  1723. */
  1724. function sortAllFragments() {
  1725. var horizontalSlides = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  1726. horizontalSlides.forEach( function( horizontalSlide ) {
  1727. var verticalSlides = toArray( horizontalSlide.querySelectorAll( 'section' ) );
  1728. verticalSlides.forEach( function( verticalSlide, y ) {
  1729. sortFragments( verticalSlide.querySelectorAll( '.fragment' ) );
  1730. } );
  1731. if( verticalSlides.length === 0 ) sortFragments( horizontalSlide.querySelectorAll( '.fragment' ) );
  1732. } );
  1733. }
  1734. /**
  1735. * Updates one dimension of slides by showing the slide
  1736. * with the specified index.
  1737. *
  1738. * @param {String} selector A CSS selector that will fetch
  1739. * the group of slides we are working with
  1740. * @param {Number} index The index of the slide that should be
  1741. * shown
  1742. *
  1743. * @return {Number} The index of the slide that is now shown,
  1744. * might differ from the passed in index if it was out of
  1745. * bounds.
  1746. */
  1747. function updateSlides( selector, index ) {
  1748. // Select all slides and convert the NodeList result to
  1749. // an array
  1750. var slides = toArray( dom.wrapper.querySelectorAll( selector ) ),
  1751. slidesLength = slides.length;
  1752. var printMode = isPrintingPDF();
  1753. if( slidesLength ) {
  1754. // Should the index loop?
  1755. if( config.loop ) {
  1756. index %= slidesLength;
  1757. if( index < 0 ) {
  1758. index = slidesLength + index;
  1759. }
  1760. }
  1761. // Enforce max and minimum index bounds
  1762. index = Math.max( Math.min( index, slidesLength - 1 ), 0 );
  1763. for( var i = 0; i < slidesLength; i++ ) {
  1764. var element = slides[i];
  1765. var reverse = config.rtl && !isVerticalSlide( element );
  1766. element.classList.remove( 'past' );
  1767. element.classList.remove( 'present' );
  1768. element.classList.remove( 'future' );
  1769. // http://www.w3.org/html/wg/drafts/html/master/editing.html#the-hidden-attribute
  1770. element.setAttribute( 'hidden', '' );
  1771. element.setAttribute( 'aria-hidden', 'true' );
  1772. // If this element contains vertical slides
  1773. if( element.querySelector( 'section' ) ) {
  1774. element.classList.add( 'stack' );
  1775. }
  1776. // If we're printing static slides, all slides are "present"
  1777. if( printMode ) {
  1778. element.classList.add( 'present' );
  1779. continue;
  1780. }
  1781. if( i < index ) {
  1782. // Any element previous to index is given the 'past' class
  1783. element.classList.add( reverse ? 'future' : 'past' );
  1784. if( config.fragments ) {
  1785. var pastFragments = toArray( element.querySelectorAll( '.fragment' ) );
  1786. // Show all fragments on prior slides
  1787. while( pastFragments.length ) {
  1788. var pastFragment = pastFragments.pop();
  1789. pastFragment.classList.add( 'visible' );
  1790. pastFragment.classList.remove( 'current-fragment' );
  1791. }
  1792. }
  1793. }
  1794. else if( i > index ) {
  1795. // Any element subsequent to index is given the 'future' class
  1796. element.classList.add( reverse ? 'past' : 'future' );
  1797. if( config.fragments ) {
  1798. var futureFragments = toArray( element.querySelectorAll( '.fragment.visible' ) );
  1799. // No fragments in future slides should be visible ahead of time
  1800. while( futureFragments.length ) {
  1801. var futureFragment = futureFragments.pop();
  1802. futureFragment.classList.remove( 'visible' );
  1803. futureFragment.classList.remove( 'current-fragment' );
  1804. }
  1805. }
  1806. }
  1807. }
  1808. // Mark the current slide as present
  1809. slides[index].classList.add( 'present' );
  1810. slides[index].removeAttribute( 'hidden' );
  1811. slides[index].removeAttribute( 'aria-hidden' );
  1812. // If this slide has a state associated with it, add it
  1813. // onto the current state of the deck
  1814. var slideState = slides[index].getAttribute( 'data-state' );
  1815. if( slideState ) {
  1816. state = state.concat( slideState.split( ' ' ) );
  1817. }
  1818. }
  1819. else {
  1820. // Since there are no slides we can't be anywhere beyond the
  1821. // zeroth index
  1822. index = 0;
  1823. }
  1824. return index;
  1825. }
  1826. /**
  1827. * Optimization method; hide all slides that are far away
  1828. * from the present slide.
  1829. */
  1830. function updateSlidesVisibility() {
  1831. // Select all slides and convert the NodeList result to
  1832. // an array
  1833. var horizontalSlides = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ),
  1834. horizontalSlidesLength = horizontalSlides.length,
  1835. distanceX,
  1836. distanceY;
  1837. if( horizontalSlidesLength && typeof indexh !== 'undefined' ) {
  1838. // The number of steps away from the present slide that will
  1839. // be visible
  1840. var viewDistance = isOverview() ? 10 : config.viewDistance;
  1841. // Limit view distance on weaker devices
  1842. if( isMobileDevice ) {
  1843. viewDistance = isOverview() ? 6 : 2;
  1844. }
  1845. // All slides need to be visible when exporting to PDF
  1846. if( isPrintingPDF() ) {
  1847. viewDistance = Number.MAX_VALUE;
  1848. }
  1849. for( var x = 0; x < horizontalSlidesLength; x++ ) {
  1850. var horizontalSlide = horizontalSlides[x];
  1851. var verticalSlides = toArray( horizontalSlide.querySelectorAll( 'section' ) ),
  1852. verticalSlidesLength = verticalSlides.length;
  1853. // Determine how far away this slide is from the present
  1854. distanceX = Math.abs( ( indexh || 0 ) - x ) || 0;
  1855. // If the presentation is looped, distance should measure
  1856. // 1 between the first and last slides
  1857. if( config.loop ) {
  1858. distanceX = Math.abs( ( ( indexh || 0 ) - x ) % ( horizontalSlidesLength - viewDistance ) ) || 0;
  1859. }
  1860. // Show the horizontal slide if it's within the view distance
  1861. if( distanceX < viewDistance ) {
  1862. showSlide( horizontalSlide );
  1863. }
  1864. else {
  1865. hideSlide( horizontalSlide );
  1866. }
  1867. if( verticalSlidesLength ) {
  1868. var oy = getPreviousVerticalIndex( horizontalSlide );
  1869. for( var y = 0; y < verticalSlidesLength; y++ ) {
  1870. var verticalSlide = verticalSlides[y];
  1871. distanceY = x === ( indexh || 0 ) ? Math.abs( ( indexv || 0 ) - y ) : Math.abs( y - oy );
  1872. if( distanceX + distanceY < viewDistance ) {
  1873. showSlide( verticalSlide );
  1874. }
  1875. else {
  1876. hideSlide( verticalSlide );
  1877. }
  1878. }
  1879. }
  1880. }
  1881. }
  1882. }
  1883. /**
  1884. * Updates the progress bar to reflect the current slide.
  1885. */
  1886. function updateProgress() {
  1887. // Update progress if enabled
  1888. if( config.progress && dom.progressbar ) {
  1889. dom.progressbar.style.width = getProgress() * dom.wrapper.offsetWidth + 'px';
  1890. }
  1891. }
  1892. /**
  1893. * Updates the slide number div to reflect the current slide.
  1894. *
  1895. * Slide number format can be defined as a string using the
  1896. * following variables:
  1897. * h: current slide's horizontal index
  1898. * v: current slide's vertical index
  1899. * c: current slide index (flattened)
  1900. * t: total number of slides (flattened)
  1901. */
  1902. function updateSlideNumber() {
  1903. // Update slide number if enabled
  1904. if( config.slideNumber && dom.slideNumber) {
  1905. // Default to only showing the current slide number
  1906. var format = 'c';
  1907. // Check if a custom slide number format is available
  1908. if( typeof config.slideNumber === 'string' ) {
  1909. format = config.slideNumber;
  1910. }
  1911. dom.slideNumber.innerHTML = format.replace( /h/g, indexh )
  1912. .replace( /v/g, indexv )
  1913. .replace( /c/g, getSlidePastCount() + 1 )
  1914. .replace( /t/g, getTotalSlides() );
  1915. }
  1916. }
  1917. /**
  1918. * Updates the state of all control/navigation arrows.
  1919. */
  1920. function updateControls() {
  1921. var routes = availableRoutes();
  1922. var fragments = availableFragments();
  1923. // Remove the 'enabled' class from all directions
  1924. dom.controlsLeft.concat( dom.controlsRight )
  1925. .concat( dom.controlsUp )
  1926. .concat( dom.controlsDown )
  1927. .concat( dom.controlsPrev )
  1928. .concat( dom.controlsNext ).forEach( function( node ) {
  1929. node.classList.remove( 'enabled' );
  1930. node.classList.remove( 'fragmented' );
  1931. } );
  1932. // Add the 'enabled' class to the available routes
  1933. if( routes.left ) dom.controlsLeft.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1934. if( routes.right ) dom.controlsRight.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1935. if( routes.up ) dom.controlsUp.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1936. if( routes.down ) dom.controlsDown.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1937. // Prev/next buttons
  1938. if( routes.left || routes.up ) dom.controlsPrev.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1939. if( routes.right || routes.down ) dom.controlsNext.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1940. // Highlight fragment directions
  1941. if( currentSlide ) {
  1942. // Always apply fragment decorator to prev/next buttons
  1943. if( fragments.prev ) dom.controlsPrev.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1944. if( fragments.next ) dom.controlsNext.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1945. // Apply fragment decorators to directional buttons based on
  1946. // what slide axis they are in
  1947. if( isVerticalSlide( currentSlide ) ) {
  1948. if( fragments.prev ) dom.controlsUp.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1949. if( fragments.next ) dom.controlsDown.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1950. }
  1951. else {
  1952. if( fragments.prev ) dom.controlsLeft.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1953. if( fragments.next ) dom.controlsRight.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1954. }
  1955. }
  1956. }
  1957. /**
  1958. * Updates the background elements to reflect the current
  1959. * slide.
  1960. *
  1961. * @param {Boolean} includeAll If true, the backgrounds of
  1962. * all vertical slides (not just the present) will be updated.
  1963. */
  1964. function updateBackground( includeAll ) {
  1965. var currentBackground = null;
  1966. // Reverse past/future classes when in RTL mode
  1967. var horizontalPast = config.rtl ? 'future' : 'past',
  1968. horizontalFuture = config.rtl ? 'past' : 'future';
  1969. // Update the classes of all backgrounds to match the
  1970. // states of their slides (past/present/future)
  1971. toArray( dom.background.childNodes ).forEach( function( backgroundh, h ) {
  1972. backgroundh.classList.remove( 'past' );
  1973. backgroundh.classList.remove( 'present' );
  1974. backgroundh.classList.remove( 'future' );
  1975. if( h < indexh ) {
  1976. backgroundh.classList.add( horizontalPast );
  1977. }
  1978. else if ( h > indexh ) {
  1979. backgroundh.classList.add( horizontalFuture );
  1980. }
  1981. else {
  1982. backgroundh.classList.add( 'present' );
  1983. // Store a reference to the current background element
  1984. currentBackground = backgroundh;
  1985. }
  1986. if( includeAll || h === indexh ) {
  1987. toArray( backgroundh.querySelectorAll( '.slide-background' ) ).forEach( function( backgroundv, v ) {
  1988. backgroundv.classList.remove( 'past' );
  1989. backgroundv.classList.remove( 'present' );
  1990. backgroundv.classList.remove( 'future' );
  1991. if( v < indexv ) {
  1992. backgroundv.classList.add( 'past' );
  1993. }
  1994. else if ( v > indexv ) {
  1995. backgroundv.classList.add( 'future' );
  1996. }
  1997. else {
  1998. backgroundv.classList.add( 'present' );
  1999. // Only if this is the present horizontal and vertical slide
  2000. if( h === indexh ) currentBackground = backgroundv;
  2001. }
  2002. } );
  2003. }
  2004. } );
  2005. // Stop any currently playing video background
  2006. if( previousBackground ) {
  2007. var previousVideo = previousBackground.querySelector( 'video' );
  2008. if( previousVideo ) previousVideo.pause();
  2009. }
  2010. if( currentBackground ) {
  2011. // Start video playback
  2012. var currentVideo = currentBackground.querySelector( 'video' );
  2013. if( currentVideo ) {
  2014. currentVideo.currentTime = 0;
  2015. currentVideo.play();
  2016. }
  2017. var backgroundImageURL = currentBackground.style.backgroundImage || '';
  2018. // Restart GIFs (doesn't work in Firefox)
  2019. if( /\.gif/i.test( backgroundImageURL ) ) {
  2020. currentBackground.style.backgroundImage = '';
  2021. window.getComputedStyle( currentBackground ).opacity;
  2022. currentBackground.style.backgroundImage = backgroundImageURL;
  2023. }
  2024. // Don't transition between identical backgrounds. This
  2025. // prevents unwanted flicker.
  2026. var previousBackgroundHash = previousBackground ? previousBackground.getAttribute( 'data-background-hash' ) : null;
  2027. var currentBackgroundHash = currentBackground.getAttribute( 'data-background-hash' );
  2028. if( currentBackgroundHash && currentBackgroundHash === previousBackgroundHash && currentBackground !== previousBackground ) {
  2029. dom.background.classList.add( 'no-transition' );
  2030. }
  2031. previousBackground = currentBackground;
  2032. }
  2033. // If there's a background brightness flag for this slide,
  2034. // bubble it to the .reveal container
  2035. if( currentSlide ) {
  2036. [ 'has-light-background', 'has-dark-background' ].forEach( function( classToBubble ) {
  2037. if( currentSlide.classList.contains( classToBubble ) ) {
  2038. dom.wrapper.classList.add( classToBubble );
  2039. }
  2040. else {
  2041. dom.wrapper.classList.remove( classToBubble );
  2042. }
  2043. } );
  2044. }
  2045. // Allow the first background to apply without transition
  2046. setTimeout( function() {
  2047. dom.background.classList.remove( 'no-transition' );
  2048. }, 1 );
  2049. }
  2050. /**
  2051. * Updates the position of the parallax background based
  2052. * on the current slide index.
  2053. */
  2054. function updateParallax() {
  2055. if( config.parallaxBackgroundImage ) {
  2056. var horizontalSlides = dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ),
  2057. verticalSlides = dom.wrapper.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
  2058. var backgroundSize = dom.background.style.backgroundSize.split( ' ' ),
  2059. backgroundWidth, backgroundHeight;
  2060. if( backgroundSize.length === 1 ) {
  2061. backgroundWidth = backgroundHeight = parseInt( backgroundSize[0], 10 );
  2062. }
  2063. else {
  2064. backgroundWidth = parseInt( backgroundSize[0], 10 );
  2065. backgroundHeight = parseInt( backgroundSize[1], 10 );
  2066. }
  2067. var slideWidth = dom.background.offsetWidth,
  2068. horizontalSlideCount = horizontalSlides.length,
  2069. horizontalOffsetMultiplier,
  2070. horizontalOffset;
  2071. if( typeof config.parallaxBackgroundHorizontal === 'number' ) {
  2072. horizontalOffsetMultiplier = config.parallaxBackgroundHorizontal;
  2073. }
  2074. else {
  2075. horizontalOffsetMultiplier = ( backgroundWidth - slideWidth ) / ( horizontalSlideCount-1 );
  2076. }
  2077. horizontalOffset = horizontalOffsetMultiplier * indexh * -1;
  2078. var slideHeight = dom.background.offsetHeight,
  2079. verticalSlideCount = verticalSlides.length,
  2080. verticalOffsetMultiplier,
  2081. verticalOffset;
  2082. if( typeof config.parallaxBackgroundVertical === 'number' ) {
  2083. verticalOffsetMultiplier = config.parallaxBackgroundVertical;
  2084. }
  2085. else {
  2086. verticalOffsetMultiplier = ( backgroundHeight - slideHeight ) / ( verticalSlideCount-1 );
  2087. }
  2088. verticalOffset = verticalSlideCount > 0 ? verticalOffsetMultiplier * indexv * 1 : 0;
  2089. dom.background.style.backgroundPosition = horizontalOffset + 'px ' + -verticalOffset + 'px';
  2090. }
  2091. }
  2092. /**
  2093. * Called when the given slide is within the configured view
  2094. * distance. Shows the slide element and loads any content
  2095. * that is set to load lazily (data-src).
  2096. */
  2097. function showSlide( slide ) {
  2098. // Show the slide element
  2099. slide.style.display = 'block';
  2100. // Media elements with data-src attributes
  2101. toArray( slide.querySelectorAll( 'img[data-src], video[data-src], audio[data-src], iframe[data-src]' ) ).forEach( function( element ) {
  2102. element.setAttribute( 'src', element.getAttribute( 'data-src' ) );
  2103. element.removeAttribute( 'data-src' );
  2104. } );
  2105. // Media elements with <source> children
  2106. toArray( slide.querySelectorAll( 'video, audio' ) ).forEach( function( media ) {
  2107. var sources = 0;
  2108. toArray( media.querySelectorAll( 'source[data-src]' ) ).forEach( function( source ) {
  2109. source.setAttribute( 'src', source.getAttribute( 'data-src' ) );
  2110. source.removeAttribute( 'data-src' );
  2111. sources += 1;
  2112. } );
  2113. // If we rewrote sources for this video/audio element, we need
  2114. // to manually tell it to load from its new origin
  2115. if( sources > 0 ) {
  2116. media.load();
  2117. }
  2118. } );
  2119. // Show the corresponding background element
  2120. var indices = getIndices( slide );
  2121. var background = getSlideBackground( indices.h, indices.v );
  2122. if( background ) {
  2123. background.style.display = 'block';
  2124. // If the background contains media, load it
  2125. if( background.hasAttribute( 'data-loaded' ) === false ) {
  2126. background.setAttribute( 'data-loaded', 'true' );
  2127. var backgroundImage = slide.getAttribute( 'data-background-image' ),
  2128. backgroundVideo = slide.getAttribute( 'data-background-video' ),
  2129. backgroundVideoLoop = slide.hasAttribute( 'data-background-video-loop' ),
  2130. backgroundIframe = slide.getAttribute( 'data-background-iframe' );
  2131. // Images
  2132. if( backgroundImage ) {
  2133. background.style.backgroundImage = 'url('+ backgroundImage +')';
  2134. }
  2135. // Videos
  2136. else if ( backgroundVideo && !isSpeakerNotes() ) {
  2137. var video = document.createElement( 'video' );
  2138. if( backgroundVideoLoop ) {
  2139. video.setAttribute( 'loop', '' );
  2140. }
  2141. // Support comma separated lists of video sources
  2142. backgroundVideo.split( ',' ).forEach( function( source ) {
  2143. video.innerHTML += '<source src="'+ source +'">';
  2144. } );
  2145. background.appendChild( video );
  2146. }
  2147. // Iframes
  2148. else if( backgroundIframe ) {
  2149. var iframe = document.createElement( 'iframe' );
  2150. iframe.setAttribute( 'src', backgroundIframe );
  2151. iframe.style.width = '100%';
  2152. iframe.style.height = '100%';
  2153. iframe.style.maxHeight = '100%';
  2154. iframe.style.maxWidth = '100%';
  2155. background.appendChild( iframe );
  2156. }
  2157. }
  2158. }
  2159. }
  2160. /**
  2161. * Called when the given slide is moved outside of the
  2162. * configured view distance.
  2163. */
  2164. function hideSlide( slide ) {
  2165. // Hide the slide element
  2166. slide.style.display = 'none';
  2167. // Hide the corresponding background element
  2168. var indices = getIndices( slide );
  2169. var background = getSlideBackground( indices.h, indices.v );
  2170. if( background ) {
  2171. background.style.display = 'none';
  2172. }
  2173. }
  2174. /**
  2175. * Determine what available routes there are for navigation.
  2176. *
  2177. * @return {Object} containing four booleans: left/right/up/down
  2178. */
  2179. function availableRoutes() {
  2180. var horizontalSlides = dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ),
  2181. verticalSlides = dom.wrapper.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
  2182. var routes = {
  2183. left: indexh > 0 || config.loop,
  2184. right: indexh < horizontalSlides.length - 1 || config.loop,
  2185. up: indexv > 0,
  2186. down: indexv < verticalSlides.length - 1
  2187. };
  2188. // reverse horizontal controls for rtl
  2189. if( config.rtl ) {
  2190. var left = routes.left;
  2191. routes.left = routes.right;
  2192. routes.right = left;
  2193. }
  2194. return routes;
  2195. }
  2196. /**
  2197. * Returns an object describing the available fragment
  2198. * directions.
  2199. *
  2200. * @return {Object} two boolean properties: prev/next
  2201. */
  2202. function availableFragments() {
  2203. if( currentSlide && config.fragments ) {
  2204. var fragments = currentSlide.querySelectorAll( '.fragment' );
  2205. var hiddenFragments = currentSlide.querySelectorAll( '.fragment:not(.visible)' );
  2206. return {
  2207. prev: fragments.length - hiddenFragments.length > 0,
  2208. next: !!hiddenFragments.length
  2209. };
  2210. }
  2211. else {
  2212. return { prev: false, next: false };
  2213. }
  2214. }
  2215. /**
  2216. * Enforces origin-specific format rules for embedded media.
  2217. */
  2218. function formatEmbeddedContent() {
  2219. // YouTube frames must include "?enablejsapi=1"
  2220. toArray( dom.slides.querySelectorAll( 'iframe[src*="youtube.com/embed/"]' ) ).forEach( function( el ) {
  2221. var src = el.getAttribute( 'src' );
  2222. if( !/enablejsapi\=1/gi.test( src ) ) {
  2223. el.setAttribute( 'src', src + ( !/\?/.test( src ) ? '?' : '&' ) + 'enablejsapi=1' );
  2224. }
  2225. });
  2226. // Vimeo frames must include "?api=1"
  2227. toArray( dom.slides.querySelectorAll( 'iframe[src*="player.vimeo.com/"]' ) ).forEach( function( el ) {
  2228. var src = el.getAttribute( 'src' );
  2229. if( !/api\=1/gi.test( src ) ) {
  2230. el.setAttribute( 'src', src + ( !/\?/.test( src ) ? '?' : '&' ) + 'api=1' );
  2231. }
  2232. });
  2233. }
  2234. /**
  2235. * Start playback of any embedded content inside of
  2236. * the targeted slide.
  2237. */
  2238. function startEmbeddedContent( slide ) {
  2239. if( slide && !isSpeakerNotes() ) {
  2240. // Restart GIFs
  2241. toArray( slide.querySelectorAll( 'img[src$=".gif"]' ) ).forEach( function( el ) {
  2242. // Setting the same unchanged source like this was confirmed
  2243. // to work in Chrome, FF & Safari
  2244. el.setAttribute( 'src', el.getAttribute( 'src' ) );
  2245. } );
  2246. // HTML5 media elements
  2247. toArray( slide.querySelectorAll( 'video, audio' ) ).forEach( function( el ) {
  2248. if( el.hasAttribute( 'data-autoplay' ) ) {
  2249. el.play();
  2250. }
  2251. } );
  2252. // iframe embeds
  2253. toArray( slide.querySelectorAll( 'iframe' ) ).forEach( function( el ) {
  2254. el.contentWindow.postMessage( 'slide:start', '*' );
  2255. });
  2256. // YouTube embeds
  2257. toArray( slide.querySelectorAll( 'iframe[src*="youtube.com/embed/"]' ) ).forEach( function( el ) {
  2258. if( el.hasAttribute( 'data-autoplay' ) ) {
  2259. el.contentWindow.postMessage( '{"event":"command","func":"playVideo","args":""}', '*' );
  2260. }
  2261. });
  2262. // Vimeo embeds
  2263. toArray( slide.querySelectorAll( 'iframe[src*="player.vimeo.com/"]' ) ).forEach( function( el ) {
  2264. if( el.hasAttribute( 'data-autoplay' ) ) {
  2265. el.contentWindow.postMessage( '{"method":"play"}', '*' );
  2266. }
  2267. });
  2268. }
  2269. }
  2270. /**
  2271. * Stop playback of any embedded content inside of
  2272. * the targeted slide.
  2273. */
  2274. function stopEmbeddedContent( slide ) {
  2275. if( slide && slide.parentNode ) {
  2276. // HTML5 media elements
  2277. toArray( slide.querySelectorAll( 'video, audio' ) ).forEach( function( el ) {
  2278. if( !el.hasAttribute( 'data-ignore' ) ) {
  2279. el.pause();
  2280. }
  2281. } );
  2282. // iframe embeds
  2283. toArray( slide.querySelectorAll( 'iframe' ) ).forEach( function( el ) {
  2284. el.contentWindow.postMessage( 'slide:stop', '*' );
  2285. });
  2286. // YouTube embeds
  2287. toArray( slide.querySelectorAll( 'iframe[src*="youtube.com/embed/"]' ) ).forEach( function( el ) {
  2288. if( !el.hasAttribute( 'data-ignore' ) && typeof el.contentWindow.postMessage === 'function' ) {
  2289. el.contentWindow.postMessage( '{"event":"command","func":"pauseVideo","args":""}', '*' );
  2290. }
  2291. });
  2292. // Vimeo embeds
  2293. toArray( slide.querySelectorAll( 'iframe[src*="player.vimeo.com/"]' ) ).forEach( function( el ) {
  2294. if( !el.hasAttribute( 'data-ignore' ) && typeof el.contentWindow.postMessage === 'function' ) {
  2295. el.contentWindow.postMessage( '{"method":"pause"}', '*' );
  2296. }
  2297. });
  2298. }
  2299. }
  2300. /**
  2301. * Returns the number of past slides. This can be used as a global
  2302. * flattened index for slides.
  2303. */
  2304. function getSlidePastCount() {
  2305. var horizontalSlides = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  2306. // The number of past slides
  2307. var pastCount = 0;
  2308. // Step through all slides and count the past ones
  2309. mainLoop: for( var i = 0; i < horizontalSlides.length; i++ ) {
  2310. var horizontalSlide = horizontalSlides[i];
  2311. var verticalSlides = toArray( horizontalSlide.querySelectorAll( 'section' ) );
  2312. for( var j = 0; j < verticalSlides.length; j++ ) {
  2313. // Stop as soon as we arrive at the present
  2314. if( verticalSlides[j].classList.contains( 'present' ) ) {
  2315. break mainLoop;
  2316. }
  2317. pastCount++;
  2318. }
  2319. // Stop as soon as we arrive at the present
  2320. if( horizontalSlide.classList.contains( 'present' ) ) {
  2321. break;
  2322. }
  2323. // Don't count the wrapping section for vertical slides
  2324. if( horizontalSlide.classList.contains( 'stack' ) === false ) {
  2325. pastCount++;
  2326. }
  2327. }
  2328. return pastCount;
  2329. }
  2330. /**
  2331. * Returns a value ranging from 0-1 that represents
  2332. * how far into the presentation we have navigated.
  2333. */
  2334. function getProgress() {
  2335. // The number of past and total slides
  2336. var totalCount = getTotalSlides();
  2337. var pastCount = getSlidePastCount();
  2338. if( currentSlide ) {
  2339. var allFragments = currentSlide.querySelectorAll( '.fragment' );
  2340. // If there are fragments in the current slide those should be
  2341. // accounted for in the progress.
  2342. if( allFragments.length > 0 ) {
  2343. var visibleFragments = currentSlide.querySelectorAll( '.fragment.visible' );
  2344. // This value represents how big a portion of the slide progress
  2345. // that is made up by its fragments (0-1)
  2346. var fragmentWeight = 0.9;
  2347. // Add fragment progress to the past slide count
  2348. pastCount += ( visibleFragments.length / allFragments.length ) * fragmentWeight;
  2349. }
  2350. }
  2351. return pastCount / ( totalCount - 1 );
  2352. }
  2353. /**
  2354. * Checks if this presentation is running inside of the
  2355. * speaker notes window.
  2356. */
  2357. function isSpeakerNotes() {
  2358. return !!window.location.search.match( /receiver/gi );
  2359. }
  2360. /**
  2361. * Reads the current URL (hash) and navigates accordingly.
  2362. */
  2363. function readURL() {
  2364. var hash = window.location.hash;
  2365. // Attempt to parse the hash as either an index or name
  2366. var bits = hash.slice( 2 ).split( '/' ),
  2367. name = hash.replace( /#|\//gi, '' );
  2368. // If the first bit is invalid and there is a name we can
  2369. // assume that this is a named link
  2370. if( isNaN( parseInt( bits[0], 10 ) ) && name.length ) {
  2371. var element;
  2372. // Ensure the named link is a valid HTML ID attribute
  2373. if( /^[a-zA-Z][\w:.-]*$/.test( name ) ) {
  2374. // Find the slide with the specified ID
  2375. element = document.getElementById( name );
  2376. }
  2377. if( element ) {
  2378. // Find the position of the named slide and navigate to it
  2379. var indices = Reveal.getIndices( element );
  2380. slide( indices.h, indices.v );
  2381. }
  2382. // If the slide doesn't exist, navigate to the current slide
  2383. else {
  2384. slide( indexh || 0, indexv || 0 );
  2385. }
  2386. }
  2387. else {
  2388. // Read the index components of the hash
  2389. var h = parseInt( bits[0], 10 ) || 0,
  2390. v = parseInt( bits[1], 10 ) || 0;
  2391. if( h !== indexh || v !== indexv ) {
  2392. slide( h, v );
  2393. }
  2394. }
  2395. }
  2396. /**
  2397. * Updates the page URL (hash) to reflect the current
  2398. * state.
  2399. *
  2400. * @param {Number} delay The time in ms to wait before
  2401. * writing the hash
  2402. */
  2403. function writeURL( delay ) {
  2404. if( config.history ) {
  2405. // Make sure there's never more than one timeout running
  2406. clearTimeout( writeURLTimeout );
  2407. // If a delay is specified, timeout this call
  2408. if( typeof delay === 'number' ) {
  2409. writeURLTimeout = setTimeout( writeURL, delay );
  2410. }
  2411. else if( currentSlide ) {
  2412. var url = '/';
  2413. // Attempt to create a named link based on the slide's ID
  2414. var id = currentSlide.getAttribute( 'id' );
  2415. if( id ) {
  2416. id = id.toLowerCase();
  2417. id = id.replace( /[^a-zA-Z0-9\-\_\:\.]/g, '' );
  2418. }
  2419. // If the current slide has an ID, use that as a named link
  2420. if( typeof id === 'string' && id.length ) {
  2421. url = '/' + id;
  2422. }
  2423. // Otherwise use the /h/v index
  2424. else {
  2425. if( indexh > 0 || indexv > 0 ) url += indexh;
  2426. if( indexv > 0 ) url += '/' + indexv;
  2427. }
  2428. window.location.hash = url;
  2429. }
  2430. }
  2431. }
  2432. /**
  2433. * Retrieves the h/v location of the current, or specified,
  2434. * slide.
  2435. *
  2436. * @param {HTMLElement} slide If specified, the returned
  2437. * index will be for this slide rather than the currently
  2438. * active one
  2439. *
  2440. * @return {Object} { h: <int>, v: <int>, f: <int> }
  2441. */
  2442. function getIndices( slide ) {
  2443. // By default, return the current indices
  2444. var h = indexh,
  2445. v = indexv,
  2446. f;
  2447. // If a slide is specified, return the indices of that slide
  2448. if( slide ) {
  2449. var isVertical = isVerticalSlide( slide );
  2450. var slideh = isVertical ? slide.parentNode : slide;
  2451. // Select all horizontal slides
  2452. var horizontalSlides = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  2453. // Now that we know which the horizontal slide is, get its index
  2454. h = Math.max( horizontalSlides.indexOf( slideh ), 0 );
  2455. // Assume we're not vertical
  2456. v = undefined;
  2457. // If this is a vertical slide, grab the vertical index
  2458. if( isVertical ) {
  2459. v = Math.max( toArray( slide.parentNode.querySelectorAll( 'section' ) ).indexOf( slide ), 0 );
  2460. }
  2461. }
  2462. if( !slide && currentSlide ) {
  2463. var hasFragments = currentSlide.querySelectorAll( '.fragment' ).length > 0;
  2464. if( hasFragments ) {
  2465. var currentFragment = currentSlide.querySelector( '.current-fragment' );
  2466. if( currentFragment && currentFragment.hasAttribute( 'data-fragment-index' ) ) {
  2467. f = parseInt( currentFragment.getAttribute( 'data-fragment-index' ), 10 );
  2468. }
  2469. else {
  2470. f = currentSlide.querySelectorAll( '.fragment.visible' ).length - 1;
  2471. }
  2472. }
  2473. }
  2474. return { h: h, v: v, f: f };
  2475. }
  2476. /**
  2477. * Retrieves the total number of slides in this presentation.
  2478. */
  2479. function getTotalSlides() {
  2480. return dom.wrapper.querySelectorAll( SLIDES_SELECTOR + ':not(.stack)' ).length;
  2481. }
  2482. /**
  2483. * Returns the slide element matching the specified index.
  2484. */
  2485. function getSlide( x, y ) {
  2486. var horizontalSlide = dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR )[ x ];
  2487. var verticalSlides = horizontalSlide && horizontalSlide.querySelectorAll( 'section' );
  2488. if( verticalSlides && verticalSlides.length && typeof y === 'number' ) {
  2489. return verticalSlides ? verticalSlides[ y ] : undefined;
  2490. }
  2491. return horizontalSlide;
  2492. }
  2493. /**
  2494. * Returns the background element for the given slide.
  2495. * All slides, even the ones with no background properties
  2496. * defined, have a background element so as long as the
  2497. * index is valid an element will be returned.
  2498. */
  2499. function getSlideBackground( x, y ) {
  2500. // When printing to PDF the slide backgrounds are nested
  2501. // inside of the slides
  2502. if( isPrintingPDF() ) {
  2503. var slide = getSlide( x, y );
  2504. if( slide ) {
  2505. var background = slide.querySelector( '.slide-background' );
  2506. if( background && background.parentNode === slide ) {
  2507. return background;
  2508. }
  2509. }
  2510. return undefined;
  2511. }
  2512. var horizontalBackground = dom.wrapper.querySelectorAll( '.backgrounds>.slide-background' )[ x ];
  2513. var verticalBackgrounds = horizontalBackground && horizontalBackground.querySelectorAll( '.slide-background' );
  2514. if( verticalBackgrounds && verticalBackgrounds.length && typeof y === 'number' ) {
  2515. return verticalBackgrounds ? verticalBackgrounds[ y ] : undefined;
  2516. }
  2517. return horizontalBackground;
  2518. }
  2519. /**
  2520. * Retrieves the current state of the presentation as
  2521. * an object. This state can then be restored at any
  2522. * time.
  2523. */
  2524. function getState() {
  2525. var indices = getIndices();
  2526. return {
  2527. indexh: indices.h,
  2528. indexv: indices.v,
  2529. indexf: indices.f,
  2530. paused: isPaused(),
  2531. overview: isOverview()
  2532. };
  2533. }
  2534. /**
  2535. * Restores the presentation to the given state.
  2536. *
  2537. * @param {Object} state As generated by getState()
  2538. */
  2539. function setState( state ) {
  2540. if( typeof state === 'object' ) {
  2541. slide( deserialize( state.indexh ), deserialize( state.indexv ), deserialize( state.indexf ) );
  2542. var pausedFlag = deserialize( state.paused ),
  2543. overviewFlag = deserialize( state.overview );
  2544. if( typeof pausedFlag === 'boolean' && pausedFlag !== isPaused() ) {
  2545. togglePause( pausedFlag );
  2546. }
  2547. if( typeof overviewFlag === 'boolean' && overviewFlag !== isOverview() ) {
  2548. toggleOverview( overviewFlag );
  2549. }
  2550. }
  2551. }
  2552. /**
  2553. * Return a sorted fragments list, ordered by an increasing
  2554. * "data-fragment-index" attribute.
  2555. *
  2556. * Fragments will be revealed in the order that they are returned by
  2557. * this function, so you can use the index attributes to control the
  2558. * order of fragment appearance.
  2559. *
  2560. * To maintain a sensible default fragment order, fragments are presumed
  2561. * to be passed in document order. This function adds a "fragment-index"
  2562. * attribute to each node if such an attribute is not already present,
  2563. * and sets that attribute to an integer value which is the position of
  2564. * the fragment within the fragments list.
  2565. */
  2566. function sortFragments( fragments ) {
  2567. fragments = toArray( fragments );
  2568. var ordered = [],
  2569. unordered = [],
  2570. sorted = [];
  2571. // Group ordered and unordered elements
  2572. fragments.forEach( function( fragment, i ) {
  2573. if( fragment.hasAttribute( 'data-fragment-index' ) ) {
  2574. var index = parseInt( fragment.getAttribute( 'data-fragment-index' ), 10 );
  2575. if( !ordered[index] ) {
  2576. ordered[index] = [];
  2577. }
  2578. ordered[index].push( fragment );
  2579. }
  2580. else {
  2581. unordered.push( [ fragment ] );
  2582. }
  2583. } );
  2584. // Append fragments without explicit indices in their
  2585. // DOM order
  2586. ordered = ordered.concat( unordered );
  2587. // Manually count the index up per group to ensure there
  2588. // are no gaps
  2589. var index = 0;
  2590. // Push all fragments in their sorted order to an array,
  2591. // this flattens the groups
  2592. ordered.forEach( function( group ) {
  2593. group.forEach( function( fragment ) {
  2594. sorted.push( fragment );
  2595. fragment.setAttribute( 'data-fragment-index', index );
  2596. } );
  2597. index ++;
  2598. } );
  2599. return sorted;
  2600. }
  2601. /**
  2602. * Navigate to the specified slide fragment.
  2603. *
  2604. * @param {Number} index The index of the fragment that
  2605. * should be shown, -1 means all are invisible
  2606. * @param {Number} offset Integer offset to apply to the
  2607. * fragment index
  2608. *
  2609. * @return {Boolean} true if a change was made in any
  2610. * fragments visibility as part of this call
  2611. */
  2612. function navigateFragment( index, offset ) {
  2613. if( currentSlide && config.fragments ) {
  2614. var fragments = sortFragments( currentSlide.querySelectorAll( '.fragment' ) );
  2615. if( fragments.length ) {
  2616. // If no index is specified, find the current
  2617. if( typeof index !== 'number' ) {
  2618. var lastVisibleFragment = sortFragments( currentSlide.querySelectorAll( '.fragment.visible' ) ).pop();
  2619. if( lastVisibleFragment ) {
  2620. index = parseInt( lastVisibleFragment.getAttribute( 'data-fragment-index' ) || 0, 10 );
  2621. }
  2622. else {
  2623. index = -1;
  2624. }
  2625. }
  2626. // If an offset is specified, apply it to the index
  2627. if( typeof offset === 'number' ) {
  2628. index += offset;
  2629. }
  2630. var fragmentsShown = [],
  2631. fragmentsHidden = [];
  2632. toArray( fragments ).forEach( function( element, i ) {
  2633. if( element.hasAttribute( 'data-fragment-index' ) ) {
  2634. i = parseInt( element.getAttribute( 'data-fragment-index' ), 10 );
  2635. }
  2636. // Visible fragments
  2637. if( i <= index ) {
  2638. if( !element.classList.contains( 'visible' ) ) fragmentsShown.push( element );
  2639. element.classList.add( 'visible' );
  2640. element.classList.remove( 'current-fragment' );
  2641. // Announce the fragments one by one to the Screen Reader
  2642. dom.statusDiv.textContent = element.textContent;
  2643. if( i === index ) {
  2644. element.classList.add( 'current-fragment' );
  2645. }
  2646. }
  2647. // Hidden fragments
  2648. else {
  2649. if( element.classList.contains( 'visible' ) ) fragmentsHidden.push( element );
  2650. element.classList.remove( 'visible' );
  2651. element.classList.remove( 'current-fragment' );
  2652. }
  2653. } );
  2654. if( fragmentsHidden.length ) {
  2655. dispatchEvent( 'fragmenthidden', { fragment: fragmentsHidden[0], fragments: fragmentsHidden } );
  2656. }
  2657. if( fragmentsShown.length ) {
  2658. dispatchEvent( 'fragmentshown', { fragment: fragmentsShown[0], fragments: fragmentsShown } );
  2659. }
  2660. updateControls();
  2661. updateProgress();
  2662. return !!( fragmentsShown.length || fragmentsHidden.length );
  2663. }
  2664. }
  2665. return false;
  2666. }
  2667. /**
  2668. * Navigate to the next slide fragment.
  2669. *
  2670. * @return {Boolean} true if there was a next fragment,
  2671. * false otherwise
  2672. */
  2673. function nextFragment() {
  2674. return navigateFragment( null, 1 );
  2675. }
  2676. /**
  2677. * Navigate to the previous slide fragment.
  2678. *
  2679. * @return {Boolean} true if there was a previous fragment,
  2680. * false otherwise
  2681. */
  2682. function previousFragment() {
  2683. return navigateFragment( null, -1 );
  2684. }
  2685. /**
  2686. * Cues a new automated slide if enabled in the config.
  2687. */
  2688. function cueAutoSlide() {
  2689. cancelAutoSlide();
  2690. if( currentSlide ) {
  2691. var currentFragment = currentSlide.querySelector( '.current-fragment' );
  2692. var fragmentAutoSlide = currentFragment ? currentFragment.getAttribute( 'data-autoslide' ) : null;
  2693. var parentAutoSlide = currentSlide.parentNode ? currentSlide.parentNode.getAttribute( 'data-autoslide' ) : null;
  2694. var slideAutoSlide = currentSlide.getAttribute( 'data-autoslide' );
  2695. // Pick value in the following priority order:
  2696. // 1. Current fragment's data-autoslide
  2697. // 2. Current slide's data-autoslide
  2698. // 3. Parent slide's data-autoslide
  2699. // 4. Global autoSlide setting
  2700. if( fragmentAutoSlide ) {
  2701. autoSlide = parseInt( fragmentAutoSlide, 10 );
  2702. }
  2703. else if( slideAutoSlide ) {
  2704. autoSlide = parseInt( slideAutoSlide, 10 );
  2705. }
  2706. else if( parentAutoSlide ) {
  2707. autoSlide = parseInt( parentAutoSlide, 10 );
  2708. }
  2709. else {
  2710. autoSlide = config.autoSlide;
  2711. }
  2712. // If there are media elements with data-autoplay,
  2713. // automatically set the autoSlide duration to the
  2714. // length of that media. Not applicable if the slide
  2715. // is divided up into fragments.
  2716. if( currentSlide.querySelectorAll( '.fragment' ).length === 0 ) {
  2717. toArray( currentSlide.querySelectorAll( 'video, audio' ) ).forEach( function( el ) {
  2718. if( el.hasAttribute( 'data-autoplay' ) ) {
  2719. if( autoSlide && el.duration * 1000 > autoSlide ) {
  2720. autoSlide = ( el.duration * 1000 ) + 1000;
  2721. }
  2722. }
  2723. } );
  2724. }
  2725. // Cue the next auto-slide if:
  2726. // - There is an autoSlide value
  2727. // - Auto-sliding isn't paused by the user
  2728. // - The presentation isn't paused
  2729. // - The overview isn't active
  2730. // - The presentation isn't over
  2731. if( autoSlide && !autoSlidePaused && !isPaused() && !isOverview() && ( !Reveal.isLastSlide() || availableFragments().next || config.loop === true ) ) {
  2732. autoSlideTimeout = setTimeout( navigateNext, autoSlide );
  2733. autoSlideStartTime = Date.now();
  2734. }
  2735. if( autoSlidePlayer ) {
  2736. autoSlidePlayer.setPlaying( autoSlideTimeout !== -1 );
  2737. }
  2738. }
  2739. }
  2740. /**
  2741. * Cancels any ongoing request to auto-slide.
  2742. */
  2743. function cancelAutoSlide() {
  2744. clearTimeout( autoSlideTimeout );
  2745. autoSlideTimeout = -1;
  2746. }
  2747. function pauseAutoSlide() {
  2748. if( autoSlide && !autoSlidePaused ) {
  2749. autoSlidePaused = true;
  2750. dispatchEvent( 'autoslidepaused' );
  2751. clearTimeout( autoSlideTimeout );
  2752. if( autoSlidePlayer ) {
  2753. autoSlidePlayer.setPlaying( false );
  2754. }
  2755. }
  2756. }
  2757. function resumeAutoSlide() {
  2758. if( autoSlide && autoSlidePaused ) {
  2759. autoSlidePaused = false;
  2760. dispatchEvent( 'autoslideresumed' );
  2761. cueAutoSlide();
  2762. }
  2763. }
  2764. function navigateLeft() {
  2765. // Reverse for RTL
  2766. if( config.rtl ) {
  2767. if( ( isOverview() || nextFragment() === false ) && availableRoutes().left ) {
  2768. slide( indexh + 1 );
  2769. }
  2770. }
  2771. // Normal navigation
  2772. else if( ( isOverview() || previousFragment() === false ) && availableRoutes().left ) {
  2773. slide( indexh - 1 );
  2774. }
  2775. }
  2776. function navigateRight() {
  2777. // Reverse for RTL
  2778. if( config.rtl ) {
  2779. if( ( isOverview() || previousFragment() === false ) && availableRoutes().right ) {
  2780. slide( indexh - 1 );
  2781. }
  2782. }
  2783. // Normal navigation
  2784. else if( ( isOverview() || nextFragment() === false ) && availableRoutes().right ) {
  2785. slide( indexh + 1 );
  2786. }
  2787. }
  2788. function navigateUp() {
  2789. // Prioritize hiding fragments
  2790. if( ( isOverview() || previousFragment() === false ) && availableRoutes().up ) {
  2791. slide( indexh, indexv - 1 );
  2792. }
  2793. }
  2794. function navigateDown() {
  2795. // Prioritize revealing fragments
  2796. if( ( isOverview() || nextFragment() === false ) && availableRoutes().down ) {
  2797. slide( indexh, indexv + 1 );
  2798. }
  2799. }
  2800. /**
  2801. * Navigates backwards, prioritized in the following order:
  2802. * 1) Previous fragment
  2803. * 2) Previous vertical slide
  2804. * 3) Previous horizontal slide
  2805. */
  2806. function navigatePrev() {
  2807. // Prioritize revealing fragments
  2808. if( previousFragment() === false ) {
  2809. if( availableRoutes().up ) {
  2810. navigateUp();
  2811. }
  2812. else {
  2813. // Fetch the previous horizontal slide, if there is one
  2814. var previousSlide;
  2815. if( config.rtl ) {
  2816. previousSlide = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.future' ) ).pop();
  2817. }
  2818. else {
  2819. previousSlide = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.past' ) ).pop();
  2820. }
  2821. if( previousSlide ) {
  2822. var v = ( previousSlide.querySelectorAll( 'section' ).length - 1 ) || undefined;
  2823. var h = indexh - 1;
  2824. slide( h, v );
  2825. }
  2826. }
  2827. }
  2828. }
  2829. /**
  2830. * The reverse of #navigatePrev().
  2831. */
  2832. function navigateNext() {
  2833. // Prioritize revealing fragments
  2834. if( nextFragment() === false ) {
  2835. if( availableRoutes().down ) {
  2836. navigateDown();
  2837. }
  2838. else if( config.rtl ) {
  2839. navigateLeft();
  2840. }
  2841. else {
  2842. navigateRight();
  2843. }
  2844. }
  2845. // If auto-sliding is enabled we need to cue up
  2846. // another timeout
  2847. cueAutoSlide();
  2848. }
  2849. // --------------------------------------------------------------------//
  2850. // ----------------------------- EVENTS -------------------------------//
  2851. // --------------------------------------------------------------------//
  2852. /**
  2853. * Called by all event handlers that are based on user
  2854. * input.
  2855. */
  2856. function onUserInput( event ) {
  2857. if( config.autoSlideStoppable ) {
  2858. pauseAutoSlide();
  2859. }
  2860. }
  2861. /**
  2862. * Handler for the document level 'keypress' event.
  2863. */
  2864. function onDocumentKeyPress( event ) {
  2865. // Check if the pressed key is question mark
  2866. if( event.shiftKey && event.charCode === 63 ) {
  2867. if( dom.overlay ) {
  2868. closeOverlay();
  2869. }
  2870. else {
  2871. showHelp( true );
  2872. }
  2873. }
  2874. }
  2875. /**
  2876. * Handler for the document level 'keydown' event.
  2877. */
  2878. function onDocumentKeyDown( event ) {
  2879. // If there's a condition specified and it returns false,
  2880. // ignore this event
  2881. if( typeof config.keyboardCondition === 'function' && config.keyboardCondition() === false ) {
  2882. return true;
  2883. }
  2884. // Remember if auto-sliding was paused so we can toggle it
  2885. var autoSlideWasPaused = autoSlidePaused;
  2886. onUserInput( event );
  2887. // Check if there's a focused element that could be using
  2888. // the keyboard
  2889. var activeElementIsCE = document.activeElement && document.activeElement.contentEditable !== 'inherit';
  2890. var activeElementIsInput = document.activeElement && document.activeElement.tagName && /input|textarea/i.test( document.activeElement.tagName );
  2891. // Disregard the event if there's a focused element or a
  2892. // keyboard modifier key is present
  2893. if( activeElementIsCE || activeElementIsInput || (event.shiftKey && event.keyCode !== 32) || event.altKey || event.ctrlKey || event.metaKey ) return;
  2894. // While paused only allow "unpausing" keyboard events (b and .)
  2895. if( isPaused() && [66,190,191].indexOf( event.keyCode ) === -1 ) {
  2896. return false;
  2897. }
  2898. var triggered = false;
  2899. // 1. User defined key bindings
  2900. if( typeof config.keyboard === 'object' ) {
  2901. for( var key in config.keyboard ) {
  2902. // Check if this binding matches the pressed key
  2903. if( parseInt( key, 10 ) === event.keyCode ) {
  2904. var value = config.keyboard[ key ];
  2905. // Callback function
  2906. if( typeof value === 'function' ) {
  2907. value.apply( null, [ event ] );
  2908. }
  2909. // String shortcuts to reveal.js API
  2910. else if( typeof value === 'string' && typeof Reveal[ value ] === 'function' ) {
  2911. Reveal[ value ].call();
  2912. }
  2913. triggered = true;
  2914. }
  2915. }
  2916. }
  2917. // 2. System defined key bindings
  2918. if( triggered === false ) {
  2919. // Assume true and try to prove false
  2920. triggered = true;
  2921. switch( event.keyCode ) {
  2922. // p, page up
  2923. case 80: case 33: navigatePrev(); break;
  2924. // n, page down
  2925. case 78: case 34: navigateNext(); break;
  2926. // h, left
  2927. case 72: case 37: navigateLeft(); break;
  2928. // l, right
  2929. case 76: case 39: navigateRight(); break;
  2930. // k, up
  2931. case 75: case 38: navigateUp(); break;
  2932. // j, down
  2933. case 74: case 40: navigateDown(); break;
  2934. // home
  2935. case 36: slide( 0 ); break;
  2936. // end
  2937. case 35: slide( Number.MAX_VALUE ); break;
  2938. // space
  2939. case 32: isOverview() ? deactivateOverview() : event.shiftKey ? navigatePrev() : navigateNext(); break;
  2940. // return
  2941. case 13: isOverview() ? deactivateOverview() : triggered = false; break;
  2942. // two-spot, semicolon, b, period, Logitech presenter tools "black screen" button
  2943. case 58: case 59: case 66: case 190: case 191: togglePause(); break;
  2944. // f
  2945. case 70: enterFullscreen(); break;
  2946. // a
  2947. case 65: if ( config.autoSlideStoppable ) toggleAutoSlide( autoSlideWasPaused ); break;
  2948. default:
  2949. triggered = false;
  2950. }
  2951. }
  2952. // If the input resulted in a triggered action we should prevent
  2953. // the browsers default behavior
  2954. if( triggered ) {
  2955. event.preventDefault && event.preventDefault();
  2956. }
  2957. // ESC or O key
  2958. else if ( ( event.keyCode === 27 || event.keyCode === 79 ) && features.transforms3d ) {
  2959. if( dom.overlay ) {
  2960. closeOverlay();
  2961. }
  2962. else {
  2963. toggleOverview();
  2964. }
  2965. event.preventDefault && event.preventDefault();
  2966. }
  2967. // If auto-sliding is enabled we need to cue up
  2968. // another timeout
  2969. cueAutoSlide();
  2970. }
  2971. /**
  2972. * Handler for the 'touchstart' event, enables support for
  2973. * swipe and pinch gestures.
  2974. */
  2975. function onTouchStart( event ) {
  2976. touch.startX = event.touches[0].clientX;
  2977. touch.startY = event.touches[0].clientY;
  2978. touch.startCount = event.touches.length;
  2979. // If there's two touches we need to memorize the distance
  2980. // between those two points to detect pinching
  2981. if( event.touches.length === 2 && config.overview ) {
  2982. touch.startSpan = distanceBetween( {
  2983. x: event.touches[1].clientX,
  2984. y: event.touches[1].clientY
  2985. }, {
  2986. x: touch.startX,
  2987. y: touch.startY
  2988. } );
  2989. }
  2990. }
  2991. /**
  2992. * Handler for the 'touchmove' event.
  2993. */
  2994. function onTouchMove( event ) {
  2995. // Each touch should only trigger one action
  2996. if( !touch.captured ) {
  2997. onUserInput( event );
  2998. var currentX = event.touches[0].clientX;
  2999. var currentY = event.touches[0].clientY;
  3000. // If the touch started with two points and still has
  3001. // two active touches; test for the pinch gesture
  3002. if( event.touches.length === 2 && touch.startCount === 2 && config.overview ) {
  3003. // The current distance in pixels between the two touch points
  3004. var currentSpan = distanceBetween( {
  3005. x: event.touches[1].clientX,
  3006. y: event.touches[1].clientY
  3007. }, {
  3008. x: touch.startX,
  3009. y: touch.startY
  3010. } );
  3011. // If the span is larger than the desire amount we've got
  3012. // ourselves a pinch
  3013. if( Math.abs( touch.startSpan - currentSpan ) > touch.threshold ) {
  3014. touch.captured = true;
  3015. if( currentSpan < touch.startSpan ) {
  3016. activateOverview();
  3017. }
  3018. else {
  3019. deactivateOverview();
  3020. }
  3021. }
  3022. event.preventDefault();
  3023. }
  3024. // There was only one touch point, look for a swipe
  3025. else if( event.touches.length === 1 && touch.startCount !== 2 ) {
  3026. var deltaX = currentX - touch.startX,
  3027. deltaY = currentY - touch.startY;
  3028. if( deltaX > touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
  3029. touch.captured = true;
  3030. navigateLeft();
  3031. }
  3032. else if( deltaX < -touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
  3033. touch.captured = true;
  3034. navigateRight();
  3035. }
  3036. else if( deltaY > touch.threshold ) {
  3037. touch.captured = true;
  3038. navigateUp();
  3039. }
  3040. else if( deltaY < -touch.threshold ) {
  3041. touch.captured = true;
  3042. navigateDown();
  3043. }
  3044. // If we're embedded, only block touch events if they have
  3045. // triggered an action
  3046. if( config.embedded ) {
  3047. if( touch.captured || isVerticalSlide( currentSlide ) ) {
  3048. event.preventDefault();
  3049. }
  3050. }
  3051. // Not embedded? Block them all to avoid needless tossing
  3052. // around of the viewport in iOS
  3053. else {
  3054. event.preventDefault();
  3055. }
  3056. }
  3057. }
  3058. // There's a bug with swiping on some Android devices unless
  3059. // the default action is always prevented
  3060. else if( navigator.userAgent.match( /android/gi ) ) {
  3061. event.preventDefault();
  3062. }
  3063. }
  3064. /**
  3065. * Handler for the 'touchend' event.
  3066. */
  3067. function onTouchEnd( event ) {
  3068. touch.captured = false;
  3069. }
  3070. /**
  3071. * Convert pointer down to touch start.
  3072. */
  3073. function onPointerDown( event ) {
  3074. if( event.pointerType === event.MSPOINTER_TYPE_TOUCH || event.pointerType === "touch" ) {
  3075. event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
  3076. onTouchStart( event );
  3077. }
  3078. }
  3079. /**
  3080. * Convert pointer move to touch move.
  3081. */
  3082. function onPointerMove( event ) {
  3083. if( event.pointerType === event.MSPOINTER_TYPE_TOUCH || event.pointerType === "touch" ) {
  3084. event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
  3085. onTouchMove( event );
  3086. }
  3087. }
  3088. /**
  3089. * Convert pointer up to touch end.
  3090. */
  3091. function onPointerUp( event ) {
  3092. if( event.pointerType === event.MSPOINTER_TYPE_TOUCH || event.pointerType === "touch" ) {
  3093. event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
  3094. onTouchEnd( event );
  3095. }
  3096. }
  3097. /**
  3098. * Handles mouse wheel scrolling, throttled to avoid skipping
  3099. * multiple slides.
  3100. */
  3101. function onDocumentMouseScroll( event ) {
  3102. if( Date.now() - lastMouseWheelStep > 600 ) {
  3103. lastMouseWheelStep = Date.now();
  3104. var delta = event.detail || -event.wheelDelta;
  3105. if( delta > 0 ) {
  3106. navigateNext();
  3107. }
  3108. else {
  3109. navigatePrev();
  3110. }
  3111. }
  3112. }
  3113. /**
  3114. * Clicking on the progress bar results in a navigation to the
  3115. * closest approximate horizontal slide using this equation:
  3116. *
  3117. * ( clickX / presentationWidth ) * numberOfSlides
  3118. */
  3119. function onProgressClicked( event ) {
  3120. onUserInput( event );
  3121. event.preventDefault();
  3122. var slidesTotal = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ).length;
  3123. var slideIndex = Math.floor( ( event.clientX / dom.wrapper.offsetWidth ) * slidesTotal );
  3124. if( config.rtl ) {
  3125. slideIndex = slidesTotal - slideIndex;
  3126. }
  3127. slide( slideIndex );
  3128. }
  3129. /**
  3130. * Event handler for navigation control buttons.
  3131. */
  3132. function onNavigateLeftClicked( event ) { event.preventDefault(); onUserInput(); navigateLeft(); }
  3133. function onNavigateRightClicked( event ) { event.preventDefault(); onUserInput(); navigateRight(); }
  3134. function onNavigateUpClicked( event ) { event.preventDefault(); onUserInput(); navigateUp(); }
  3135. function onNavigateDownClicked( event ) { event.preventDefault(); onUserInput(); navigateDown(); }
  3136. function onNavigatePrevClicked( event ) { event.preventDefault(); onUserInput(); navigatePrev(); }
  3137. function onNavigateNextClicked( event ) { event.preventDefault(); onUserInput(); navigateNext(); }
  3138. /**
  3139. * Handler for the window level 'hashchange' event.
  3140. */
  3141. function onWindowHashChange( event ) {
  3142. readURL();
  3143. }
  3144. /**
  3145. * Handler for the window level 'resize' event.
  3146. */
  3147. function onWindowResize( event ) {
  3148. layout();
  3149. }
  3150. /**
  3151. * Handle for the window level 'visibilitychange' event.
  3152. */
  3153. function onPageVisibilityChange( event ) {
  3154. var isHidden = document.webkitHidden ||
  3155. document.msHidden ||
  3156. document.hidden;
  3157. // If, after clicking a link or similar and we're coming back,
  3158. // focus the document.body to ensure we can use keyboard shortcuts
  3159. if( isHidden === false && document.activeElement !== document.body ) {
  3160. // Not all elements support .blur() - SVGs among them.
  3161. if( typeof document.activeElement.blur === 'function' ) {
  3162. document.activeElement.blur();
  3163. }
  3164. document.body.focus();
  3165. }
  3166. }
  3167. /**
  3168. * Invoked when a slide is and we're in the overview.
  3169. */
  3170. function onOverviewSlideClicked( event ) {
  3171. // TODO There's a bug here where the event listeners are not
  3172. // removed after deactivating the overview.
  3173. if( eventsAreBound && isOverview() ) {
  3174. event.preventDefault();
  3175. var element = event.target;
  3176. while( element && !element.nodeName.match( /section/gi ) ) {
  3177. element = element.parentNode;
  3178. }
  3179. if( element && !element.classList.contains( 'disabled' ) ) {
  3180. deactivateOverview();
  3181. if( element.nodeName.match( /section/gi ) ) {
  3182. var h = parseInt( element.getAttribute( 'data-index-h' ), 10 ),
  3183. v = parseInt( element.getAttribute( 'data-index-v' ), 10 );
  3184. slide( h, v );
  3185. }
  3186. }
  3187. }
  3188. }
  3189. /**
  3190. * Handles clicks on links that are set to preview in the
  3191. * iframe overlay.
  3192. */
  3193. function onPreviewLinkClicked( event ) {
  3194. if( event.currentTarget && event.currentTarget.hasAttribute( 'href' ) ) {
  3195. var url = event.currentTarget.getAttribute( 'href' );
  3196. if( url ) {
  3197. showPreview( url );
  3198. event.preventDefault();
  3199. }
  3200. }
  3201. }
  3202. /**
  3203. * Handles click on the auto-sliding controls element.
  3204. */
  3205. function onAutoSlidePlayerClick( event ) {
  3206. // Replay
  3207. if( Reveal.isLastSlide() && config.loop === false ) {
  3208. slide( 0, 0 );
  3209. resumeAutoSlide();
  3210. }
  3211. // Resume
  3212. else if( autoSlidePaused ) {
  3213. resumeAutoSlide();
  3214. }
  3215. // Pause
  3216. else {
  3217. pauseAutoSlide();
  3218. }
  3219. }
  3220. // --------------------------------------------------------------------//
  3221. // ------------------------ PLAYBACK COMPONENT ------------------------//
  3222. // --------------------------------------------------------------------//
  3223. /**
  3224. * Constructor for the playback component, which displays
  3225. * play/pause/progress controls.
  3226. *
  3227. * @param {HTMLElement} container The component will append
  3228. * itself to this
  3229. * @param {Function} progressCheck A method which will be
  3230. * called frequently to get the current progress on a range
  3231. * of 0-1
  3232. */
  3233. function Playback( container, progressCheck ) {
  3234. // Cosmetics
  3235. this.diameter = 50;
  3236. this.thickness = 3;
  3237. // Flags if we are currently playing
  3238. this.playing = false;
  3239. // Current progress on a 0-1 range
  3240. this.progress = 0;
  3241. // Used to loop the animation smoothly
  3242. this.progressOffset = 1;
  3243. this.container = container;
  3244. this.progressCheck = progressCheck;
  3245. this.canvas = document.createElement( 'canvas' );
  3246. this.canvas.className = 'playback';
  3247. this.canvas.width = this.diameter;
  3248. this.canvas.height = this.diameter;
  3249. this.context = this.canvas.getContext( '2d' );
  3250. this.container.appendChild( this.canvas );
  3251. this.render();
  3252. }
  3253. Playback.prototype.setPlaying = function( value ) {
  3254. var wasPlaying = this.playing;
  3255. this.playing = value;
  3256. // Start repainting if we weren't already
  3257. if( !wasPlaying && this.playing ) {
  3258. this.animate();
  3259. }
  3260. else {
  3261. this.render();
  3262. }
  3263. };
  3264. Playback.prototype.animate = function() {
  3265. var progressBefore = this.progress;
  3266. this.progress = this.progressCheck();
  3267. // When we loop, offset the progress so that it eases
  3268. // smoothly rather than immediately resetting
  3269. if( progressBefore > 0.8 && this.progress < 0.2 ) {
  3270. this.progressOffset = this.progress;
  3271. }
  3272. this.render();
  3273. if( this.playing ) {
  3274. features.requestAnimationFrameMethod.call( window, this.animate.bind( this ) );
  3275. }
  3276. };
  3277. /**
  3278. * Renders the current progress and playback state.
  3279. */
  3280. Playback.prototype.render = function() {
  3281. var progress = this.playing ? this.progress : 0,
  3282. radius = ( this.diameter / 2 ) - this.thickness,
  3283. x = this.diameter / 2,
  3284. y = this.diameter / 2,
  3285. iconSize = 14;
  3286. // Ease towards 1
  3287. this.progressOffset += ( 1 - this.progressOffset ) * 0.1;
  3288. var endAngle = ( - Math.PI / 2 ) + ( progress * ( Math.PI * 2 ) );
  3289. var startAngle = ( - Math.PI / 2 ) + ( this.progressOffset * ( Math.PI * 2 ) );
  3290. this.context.save();
  3291. this.context.clearRect( 0, 0, this.diameter, this.diameter );
  3292. // Solid background color
  3293. this.context.beginPath();
  3294. this.context.arc( x, y, radius + 2, 0, Math.PI * 2, false );
  3295. this.context.fillStyle = 'rgba( 0, 0, 0, 0.4 )';
  3296. this.context.fill();
  3297. // Draw progress track
  3298. this.context.beginPath();
  3299. this.context.arc( x, y, radius, 0, Math.PI * 2, false );
  3300. this.context.lineWidth = this.thickness;
  3301. this.context.strokeStyle = '#666';
  3302. this.context.stroke();
  3303. if( this.playing ) {
  3304. // Draw progress on top of track
  3305. this.context.beginPath();
  3306. this.context.arc( x, y, radius, startAngle, endAngle, false );
  3307. this.context.lineWidth = this.thickness;
  3308. this.context.strokeStyle = '#fff';
  3309. this.context.stroke();
  3310. }
  3311. this.context.translate( x - ( iconSize / 2 ), y - ( iconSize / 2 ) );
  3312. // Draw play/pause icons
  3313. if( this.playing ) {
  3314. this.context.fillStyle = '#fff';
  3315. this.context.fillRect( 0, 0, iconSize / 2 - 2, iconSize );
  3316. this.context.fillRect( iconSize / 2 + 2, 0, iconSize / 2 - 2, iconSize );
  3317. }
  3318. else {
  3319. this.context.beginPath();
  3320. this.context.translate( 2, 0 );
  3321. this.context.moveTo( 0, 0 );
  3322. this.context.lineTo( iconSize - 2, iconSize / 2 );
  3323. this.context.lineTo( 0, iconSize );
  3324. this.context.fillStyle = '#fff';
  3325. this.context.fill();
  3326. }
  3327. this.context.restore();
  3328. };
  3329. Playback.prototype.on = function( type, listener ) {
  3330. this.canvas.addEventListener( type, listener, false );
  3331. };
  3332. Playback.prototype.off = function( type, listener ) {
  3333. this.canvas.removeEventListener( type, listener, false );
  3334. };
  3335. Playback.prototype.destroy = function() {
  3336. this.playing = false;
  3337. if( this.canvas.parentNode ) {
  3338. this.container.removeChild( this.canvas );
  3339. }
  3340. };
  3341. // --------------------------------------------------------------------//
  3342. // ------------------------------- API --------------------------------//
  3343. // --------------------------------------------------------------------//
  3344. Reveal = {
  3345. initialize: initialize,
  3346. configure: configure,
  3347. sync: sync,
  3348. // Navigation methods
  3349. slide: slide,
  3350. left: navigateLeft,
  3351. right: navigateRight,
  3352. up: navigateUp,
  3353. down: navigateDown,
  3354. prev: navigatePrev,
  3355. next: navigateNext,
  3356. // Fragment methods
  3357. navigateFragment: navigateFragment,
  3358. prevFragment: previousFragment,
  3359. nextFragment: nextFragment,
  3360. // Deprecated aliases
  3361. navigateTo: slide,
  3362. navigateLeft: navigateLeft,
  3363. navigateRight: navigateRight,
  3364. navigateUp: navigateUp,
  3365. navigateDown: navigateDown,
  3366. navigatePrev: navigatePrev,
  3367. navigateNext: navigateNext,
  3368. // Forces an update in slide layout
  3369. layout: layout,
  3370. // Returns an object with the available routes as booleans (left/right/top/bottom)
  3371. availableRoutes: availableRoutes,
  3372. // Returns an object with the available fragments as booleans (prev/next)
  3373. availableFragments: availableFragments,
  3374. // Toggles the overview mode on/off
  3375. toggleOverview: toggleOverview,
  3376. // Toggles the "black screen" mode on/off
  3377. togglePause: togglePause,
  3378. // Toggles the auto slide mode on/off
  3379. toggleAutoSlide: toggleAutoSlide,
  3380. // State checks
  3381. isOverview: isOverview,
  3382. isPaused: isPaused,
  3383. isAutoSliding: isAutoSliding,
  3384. // Adds or removes all internal event listeners (such as keyboard)
  3385. addEventListeners: addEventListeners,
  3386. removeEventListeners: removeEventListeners,
  3387. // Facility for persisting and restoring the presentation state
  3388. getState: getState,
  3389. setState: setState,
  3390. // Presentation progress on range of 0-1
  3391. getProgress: getProgress,
  3392. // Returns the indices of the current, or specified, slide
  3393. getIndices: getIndices,
  3394. getTotalSlides: getTotalSlides,
  3395. // Returns the slide element at the specified index
  3396. getSlide: getSlide,
  3397. // Returns the slide background element at the specified index
  3398. getSlideBackground: getSlideBackground,
  3399. // Returns the previous slide element, may be null
  3400. getPreviousSlide: function() {
  3401. return previousSlide;
  3402. },
  3403. // Returns the current slide element
  3404. getCurrentSlide: function() {
  3405. return currentSlide;
  3406. },
  3407. // Returns the current scale of the presentation content
  3408. getScale: function() {
  3409. return scale;
  3410. },
  3411. // Returns the current configuration object
  3412. getConfig: function() {
  3413. return config;
  3414. },
  3415. // Helper method, retrieves query string as a key/value hash
  3416. getQueryHash: function() {
  3417. var query = {};
  3418. location.search.replace( /[A-Z0-9]+?=([\w\.%-]*)/gi, function(a) {
  3419. query[ a.split( '=' ).shift() ] = a.split( '=' ).pop();
  3420. } );
  3421. // Basic deserialization
  3422. for( var i in query ) {
  3423. var value = query[ i ];
  3424. query[ i ] = deserialize( unescape( value ) );
  3425. }
  3426. return query;
  3427. },
  3428. // Returns true if we're currently on the first slide
  3429. isFirstSlide: function() {
  3430. return ( indexh === 0 && indexv === 0 );
  3431. },
  3432. // Returns true if we're currently on the last slide
  3433. isLastSlide: function() {
  3434. if( currentSlide ) {
  3435. // Does this slide has next a sibling?
  3436. if( currentSlide.nextElementSibling ) return false;
  3437. // If it's vertical, does its parent have a next sibling?
  3438. if( isVerticalSlide( currentSlide ) && currentSlide.parentNode.nextElementSibling ) return false;
  3439. return true;
  3440. }
  3441. return false;
  3442. },
  3443. // Checks if reveal.js has been loaded and is ready for use
  3444. isReady: function() {
  3445. return loaded;
  3446. },
  3447. // Forward event binding to the reveal DOM element
  3448. addEventListener: function( type, listener, useCapture ) {
  3449. if( 'addEventListener' in window ) {
  3450. ( dom.wrapper || document.querySelector( '.reveal' ) ).addEventListener( type, listener, useCapture );
  3451. }
  3452. },
  3453. removeEventListener: function( type, listener, useCapture ) {
  3454. if( 'addEventListener' in window ) {
  3455. ( dom.wrapper || document.querySelector( '.reveal' ) ).removeEventListener( type, listener, useCapture );
  3456. }
  3457. },
  3458. // Programatically triggers a keyboard event
  3459. triggerKey: function( keyCode ) {
  3460. onDocumentKeyDown( { keyCode: keyCode } );
  3461. }
  3462. };
  3463. return Reveal;
  3464. }));