reveal.js 128 KB

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