plugins.js 4.8 KB

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