reveal.js 141 KB

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