reveal.js 147 KB

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