reveal.js 143 KB

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