reveal.js 128 KB

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