plugins.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import { loadScript } from '../utils/loader.js'
  2. /**
  3. * Manages loading and registering of reveal.js plugins.
  4. */
  5. export default class Plugins {
  6. constructor() {
  7. // Flags our current state (idle -> loading -> loaded)
  8. this.state = 'idle';
  9. // An id:instance map of currently registed plugins
  10. this.registeredPlugins = {};
  11. this.asyncDependencies = [];
  12. }
  13. /**
  14. * Loads the dependencies of reveal.js. Dependencies are
  15. * defined via the configuration option 'dependencies'
  16. * and will be loaded prior to starting/binding reveal.js.
  17. * Some dependencies may have an 'async' flag, if so they
  18. * will load after reveal.js has been started up.
  19. */
  20. load( dependencies ) {
  21. this.state = 'loading';
  22. return new Promise( resolve => {
  23. let scripts = [],
  24. scriptsToLoad = 0;
  25. dependencies.forEach( s => {
  26. // Load if there's no condition or the condition is truthy
  27. if( !s.condition || s.condition() ) {
  28. if( s.async ) {
  29. this.asyncDependencies.push( s );
  30. }
  31. else {
  32. scripts.push( s );
  33. }
  34. }
  35. } );
  36. if( scripts.length ) {
  37. scriptsToLoad = scripts.length;
  38. const scriptLoadedCallback = (s) => {
  39. if( typeof s.callback === 'function' ) s.callback();
  40. if( --scriptsToLoad === 0 ) {
  41. this.initPlugins().then( resolve );
  42. }
  43. };
  44. // Load synchronous scripts
  45. scripts.forEach( s => {
  46. if( s.id ) {
  47. this.registerPlugin( s.id, s.plugin );
  48. scriptLoadedCallback( s );
  49. }
  50. else {
  51. loadScript( s.src, () => scriptLoadedCallback(s) );
  52. }
  53. } );
  54. }
  55. else {
  56. this.initPlugins().then( resolve );
  57. }
  58. } );
  59. }
  60. /**
  61. * Initializes our plugins and waits for them to be ready
  62. * before proceeding.
  63. */
  64. initPlugins() {
  65. return new Promise( resolve => {
  66. let pluginsToInitialize = Object.keys( this.registeredPlugins ).length;
  67. // If there are no plugins, skip this step
  68. if( pluginsToInitialize === 0 ) {
  69. this.loadAsync().then( resolve );
  70. }
  71. // ... otherwise initialize plugins
  72. else {
  73. let afterPlugInitialized = () => {
  74. if( --pluginsToInitialize === 0 ) {
  75. this.loadAsync().then( resolve );
  76. }
  77. };
  78. for( let i in this.registeredPlugins ) {
  79. let plugin = this.registeredPlugins[i];
  80. // If the plugin has an 'init' method, invoke it
  81. if( typeof plugin.init === 'function' ) {
  82. let callback = plugin.init();
  83. // If the plugin returned a Promise, wait for it
  84. if( callback && typeof callback.then === 'function' ) {
  85. callback.then( afterPlugInitialized );
  86. }
  87. else {
  88. afterPlugInitialized();
  89. }
  90. }
  91. else {
  92. afterPlugInitialized();
  93. }
  94. }
  95. }
  96. } )
  97. }
  98. /**
  99. * Loads all async reveal.js dependencies.
  100. */
  101. loadAsync() {
  102. this.state = 'loaded';
  103. if( this.asyncDependencies.length ) {
  104. this.asyncDependencies.forEach( s => {
  105. if( s.id ) {
  106. this.registerPlugin( s.id, s.plugin );
  107. if( typeof s.plugin.init === 'function' ) s.plugin.init();
  108. if( typeof s.callback === 'function' ) s.callback();
  109. }
  110. else {
  111. loadScript( s.src, s.callback );
  112. }
  113. } );
  114. }
  115. return Promise.resolve();
  116. }
  117. /**
  118. * Registers a new plugin with this reveal.js instance.
  119. *
  120. * reveal.js waits for all regisered plugins to initialize
  121. * before considering itself ready, as long as the plugin
  122. * is registered before calling `Reveal.initialize()`.
  123. */
  124. registerPlugin( id, plugin ) {
  125. if( this.registeredPlugins[id] === undefined ) {
  126. this.registeredPlugins[id] = plugin;
  127. // If a plugin is registered after reveal.js is loaded,
  128. // initialize it right away
  129. if( this.state === 'loaded' && typeof plugin.init === 'function' ) {
  130. plugin.init();
  131. }
  132. }
  133. else {
  134. console.warn( 'reveal.js: "'+ id +'" plugin has already been registered' );
  135. }
  136. }
  137. /**
  138. * Checks if a specific plugin has been registered.
  139. *
  140. * @param {String} id Unique plugin identifier
  141. */
  142. hasPlugin( id ) {
  143. return !!this.registeredPlugins[id];
  144. }
  145. /**
  146. * Returns the specific plugin instance, if a plugin
  147. * with the given ID has been registered.
  148. *
  149. * @param {String} id Unique plugin identifier
  150. */
  151. getPlugin( id ) {
  152. return this.registeredPlugins[id];
  153. }
  154. getRegisteredPlugins() {
  155. return this.registeredPlugins;
  156. }
  157. }