reveal.js 143 KB

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