8
0

componentfactory.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module ui/componentfactory
  7. */
  8. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  9. /**
  10. * Class implementing the UI component factory.
  11. *
  12. * Factories of specific UI components can be registered under their unique names. Registered
  13. * components can be later instantiated by providing the name of the component.
  14. *
  15. * The main use case for the component factory is the {@link module:core/editor/editorui~EditorUI#componentFactory} factory.
  16. */
  17. export default class ComponentFactory {
  18. /**
  19. * Creates ComponentFactory instance.
  20. *
  21. * @constructor
  22. * @param {module:core/editor/editor~Editor} editor The editor instance.
  23. */
  24. constructor( editor ) {
  25. /**
  26. * The editor instance.
  27. *
  28. * @readonly
  29. * @member {module:core/editor/editor~Editor}
  30. */
  31. this.editor = editor;
  32. /**
  33. * Registered component factories.
  34. *
  35. * @private
  36. * @member {Map}
  37. */
  38. this._components = new Map();
  39. }
  40. /**
  41. * Registers a component factory.
  42. *
  43. * @param {String} name The name of the component.
  44. * @param {Function} ControllerClass The component controller constructor.
  45. * @param {Function} ViewClass The component view constructor.
  46. * @param {Function} [callback] The callback to process the view instance,
  47. * i.e. to set attribute values, create attribute bindings, etc.
  48. */
  49. add( name, callback ) {
  50. if ( this._components.get( name ) ) {
  51. /**
  52. * The item already exists in the component factory.
  53. *
  54. * @error componentfactory-item-exists
  55. * @param {String} name The name of the component.
  56. */
  57. throw new CKEditorError(
  58. 'componentfactory-item-exists: The item already exists in the component factory.', { name }
  59. );
  60. }
  61. this._components.set( name, callback );
  62. }
  63. /**
  64. * Creates a component view instance.
  65. *
  66. * @param {String} name The name of the component.
  67. * @returns {module:ui/view~View} The instantiated component view.
  68. */
  69. create( name ) {
  70. const component = this._components.get( name );
  71. if ( !component ) {
  72. /**
  73. * There is no such UI component in the factory.
  74. *
  75. * @error componentfactory-item-missing
  76. * @param {String} name The name of the missing component.
  77. */
  78. throw new CKEditorError(
  79. 'componentfactory-item-missing: There is no such UI component in the factory.', { name }
  80. );
  81. }
  82. return component( this.editor.locale );
  83. }
  84. }