reveal.js 128 KB

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