reveal.js 143 KB

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