componentfactory.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. * Returns iterator of component names.
  42. *
  43. * @returns {Iterator.<String>}
  44. */
  45. * names() {
  46. yield* this._components.keys();
  47. }
  48. /**
  49. * Registers a component factory.
  50. *
  51. * @param {String} name The name of the component.
  52. * @param {Function} ControllerClass The component controller constructor.
  53. * @param {Function} ViewClass The component view constructor.
  54. * @param {Function} [callback] The callback to process the view instance,
  55. * i.e. to set attribute values, create attribute bindings, etc.
  56. */
  57. add( name, callback ) {
  58. if ( this._components.get( name ) ) {
  59. /**
  60. * The item already exists in the component factory.
  61. *
  62. * @error componentfactory-item-exists
  63. * @param {String} name The name of the component.
  64. */
  65. throw new CKEditorError(
  66. 'componentfactory-item-exists: The item already exists in the component factory.', { name }
  67. );
  68. }
  69. this._components.set( name, callback );
  70. }
  71. /**
  72. * Creates a component view instance.
  73. *
  74. * @param {String} name The name of the component.
  75. * @returns {module:ui/view~View} The instantiated component view.
  76. */
  77. create( name ) {
  78. const component = this._components.get( name );
  79. if ( !component ) {
  80. /**
  81. * There is no such UI component in the factory.
  82. *
  83. * @error componentfactory-item-missing
  84. * @param {String} name The name of the missing component.
  85. */
  86. throw new CKEditorError(
  87. 'componentfactory-item-missing: There is no such UI component in the factory.', { name }
  88. );
  89. }
  90. return component( this.editor.locale );
  91. }
  92. }