reveal.js 129 KB

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