reveal.js 147 KB

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