reveal.js 136 KB

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