plugins.js 4.5 KB

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