reveal.js 127 KB

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