plugins.js 4.4 KB

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