1
0

plugins.js 3.8 KB

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