plugincollection.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. /**
  7. * Manages a list of CKEditor plugins, including loading, initialization and destruction.
  8. *
  9. * @class PluginCollection
  10. * @extends Collection
  11. */
  12. CKEDITOR.define( [ 'mvc/collection', 'promise' ], function( Collection, Promise ) {
  13. var PluginCollection = Collection.extend( {
  14. /**
  15. * Creates an instance of the PluginCollection class, initializing it with a set of plugins.
  16. *
  17. * @constructor
  18. */
  19. constructor: function PluginCollection( editor ) {
  20. // Call the base constructor.
  21. Collection.apply( this );
  22. this._editor = editor;
  23. // The hash table used to store pointers to loaded plugins by name.
  24. this._names = {};
  25. },
  26. /**
  27. * Loads a set of plugins and add them to the collection.
  28. *
  29. * @param {String} plugins A comma separated list of plugin names to get loaded.
  30. */
  31. load: function( plugins ) {
  32. var that = this;
  33. plugins = plugins.split( ',' );
  34. // Creates a promise for the loading of each plugin and returns a main promise that resolves when all are
  35. // done.
  36. return Promise.all( plugins.map( pluginPromise ) );
  37. // Returns a promise that will load the plugin and add it to the collection before resolving.
  38. function pluginPromise( plugin ) {
  39. return new Promise( function( resolve, reject ) {
  40. CKEDITOR.require( [ 'plugin!' + plugin ],
  41. // Success callback.
  42. function( LoadedPlugin ) {
  43. var loadedPlugin = new LoadedPlugin( that._editor );
  44. loadedPlugin.name = plugin;
  45. // Adds a new instance of the loaded plugin to the collection.
  46. that.add( loadedPlugin );
  47. // Done! Resolve this promise.
  48. resolve();
  49. },
  50. // Error callback.
  51. function() {
  52. var err = new Error( 'It was not possible to load the "' + plugin + '" plugin.' );
  53. err.name = 'CKEditor Error';
  54. reject( err );
  55. }
  56. );
  57. } );
  58. }
  59. },
  60. /**
  61. * Adds a plugin to the collection.
  62. *
  63. * The `name` property must be set to the plugin object before passing it to this function. Adding plugins
  64. * with the same name has no effect and silently fails.
  65. *
  66. * @param {Plugin} plugin The plugin to be added.
  67. */
  68. add: function( plugin ) {
  69. // Do nothing if the plugin is already loaded.
  70. if ( this._names[ plugin.name ] ) {
  71. return;
  72. }
  73. // Save a pointer to the plugin by its name.
  74. this._names[ plugin.name ] = plugin;
  75. // Call the original implementation.
  76. Collection.prototype.add.apply( this, arguments );
  77. },
  78. /**
  79. * Gets a plugin from the collection.
  80. *
  81. * @param {String} name The plugin name.
  82. * @returns {Plugin} The requested plugin, if available in the collection.
  83. */
  84. get: function( name ) {
  85. if ( typeof name != 'string' ) {
  86. return Collection.prototype.get.apply( this, arguments );
  87. }
  88. return this._names[ name ];
  89. }
  90. } );
  91. return PluginCollection;
  92. } );