plugins.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. } else {
  50. loadScript( s.src, () => scriptLoadedCallback(s));
  51. }
  52. } );
  53. }
  54. else {
  55. this.initPlugins().then( resolve );
  56. }
  57. } );
  58. }
  59. /**
  60. * Initializes our plugins and waits for them to be ready
  61. * before proceeding.
  62. */
  63. initPlugins() {
  64. return new Promise( resolve => {
  65. let pluginsToInitialize = Object.keys( this.registeredPlugins ).length;
  66. // If there are no plugins, skip this step
  67. if( pluginsToInitialize === 0 ) {
  68. this.loadAsync().then( resolve );
  69. }
  70. // ... otherwise initialize plugins
  71. else {
  72. let afterPlugInitialized = () => {
  73. if( --pluginsToInitialize === 0 ) {
  74. this.loadAsync().then( resolve );
  75. }
  76. };
  77. for( let i in this.registeredPlugins ) {
  78. let plugin = this.registeredPlugins[i];
  79. // If the plugin has an 'init' method, invoke it
  80. if( typeof plugin.init === 'function' ) {
  81. let callback = plugin.init();
  82. // If the plugin returned a Promise, wait for it
  83. if( callback && typeof callback.then === 'function' ) {
  84. callback.then( afterPlugInitialized );
  85. }
  86. else {
  87. afterPlugInitialized();
  88. }
  89. }
  90. else {
  91. afterPlugInitialized();
  92. }
  93. }
  94. }
  95. } )
  96. }
  97. /**
  98. * Loads all async reveal.js dependencies.
  99. */
  100. loadAsync() {
  101. this.state = 'loaded';
  102. if( this.asyncDependencies.length ) {
  103. this.asyncDependencies.forEach( s => {
  104. if (s.id) {
  105. this.registerPlugin(s.id, s.plugin);
  106. if (typeof s.plugin.init === 'function') { s.plugin.init(); }
  107. if (typeof s.callback === 'function') { s.callback(); }
  108. } else {
  109. loadScript( s.src, s.callback );
  110. }
  111. } );
  112. }
  113. return Promise.resolve();
  114. }
  115. /**
  116. * Registers a new plugin with this reveal.js instance.
  117. *
  118. * reveal.js waits for all regisered plugins to initialize
  119. * before considering itself ready, as long as the plugin
  120. * is registered before calling `Reveal.initialize()`.
  121. */
  122. registerPlugin( id, plugin ) {
  123. if( this.registeredPlugins[id] === undefined ) {
  124. this.registeredPlugins[id] = plugin;
  125. // If a plugin is registered after reveal.js is loaded,
  126. // initialize it right away
  127. if( this.state === 'loaded' && typeof plugin.init === 'function' ) {
  128. plugin.init();
  129. }
  130. }
  131. else {
  132. console.warn( 'reveal.js: "'+ id +'" plugin has already been registered' );
  133. }
  134. }
  135. /**
  136. * Checks if a specific plugin has been registered.
  137. *
  138. * @param {String} id Unique plugin identifier
  139. */
  140. hasPlugin( id ) {
  141. return !!this.registeredPlugins[id];
  142. }
  143. /**
  144. * Returns the specific plugin instance, if a plugin
  145. * with the given ID has been registered.
  146. *
  147. * @param {String} id Unique plugin identifier
  148. */
  149. getPlugin( id ) {
  150. return this.registeredPlugins[id];
  151. }
  152. getRegisteredPlugins() {
  153. return this.registeredPlugins;
  154. }
  155. }