reveal.js 145 KB

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