reveal.js 128 KB

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