reveal.js 126 KB

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